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); |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 899 | |
| 900 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 901 | _PyUnicode_HASH(unicode) = -1; |
| 902 | _PyUnicode_STATE(unicode).interned = 0; |
| 903 | _PyUnicode_STATE(unicode).kind = 0; |
| 904 | _PyUnicode_STATE(unicode).compact = 0; |
| 905 | _PyUnicode_STATE(unicode).ready = 0; |
| 906 | _PyUnicode_STATE(unicode).ascii = 0; |
| 907 | _PyUnicode_DATA_ANY(unicode) = NULL; |
| 908 | _PyUnicode_LENGTH(unicode) = 0; |
| 909 | _PyUnicode_UTF8(unicode) = NULL; |
| 910 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 911 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 913 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 914 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 915 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 916 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 917 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 918 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 919 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 920 | * the caller fails before initializing str -- unicode_resize() |
| 921 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 922 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 923 | * We don't want unicode_resize to read uninitialized memory in |
| 924 | * that case. |
| 925 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 926 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 927 | _PyUnicode_WSTR(unicode)[length] = 0; |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 928 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 929 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 930 | return unicode; |
| 931 | } |
| 932 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 933 | static const char* |
| 934 | unicode_kind_name(PyObject *unicode) |
| 935 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 936 | /* don't check consistency: unicode_kind_name() is called from |
| 937 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 938 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 939 | { |
| 940 | if (!PyUnicode_IS_READY(unicode)) |
| 941 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 942 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 943 | { |
| 944 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 945 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 946 | return "legacy ascii"; |
| 947 | else |
| 948 | return "legacy latin1"; |
| 949 | case PyUnicode_2BYTE_KIND: |
| 950 | return "legacy UCS2"; |
| 951 | case PyUnicode_4BYTE_KIND: |
| 952 | return "legacy UCS4"; |
| 953 | default: |
| 954 | return "<legacy invalid kind>"; |
| 955 | } |
| 956 | } |
| 957 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 958 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 959 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 960 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 961 | return "ascii"; |
| 962 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 963 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 964 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 965 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 966 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 967 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 968 | default: |
| 969 | return "<invalid compact kind>"; |
| 970 | } |
| 971 | } |
| 972 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 973 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | /* Functions wrapping macros for use in debugger */ |
| 975 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 976 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void *_PyUnicode_compact_data(void *unicode) { |
| 980 | return _PyUnicode_COMPACT_DATA(unicode); |
| 981 | } |
| 982 | void *_PyUnicode_data(void *unicode){ |
| 983 | printf("obj %p\n", unicode); |
| 984 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 985 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 986 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 987 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 988 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 989 | return PyUnicode_DATA(unicode); |
| 990 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 991 | |
| 992 | void |
| 993 | _PyUnicode_Dump(PyObject *op) |
| 994 | { |
| 995 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 996 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 997 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 998 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 999 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1000 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1001 | { |
| 1002 | if (ascii->state.ascii) |
| 1003 | data = (ascii + 1); |
| 1004 | else |
| 1005 | data = (compact + 1); |
| 1006 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1007 | else |
| 1008 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1009 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 1010 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1011 | if (ascii->wstr == data) |
| 1012 | printf("shared "); |
| 1013 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1014 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1015 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1016 | printf(" (%zu), ", compact->wstr_length); |
| 1017 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1018 | printf("shared "); |
| 1019 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1020 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1021 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1022 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1023 | #endif |
| 1024 | |
| 1025 | PyObject * |
| 1026 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1027 | { |
| 1028 | PyObject *obj; |
| 1029 | PyCompactUnicodeObject *unicode; |
| 1030 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1031 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1032 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1033 | Py_ssize_t char_size; |
| 1034 | Py_ssize_t struct_size; |
| 1035 | |
| 1036 | /* Optimization for empty strings */ |
| 1037 | if (size == 0 && unicode_empty != NULL) { |
| 1038 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1039 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1042 | is_ascii = 0; |
| 1043 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1045 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1046 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1047 | char_size = 1; |
| 1048 | is_ascii = 1; |
| 1049 | struct_size = sizeof(PyASCIIObject); |
| 1050 | } |
| 1051 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1052 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | char_size = 1; |
| 1054 | } |
| 1055 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1056 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1057 | char_size = 2; |
| 1058 | if (sizeof(wchar_t) == 2) |
| 1059 | is_sharing = 1; |
| 1060 | } |
| 1061 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1062 | if (maxchar > MAX_UNICODE) { |
| 1063 | PyErr_SetString(PyExc_SystemError, |
| 1064 | "invalid maximum character passed to PyUnicode_New"); |
| 1065 | return NULL; |
| 1066 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1067 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1068 | char_size = 4; |
| 1069 | if (sizeof(wchar_t) == 4) |
| 1070 | is_sharing = 1; |
| 1071 | } |
| 1072 | |
| 1073 | /* Ensure we won't overflow the size. */ |
| 1074 | if (size < 0) { |
| 1075 | PyErr_SetString(PyExc_SystemError, |
| 1076 | "Negative size passed to PyUnicode_New"); |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1080 | return PyErr_NoMemory(); |
| 1081 | |
| 1082 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1083 | * PyObject_New() so we are able to allocate space for the object and |
| 1084 | * it's data buffer. |
| 1085 | */ |
| 1086 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1087 | if (obj == NULL) |
| 1088 | return PyErr_NoMemory(); |
| 1089 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1090 | if (obj == NULL) |
| 1091 | return NULL; |
| 1092 | |
| 1093 | unicode = (PyCompactUnicodeObject *)obj; |
| 1094 | if (is_ascii) |
| 1095 | data = ((PyASCIIObject*)obj) + 1; |
| 1096 | else |
| 1097 | data = unicode + 1; |
| 1098 | _PyUnicode_LENGTH(unicode) = size; |
| 1099 | _PyUnicode_HASH(unicode) = -1; |
| 1100 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1101 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1102 | _PyUnicode_STATE(unicode).compact = 1; |
| 1103 | _PyUnicode_STATE(unicode).ready = 1; |
| 1104 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1105 | if (is_ascii) { |
| 1106 | ((char*)data)[size] = 0; |
| 1107 | _PyUnicode_WSTR(unicode) = NULL; |
| 1108 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1109 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | ((char*)data)[size] = 0; |
| 1111 | _PyUnicode_WSTR(unicode) = NULL; |
| 1112 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1113 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1114 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1115 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1116 | else { |
| 1117 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1118 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1119 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1121 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1122 | ((Py_UCS4*)data)[size] = 0; |
| 1123 | if (is_sharing) { |
| 1124 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1125 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1126 | } |
| 1127 | else { |
| 1128 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1129 | _PyUnicode_WSTR(unicode) = NULL; |
| 1130 | } |
| 1131 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1132 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1133 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1134 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1135 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1136 | return obj; |
| 1137 | } |
| 1138 | |
| 1139 | #if SIZEOF_WCHAR_T == 2 |
| 1140 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1141 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1142 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1143 | |
| 1144 | This function assumes that unicode can hold one more code point than wstr |
| 1145 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1146 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1148 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1149 | { |
| 1150 | const wchar_t *iter; |
| 1151 | Py_UCS4 *ucs4_out; |
| 1152 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1153 | assert(unicode != NULL); |
| 1154 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1155 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1156 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1157 | |
| 1158 | for (iter = begin; iter < end; ) { |
| 1159 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1160 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1161 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1162 | && (iter+1) < end |
| 1163 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1164 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1165 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1166 | iter += 2; |
| 1167 | } |
| 1168 | else { |
| 1169 | *ucs4_out++ = *iter; |
| 1170 | iter++; |
| 1171 | } |
| 1172 | } |
| 1173 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1174 | _PyUnicode_GET_LENGTH(unicode))); |
| 1175 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1176 | } |
| 1177 | #endif |
| 1178 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1179 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1180 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1181 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1182 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1183 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1184 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1185 | return -1; |
| 1186 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1187 | return 0; |
| 1188 | } |
| 1189 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1190 | static int |
| 1191 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1192 | PyObject *from, Py_ssize_t from_start, |
| 1193 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1194 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1195 | unsigned int from_kind, to_kind; |
| 1196 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1197 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1198 | assert(0 <= how_many); |
| 1199 | assert(0 <= from_start); |
| 1200 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1201 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1202 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1203 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1204 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1205 | assert(PyUnicode_Check(to)); |
| 1206 | assert(PyUnicode_IS_READY(to)); |
| 1207 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1208 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1209 | if (how_many == 0) |
| 1210 | return 0; |
| 1211 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1212 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1213 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1214 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1215 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1217 | #ifdef Py_DEBUG |
| 1218 | if (!check_maxchar |
| 1219 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1220 | { |
| 1221 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1222 | Py_UCS4 ch; |
| 1223 | Py_ssize_t i; |
| 1224 | for (i=0; i < how_many; i++) { |
| 1225 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1226 | assert(ch <= to_maxchar); |
| 1227 | } |
| 1228 | } |
| 1229 | #endif |
| 1230 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1231 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1232 | if (check_maxchar |
| 1233 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1234 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1235 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1236 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1237 | Py_UCS4 max_char; |
| 1238 | max_char = ucs1lib_find_max_char(from_data, |
| 1239 | (Py_UCS1*)from_data + how_many); |
| 1240 | if (max_char >= 128) |
| 1241 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1242 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1243 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1244 | (char*)from_data + from_kind * from_start, |
| 1245 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1246 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1247 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1248 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1249 | { |
| 1250 | _PyUnicode_CONVERT_BYTES( |
| 1251 | Py_UCS1, Py_UCS2, |
| 1252 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1253 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1254 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1255 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1256 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1257 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1258 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1259 | { |
| 1260 | _PyUnicode_CONVERT_BYTES( |
| 1261 | Py_UCS1, Py_UCS4, |
| 1262 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1263 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1264 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1265 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1266 | } |
| 1267 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1268 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1269 | { |
| 1270 | _PyUnicode_CONVERT_BYTES( |
| 1271 | Py_UCS2, Py_UCS4, |
| 1272 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1273 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1274 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1275 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1276 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1277 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1278 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1279 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1280 | if (!check_maxchar) { |
| 1281 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1282 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1283 | { |
| 1284 | _PyUnicode_CONVERT_BYTES( |
| 1285 | Py_UCS2, Py_UCS1, |
| 1286 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1287 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1288 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1289 | ); |
| 1290 | } |
| 1291 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1292 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1293 | { |
| 1294 | _PyUnicode_CONVERT_BYTES( |
| 1295 | Py_UCS4, Py_UCS1, |
| 1296 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1297 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1298 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1299 | ); |
| 1300 | } |
| 1301 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1302 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1303 | { |
| 1304 | _PyUnicode_CONVERT_BYTES( |
| 1305 | Py_UCS4, Py_UCS2, |
| 1306 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1307 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1308 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1309 | ); |
| 1310 | } |
| 1311 | else { |
| 1312 | assert(0); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1316 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1317 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1318 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1319 | Py_ssize_t i; |
| 1320 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1321 | for (i=0; i < how_many; i++) { |
| 1322 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1323 | if (ch > to_maxchar) |
| 1324 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1325 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1326 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1327 | } |
| 1328 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1329 | return 0; |
| 1330 | } |
| 1331 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1332 | void |
| 1333 | _PyUnicode_FastCopyCharacters( |
| 1334 | PyObject *to, Py_ssize_t to_start, |
| 1335 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1336 | { |
| 1337 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1338 | } |
| 1339 | |
| 1340 | Py_ssize_t |
| 1341 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1342 | PyObject *from, Py_ssize_t from_start, |
| 1343 | Py_ssize_t how_many) |
| 1344 | { |
| 1345 | int err; |
| 1346 | |
| 1347 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1348 | PyErr_BadInternalCall(); |
| 1349 | return -1; |
| 1350 | } |
| 1351 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1352 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1353 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1354 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1355 | return -1; |
| 1356 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1357 | if (from_start < 0) { |
| 1358 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1359 | return -1; |
| 1360 | } |
| 1361 | if (to_start < 0) { |
| 1362 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1363 | return -1; |
| 1364 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1365 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1366 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1367 | PyErr_Format(PyExc_SystemError, |
| 1368 | "Cannot write %zi characters at %zi " |
| 1369 | "in a string of %zi characters", |
| 1370 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1371 | return -1; |
| 1372 | } |
| 1373 | |
| 1374 | if (how_many == 0) |
| 1375 | return 0; |
| 1376 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1377 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1378 | return -1; |
| 1379 | |
| 1380 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1381 | if (err) { |
| 1382 | PyErr_Format(PyExc_SystemError, |
| 1383 | "Cannot copy %s characters " |
| 1384 | "into a string of %s characters", |
| 1385 | unicode_kind_name(from), |
| 1386 | unicode_kind_name(to)); |
| 1387 | return -1; |
| 1388 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1389 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1392 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1393 | correct string length can be computed before converting a string to UCS4. |
| 1394 | This function counts single surrogates as a character and not as a pair. |
| 1395 | |
| 1396 | Return 0 on success, or -1 on error. */ |
| 1397 | static int |
| 1398 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1399 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | { |
| 1401 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1402 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1403 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1404 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1405 | *num_surrogates = 0; |
| 1406 | *maxchar = 0; |
| 1407 | |
| 1408 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1409 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1410 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1411 | && (iter+1) < end |
| 1412 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1413 | { |
| 1414 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1415 | ++(*num_surrogates); |
| 1416 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1417 | } |
| 1418 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1419 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1420 | { |
| 1421 | ch = *iter; |
| 1422 | iter++; |
| 1423 | } |
| 1424 | if (ch > *maxchar) { |
| 1425 | *maxchar = ch; |
| 1426 | if (*maxchar > MAX_UNICODE) { |
| 1427 | PyErr_Format(PyExc_ValueError, |
| 1428 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1429 | ch); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1433 | } |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1437 | int |
| 1438 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1439 | { |
| 1440 | wchar_t *end; |
| 1441 | Py_UCS4 maxchar = 0; |
| 1442 | Py_ssize_t num_surrogates; |
| 1443 | #if SIZEOF_WCHAR_T == 2 |
| 1444 | Py_ssize_t length_wo_surrogates; |
| 1445 | #endif |
| 1446 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1447 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1448 | strings were created using _PyObject_New() and where no canonical |
| 1449 | representation (the str field) has been set yet aka strings |
| 1450 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1451 | assert(_PyUnicode_CHECK(unicode)); |
| 1452 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1453 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1454 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1455 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1456 | /* Actually, it should neither be interned nor be anything else: */ |
| 1457 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1458 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1460 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1461 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1462 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | |
| 1464 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1465 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1466 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | PyErr_NoMemory(); |
| 1468 | return -1; |
| 1469 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1470 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1471 | _PyUnicode_WSTR(unicode), end, |
| 1472 | PyUnicode_1BYTE_DATA(unicode)); |
| 1473 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1474 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1475 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1476 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1477 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1478 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1479 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1480 | } |
| 1481 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1482 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1483 | _PyUnicode_UTF8(unicode) = NULL; |
| 1484 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1485 | } |
| 1486 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1487 | _PyUnicode_WSTR(unicode) = NULL; |
| 1488 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1489 | } |
| 1490 | /* In this case we might have to convert down from 4-byte native |
| 1491 | wchar_t to 2-byte unicode. */ |
| 1492 | else if (maxchar < 65536) { |
| 1493 | assert(num_surrogates == 0 && |
| 1494 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1495 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1496 | #if SIZEOF_WCHAR_T == 2 |
| 1497 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1498 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1499 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1500 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1501 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1502 | _PyUnicode_UTF8(unicode) = NULL; |
| 1503 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1504 | #else |
| 1505 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1506 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1507 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1508 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1509 | PyErr_NoMemory(); |
| 1510 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1511 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1512 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1513 | _PyUnicode_WSTR(unicode), end, |
| 1514 | PyUnicode_2BYTE_DATA(unicode)); |
| 1515 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1516 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1517 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1518 | _PyUnicode_UTF8(unicode) = NULL; |
| 1519 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1520 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1521 | _PyUnicode_WSTR(unicode) = NULL; |
| 1522 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1523 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1524 | } |
| 1525 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1526 | else { |
| 1527 | #if SIZEOF_WCHAR_T == 2 |
| 1528 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1529 | new normalized 4-byte version. */ |
| 1530 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1531 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1532 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1533 | PyErr_NoMemory(); |
| 1534 | return -1; |
| 1535 | } |
| 1536 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1537 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1538 | _PyUnicode_UTF8(unicode) = NULL; |
| 1539 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1540 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1541 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1542 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1543 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1544 | _PyUnicode_WSTR(unicode) = NULL; |
| 1545 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1546 | #else |
| 1547 | assert(num_surrogates == 0); |
| 1548 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1549 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1550 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1551 | _PyUnicode_UTF8(unicode) = NULL; |
| 1552 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1553 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1554 | #endif |
| 1555 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1556 | } |
| 1557 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1558 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1562 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1563 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1564 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1565 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1566 | case SSTATE_NOT_INTERNED: |
| 1567 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1568 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1569 | case SSTATE_INTERNED_MORTAL: |
| 1570 | /* revive dead object temporarily for DelItem */ |
| 1571 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1572 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1573 | Py_FatalError( |
| 1574 | "deletion of interned string failed"); |
| 1575 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1576 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1577 | case SSTATE_INTERNED_IMMORTAL: |
| 1578 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1579 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1580 | default: |
| 1581 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1584 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1585 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1586 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1587 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1588 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1589 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1590 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1591 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1594 | #ifdef Py_DEBUG |
| 1595 | static int |
| 1596 | unicode_is_singleton(PyObject *unicode) |
| 1597 | { |
| 1598 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1599 | if (unicode == unicode_empty) |
| 1600 | return 1; |
| 1601 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1602 | { |
| 1603 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1604 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1605 | return 1; |
| 1606 | } |
| 1607 | return 0; |
| 1608 | } |
| 1609 | #endif |
| 1610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1611 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1612 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1613 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1614 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1615 | if (Py_REFCNT(unicode) != 1) |
| 1616 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1617 | if (_PyUnicode_HASH(unicode) != -1) |
| 1618 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1619 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1620 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1621 | if (!PyUnicode_CheckExact(unicode)) |
| 1622 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1623 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1624 | /* singleton refcount is greater than 1 */ |
| 1625 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1626 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1627 | return 1; |
| 1628 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1629 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1630 | static int |
| 1631 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1632 | { |
| 1633 | PyObject *unicode; |
| 1634 | Py_ssize_t old_length; |
| 1635 | |
| 1636 | assert(p_unicode != NULL); |
| 1637 | unicode = *p_unicode; |
| 1638 | |
| 1639 | assert(unicode != NULL); |
| 1640 | assert(PyUnicode_Check(unicode)); |
| 1641 | assert(0 <= length); |
| 1642 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1643 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1644 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1645 | else |
| 1646 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1647 | if (old_length == length) |
| 1648 | return 0; |
| 1649 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1650 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1651 | _Py_INCREF_UNICODE_EMPTY(); |
| 1652 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1653 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1654 | Py_DECREF(*p_unicode); |
| 1655 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1656 | return 0; |
| 1657 | } |
| 1658 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1659 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1660 | PyObject *copy = resize_copy(unicode, length); |
| 1661 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1662 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1663 | Py_DECREF(*p_unicode); |
| 1664 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1665 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1668 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1669 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1670 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1671 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1672 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1673 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1674 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1675 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1678 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1679 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1680 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1681 | PyObject *unicode; |
| 1682 | if (p_unicode == NULL) { |
| 1683 | PyErr_BadInternalCall(); |
| 1684 | return -1; |
| 1685 | } |
| 1686 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1687 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1688 | { |
| 1689 | PyErr_BadInternalCall(); |
| 1690 | return -1; |
| 1691 | } |
| 1692 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1693 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1694 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1695 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1696 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1697 | WARNING: The function doesn't copy the terminating null character and |
| 1698 | doesn't check the maximum character (may write a latin1 character in an |
| 1699 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1700 | static void |
| 1701 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1702 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1703 | { |
| 1704 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1705 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1706 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1707 | |
| 1708 | switch (kind) { |
| 1709 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1710 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1711 | #ifdef Py_DEBUG |
| 1712 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1713 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1714 | (const Py_UCS1*)str, |
| 1715 | (const Py_UCS1*)str + len); |
| 1716 | assert(maxchar < 128); |
| 1717 | } |
| 1718 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1719 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1720 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1721 | } |
| 1722 | case PyUnicode_2BYTE_KIND: { |
| 1723 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1724 | Py_UCS2 *ucs2 = start; |
| 1725 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1726 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1727 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1728 | *ucs2 = (Py_UCS2)*str; |
| 1729 | |
| 1730 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1731 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1732 | } |
| 1733 | default: { |
| 1734 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1735 | Py_UCS4 *ucs4 = start; |
| 1736 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1737 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1738 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1739 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1740 | *ucs4 = (Py_UCS4)*str; |
| 1741 | |
| 1742 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1748 | static PyObject* |
| 1749 | get_latin1_char(unsigned char ch) |
| 1750 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1751 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1752 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1753 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1754 | if (!unicode) |
| 1755 | return NULL; |
| 1756 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1757 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | unicode_latin1[ch] = unicode; |
| 1759 | } |
| 1760 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1761 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | } |
| 1763 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1764 | PyObject * |
| 1765 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1766 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1767 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1768 | Py_UCS4 maxchar = 0; |
| 1769 | Py_ssize_t num_surrogates; |
| 1770 | |
| 1771 | if (u == NULL) |
| 1772 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1774 | /* If the Unicode data is known at construction time, we can apply |
| 1775 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1777 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1778 | if (size == 0) |
| 1779 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1781 | /* Single character Unicode objects in the Latin-1 range are |
| 1782 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1783 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1784 | return get_latin1_char((unsigned char)*u); |
| 1785 | |
| 1786 | /* If not empty and not single character, copy the Unicode data |
| 1787 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1788 | if (find_maxchar_surrogates(u, u + size, |
| 1789 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1790 | return NULL; |
| 1791 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1792 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1793 | if (!unicode) |
| 1794 | return NULL; |
| 1795 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1796 | switch (PyUnicode_KIND(unicode)) { |
| 1797 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1798 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1799 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1800 | break; |
| 1801 | case PyUnicode_2BYTE_KIND: |
| 1802 | #if Py_UNICODE_SIZE == 2 |
| 1803 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1804 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1805 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1806 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1807 | #endif |
| 1808 | break; |
| 1809 | case PyUnicode_4BYTE_KIND: |
| 1810 | #if SIZEOF_WCHAR_T == 2 |
| 1811 | /* This is the only case which has to process surrogates, thus |
| 1812 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1813 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1814 | #else |
| 1815 | assert(num_surrogates == 0); |
| 1816 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1817 | #endif |
| 1818 | break; |
| 1819 | default: |
| 1820 | assert(0 && "Impossible state"); |
| 1821 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1822 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1823 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1826 | PyObject * |
| 1827 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1828 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1829 | if (size < 0) { |
| 1830 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1831 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1832 | return NULL; |
| 1833 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1834 | if (u != NULL) |
| 1835 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1836 | else |
| 1837 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1840 | PyObject * |
| 1841 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1842 | { |
| 1843 | size_t size = strlen(u); |
| 1844 | if (size > PY_SSIZE_T_MAX) { |
| 1845 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1846 | return NULL; |
| 1847 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1848 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1851 | PyObject * |
| 1852 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1853 | { |
| 1854 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1855 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1856 | strlen(id->string), |
| 1857 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1858 | if (!id->object) |
| 1859 | return NULL; |
| 1860 | PyUnicode_InternInPlace(&id->object); |
| 1861 | assert(!id->next); |
| 1862 | id->next = static_strings; |
| 1863 | static_strings = id; |
| 1864 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1865 | return id->object; |
| 1866 | } |
| 1867 | |
| 1868 | void |
| 1869 | _PyUnicode_ClearStaticStrings() |
| 1870 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1871 | _Py_Identifier *tmp, *s = static_strings; |
| 1872 | while (s) { |
| 1873 | Py_DECREF(s->object); |
| 1874 | s->object = NULL; |
| 1875 | tmp = s->next; |
| 1876 | s->next = NULL; |
| 1877 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1878 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1879 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1882 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1883 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1884 | PyObject* |
| 1885 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1886 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1887 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1888 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1889 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1890 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1891 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1892 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1893 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1894 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1895 | unicode = PyUnicode_New(size, 127); |
| 1896 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1897 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1898 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1899 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1900 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1901 | } |
| 1902 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1903 | static Py_UCS4 |
| 1904 | kind_maxchar_limit(unsigned int kind) |
| 1905 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1906 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1907 | case PyUnicode_1BYTE_KIND: |
| 1908 | return 0x80; |
| 1909 | case PyUnicode_2BYTE_KIND: |
| 1910 | return 0x100; |
| 1911 | case PyUnicode_4BYTE_KIND: |
| 1912 | return 0x10000; |
| 1913 | default: |
| 1914 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1915 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1919 | Py_LOCAL_INLINE(Py_UCS4) |
| 1920 | align_maxchar(Py_UCS4 maxchar) |
| 1921 | { |
| 1922 | if (maxchar <= 127) |
| 1923 | return 127; |
| 1924 | else if (maxchar <= 255) |
| 1925 | return 255; |
| 1926 | else if (maxchar <= 65535) |
| 1927 | return 65535; |
| 1928 | else |
| 1929 | return MAX_UNICODE; |
| 1930 | } |
| 1931 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1932 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1933 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1934 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1935 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1936 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1937 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1938 | if (size == 0) |
| 1939 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1940 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1941 | if (size == 1) |
| 1942 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1943 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1944 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1945 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | if (!res) |
| 1947 | return NULL; |
| 1948 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1949 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1950 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1953 | static PyObject* |
| 1954 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1955 | { |
| 1956 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1957 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1958 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1959 | if (size == 0) |
| 1960 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1961 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1962 | if (size == 1) { |
| 1963 | Py_UCS4 ch = u[0]; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1964 | int kind; |
| 1965 | void *data; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1966 | if (ch < 256) |
| 1967 | return get_latin1_char((unsigned char)ch); |
| 1968 | |
| 1969 | res = PyUnicode_New(1, ch); |
| 1970 | if (res == NULL) |
| 1971 | return NULL; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1972 | kind = PyUnicode_KIND(res); |
| 1973 | data = PyUnicode_DATA(res); |
| 1974 | PyUnicode_WRITE(kind, data, 0, ch); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1975 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1976 | return res; |
| 1977 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1978 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1979 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1980 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 | if (!res) |
| 1982 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1983 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1984 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1985 | else { |
| 1986 | _PyUnicode_CONVERT_BYTES( |
| 1987 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1988 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1989 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1990 | return res; |
| 1991 | } |
| 1992 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1993 | static PyObject* |
| 1994 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1995 | { |
| 1996 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1997 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1998 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1999 | if (size == 0) |
| 2000 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2001 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2002 | if (size == 1) { |
| 2003 | Py_UCS4 ch = u[0]; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2004 | int kind; |
| 2005 | void *data; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2006 | if (ch < 256) |
| 2007 | return get_latin1_char((unsigned char)ch); |
| 2008 | |
| 2009 | res = PyUnicode_New(1, ch); |
| 2010 | if (res == NULL) |
| 2011 | return NULL; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2012 | kind = PyUnicode_KIND(res); |
| 2013 | data = PyUnicode_DATA(res); |
| 2014 | PyUnicode_WRITE(kind, data, 0, ch); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2015 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 2016 | return res; |
| 2017 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2018 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2019 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2020 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2021 | if (!res) |
| 2022 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2023 | if (max_char < 256) |
| 2024 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2025 | PyUnicode_1BYTE_DATA(res)); |
| 2026 | else if (max_char < 0x10000) |
| 2027 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2028 | PyUnicode_2BYTE_DATA(res)); |
| 2029 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2030 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2031 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2032 | return res; |
| 2033 | } |
| 2034 | |
| 2035 | PyObject* |
| 2036 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2037 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2038 | if (size < 0) { |
| 2039 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2040 | return NULL; |
| 2041 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2042 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2043 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2044 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2045 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2046 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2047 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2048 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2049 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2050 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2051 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2052 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2055 | Py_UCS4 |
| 2056 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2057 | { |
| 2058 | enum PyUnicode_Kind kind; |
| 2059 | void *startptr, *endptr; |
| 2060 | |
| 2061 | assert(PyUnicode_IS_READY(unicode)); |
| 2062 | assert(0 <= start); |
| 2063 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2064 | assert(start <= end); |
| 2065 | |
| 2066 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2067 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2068 | |
| 2069 | if (start == end) |
| 2070 | return 127; |
| 2071 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2072 | if (PyUnicode_IS_ASCII(unicode)) |
| 2073 | return 127; |
| 2074 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2075 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2076 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2077 | endptr = (char *)startptr + end * kind; |
| 2078 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2079 | switch(kind) { |
| 2080 | case PyUnicode_1BYTE_KIND: |
| 2081 | return ucs1lib_find_max_char(startptr, endptr); |
| 2082 | case PyUnicode_2BYTE_KIND: |
| 2083 | return ucs2lib_find_max_char(startptr, endptr); |
| 2084 | case PyUnicode_4BYTE_KIND: |
| 2085 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2086 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2087 | assert(0); |
| 2088 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2089 | } |
| 2090 | } |
| 2091 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2092 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2093 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2094 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2095 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2096 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2097 | { |
| 2098 | PyObject *unicode, *copy; |
| 2099 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2100 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2101 | unsigned int kind; |
| 2102 | |
| 2103 | assert(p_unicode != NULL); |
| 2104 | unicode = *p_unicode; |
| 2105 | assert(PyUnicode_IS_READY(unicode)); |
| 2106 | if (PyUnicode_IS_ASCII(unicode)) |
| 2107 | return; |
| 2108 | |
| 2109 | len = PyUnicode_GET_LENGTH(unicode); |
| 2110 | kind = PyUnicode_KIND(unicode); |
| 2111 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2112 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2113 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2114 | if (max_char >= 128) |
| 2115 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2116 | } |
| 2117 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2118 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2119 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2120 | if (max_char >= 256) |
| 2121 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2122 | } |
| 2123 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2124 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2125 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2126 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2127 | if (max_char >= 0x10000) |
| 2128 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2129 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2130 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2131 | if (copy != NULL) |
| 2132 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2133 | Py_DECREF(unicode); |
| 2134 | *p_unicode = copy; |
| 2135 | } |
| 2136 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2137 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2138 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2139 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2140 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2141 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2142 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2143 | if (!PyUnicode_Check(unicode)) { |
| 2144 | PyErr_BadInternalCall(); |
| 2145 | return NULL; |
| 2146 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2147 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2148 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2149 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2150 | length = PyUnicode_GET_LENGTH(unicode); |
| 2151 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2152 | if (!copy) |
| 2153 | return NULL; |
| 2154 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2155 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2156 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2157 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2158 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2159 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2160 | } |
| 2161 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2163 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2164 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2165 | |
| 2166 | void* |
| 2167 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2168 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2169 | Py_ssize_t len; |
| 2170 | void *result; |
| 2171 | unsigned int skind; |
| 2172 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2173 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2174 | return NULL; |
| 2175 | |
| 2176 | len = PyUnicode_GET_LENGTH(s); |
| 2177 | skind = PyUnicode_KIND(s); |
| 2178 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2179 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2180 | return NULL; |
| 2181 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2182 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2183 | case PyUnicode_2BYTE_KIND: |
| 2184 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2185 | if (!result) |
| 2186 | return PyErr_NoMemory(); |
| 2187 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2188 | _PyUnicode_CONVERT_BYTES( |
| 2189 | Py_UCS1, Py_UCS2, |
| 2190 | PyUnicode_1BYTE_DATA(s), |
| 2191 | PyUnicode_1BYTE_DATA(s) + len, |
| 2192 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2193 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2194 | case PyUnicode_4BYTE_KIND: |
| 2195 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2196 | if (!result) |
| 2197 | return PyErr_NoMemory(); |
| 2198 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2199 | _PyUnicode_CONVERT_BYTES( |
| 2200 | Py_UCS2, Py_UCS4, |
| 2201 | PyUnicode_2BYTE_DATA(s), |
| 2202 | PyUnicode_2BYTE_DATA(s) + len, |
| 2203 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2204 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2205 | else { |
| 2206 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2207 | _PyUnicode_CONVERT_BYTES( |
| 2208 | Py_UCS1, Py_UCS4, |
| 2209 | PyUnicode_1BYTE_DATA(s), |
| 2210 | PyUnicode_1BYTE_DATA(s) + len, |
| 2211 | result); |
| 2212 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2213 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2214 | default: |
| 2215 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2216 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2217 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2218 | return NULL; |
| 2219 | } |
| 2220 | |
| 2221 | static Py_UCS4* |
| 2222 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2223 | int copy_null) |
| 2224 | { |
| 2225 | int kind; |
| 2226 | void *data; |
| 2227 | Py_ssize_t len, targetlen; |
| 2228 | if (PyUnicode_READY(string) == -1) |
| 2229 | return NULL; |
| 2230 | kind = PyUnicode_KIND(string); |
| 2231 | data = PyUnicode_DATA(string); |
| 2232 | len = PyUnicode_GET_LENGTH(string); |
| 2233 | targetlen = len; |
| 2234 | if (copy_null) |
| 2235 | targetlen++; |
| 2236 | if (!target) { |
| 2237 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2238 | PyErr_NoMemory(); |
| 2239 | return NULL; |
| 2240 | } |
| 2241 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2242 | if (!target) { |
| 2243 | PyErr_NoMemory(); |
| 2244 | return NULL; |
| 2245 | } |
| 2246 | } |
| 2247 | else { |
| 2248 | if (targetsize < targetlen) { |
| 2249 | PyErr_Format(PyExc_SystemError, |
| 2250 | "string is longer than the buffer"); |
| 2251 | if (copy_null && 0 < targetsize) |
| 2252 | target[0] = 0; |
| 2253 | return NULL; |
| 2254 | } |
| 2255 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2256 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2257 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2258 | _PyUnicode_CONVERT_BYTES(Py_UCS1, Py_UCS4, start, start + len, target); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2259 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2260 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2261 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2262 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2263 | } |
| 2264 | else { |
| 2265 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2266 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2267 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2268 | if (copy_null) |
| 2269 | target[len] = 0; |
| 2270 | return target; |
| 2271 | } |
| 2272 | |
| 2273 | Py_UCS4* |
| 2274 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2275 | int copy_null) |
| 2276 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2277 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2278 | PyErr_BadInternalCall(); |
| 2279 | return NULL; |
| 2280 | } |
| 2281 | return as_ucs4(string, target, targetsize, copy_null); |
| 2282 | } |
| 2283 | |
| 2284 | Py_UCS4* |
| 2285 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2286 | { |
| 2287 | return as_ucs4(string, NULL, 0, 1); |
| 2288 | } |
| 2289 | |
| 2290 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2291 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2292 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2293 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2294 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2295 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2296 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2297 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2298 | PyErr_BadInternalCall(); |
| 2299 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2302 | if (size == -1) { |
| 2303 | size = wcslen(w); |
| 2304 | } |
| 2305 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2306 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2309 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2310 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2311 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2312 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2313 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2314 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2315 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2316 | if (longflag) |
| 2317 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2318 | else if (longlongflag) { |
| 2319 | /* longlongflag should only ever be nonzero on machines with |
| 2320 | HAVE_LONG_LONG defined */ |
| 2321 | #ifdef HAVE_LONG_LONG |
| 2322 | char *f = PY_FORMAT_LONG_LONG; |
| 2323 | while (*f) |
| 2324 | *fmt++ = *f++; |
| 2325 | #else |
| 2326 | /* we shouldn't ever get here */ |
| 2327 | assert(0); |
| 2328 | *fmt++ = 'l'; |
| 2329 | #endif |
| 2330 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2331 | else if (size_tflag) { |
| 2332 | char *f = PY_FORMAT_SIZE_T; |
| 2333 | while (*f) |
| 2334 | *fmt++ = *f++; |
| 2335 | } |
| 2336 | *fmt++ = c; |
| 2337 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2340 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2341 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2342 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2343 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2344 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2345 | static int |
| 2346 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2347 | Py_ssize_t width, Py_ssize_t precision) |
| 2348 | { |
| 2349 | Py_ssize_t length, fill, arglen; |
| 2350 | Py_UCS4 maxchar; |
| 2351 | |
| 2352 | if (PyUnicode_READY(str) == -1) |
| 2353 | return -1; |
| 2354 | |
| 2355 | length = PyUnicode_GET_LENGTH(str); |
| 2356 | if ((precision == -1 || precision >= length) |
| 2357 | && width <= length) |
| 2358 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2359 | |
| 2360 | if (precision != -1) |
| 2361 | length = Py_MIN(precision, length); |
| 2362 | |
| 2363 | arglen = Py_MAX(length, width); |
| 2364 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2365 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2366 | else |
| 2367 | maxchar = writer->maxchar; |
| 2368 | |
| 2369 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2370 | return -1; |
| 2371 | |
| 2372 | if (width > length) { |
| 2373 | fill = width - length; |
| 2374 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2375 | return -1; |
| 2376 | writer->pos += fill; |
| 2377 | } |
| 2378 | |
| 2379 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2380 | str, 0, length); |
| 2381 | writer->pos += length; |
| 2382 | return 0; |
| 2383 | } |
| 2384 | |
| 2385 | static int |
| 2386 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2387 | Py_ssize_t width, Py_ssize_t precision) |
| 2388 | { |
| 2389 | /* UTF-8 */ |
| 2390 | Py_ssize_t length; |
| 2391 | PyObject *unicode; |
| 2392 | int res; |
| 2393 | |
| 2394 | length = strlen(str); |
| 2395 | if (precision != -1) |
| 2396 | length = Py_MIN(length, precision); |
| 2397 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2398 | if (unicode == NULL) |
| 2399 | return -1; |
| 2400 | |
| 2401 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2402 | Py_DECREF(unicode); |
| 2403 | return res; |
| 2404 | } |
| 2405 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2406 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2407 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2408 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2409 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2410 | const char *p; |
| 2411 | Py_ssize_t len; |
| 2412 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2413 | Py_ssize_t width; |
| 2414 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2415 | int longflag; |
| 2416 | int longlongflag; |
| 2417 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2418 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2419 | |
| 2420 | p = f; |
| 2421 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2422 | zeropad = 0; |
| 2423 | if (*f == '0') { |
| 2424 | zeropad = 1; |
| 2425 | f++; |
| 2426 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2427 | |
| 2428 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2429 | width = -1; |
| 2430 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2431 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2432 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2433 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2434 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2435 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2436 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2437 | return NULL; |
| 2438 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2439 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2440 | f++; |
| 2441 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2442 | } |
| 2443 | precision = -1; |
| 2444 | if (*f == '.') { |
| 2445 | f++; |
| 2446 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2447 | precision = (*f - '0'); |
| 2448 | f++; |
| 2449 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2450 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2451 | PyErr_SetString(PyExc_ValueError, |
| 2452 | "precision too big"); |
| 2453 | return NULL; |
| 2454 | } |
| 2455 | precision = (precision * 10) + (*f - '0'); |
| 2456 | f++; |
| 2457 | } |
| 2458 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2459 | if (*f == '%') { |
| 2460 | /* "%.3%s" => f points to "3" */ |
| 2461 | f--; |
| 2462 | } |
| 2463 | } |
| 2464 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2465 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2466 | f--; |
| 2467 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2468 | |
| 2469 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2470 | longflag = 0; |
| 2471 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2472 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2473 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2474 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2475 | longflag = 1; |
| 2476 | ++f; |
| 2477 | } |
| 2478 | #ifdef HAVE_LONG_LONG |
| 2479 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2480 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2481 | longlongflag = 1; |
| 2482 | f += 2; |
| 2483 | } |
| 2484 | #endif |
| 2485 | } |
| 2486 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2487 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2488 | size_tflag = 1; |
| 2489 | ++f; |
| 2490 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2491 | |
| 2492 | if (f[1] == '\0') |
| 2493 | writer->overallocate = 0; |
| 2494 | |
| 2495 | switch (*f) { |
| 2496 | case 'c': |
| 2497 | { |
| 2498 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2499 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2500 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2501 | "character argument not in range(0x110000)"); |
| 2502 | return NULL; |
| 2503 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2504 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2505 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | case 'i': |
| 2510 | case 'd': |
| 2511 | case 'u': |
| 2512 | case 'x': |
| 2513 | { |
| 2514 | /* used by sprintf */ |
| 2515 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2516 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2517 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2518 | |
| 2519 | if (*f == 'u') { |
| 2520 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2521 | |
| 2522 | if (longflag) |
| 2523 | len = sprintf(buffer, fmt, |
| 2524 | va_arg(*vargs, unsigned long)); |
| 2525 | #ifdef HAVE_LONG_LONG |
| 2526 | else if (longlongflag) |
| 2527 | len = sprintf(buffer, fmt, |
| 2528 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2529 | #endif |
| 2530 | else if (size_tflag) |
| 2531 | len = sprintf(buffer, fmt, |
| 2532 | va_arg(*vargs, size_t)); |
| 2533 | else |
| 2534 | len = sprintf(buffer, fmt, |
| 2535 | va_arg(*vargs, unsigned int)); |
| 2536 | } |
| 2537 | else if (*f == 'x') { |
| 2538 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2539 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2540 | } |
| 2541 | else { |
| 2542 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2543 | |
| 2544 | if (longflag) |
| 2545 | len = sprintf(buffer, fmt, |
| 2546 | va_arg(*vargs, long)); |
| 2547 | #ifdef HAVE_LONG_LONG |
| 2548 | else if (longlongflag) |
| 2549 | len = sprintf(buffer, fmt, |
| 2550 | va_arg(*vargs, PY_LONG_LONG)); |
| 2551 | #endif |
| 2552 | else if (size_tflag) |
| 2553 | len = sprintf(buffer, fmt, |
| 2554 | va_arg(*vargs, Py_ssize_t)); |
| 2555 | else |
| 2556 | len = sprintf(buffer, fmt, |
| 2557 | va_arg(*vargs, int)); |
| 2558 | } |
| 2559 | assert(len >= 0); |
| 2560 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2561 | if (precision < len) |
| 2562 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2563 | |
| 2564 | arglen = Py_MAX(precision, width); |
| 2565 | assert(ucs1lib_find_max_char((Py_UCS1*)buffer, (Py_UCS1*)buffer + len) <= 127); |
| 2566 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2567 | return NULL; |
| 2568 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2569 | if (width > precision) { |
| 2570 | Py_UCS4 fillchar; |
| 2571 | fill = width - precision; |
| 2572 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2573 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2574 | return NULL; |
| 2575 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2576 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2577 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2578 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2579 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2580 | return NULL; |
| 2581 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2582 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2583 | |
| 2584 | unicode_write_cstr(writer->buffer, writer->pos, buffer, len); |
| 2585 | writer->pos += len; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2586 | break; |
| 2587 | } |
| 2588 | |
| 2589 | case 'p': |
| 2590 | { |
| 2591 | char number[MAX_LONG_LONG_CHARS]; |
| 2592 | |
| 2593 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2594 | assert(len >= 0); |
| 2595 | |
| 2596 | /* %p is ill-defined: ensure leading 0x. */ |
| 2597 | if (number[1] == 'X') |
| 2598 | number[1] = 'x'; |
| 2599 | else if (number[1] != 'x') { |
| 2600 | memmove(number + 2, number, |
| 2601 | strlen(number) + 1); |
| 2602 | number[0] = '0'; |
| 2603 | number[1] = 'x'; |
| 2604 | len += 2; |
| 2605 | } |
| 2606 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2607 | assert(ucs1lib_find_max_char((Py_UCS1*)number, (Py_UCS1*)number + len) <= 127); |
| 2608 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2609 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2610 | unicode_write_cstr(writer->buffer, writer->pos, number, len); |
| 2611 | writer->pos += len; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2612 | break; |
| 2613 | } |
| 2614 | |
| 2615 | case 's': |
| 2616 | { |
| 2617 | /* UTF-8 */ |
| 2618 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2619 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2620 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2621 | break; |
| 2622 | } |
| 2623 | |
| 2624 | case 'U': |
| 2625 | { |
| 2626 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2627 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2628 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2629 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2630 | return NULL; |
| 2631 | break; |
| 2632 | } |
| 2633 | |
| 2634 | case 'V': |
| 2635 | { |
| 2636 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2637 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2638 | if (obj) { |
| 2639 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2640 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2641 | return NULL; |
| 2642 | } |
| 2643 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2644 | assert(str != NULL); |
| 2645 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2646 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2647 | } |
| 2648 | break; |
| 2649 | } |
| 2650 | |
| 2651 | case 'S': |
| 2652 | { |
| 2653 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2654 | PyObject *str; |
| 2655 | assert(obj); |
| 2656 | str = PyObject_Str(obj); |
| 2657 | if (!str) |
| 2658 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2659 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2660 | Py_DECREF(str); |
| 2661 | return NULL; |
| 2662 | } |
| 2663 | Py_DECREF(str); |
| 2664 | break; |
| 2665 | } |
| 2666 | |
| 2667 | case 'R': |
| 2668 | { |
| 2669 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2670 | PyObject *repr; |
| 2671 | assert(obj); |
| 2672 | repr = PyObject_Repr(obj); |
| 2673 | if (!repr) |
| 2674 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2675 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2676 | Py_DECREF(repr); |
| 2677 | return NULL; |
| 2678 | } |
| 2679 | Py_DECREF(repr); |
| 2680 | break; |
| 2681 | } |
| 2682 | |
| 2683 | case 'A': |
| 2684 | { |
| 2685 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2686 | PyObject *ascii; |
| 2687 | assert(obj); |
| 2688 | ascii = PyObject_ASCII(obj); |
| 2689 | if (!ascii) |
| 2690 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2691 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2692 | Py_DECREF(ascii); |
| 2693 | return NULL; |
| 2694 | } |
| 2695 | Py_DECREF(ascii); |
| 2696 | break; |
| 2697 | } |
| 2698 | |
| 2699 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2700 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2701 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2702 | break; |
| 2703 | |
| 2704 | default: |
| 2705 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2706 | of the format string to the output string. (we cannot just |
| 2707 | skip the code, since there's no way to know what's in the |
| 2708 | argument list) */ |
| 2709 | len = strlen(p); |
| 2710 | if (_PyUnicodeWriter_WriteCstr(writer, p, len) == -1) |
| 2711 | return NULL; |
| 2712 | f = p+len; |
| 2713 | return f; |
| 2714 | } |
| 2715 | |
| 2716 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2717 | return f; |
| 2718 | } |
| 2719 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2720 | PyObject * |
| 2721 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2722 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2723 | va_list vargs2; |
| 2724 | const char *f; |
| 2725 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2726 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2727 | _PyUnicodeWriter_Init(&writer); |
| 2728 | writer.min_length = strlen(format) + 100; |
| 2729 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2730 | |
| 2731 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2732 | Copy it to be able to pass a reference to a subfunction. */ |
| 2733 | Py_VA_COPY(vargs2, vargs); |
| 2734 | |
| 2735 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2736 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2737 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2738 | if (f == NULL) |
| 2739 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2740 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2741 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2742 | const char *p; |
| 2743 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2744 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2745 | p = f; |
| 2746 | do |
| 2747 | { |
| 2748 | if ((unsigned char)*p > 127) { |
| 2749 | PyErr_Format(PyExc_ValueError, |
| 2750 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2751 | "string, got a non-ASCII byte: 0x%02x", |
| 2752 | (unsigned char)*p); |
| 2753 | return NULL; |
| 2754 | } |
| 2755 | p++; |
| 2756 | } |
| 2757 | while (*p != '\0' && *p != '%'); |
| 2758 | len = p - f; |
| 2759 | |
| 2760 | if (*p == '\0') |
| 2761 | writer.overallocate = 0; |
| 2762 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
| 2763 | goto fail; |
| 2764 | unicode_write_cstr(writer.buffer, writer.pos, f, len); |
| 2765 | writer.pos += len; |
| 2766 | |
| 2767 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2768 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2769 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2770 | return _PyUnicodeWriter_Finish(&writer); |
| 2771 | |
| 2772 | fail: |
| 2773 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2774 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2777 | PyObject * |
| 2778 | PyUnicode_FromFormat(const char *format, ...) |
| 2779 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2780 | PyObject* ret; |
| 2781 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2782 | |
| 2783 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2784 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2785 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2786 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2787 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2788 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2789 | va_end(vargs); |
| 2790 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2793 | #ifdef HAVE_WCHAR_H |
| 2794 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2795 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2796 | convert a Unicode object to a wide character string. |
| 2797 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2798 | - 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] | 2799 | character) required to convert the unicode object. Ignore size argument. |
| 2800 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2801 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2802 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2803 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2804 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2805 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2806 | wchar_t *w, |
| 2807 | Py_ssize_t size) |
| 2808 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2809 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2810 | const wchar_t *wstr; |
| 2811 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2812 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2813 | if (wstr == NULL) |
| 2814 | return -1; |
| 2815 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2816 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2817 | if (size > res) |
| 2818 | size = res + 1; |
| 2819 | else |
| 2820 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2821 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2822 | return res; |
| 2823 | } |
| 2824 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2825 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2829 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2830 | wchar_t *w, |
| 2831 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2832 | { |
| 2833 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2834 | PyErr_BadInternalCall(); |
| 2835 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2836 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2837 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2840 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2841 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2842 | Py_ssize_t *size) |
| 2843 | { |
| 2844 | wchar_t* buffer; |
| 2845 | Py_ssize_t buflen; |
| 2846 | |
| 2847 | if (unicode == NULL) { |
| 2848 | PyErr_BadInternalCall(); |
| 2849 | return NULL; |
| 2850 | } |
| 2851 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2852 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2853 | if (buflen == -1) |
| 2854 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2855 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2856 | PyErr_NoMemory(); |
| 2857 | return NULL; |
| 2858 | } |
| 2859 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2860 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2861 | if (buffer == NULL) { |
| 2862 | PyErr_NoMemory(); |
| 2863 | return NULL; |
| 2864 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2865 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2866 | if (buflen == -1) { |
| 2867 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2868 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2869 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2870 | if (size != NULL) |
| 2871 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2872 | return buffer; |
| 2873 | } |
| 2874 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2875 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2876 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2877 | PyObject * |
| 2878 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2879 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2880 | PyObject *v; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2881 | void *data; |
| 2882 | int kind; |
| 2883 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2884 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2885 | PyErr_SetString(PyExc_ValueError, |
| 2886 | "chr() arg not in range(0x110000)"); |
| 2887 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2888 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2889 | |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2890 | if ((Py_UCS4)ordinal < 256) |
| 2891 | return get_latin1_char((unsigned char)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2892 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2893 | v = PyUnicode_New(1, ordinal); |
| 2894 | if (v == NULL) |
| 2895 | return NULL; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2896 | kind = PyUnicode_KIND(v); |
| 2897 | data = PyUnicode_DATA(v); |
| 2898 | PyUnicode_WRITE(kind, data, 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2899 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2900 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2903 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2904 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2905 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2906 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2907 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2908 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2909 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2910 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2911 | Py_INCREF(obj); |
| 2912 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2913 | } |
| 2914 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2915 | /* For a Unicode subtype that's not a Unicode object, |
| 2916 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2917 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2918 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2919 | PyErr_Format(PyExc_TypeError, |
| 2920 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2921 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2922 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2925 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2926 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2927 | const char *encoding, |
| 2928 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2929 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2930 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2931 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2932 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2933 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2934 | PyErr_BadInternalCall(); |
| 2935 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2936 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2937 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2938 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2939 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2940 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2941 | _Py_RETURN_UNICODE_EMPTY(); |
| 2942 | v = PyUnicode_Decode( |
| 2943 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2944 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2945 | return v; |
| 2946 | } |
| 2947 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2948 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2949 | PyErr_SetString(PyExc_TypeError, |
| 2950 | "decoding str is not supported"); |
| 2951 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2952 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2953 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2954 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2955 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2956 | PyErr_Format(PyExc_TypeError, |
| 2957 | "coercing to str: need bytes, bytearray " |
| 2958 | "or buffer-like object, %.80s found", |
| 2959 | Py_TYPE(obj)->tp_name); |
| 2960 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2961 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2962 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2963 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2964 | PyBuffer_Release(&buffer); |
| 2965 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2966 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2967 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2968 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2969 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2970 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2973 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2974 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2975 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2976 | int |
| 2977 | _Py_normalize_encoding(const char *encoding, |
| 2978 | char *lower, |
| 2979 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2980 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2981 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2982 | char *l; |
| 2983 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2984 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2985 | if (encoding == NULL) { |
| 2986 | strcpy(lower, "utf-8"); |
| 2987 | return 1; |
| 2988 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2989 | e = encoding; |
| 2990 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2991 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2992 | while (*e) { |
| 2993 | if (l == l_end) |
| 2994 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2995 | if (Py_ISUPPER(*e)) { |
| 2996 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2997 | } |
| 2998 | else if (*e == '_') { |
| 2999 | *l++ = '-'; |
| 3000 | e++; |
| 3001 | } |
| 3002 | else { |
| 3003 | *l++ = *e++; |
| 3004 | } |
| 3005 | } |
| 3006 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3007 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3010 | PyObject * |
| 3011 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3012 | Py_ssize_t size, |
| 3013 | const char *encoding, |
| 3014 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3015 | { |
| 3016 | PyObject *buffer = NULL, *unicode; |
| 3017 | Py_buffer info; |
| 3018 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3019 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3020 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3021 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3022 | if ((strcmp(lower, "utf-8") == 0) || |
| 3023 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3024 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3025 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3026 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3027 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3028 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3029 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3030 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3031 | else if (strcmp(lower, "mbcs") == 0) |
| 3032 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3033 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3034 | else if (strcmp(lower, "ascii") == 0) |
| 3035 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3036 | else if (strcmp(lower, "utf-16") == 0) |
| 3037 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3038 | else if (strcmp(lower, "utf-32") == 0) |
| 3039 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3040 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3041 | |
| 3042 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3043 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3044 | 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] | 3045 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3046 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3047 | if (buffer == NULL) |
| 3048 | goto onError; |
| 3049 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 3050 | if (unicode == NULL) |
| 3051 | goto onError; |
| 3052 | if (!PyUnicode_Check(unicode)) { |
| 3053 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3054 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3055 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | Py_DECREF(unicode); |
| 3057 | goto onError; |
| 3058 | } |
| 3059 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3060 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3061 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3062 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3063 | Py_XDECREF(buffer); |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3067 | PyObject * |
| 3068 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3069 | const char *encoding, |
| 3070 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3071 | { |
| 3072 | PyObject *v; |
| 3073 | |
| 3074 | if (!PyUnicode_Check(unicode)) { |
| 3075 | PyErr_BadArgument(); |
| 3076 | goto onError; |
| 3077 | } |
| 3078 | |
| 3079 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3080 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3081 | |
| 3082 | /* Decode via the codec registry */ |
| 3083 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3084 | if (v == NULL) |
| 3085 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3086 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3087 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3088 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3089 | return NULL; |
| 3090 | } |
| 3091 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3092 | PyObject * |
| 3093 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3094 | const char *encoding, |
| 3095 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3096 | { |
| 3097 | PyObject *v; |
| 3098 | |
| 3099 | if (!PyUnicode_Check(unicode)) { |
| 3100 | PyErr_BadArgument(); |
| 3101 | goto onError; |
| 3102 | } |
| 3103 | |
| 3104 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3105 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3106 | |
| 3107 | /* Decode via the codec registry */ |
| 3108 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3109 | if (v == NULL) |
| 3110 | goto onError; |
| 3111 | if (!PyUnicode_Check(v)) { |
| 3112 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3113 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3114 | Py_TYPE(v)->tp_name); |
| 3115 | Py_DECREF(v); |
| 3116 | goto onError; |
| 3117 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3118 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3120 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3121 | return NULL; |
| 3122 | } |
| 3123 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3124 | PyObject * |
| 3125 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3126 | Py_ssize_t size, |
| 3127 | const char *encoding, |
| 3128 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3129 | { |
| 3130 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3131 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | unicode = PyUnicode_FromUnicode(s, size); |
| 3133 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3134 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3135 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3136 | Py_DECREF(unicode); |
| 3137 | return v; |
| 3138 | } |
| 3139 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3140 | PyObject * |
| 3141 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3142 | const char *encoding, |
| 3143 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3144 | { |
| 3145 | PyObject *v; |
| 3146 | |
| 3147 | if (!PyUnicode_Check(unicode)) { |
| 3148 | PyErr_BadArgument(); |
| 3149 | goto onError; |
| 3150 | } |
| 3151 | |
| 3152 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3153 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3154 | |
| 3155 | /* Encode via the codec registry */ |
| 3156 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3157 | if (v == NULL) |
| 3158 | goto onError; |
| 3159 | return v; |
| 3160 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3161 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3162 | return NULL; |
| 3163 | } |
| 3164 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3165 | static size_t |
| 3166 | wcstombs_errorpos(const wchar_t *wstr) |
| 3167 | { |
| 3168 | size_t len; |
| 3169 | #if SIZEOF_WCHAR_T == 2 |
| 3170 | wchar_t buf[3]; |
| 3171 | #else |
| 3172 | wchar_t buf[2]; |
| 3173 | #endif |
| 3174 | char outbuf[MB_LEN_MAX]; |
| 3175 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3176 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3177 | #if SIZEOF_WCHAR_T == 2 |
| 3178 | buf[2] = 0; |
| 3179 | #else |
| 3180 | buf[1] = 0; |
| 3181 | #endif |
| 3182 | start = wstr; |
| 3183 | while (*wstr != L'\0') |
| 3184 | { |
| 3185 | previous = wstr; |
| 3186 | #if SIZEOF_WCHAR_T == 2 |
| 3187 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3188 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3189 | { |
| 3190 | buf[0] = wstr[0]; |
| 3191 | buf[1] = wstr[1]; |
| 3192 | wstr += 2; |
| 3193 | } |
| 3194 | else { |
| 3195 | buf[0] = *wstr; |
| 3196 | buf[1] = 0; |
| 3197 | wstr++; |
| 3198 | } |
| 3199 | #else |
| 3200 | buf[0] = *wstr; |
| 3201 | wstr++; |
| 3202 | #endif |
| 3203 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3204 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3205 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3209 | return 0; |
| 3210 | } |
| 3211 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3212 | static int |
| 3213 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3214 | { |
| 3215 | if (errors == NULL) { |
| 3216 | *surrogateescape = 0; |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
| 3220 | if (strcmp(errors, "strict") == 0) { |
| 3221 | *surrogateescape = 0; |
| 3222 | return 0; |
| 3223 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3224 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3225 | *surrogateescape = 1; |
| 3226 | return 0; |
| 3227 | } |
| 3228 | PyErr_Format(PyExc_ValueError, |
| 3229 | "only 'strict' and 'surrogateescape' error handlers " |
| 3230 | "are supported, not '%s'", |
| 3231 | errors); |
| 3232 | return -1; |
| 3233 | } |
| 3234 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3235 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3236 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3237 | { |
| 3238 | Py_ssize_t wlen, wlen2; |
| 3239 | wchar_t *wstr; |
| 3240 | PyObject *bytes = NULL; |
| 3241 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3242 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3243 | PyObject *exc; |
| 3244 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3245 | int surrogateescape; |
| 3246 | |
| 3247 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3248 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3249 | |
| 3250 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3251 | if (wstr == NULL) |
| 3252 | return NULL; |
| 3253 | |
| 3254 | wlen2 = wcslen(wstr); |
| 3255 | if (wlen2 != wlen) { |
| 3256 | PyMem_Free(wstr); |
| 3257 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3258 | return NULL; |
| 3259 | } |
| 3260 | |
| 3261 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3262 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3263 | char *str; |
| 3264 | |
| 3265 | str = _Py_wchar2char(wstr, &error_pos); |
| 3266 | if (str == NULL) { |
| 3267 | if (error_pos == (size_t)-1) { |
| 3268 | PyErr_NoMemory(); |
| 3269 | PyMem_Free(wstr); |
| 3270 | return NULL; |
| 3271 | } |
| 3272 | else { |
| 3273 | goto encode_error; |
| 3274 | } |
| 3275 | } |
| 3276 | PyMem_Free(wstr); |
| 3277 | |
| 3278 | bytes = PyBytes_FromString(str); |
| 3279 | PyMem_Free(str); |
| 3280 | } |
| 3281 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3282 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3283 | size_t len, len2; |
| 3284 | |
| 3285 | len = wcstombs(NULL, wstr, 0); |
| 3286 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3287 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3288 | goto encode_error; |
| 3289 | } |
| 3290 | |
| 3291 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3292 | if (bytes == NULL) { |
| 3293 | PyMem_Free(wstr); |
| 3294 | return NULL; |
| 3295 | } |
| 3296 | |
| 3297 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3298 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3299 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3300 | goto encode_error; |
| 3301 | } |
| 3302 | PyMem_Free(wstr); |
| 3303 | } |
| 3304 | return bytes; |
| 3305 | |
| 3306 | encode_error: |
| 3307 | errmsg = strerror(errno); |
| 3308 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3309 | |
| 3310 | if (error_pos == (size_t)-1) |
| 3311 | error_pos = wcstombs_errorpos(wstr); |
| 3312 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3313 | PyMem_Free(wstr); |
| 3314 | Py_XDECREF(bytes); |
| 3315 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3316 | if (errmsg != NULL) { |
| 3317 | size_t errlen; |
| 3318 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3319 | if (wstr != NULL) { |
| 3320 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3321 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3322 | } else |
| 3323 | errmsg = NULL; |
| 3324 | } |
| 3325 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3326 | reason = PyUnicode_FromString( |
| 3327 | "wcstombs() encountered an unencodable " |
| 3328 | "wide character"); |
| 3329 | if (reason == NULL) |
| 3330 | return NULL; |
| 3331 | |
| 3332 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3333 | "locale", unicode, |
| 3334 | (Py_ssize_t)error_pos, |
| 3335 | (Py_ssize_t)(error_pos+1), |
| 3336 | reason); |
| 3337 | Py_DECREF(reason); |
| 3338 | if (exc != NULL) { |
| 3339 | PyCodec_StrictErrors(exc); |
| 3340 | Py_XDECREF(exc); |
| 3341 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3342 | return NULL; |
| 3343 | } |
| 3344 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3345 | PyObject * |
| 3346 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3347 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3348 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3349 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3350 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3351 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3352 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3353 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3354 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3355 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3356 | the Python codec requires to encode at least its own filename. Use the C |
| 3357 | version of the locale codec until the codec registry is initialized and |
| 3358 | the Python codec is loaded. |
| 3359 | |
| 3360 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3361 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3362 | subinterpreters. */ |
| 3363 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3364 | return PyUnicode_AsEncodedString(unicode, |
| 3365 | Py_FileSystemDefaultEncoding, |
| 3366 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3367 | } |
| 3368 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3369 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3370 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3371 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3374 | PyObject * |
| 3375 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3376 | const char *encoding, |
| 3377 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 | { |
| 3379 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3380 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3381 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3382 | if (!PyUnicode_Check(unicode)) { |
| 3383 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3384 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3385 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3386 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3387 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3388 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3389 | if ((strcmp(lower, "utf-8") == 0) || |
| 3390 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3391 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3392 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3393 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3394 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3395 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3396 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3397 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3398 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3399 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3400 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3401 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3402 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3403 | else if (strcmp(lower, "mbcs") == 0) |
| 3404 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3405 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3406 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3407 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3408 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3409 | |
| 3410 | /* Encode via the codec registry */ |
| 3411 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3412 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3413 | return NULL; |
| 3414 | |
| 3415 | /* The normal path */ |
| 3416 | if (PyBytes_Check(v)) |
| 3417 | return v; |
| 3418 | |
| 3419 | /* 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] | 3420 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3421 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3422 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3423 | |
| 3424 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3425 | "encoder %s returned bytearray instead of bytes", |
| 3426 | encoding); |
| 3427 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3428 | Py_DECREF(v); |
| 3429 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3430 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3431 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3432 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3433 | Py_DECREF(v); |
| 3434 | return b; |
| 3435 | } |
| 3436 | |
| 3437 | PyErr_Format(PyExc_TypeError, |
| 3438 | "encoder did not return a bytes object (type=%.400s)", |
| 3439 | Py_TYPE(v)->tp_name); |
| 3440 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3441 | return NULL; |
| 3442 | } |
| 3443 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3444 | PyObject * |
| 3445 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3446 | const char *encoding, |
| 3447 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3448 | { |
| 3449 | PyObject *v; |
| 3450 | |
| 3451 | if (!PyUnicode_Check(unicode)) { |
| 3452 | PyErr_BadArgument(); |
| 3453 | goto onError; |
| 3454 | } |
| 3455 | |
| 3456 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3457 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3458 | |
| 3459 | /* Encode via the codec registry */ |
| 3460 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3461 | if (v == NULL) |
| 3462 | goto onError; |
| 3463 | if (!PyUnicode_Check(v)) { |
| 3464 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3465 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3466 | Py_TYPE(v)->tp_name); |
| 3467 | Py_DECREF(v); |
| 3468 | goto onError; |
| 3469 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3470 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3471 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3472 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3473 | return NULL; |
| 3474 | } |
| 3475 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3476 | static size_t |
| 3477 | mbstowcs_errorpos(const char *str, size_t len) |
| 3478 | { |
| 3479 | #ifdef HAVE_MBRTOWC |
| 3480 | const char *start = str; |
| 3481 | mbstate_t mbs; |
| 3482 | size_t converted; |
| 3483 | wchar_t ch; |
| 3484 | |
| 3485 | memset(&mbs, 0, sizeof mbs); |
| 3486 | while (len) |
| 3487 | { |
| 3488 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3489 | if (converted == 0) |
| 3490 | /* Reached end of string */ |
| 3491 | break; |
| 3492 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3493 | /* Conversion error or incomplete character */ |
| 3494 | return str - start; |
| 3495 | } |
| 3496 | else { |
| 3497 | str += converted; |
| 3498 | len -= converted; |
| 3499 | } |
| 3500 | } |
| 3501 | /* failed to find the undecodable byte sequence */ |
| 3502 | return 0; |
| 3503 | #endif |
| 3504 | return 0; |
| 3505 | } |
| 3506 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3507 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3508 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3509 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3510 | { |
| 3511 | wchar_t smallbuf[256]; |
| 3512 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3513 | wchar_t *wstr; |
| 3514 | size_t wlen, wlen2; |
| 3515 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3516 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3517 | size_t error_pos; |
| 3518 | char *errmsg; |
| 3519 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3520 | |
| 3521 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3522 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3523 | |
| 3524 | if (str[len] != '\0' || len != strlen(str)) { |
| 3525 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3526 | return NULL; |
| 3527 | } |
| 3528 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3529 | if (surrogateescape) { |
| 3530 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3531 | wstr = _Py_char2wchar(str, &wlen); |
| 3532 | if (wstr == NULL) { |
| 3533 | if (wlen == (size_t)-1) |
| 3534 | PyErr_NoMemory(); |
| 3535 | else |
| 3536 | PyErr_SetFromErrno(PyExc_OSError); |
| 3537 | return NULL; |
| 3538 | } |
| 3539 | |
| 3540 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3541 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3542 | } |
| 3543 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3544 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3545 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3546 | wlen = mbstowcs(NULL, str, 0); |
| 3547 | #else |
| 3548 | wlen = len; |
| 3549 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3550 | if (wlen == (size_t)-1) |
| 3551 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3552 | if (wlen+1 <= smallbuf_len) { |
| 3553 | wstr = smallbuf; |
| 3554 | } |
| 3555 | else { |
| 3556 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3557 | return PyErr_NoMemory(); |
| 3558 | |
| 3559 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3560 | if (!wstr) |
| 3561 | return PyErr_NoMemory(); |
| 3562 | } |
| 3563 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3564 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3565 | if (wlen2 == (size_t)-1) { |
| 3566 | if (wstr != smallbuf) |
| 3567 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3568 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3569 | } |
| 3570 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3571 | assert(wlen2 == wlen); |
| 3572 | #endif |
| 3573 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3574 | if (wstr != smallbuf) |
| 3575 | PyMem_Free(wstr); |
| 3576 | } |
| 3577 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3578 | |
| 3579 | decode_error: |
| 3580 | errmsg = strerror(errno); |
| 3581 | assert(errmsg != NULL); |
| 3582 | |
| 3583 | error_pos = mbstowcs_errorpos(str, len); |
| 3584 | if (errmsg != NULL) { |
| 3585 | size_t errlen; |
| 3586 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3587 | if (wstr != NULL) { |
| 3588 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3589 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3590 | } else |
| 3591 | errmsg = NULL; |
| 3592 | } |
| 3593 | if (errmsg == NULL) |
| 3594 | reason = PyUnicode_FromString( |
| 3595 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3596 | if (reason == NULL) |
| 3597 | return NULL; |
| 3598 | |
| 3599 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3600 | "locale", str, len, |
| 3601 | (Py_ssize_t)error_pos, |
| 3602 | (Py_ssize_t)(error_pos+1), |
| 3603 | reason); |
| 3604 | Py_DECREF(reason); |
| 3605 | if (exc != NULL) { |
| 3606 | PyCodec_StrictErrors(exc); |
| 3607 | Py_XDECREF(exc); |
| 3608 | } |
| 3609 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3613 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3614 | { |
| 3615 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3616 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | |
| 3620 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3621 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3622 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3623 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3624 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3625 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3626 | PyObject* |
| 3627 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3628 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3629 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3630 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3631 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3632 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3633 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3634 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3635 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3636 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3637 | the Python codec requires to encode at least its own filename. Use the C |
| 3638 | version of the locale codec until the codec registry is initialized and |
| 3639 | the Python codec is loaded. |
| 3640 | |
| 3641 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3642 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3643 | subinterpreters. */ |
| 3644 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3645 | return PyUnicode_Decode(s, size, |
| 3646 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3647 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3648 | } |
| 3649 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3650 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3651 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3652 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3655 | |
| 3656 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3657 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3658 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3659 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3660 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3661 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3662 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3663 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3664 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3665 | if (pos == -1) |
| 3666 | return 0; |
| 3667 | else |
| 3668 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3669 | } |
| 3670 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3671 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3672 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3673 | { |
| 3674 | PyObject *output = NULL; |
| 3675 | Py_ssize_t size; |
| 3676 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3677 | if (arg == NULL) { |
| 3678 | Py_DECREF(*(PyObject**)addr); |
| 3679 | return 1; |
| 3680 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3681 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3682 | output = arg; |
| 3683 | Py_INCREF(output); |
| 3684 | } |
| 3685 | else { |
| 3686 | arg = PyUnicode_FromObject(arg); |
| 3687 | if (!arg) |
| 3688 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3689 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3690 | Py_DECREF(arg); |
| 3691 | if (!output) |
| 3692 | return 0; |
| 3693 | if (!PyBytes_Check(output)) { |
| 3694 | Py_DECREF(output); |
| 3695 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3696 | return 0; |
| 3697 | } |
| 3698 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3699 | size = PyBytes_GET_SIZE(output); |
| 3700 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3701 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3702 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3703 | Py_DECREF(output); |
| 3704 | return 0; |
| 3705 | } |
| 3706 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3707 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3711 | int |
| 3712 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3713 | { |
| 3714 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3715 | if (arg == NULL) { |
| 3716 | Py_DECREF(*(PyObject**)addr); |
| 3717 | return 1; |
| 3718 | } |
| 3719 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3720 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3721 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3722 | output = arg; |
| 3723 | Py_INCREF(output); |
| 3724 | } |
| 3725 | else { |
| 3726 | arg = PyBytes_FromObject(arg); |
| 3727 | if (!arg) |
| 3728 | return 0; |
| 3729 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3730 | PyBytes_GET_SIZE(arg)); |
| 3731 | Py_DECREF(arg); |
| 3732 | if (!output) |
| 3733 | return 0; |
| 3734 | if (!PyUnicode_Check(output)) { |
| 3735 | Py_DECREF(output); |
| 3736 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3737 | return 0; |
| 3738 | } |
| 3739 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3740 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3741 | Py_DECREF(output); |
| 3742 | return 0; |
| 3743 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3744 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3745 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3746 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3747 | Py_DECREF(output); |
| 3748 | return 0; |
| 3749 | } |
| 3750 | *(PyObject**)addr = output; |
| 3751 | return Py_CLEANUP_SUPPORTED; |
| 3752 | } |
| 3753 | |
| 3754 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3755 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3756 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3757 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3758 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3759 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3760 | if (!PyUnicode_Check(unicode)) { |
| 3761 | PyErr_BadArgument(); |
| 3762 | return NULL; |
| 3763 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3764 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3765 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3767 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3768 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3769 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3770 | if (bytes == NULL) |
| 3771 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3772 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3773 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3774 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3775 | Py_DECREF(bytes); |
| 3776 | return NULL; |
| 3777 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3778 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3779 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3780 | PyBytes_AS_STRING(bytes), |
| 3781 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3782 | Py_DECREF(bytes); |
| 3783 | } |
| 3784 | |
| 3785 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3786 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3787 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3791 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3792 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3793 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3794 | } |
| 3795 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3796 | Py_UNICODE * |
| 3797 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3798 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3799 | const unsigned char *one_byte; |
| 3800 | #if SIZEOF_WCHAR_T == 4 |
| 3801 | const Py_UCS2 *two_bytes; |
| 3802 | #else |
| 3803 | const Py_UCS4 *four_bytes; |
| 3804 | const Py_UCS4 *ucs4_end; |
| 3805 | Py_ssize_t num_surrogates; |
| 3806 | #endif |
| 3807 | wchar_t *w; |
| 3808 | wchar_t *wchar_end; |
| 3809 | |
| 3810 | if (!PyUnicode_Check(unicode)) { |
| 3811 | PyErr_BadArgument(); |
| 3812 | return NULL; |
| 3813 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3814 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3815 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3816 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3817 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3818 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3819 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3820 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3821 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3822 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3823 | num_surrogates = 0; |
| 3824 | |
| 3825 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3826 | if (*four_bytes > 0xFFFF) |
| 3827 | ++num_surrogates; |
| 3828 | } |
| 3829 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3830 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3831 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3832 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3833 | PyErr_NoMemory(); |
| 3834 | return NULL; |
| 3835 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3836 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3837 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3838 | w = _PyUnicode_WSTR(unicode); |
| 3839 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3840 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3841 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3842 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3843 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3844 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3845 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3846 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3847 | } |
| 3848 | else |
| 3849 | *w = *four_bytes; |
| 3850 | |
| 3851 | if (w > wchar_end) { |
| 3852 | assert(0 && "Miscalculated string end"); |
| 3853 | } |
| 3854 | } |
| 3855 | *w = 0; |
| 3856 | #else |
| 3857 | /* sizeof(wchar_t) == 4 */ |
| 3858 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3859 | "should share memory already."); |
| 3860 | return NULL; |
| 3861 | #endif |
| 3862 | } |
| 3863 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3864 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3865 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3866 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3867 | PyErr_NoMemory(); |
| 3868 | return NULL; |
| 3869 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3870 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3871 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3872 | w = _PyUnicode_WSTR(unicode); |
| 3873 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3874 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3875 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3876 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3877 | for (; w < wchar_end; ++one_byte, ++w) |
| 3878 | *w = *one_byte; |
| 3879 | /* null-terminate the wstr */ |
| 3880 | *w = 0; |
| 3881 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3882 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3883 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3884 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3885 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3886 | *w = *two_bytes; |
| 3887 | /* null-terminate the wstr */ |
| 3888 | *w = 0; |
| 3889 | #else |
| 3890 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3891 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3892 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3893 | Py_FatalError("Impossible unicode object state, wstr " |
| 3894 | "and str should share memory already."); |
| 3895 | return NULL; |
| 3896 | #endif |
| 3897 | } |
| 3898 | else { |
| 3899 | assert(0 && "This should never happen."); |
| 3900 | } |
| 3901 | } |
| 3902 | } |
| 3903 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3904 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3905 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3908 | Py_UNICODE * |
| 3909 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3910 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3911 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3914 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3915 | Py_ssize_t |
| 3916 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3917 | { |
| 3918 | if (!PyUnicode_Check(unicode)) { |
| 3919 | PyErr_BadArgument(); |
| 3920 | goto onError; |
| 3921 | } |
| 3922 | return PyUnicode_GET_SIZE(unicode); |
| 3923 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3924 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3925 | return -1; |
| 3926 | } |
| 3927 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3928 | Py_ssize_t |
| 3929 | PyUnicode_GetLength(PyObject *unicode) |
| 3930 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3931 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3932 | PyErr_BadArgument(); |
| 3933 | return -1; |
| 3934 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3935 | if (PyUnicode_READY(unicode) == -1) |
| 3936 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3937 | return PyUnicode_GET_LENGTH(unicode); |
| 3938 | } |
| 3939 | |
| 3940 | Py_UCS4 |
| 3941 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3942 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3943 | void *data; |
| 3944 | int kind; |
| 3945 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3946 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3947 | PyErr_BadArgument(); |
| 3948 | return (Py_UCS4)-1; |
| 3949 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3950 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3951 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3952 | return (Py_UCS4)-1; |
| 3953 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3954 | data = PyUnicode_DATA(unicode); |
| 3955 | kind = PyUnicode_KIND(unicode); |
| 3956 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3957 | } |
| 3958 | |
| 3959 | int |
| 3960 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3961 | { |
| 3962 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3963 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3964 | return -1; |
| 3965 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3966 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3967 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3968 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3969 | return -1; |
| 3970 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3971 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3972 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3973 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3974 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3975 | return -1; |
| 3976 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3977 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3978 | index, ch); |
| 3979 | return 0; |
| 3980 | } |
| 3981 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3982 | const char * |
| 3983 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3984 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3985 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3988 | /* create or adjust a UnicodeDecodeError */ |
| 3989 | static void |
| 3990 | make_decode_exception(PyObject **exceptionObject, |
| 3991 | const char *encoding, |
| 3992 | const char *input, Py_ssize_t length, |
| 3993 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3994 | const char *reason) |
| 3995 | { |
| 3996 | if (*exceptionObject == NULL) { |
| 3997 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3998 | encoding, input, length, startpos, endpos, reason); |
| 3999 | } |
| 4000 | else { |
| 4001 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4002 | goto onError; |
| 4003 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4004 | goto onError; |
| 4005 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4006 | goto onError; |
| 4007 | } |
| 4008 | return; |
| 4009 | |
| 4010 | onError: |
| 4011 | Py_DECREF(*exceptionObject); |
| 4012 | *exceptionObject = NULL; |
| 4013 | } |
| 4014 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4015 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4016 | /* error handling callback helper: |
| 4017 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4018 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4019 | and adjust various state variables. |
| 4020 | return 0 on success, -1 on error |
| 4021 | */ |
| 4022 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4023 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4024 | unicode_decode_call_errorhandler_wchar( |
| 4025 | const char *errors, PyObject **errorHandler, |
| 4026 | const char *encoding, const char *reason, |
| 4027 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4028 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4029 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4030 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4031 | 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] | 4032 | |
| 4033 | PyObject *restuple = NULL; |
| 4034 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4035 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4036 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4037 | Py_ssize_t requiredsize; |
| 4038 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4039 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4040 | wchar_t *repwstr; |
| 4041 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4042 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4043 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 4044 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4045 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4046 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4047 | *errorHandler = PyCodec_LookupError(errors); |
| 4048 | if (*errorHandler == NULL) |
| 4049 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4052 | make_decode_exception(exceptionObject, |
| 4053 | encoding, |
| 4054 | *input, *inend - *input, |
| 4055 | *startinpos, *endinpos, |
| 4056 | reason); |
| 4057 | if (*exceptionObject == NULL) |
| 4058 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4059 | |
| 4060 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4061 | if (restuple == NULL) |
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 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4064 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4065 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4066 | } |
| 4067 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4068 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4069 | |
| 4070 | /* Copy back the bytes variables, which might have been modified by the |
| 4071 | callback */ |
| 4072 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4073 | if (!inputobj) |
| 4074 | goto onError; |
| 4075 | if (!PyBytes_Check(inputobj)) { |
| 4076 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4077 | } |
| 4078 | *input = PyBytes_AS_STRING(inputobj); |
| 4079 | insize = PyBytes_GET_SIZE(inputobj); |
| 4080 | *inend = *input + insize; |
| 4081 | /* we can DECREF safely, as the exception has another reference, |
| 4082 | so the object won't go away. */ |
| 4083 | Py_DECREF(inputobj); |
| 4084 | |
| 4085 | if (newpos<0) |
| 4086 | newpos = insize+newpos; |
| 4087 | if (newpos<0 || newpos>insize) { |
| 4088 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4089 | goto onError; |
| 4090 | } |
| 4091 | |
| 4092 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4093 | if (repwstr == NULL) |
| 4094 | goto onError; |
| 4095 | /* need more space? (at least enough for what we |
| 4096 | have+the replacement+the rest of the string (starting |
| 4097 | at the new input position), so we won't have to check space |
| 4098 | when there are no errors in the rest of the string) */ |
| 4099 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4100 | if (requiredsize > outsize) { |
| 4101 | if (requiredsize < 2*outsize) |
| 4102 | requiredsize = 2*outsize; |
| 4103 | if (unicode_resize(output, requiredsize) < 0) |
| 4104 | goto onError; |
| 4105 | } |
| 4106 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4107 | *outpos += repwlen; |
| 4108 | |
| 4109 | *endinpos = newpos; |
| 4110 | *inptr = *input + newpos; |
| 4111 | |
| 4112 | /* we made it! */ |
| 4113 | Py_XDECREF(restuple); |
| 4114 | return 0; |
| 4115 | |
| 4116 | onError: |
| 4117 | Py_XDECREF(restuple); |
| 4118 | return -1; |
| 4119 | } |
| 4120 | #endif /* HAVE_MBCS */ |
| 4121 | |
| 4122 | static int |
| 4123 | unicode_decode_call_errorhandler_writer( |
| 4124 | const char *errors, PyObject **errorHandler, |
| 4125 | const char *encoding, const char *reason, |
| 4126 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4127 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4128 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4129 | { |
| 4130 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4131 | |
| 4132 | PyObject *restuple = NULL; |
| 4133 | PyObject *repunicode = NULL; |
| 4134 | Py_ssize_t insize; |
| 4135 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4136 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4137 | PyObject *inputobj = NULL; |
| 4138 | |
| 4139 | if (*errorHandler == NULL) { |
| 4140 | *errorHandler = PyCodec_LookupError(errors); |
| 4141 | if (*errorHandler == NULL) |
| 4142 | goto onError; |
| 4143 | } |
| 4144 | |
| 4145 | make_decode_exception(exceptionObject, |
| 4146 | encoding, |
| 4147 | *input, *inend - *input, |
| 4148 | *startinpos, *endinpos, |
| 4149 | reason); |
| 4150 | if (*exceptionObject == NULL) |
| 4151 | goto onError; |
| 4152 | |
| 4153 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4154 | if (restuple == NULL) |
| 4155 | goto onError; |
| 4156 | if (!PyTuple_Check(restuple)) { |
| 4157 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4158 | goto onError; |
| 4159 | } |
| 4160 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4161 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4162 | |
| 4163 | /* Copy back the bytes variables, which might have been modified by the |
| 4164 | callback */ |
| 4165 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4166 | if (!inputobj) |
| 4167 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4168 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4169 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4170 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4171 | *input = PyBytes_AS_STRING(inputobj); |
| 4172 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4173 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4174 | /* we can DECREF safely, as the exception has another reference, |
| 4175 | so the object won't go away. */ |
| 4176 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4177 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4178 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4179 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4180 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4181 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4182 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4183 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4184 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4185 | if (PyUnicode_READY(repunicode) < 0) |
| 4186 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4187 | replen = PyUnicode_GET_LENGTH(repunicode); |
| 4188 | writer->min_length += replen; |
| 4189 | if (replen > 1) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4190 | writer->overallocate = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4191 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4192 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4193 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4194 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4195 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4196 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4197 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4198 | Py_XDECREF(restuple); |
| 4199 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4201 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4202 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4203 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4206 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4207 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4208 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4209 | |
| 4210 | /* Three simple macros defining base-64. */ |
| 4211 | |
| 4212 | /* Is c a base-64 character? */ |
| 4213 | |
| 4214 | #define IS_BASE64(c) \ |
| 4215 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4216 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4217 | ((c) >= '0' && (c) <= '9') || \ |
| 4218 | (c) == '+' || (c) == '/') |
| 4219 | |
| 4220 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4221 | |
| 4222 | #define FROM_BASE64(c) \ |
| 4223 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4224 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4225 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4226 | (c) == '+' ? 62 : 63) |
| 4227 | |
| 4228 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4229 | |
| 4230 | #define TO_BASE64(n) \ |
| 4231 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4232 | |
| 4233 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4234 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4235 | * byte not decoding to itself is the + which begins a base64 |
| 4236 | * string. */ |
| 4237 | |
| 4238 | #define DECODE_DIRECT(c) \ |
| 4239 | ((c) <= 127 && (c) != '+') |
| 4240 | |
| 4241 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4242 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4243 | * the above). See RFC2152. This array identifies these different |
| 4244 | * sets: |
| 4245 | * 0 : "Set D" |
| 4246 | * alphanumeric and '(),-./:? |
| 4247 | * 1 : "Set O" |
| 4248 | * !"#$%&*;<=>@[]^_`{|} |
| 4249 | * 2 : "whitespace" |
| 4250 | * ht nl cr sp |
| 4251 | * 3 : special (must be base64 encoded) |
| 4252 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4253 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4254 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4255 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4256 | char utf7_category[128] = { |
| 4257 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4258 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4259 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4260 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4261 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4262 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4263 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4264 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4265 | /* @ A B C D E F G H I J K L M N O */ |
| 4266 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4267 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4268 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4269 | /* ` a b c d e f g h i j k l m n o */ |
| 4270 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4271 | /* p q r s t u v w x y z { | } ~ del */ |
| 4272 | 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] | 4273 | }; |
| 4274 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4275 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4276 | * answer depends on whether we are encoding set O as itself, and also |
| 4277 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4278 | * clear that the answers to these questions vary between |
| 4279 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4280 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4281 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4282 | ((c) < 128 && (c) > 0 && \ |
| 4283 | ((utf7_category[(c)] == 0) || \ |
| 4284 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4285 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4286 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4287 | PyObject * |
| 4288 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4289 | Py_ssize_t size, |
| 4290 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4291 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4292 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4293 | } |
| 4294 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4295 | /* The decoder. The only state we preserve is our read position, |
| 4296 | * i.e. how many characters we have consumed. So if we end in the |
| 4297 | * middle of a shift sequence we have to back off the read position |
| 4298 | * and the output to the beginning of the sequence, otherwise we lose |
| 4299 | * all the shift state (seen bits, number of bits seen, high |
| 4300 | * surrogate). */ |
| 4301 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4302 | PyObject * |
| 4303 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4304 | Py_ssize_t size, |
| 4305 | const char *errors, |
| 4306 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4307 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4308 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4309 | Py_ssize_t startinpos; |
| 4310 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4311 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4312 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4313 | const char *errmsg = ""; |
| 4314 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4315 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4316 | unsigned int base64bits = 0; |
| 4317 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4318 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4319 | PyObject *errorHandler = NULL; |
| 4320 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4321 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4322 | if (size == 0) { |
| 4323 | if (consumed) |
| 4324 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4325 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4326 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4327 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4328 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4329 | _PyUnicodeWriter_Init(&writer); |
| 4330 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4331 | |
| 4332 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4333 | e = s + size; |
| 4334 | |
| 4335 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4336 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4337 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4338 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4339 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4340 | if (inShift) { /* in a base-64 section */ |
| 4341 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4342 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4343 | base64bits += 6; |
| 4344 | s++; |
| 4345 | if (base64bits >= 16) { |
| 4346 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4347 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4348 | base64bits -= 16; |
| 4349 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4350 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4351 | if (surrogate) { |
| 4352 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4353 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4354 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4355 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4356 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4357 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4358 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4359 | } |
| 4360 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4361 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4362 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4363 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | } |
| 4365 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4366 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4367 | /* first surrogate */ |
| 4368 | surrogate = outCh; |
| 4369 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4370 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4371 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4372 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4373 | } |
| 4374 | } |
| 4375 | } |
| 4376 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4377 | inShift = 0; |
| 4378 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4379 | if (surrogate) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4380 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4381 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4382 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4383 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4384 | if (base64bits > 0) { /* left-over bits */ |
| 4385 | if (base64bits >= 6) { |
| 4386 | /* We've seen at least one base-64 character */ |
| 4387 | errmsg = "partial character in shift sequence"; |
| 4388 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4389 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4390 | else { |
| 4391 | /* Some bits remain; they should be zero */ |
| 4392 | if (base64buffer != 0) { |
| 4393 | errmsg = "non-zero padding bits in shift sequence"; |
| 4394 | goto utf7Error; |
| 4395 | } |
| 4396 | } |
| 4397 | } |
| 4398 | if (ch != '-') { |
| 4399 | /* '-' is absorbed; other terminating |
| 4400 | characters are preserved */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4401 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4402 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4403 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4407 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4408 | s++; /* consume '+' */ |
| 4409 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4410 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4411 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4412 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4413 | } |
| 4414 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4415 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4416 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4417 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4418 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4419 | } |
| 4420 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4421 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4422 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4423 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4424 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4425 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4426 | else { |
| 4427 | startinpos = s-starts; |
| 4428 | s++; |
| 4429 | errmsg = "unexpected special character"; |
| 4430 | goto utf7Error; |
| 4431 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4432 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4434 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4435 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4436 | errors, &errorHandler, |
| 4437 | "utf7", errmsg, |
| 4438 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4439 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4440 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4441 | } |
| 4442 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4443 | /* end of string */ |
| 4444 | |
| 4445 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4446 | /* if we're in an inconsistent state, that's an error */ |
| 4447 | if (surrogate || |
| 4448 | (base64bits >= 6) || |
| 4449 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4450 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4451 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4452 | errors, &errorHandler, |
| 4453 | "utf7", "unterminated shift sequence", |
| 4454 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4455 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4456 | goto onError; |
| 4457 | if (s < e) |
| 4458 | goto restart; |
| 4459 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4460 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | |
| 4462 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4463 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4464 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4465 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4466 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | } |
| 4468 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4469 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4470 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4471 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4472 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4473 | Py_XDECREF(errorHandler); |
| 4474 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4475 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4476 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4477 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4478 | Py_XDECREF(errorHandler); |
| 4479 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4480 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4481 | return NULL; |
| 4482 | } |
| 4483 | |
| 4484 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4485 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4486 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4487 | int base64SetO, |
| 4488 | int base64WhiteSpace, |
| 4489 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4490 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4491 | int kind; |
| 4492 | void *data; |
| 4493 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4494 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4495 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4496 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4497 | unsigned int base64bits = 0; |
| 4498 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4499 | char * out; |
| 4500 | char * start; |
| 4501 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4502 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4503 | return NULL; |
| 4504 | kind = PyUnicode_KIND(str); |
| 4505 | data = PyUnicode_DATA(str); |
| 4506 | len = PyUnicode_GET_LENGTH(str); |
| 4507 | |
| 4508 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4509 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4510 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4511 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4512 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4513 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4514 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4515 | if (v == NULL) |
| 4516 | return NULL; |
| 4517 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4518 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4519 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4520 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4521 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4522 | if (inShift) { |
| 4523 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4524 | /* shifting out */ |
| 4525 | if (base64bits) { /* output remaining bits */ |
| 4526 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4527 | base64buffer = 0; |
| 4528 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4529 | } |
| 4530 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4531 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4532 | so no '-' is required, except if the character is itself a '-' */ |
| 4533 | if (IS_BASE64(ch) || ch == '-') { |
| 4534 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4535 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4536 | *out++ = (char) ch; |
| 4537 | } |
| 4538 | else { |
| 4539 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4540 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4541 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4542 | else { /* not in a shift sequence */ |
| 4543 | if (ch == '+') { |
| 4544 | *out++ = '+'; |
| 4545 | *out++ = '-'; |
| 4546 | } |
| 4547 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4548 | *out++ = (char) ch; |
| 4549 | } |
| 4550 | else { |
| 4551 | *out++ = '+'; |
| 4552 | inShift = 1; |
| 4553 | goto encode_char; |
| 4554 | } |
| 4555 | } |
| 4556 | continue; |
| 4557 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4558 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4559 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4560 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4561 | /* code first surrogate */ |
| 4562 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4563 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4564 | while (base64bits >= 6) { |
| 4565 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4566 | base64bits -= 6; |
| 4567 | } |
| 4568 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4569 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4570 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4571 | base64bits += 16; |
| 4572 | base64buffer = (base64buffer << 16) | ch; |
| 4573 | while (base64bits >= 6) { |
| 4574 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4575 | base64bits -= 6; |
| 4576 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4577 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4578 | if (base64bits) |
| 4579 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4580 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4581 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4582 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4583 | return NULL; |
| 4584 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4585 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4586 | PyObject * |
| 4587 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4588 | Py_ssize_t size, |
| 4589 | int base64SetO, |
| 4590 | int base64WhiteSpace, |
| 4591 | const char *errors) |
| 4592 | { |
| 4593 | PyObject *result; |
| 4594 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4595 | if (tmp == NULL) |
| 4596 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4597 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4598 | base64WhiteSpace, errors); |
| 4599 | Py_DECREF(tmp); |
| 4600 | return result; |
| 4601 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4602 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4603 | #undef IS_BASE64 |
| 4604 | #undef FROM_BASE64 |
| 4605 | #undef TO_BASE64 |
| 4606 | #undef DECODE_DIRECT |
| 4607 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4608 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4609 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4611 | PyObject * |
| 4612 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4613 | Py_ssize_t size, |
| 4614 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4615 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4616 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4617 | } |
| 4618 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4619 | #include "stringlib/asciilib.h" |
| 4620 | #include "stringlib/codecs.h" |
| 4621 | #include "stringlib/undef.h" |
| 4622 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4623 | #include "stringlib/ucs1lib.h" |
| 4624 | #include "stringlib/codecs.h" |
| 4625 | #include "stringlib/undef.h" |
| 4626 | |
| 4627 | #include "stringlib/ucs2lib.h" |
| 4628 | #include "stringlib/codecs.h" |
| 4629 | #include "stringlib/undef.h" |
| 4630 | |
| 4631 | #include "stringlib/ucs4lib.h" |
| 4632 | #include "stringlib/codecs.h" |
| 4633 | #include "stringlib/undef.h" |
| 4634 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4635 | /* Mask to quickly check whether a C 'long' contains a |
| 4636 | non-ASCII, UTF8-encoded char. */ |
| 4637 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4638 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4639 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4640 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4641 | #else |
| 4642 | # error C 'long' size should be either 4 or 8! |
| 4643 | #endif |
| 4644 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4645 | static Py_ssize_t |
| 4646 | 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] | 4647 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4648 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4649 | 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] | 4650 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4651 | /* |
| 4652 | * Issue #17237: m68k is a bit different from most architectures in |
| 4653 | * that objects do not use "natural alignment" - for example, int and |
| 4654 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4655 | * won't work; also, tests have shown that skipping the "optimised |
| 4656 | * version" will even speed up m68k. |
| 4657 | */ |
| 4658 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4659 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4660 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4661 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4662 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4663 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4664 | /* Help allocation */ |
| 4665 | const char *_p = p; |
| 4666 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4667 | while (_p < aligned_end) { |
| 4668 | unsigned long value = *(const unsigned long *) _p; |
| 4669 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4670 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4671 | *((unsigned long *)q) = value; |
| 4672 | _p += SIZEOF_LONG; |
| 4673 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4674 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4675 | p = _p; |
| 4676 | while (p < end) { |
| 4677 | if ((unsigned char)*p & 0x80) |
| 4678 | break; |
| 4679 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4680 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4681 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4682 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4683 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4684 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4685 | while (p < end) { |
| 4686 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4687 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4688 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4689 | /* Help allocation */ |
| 4690 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4691 | while (_p < aligned_end) { |
| 4692 | unsigned long value = *(unsigned long *) _p; |
| 4693 | if (value & ASCII_CHAR_MASK) |
| 4694 | break; |
| 4695 | _p += SIZEOF_LONG; |
| 4696 | } |
| 4697 | p = _p; |
| 4698 | if (_p == end) |
| 4699 | break; |
| 4700 | } |
| 4701 | if ((unsigned char)*p & 0x80) |
| 4702 | break; |
| 4703 | ++p; |
| 4704 | } |
| 4705 | memcpy(dest, start, p - start); |
| 4706 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4707 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4708 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4709 | PyObject * |
| 4710 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4711 | Py_ssize_t size, |
| 4712 | const char *errors, |
| 4713 | Py_ssize_t *consumed) |
| 4714 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4715 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4716 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4717 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4718 | |
| 4719 | Py_ssize_t startinpos; |
| 4720 | Py_ssize_t endinpos; |
| 4721 | const char *errmsg = ""; |
| 4722 | PyObject *errorHandler = NULL; |
| 4723 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4724 | |
| 4725 | if (size == 0) { |
| 4726 | if (consumed) |
| 4727 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4728 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4729 | } |
| 4730 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4731 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4732 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4733 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4734 | *consumed = 1; |
| 4735 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4736 | } |
| 4737 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4738 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4739 | writer.min_length = size; |
| 4740 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4741 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4742 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4743 | writer.pos = ascii_decode(s, end, writer.data); |
| 4744 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4745 | while (s < end) { |
| 4746 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4747 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4748 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4749 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4750 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4751 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4752 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4753 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4754 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4755 | } else { |
| 4756 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4757 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | switch (ch) { |
| 4761 | case 0: |
| 4762 | if (s == end || consumed) |
| 4763 | goto End; |
| 4764 | errmsg = "unexpected end of data"; |
| 4765 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4766 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4767 | break; |
| 4768 | case 1: |
| 4769 | errmsg = "invalid start byte"; |
| 4770 | startinpos = s - starts; |
| 4771 | endinpos = startinpos + 1; |
| 4772 | break; |
| 4773 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4774 | case 3: |
| 4775 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4776 | errmsg = "invalid continuation byte"; |
| 4777 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4778 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4779 | break; |
| 4780 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4781 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4782 | goto onError; |
| 4783 | continue; |
| 4784 | } |
| 4785 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4786 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4787 | errors, &errorHandler, |
| 4788 | "utf-8", errmsg, |
| 4789 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4790 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4791 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4792 | } |
| 4793 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4794 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4795 | if (consumed) |
| 4796 | *consumed = s - starts; |
| 4797 | |
| 4798 | Py_XDECREF(errorHandler); |
| 4799 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4800 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4801 | |
| 4802 | onError: |
| 4803 | Py_XDECREF(errorHandler); |
| 4804 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4805 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4806 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4807 | } |
| 4808 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4809 | #ifdef __APPLE__ |
| 4810 | |
| 4811 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4812 | used to decode the command line arguments on Mac OS X. |
| 4813 | |
| 4814 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4815 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4816 | |
| 4817 | wchar_t* |
| 4818 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4819 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4820 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4821 | wchar_t *unicode; |
| 4822 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4823 | |
| 4824 | /* Note: size will always be longer than the resulting Unicode |
| 4825 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4826 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4827 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4828 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4829 | if (!unicode) |
| 4830 | return NULL; |
| 4831 | |
| 4832 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4833 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4834 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4835 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4836 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4837 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4838 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4839 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4840 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4841 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4842 | if (ch > 0xFF) { |
| 4843 | #if SIZEOF_WCHAR_T == 4 |
| 4844 | assert(0); |
| 4845 | #else |
| 4846 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4847 | /* compute and append the two surrogates: */ |
| 4848 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4849 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4850 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4851 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4852 | else { |
| 4853 | if (!ch && s == e) |
| 4854 | break; |
| 4855 | /* surrogateescape */ |
| 4856 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4857 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4858 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4859 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4860 | return unicode; |
| 4861 | } |
| 4862 | |
| 4863 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4864 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4865 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4866 | |
| 4867 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4868 | and allocate exactly as much space needed at the end. Else allocate the |
| 4869 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4870 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4871 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4872 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4873 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4874 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4875 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4876 | void *data; |
| 4877 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4878 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4879 | if (!PyUnicode_Check(unicode)) { |
| 4880 | PyErr_BadArgument(); |
| 4881 | return NULL; |
| 4882 | } |
| 4883 | |
| 4884 | if (PyUnicode_READY(unicode) == -1) |
| 4885 | return NULL; |
| 4886 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4887 | if (PyUnicode_UTF8(unicode)) |
| 4888 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4889 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4890 | |
| 4891 | kind = PyUnicode_KIND(unicode); |
| 4892 | data = PyUnicode_DATA(unicode); |
| 4893 | size = PyUnicode_GET_LENGTH(unicode); |
| 4894 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4895 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4896 | default: |
| 4897 | assert(0); |
| 4898 | case PyUnicode_1BYTE_KIND: |
| 4899 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4900 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4901 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4902 | case PyUnicode_2BYTE_KIND: |
| 4903 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4904 | case PyUnicode_4BYTE_KIND: |
| 4905 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4906 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4909 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4910 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4911 | Py_ssize_t size, |
| 4912 | const char *errors) |
| 4913 | { |
| 4914 | PyObject *v, *unicode; |
| 4915 | |
| 4916 | unicode = PyUnicode_FromUnicode(s, size); |
| 4917 | if (unicode == NULL) |
| 4918 | return NULL; |
| 4919 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4920 | Py_DECREF(unicode); |
| 4921 | return v; |
| 4922 | } |
| 4923 | |
| 4924 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4925 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4926 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4927 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4928 | } |
| 4929 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4930 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4931 | |
| 4932 | PyObject * |
| 4933 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4934 | Py_ssize_t size, |
| 4935 | const char *errors, |
| 4936 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4937 | { |
| 4938 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4939 | } |
| 4940 | |
| 4941 | PyObject * |
| 4942 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4943 | Py_ssize_t size, |
| 4944 | const char *errors, |
| 4945 | int *byteorder, |
| 4946 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4947 | { |
| 4948 | const char *starts = s; |
| 4949 | Py_ssize_t startinpos; |
| 4950 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4951 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4952 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4953 | int le, bo = 0; /* assume native ordering by default */ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4954 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4955 | PyObject *errorHandler = NULL; |
| 4956 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4957 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4958 | q = (unsigned char *)s; |
| 4959 | e = q + size; |
| 4960 | |
| 4961 | if (byteorder) |
| 4962 | bo = *byteorder; |
| 4963 | |
| 4964 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4965 | byte order setting accordingly. In native mode, the leading BOM |
| 4966 | mark is skipped, in all other modes, it is copied to the output |
| 4967 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4968 | if (bo == 0 && size >= 4) { |
| 4969 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4970 | if (bom == 0x0000FEFF) { |
| 4971 | bo = -1; |
| 4972 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4973 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4974 | else if (bom == 0xFFFE0000) { |
| 4975 | bo = 1; |
| 4976 | q += 4; |
| 4977 | } |
| 4978 | if (byteorder) |
| 4979 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4982 | if (q == e) { |
| 4983 | if (consumed) |
| 4984 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4985 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4988 | #ifdef WORDS_BIGENDIAN |
| 4989 | le = bo < 0; |
| 4990 | #else |
| 4991 | le = bo <= 0; |
| 4992 | #endif |
| 4993 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4994 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4995 | writer.min_length = (e - q + 3) / 4; |
| 4996 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4997 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4998 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4999 | while (1) { |
| 5000 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5001 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5002 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5003 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5004 | enum PyUnicode_Kind kind = writer.kind; |
| 5005 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5006 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5007 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5008 | if (le) { |
| 5009 | do { |
| 5010 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5011 | if (ch > maxch) |
| 5012 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5013 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5014 | q += 4; |
| 5015 | } while (q <= last); |
| 5016 | } |
| 5017 | else { |
| 5018 | do { |
| 5019 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5020 | if (ch > maxch) |
| 5021 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5022 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5023 | q += 4; |
| 5024 | } while (q <= last); |
| 5025 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5026 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | if (ch <= maxch) { |
| 5030 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5031 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5032 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5033 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5034 | startinpos = ((const char *)q) - starts; |
| 5035 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5036 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5037 | else { |
| 5038 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5039 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5040 | goto onError; |
| 5041 | q += 4; |
| 5042 | continue; |
| 5043 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5044 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5045 | startinpos = ((const char *)q) - starts; |
| 5046 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5047 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5048 | |
| 5049 | /* The remaining input chars are ignored if the callback |
| 5050 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5051 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5052 | errors, &errorHandler, |
| 5053 | "utf32", errmsg, |
| 5054 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5055 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5056 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5059 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5060 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5061 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5062 | Py_XDECREF(errorHandler); |
| 5063 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5064 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5065 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5066 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5067 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5068 | Py_XDECREF(errorHandler); |
| 5069 | Py_XDECREF(exc); |
| 5070 | return NULL; |
| 5071 | } |
| 5072 | |
| 5073 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5074 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5075 | const char *errors, |
| 5076 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5077 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5078 | int kind; |
| 5079 | void *data; |
| 5080 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5081 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5082 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5083 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5084 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5085 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5086 | int iorder[] = {0, 1, 2, 3}; |
| 5087 | #else |
| 5088 | int iorder[] = {3, 2, 1, 0}; |
| 5089 | #endif |
| 5090 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5091 | #define STORECHAR(CH) \ |
| 5092 | do { \ |
| 5093 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5094 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5095 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5096 | p[iorder[0]] = (CH) & 0xff; \ |
| 5097 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5098 | } while(0) |
| 5099 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5100 | if (!PyUnicode_Check(str)) { |
| 5101 | PyErr_BadArgument(); |
| 5102 | return NULL; |
| 5103 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5104 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5105 | return NULL; |
| 5106 | kind = PyUnicode_KIND(str); |
| 5107 | data = PyUnicode_DATA(str); |
| 5108 | len = PyUnicode_GET_LENGTH(str); |
| 5109 | |
| 5110 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5111 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5112 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5113 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5114 | if (v == NULL) |
| 5115 | return NULL; |
| 5116 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5117 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5118 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5119 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5120 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5121 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5122 | |
| 5123 | if (byteorder == -1) { |
| 5124 | /* force LE */ |
| 5125 | iorder[0] = 0; |
| 5126 | iorder[1] = 1; |
| 5127 | iorder[2] = 2; |
| 5128 | iorder[3] = 3; |
| 5129 | } |
| 5130 | else if (byteorder == 1) { |
| 5131 | /* force BE */ |
| 5132 | iorder[0] = 3; |
| 5133 | iorder[1] = 2; |
| 5134 | iorder[2] = 1; |
| 5135 | iorder[3] = 0; |
| 5136 | } |
| 5137 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5138 | for (i = 0; i < len; i++) |
| 5139 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5140 | |
| 5141 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5142 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5143 | #undef STORECHAR |
| 5144 | } |
| 5145 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5146 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5147 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5148 | Py_ssize_t size, |
| 5149 | const char *errors, |
| 5150 | int byteorder) |
| 5151 | { |
| 5152 | PyObject *result; |
| 5153 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5154 | if (tmp == NULL) |
| 5155 | return NULL; |
| 5156 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5157 | Py_DECREF(tmp); |
| 5158 | return result; |
| 5159 | } |
| 5160 | |
| 5161 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5162 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5163 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5164 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5167 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5168 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5169 | PyObject * |
| 5170 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5171 | Py_ssize_t size, |
| 5172 | const char *errors, |
| 5173 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5174 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5175 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5176 | } |
| 5177 | |
| 5178 | PyObject * |
| 5179 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5180 | Py_ssize_t size, |
| 5181 | const char *errors, |
| 5182 | int *byteorder, |
| 5183 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5184 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5185 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5186 | Py_ssize_t startinpos; |
| 5187 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5188 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5189 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5190 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5191 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5192 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5193 | PyObject *errorHandler = NULL; |
| 5194 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5195 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5196 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5197 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5198 | |
| 5199 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5200 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5201 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5202 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5203 | byte order setting accordingly. In native mode, the leading BOM |
| 5204 | mark is skipped, in all other modes, it is copied to the output |
| 5205 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5206 | if (bo == 0 && size >= 2) { |
| 5207 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5208 | if (bom == 0xFEFF) { |
| 5209 | q += 2; |
| 5210 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5211 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5212 | else if (bom == 0xFFFE) { |
| 5213 | q += 2; |
| 5214 | bo = 1; |
| 5215 | } |
| 5216 | if (byteorder) |
| 5217 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5218 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5220 | if (q == e) { |
| 5221 | if (consumed) |
| 5222 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5223 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5224 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5225 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5226 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5227 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5228 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5229 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5230 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5231 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5232 | /* Note: size will always be longer than the resulting Unicode |
| 5233 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5234 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5235 | writer.min_length = (e - q + 1) / 2; |
| 5236 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5237 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5238 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5239 | while (1) { |
| 5240 | Py_UCS4 ch = 0; |
| 5241 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5242 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5243 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5244 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5245 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5246 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5247 | native_ordering); |
| 5248 | else |
| 5249 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5250 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5251 | native_ordering); |
| 5252 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5253 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5254 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5255 | native_ordering); |
| 5256 | } else { |
| 5257 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5258 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5259 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5260 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5261 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5262 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5263 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5264 | switch (ch) |
| 5265 | { |
| 5266 | case 0: |
| 5267 | /* remaining byte at the end? (size should be even) */ |
| 5268 | if (q == e || consumed) |
| 5269 | goto End; |
| 5270 | errmsg = "truncated data"; |
| 5271 | startinpos = ((const char *)q) - starts; |
| 5272 | endinpos = ((const char *)e) - starts; |
| 5273 | break; |
| 5274 | /* The remaining input chars are ignored if the callback |
| 5275 | chooses to skip the input */ |
| 5276 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5277 | q -= 2; |
| 5278 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5279 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5280 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5281 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5282 | endinpos = ((const char *)e) - starts; |
| 5283 | break; |
| 5284 | case 2: |
| 5285 | errmsg = "illegal encoding"; |
| 5286 | startinpos = ((const char *)q) - 2 - starts; |
| 5287 | endinpos = startinpos + 2; |
| 5288 | break; |
| 5289 | case 3: |
| 5290 | errmsg = "illegal UTF-16 surrogate"; |
| 5291 | startinpos = ((const char *)q) - 4 - starts; |
| 5292 | endinpos = startinpos + 2; |
| 5293 | break; |
| 5294 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5295 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5296 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5297 | continue; |
| 5298 | } |
| 5299 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5300 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5301 | errors, |
| 5302 | &errorHandler, |
| 5303 | "utf16", errmsg, |
| 5304 | &starts, |
| 5305 | (const char **)&e, |
| 5306 | &startinpos, |
| 5307 | &endinpos, |
| 5308 | &exc, |
| 5309 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5310 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5311 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5314 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5315 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5316 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5317 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5318 | Py_XDECREF(errorHandler); |
| 5319 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5320 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5322 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5323 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5324 | Py_XDECREF(errorHandler); |
| 5325 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5326 | return NULL; |
| 5327 | } |
| 5328 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5329 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5330 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5331 | const char *errors, |
| 5332 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5333 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5334 | enum PyUnicode_Kind kind; |
| 5335 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5336 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5337 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5338 | unsigned short *out; |
| 5339 | Py_ssize_t bytesize; |
| 5340 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5341 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5342 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5343 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5344 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5345 | #endif |
| 5346 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5347 | if (!PyUnicode_Check(str)) { |
| 5348 | PyErr_BadArgument(); |
| 5349 | return NULL; |
| 5350 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5351 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5352 | return NULL; |
| 5353 | kind = PyUnicode_KIND(str); |
| 5354 | data = PyUnicode_DATA(str); |
| 5355 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5356 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5357 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5358 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5359 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5360 | const Py_UCS4 *end = in + len; |
| 5361 | while (in < end) |
| 5362 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5363 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5364 | } |
| 5365 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5366 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5367 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5368 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | if (v == NULL) |
| 5370 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5371 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5372 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5373 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5374 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5376 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5377 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5378 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5379 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5380 | switch (kind) { |
| 5381 | case PyUnicode_1BYTE_KIND: { |
| 5382 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5383 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5384 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5385 | case PyUnicode_2BYTE_KIND: { |
| 5386 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5387 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5388 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5389 | case PyUnicode_4BYTE_KIND: { |
| 5390 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5391 | break; |
| 5392 | } |
| 5393 | default: |
| 5394 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5395 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5396 | |
| 5397 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5398 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5399 | } |
| 5400 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5401 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5402 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5403 | Py_ssize_t size, |
| 5404 | const char *errors, |
| 5405 | int byteorder) |
| 5406 | { |
| 5407 | PyObject *result; |
| 5408 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5409 | if (tmp == NULL) |
| 5410 | return NULL; |
| 5411 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5412 | Py_DECREF(tmp); |
| 5413 | return result; |
| 5414 | } |
| 5415 | |
| 5416 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5417 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5419 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5424 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5425 | if all the escapes in the string make it still a valid ASCII string. |
| 5426 | Returns -1 if any escapes were found which cause the string to |
| 5427 | pop out of ASCII range. Otherwise returns the length of the |
| 5428 | required buffer to hold the string. |
| 5429 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5430 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5431 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5432 | { |
| 5433 | const unsigned char *p = (const unsigned char *)s; |
| 5434 | const unsigned char *end = p + size; |
| 5435 | Py_ssize_t length = 0; |
| 5436 | |
| 5437 | if (size < 0) |
| 5438 | return -1; |
| 5439 | |
| 5440 | for (; p < end; ++p) { |
| 5441 | if (*p > 127) { |
| 5442 | /* Non-ASCII */ |
| 5443 | return -1; |
| 5444 | } |
| 5445 | else if (*p != '\\') { |
| 5446 | /* Normal character */ |
| 5447 | ++length; |
| 5448 | } |
| 5449 | else { |
| 5450 | /* Backslash-escape, check next char */ |
| 5451 | ++p; |
| 5452 | /* Escape sequence reaches till end of string or |
| 5453 | non-ASCII follow-up. */ |
| 5454 | if (p >= end || *p > 127) |
| 5455 | return -1; |
| 5456 | switch (*p) { |
| 5457 | case '\n': |
| 5458 | /* backslash + \n result in zero characters */ |
| 5459 | break; |
| 5460 | case '\\': case '\'': case '\"': |
| 5461 | case 'b': case 'f': case 't': |
| 5462 | case 'n': case 'r': case 'v': case 'a': |
| 5463 | ++length; |
| 5464 | break; |
| 5465 | case '0': case '1': case '2': case '3': |
| 5466 | case '4': case '5': case '6': case '7': |
| 5467 | case 'x': case 'u': case 'U': case 'N': |
| 5468 | /* these do not guarantee ASCII characters */ |
| 5469 | return -1; |
| 5470 | default: |
| 5471 | /* count the backslash + the other character */ |
| 5472 | length += 2; |
| 5473 | } |
| 5474 | } |
| 5475 | } |
| 5476 | return length; |
| 5477 | } |
| 5478 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5479 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5480 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5481 | PyObject * |
| 5482 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5483 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5484 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5485 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5486 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5487 | Py_ssize_t startinpos; |
| 5488 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5489 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5490 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5491 | char* message; |
| 5492 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5493 | PyObject *errorHandler = NULL; |
| 5494 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5495 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5496 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5497 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5498 | if (len == 0) |
| 5499 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5500 | |
| 5501 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5502 | either the string is pure ASCII with named escapes like \n, etc. |
| 5503 | and we determined it's exact size (common case) |
| 5504 | or it contains \x, \u, ... escape sequences. then we create a |
| 5505 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5506 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5507 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5508 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5509 | } |
| 5510 | else { |
| 5511 | /* Escaped strings will always be longer than the resulting |
| 5512 | Unicode string, so we start with size here and then reduce the |
| 5513 | length after conversion to the true value. |
| 5514 | (but if the error callback returns a long replacement string |
| 5515 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5516 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5517 | } |
| 5518 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5520 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5521 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5522 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5523 | while (s < end) { |
| 5524 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5525 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5526 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | |
| 5528 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5529 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5530 | x = (unsigned char)*s; |
| 5531 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5532 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5533 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5534 | continue; |
| 5535 | } |
| 5536 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5537 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | /* \ - Escapes */ |
| 5539 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5540 | c = *s++; |
| 5541 | if (s > end) |
| 5542 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5543 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5544 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5545 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5546 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5547 | #define WRITECHAR(ch) \ |
| 5548 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5549 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5550 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5551 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5554 | case '\\': WRITECHAR('\\'); break; |
| 5555 | case '\'': WRITECHAR('\''); break; |
| 5556 | case '\"': WRITECHAR('\"'); break; |
| 5557 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5558 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5559 | case 'f': WRITECHAR('\014'); break; |
| 5560 | case 't': WRITECHAR('\t'); break; |
| 5561 | case 'n': WRITECHAR('\n'); break; |
| 5562 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5563 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5564 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5565 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5566 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5568 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5569 | case '0': case '1': case '2': case '3': |
| 5570 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5571 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5572 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5573 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5574 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5575 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5576 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5577 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | break; |
| 5579 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5580 | /* hex escapes */ |
| 5581 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5583 | digits = 2; |
| 5584 | message = "truncated \\xXX escape"; |
| 5585 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5586 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5587 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5589 | digits = 4; |
| 5590 | message = "truncated \\uXXXX escape"; |
| 5591 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5593 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5594 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5595 | digits = 8; |
| 5596 | message = "truncated \\UXXXXXXXX escape"; |
| 5597 | hexescape: |
| 5598 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5599 | if (end - s < digits) { |
| 5600 | /* count only hex digits */ |
| 5601 | for (; s < end; ++s) { |
| 5602 | c = (unsigned char)*s; |
| 5603 | if (!Py_ISXDIGIT(c)) |
| 5604 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5605 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5606 | goto error; |
| 5607 | } |
| 5608 | for (; digits--; ++s) { |
| 5609 | c = (unsigned char)*s; |
| 5610 | if (!Py_ISXDIGIT(c)) |
| 5611 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5612 | chr = (chr<<4) & ~0xF; |
| 5613 | if (c >= '0' && c <= '9') |
| 5614 | chr += c - '0'; |
| 5615 | else if (c >= 'a' && c <= 'f') |
| 5616 | chr += 10 + c - 'a'; |
| 5617 | else |
| 5618 | chr += 10 + c - 'A'; |
| 5619 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5620 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5621 | /* _decoding_error will have already written into the |
| 5622 | target buffer. */ |
| 5623 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5624 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5625 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5626 | message = "illegal Unicode character"; |
| 5627 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5628 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5629 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5630 | break; |
| 5631 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5632 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5633 | case 'N': |
| 5634 | message = "malformed \\N character escape"; |
| 5635 | if (ucnhash_CAPI == NULL) { |
| 5636 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5637 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5638 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5639 | if (ucnhash_CAPI == NULL) |
| 5640 | goto ucnhashError; |
| 5641 | } |
| 5642 | if (*s == '{') { |
| 5643 | const char *start = s+1; |
| 5644 | /* look for the closing brace */ |
| 5645 | while (*s != '}' && s < end) |
| 5646 | s++; |
| 5647 | if (s > start && s < end && *s == '}') { |
| 5648 | /* found a name. look it up in the unicode database */ |
| 5649 | message = "unknown Unicode character name"; |
| 5650 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5651 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5652 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5653 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5654 | goto store; |
| 5655 | } |
| 5656 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5657 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5658 | |
| 5659 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5660 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5661 | message = "\\ at end of string"; |
| 5662 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5663 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5664 | } |
| 5665 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5666 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5667 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5668 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5669 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5671 | continue; |
| 5672 | |
| 5673 | error: |
| 5674 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5675 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5676 | errors, &errorHandler, |
| 5677 | "unicodeescape", message, |
| 5678 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5679 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5680 | goto onError; |
| 5681 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5683 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5684 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5685 | Py_XDECREF(errorHandler); |
| 5686 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5687 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5688 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5689 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5690 | PyErr_SetString( |
| 5691 | PyExc_UnicodeError, |
| 5692 | "\\N escapes not supported (can't load unicodedata module)" |
| 5693 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5694 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5695 | Py_XDECREF(errorHandler); |
| 5696 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5697 | return NULL; |
| 5698 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5699 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5700 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5701 | Py_XDECREF(errorHandler); |
| 5702 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5703 | return NULL; |
| 5704 | } |
| 5705 | |
| 5706 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5707 | |
| 5708 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5709 | appropriate. |
| 5710 | |
| 5711 | */ |
| 5712 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5713 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5714 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5715 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5716 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5717 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5718 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5719 | int kind; |
| 5720 | void *data; |
| 5721 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5722 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5723 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5724 | escape. |
| 5725 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5726 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5727 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5728 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5729 | */ |
| 5730 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5731 | if (!PyUnicode_Check(unicode)) { |
| 5732 | PyErr_BadArgument(); |
| 5733 | return NULL; |
| 5734 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5735 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5736 | return NULL; |
| 5737 | len = PyUnicode_GET_LENGTH(unicode); |
| 5738 | kind = PyUnicode_KIND(unicode); |
| 5739 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5740 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5741 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5742 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5743 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5744 | } |
| 5745 | |
| 5746 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5747 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5748 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5749 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5750 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5751 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5752 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5753 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5754 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5755 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5756 | if (repr == NULL) |
| 5757 | return NULL; |
| 5758 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5759 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5761 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5762 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5763 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5764 | /* Escape backslashes */ |
| 5765 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | *p++ = '\\'; |
| 5767 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5768 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5769 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5770 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5771 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5772 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5773 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5774 | *p++ = '\\'; |
| 5775 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5776 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5777 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5778 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5779 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5780 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5781 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5782 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5783 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5784 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5785 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5786 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5787 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5788 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 | *p++ = '\\'; |
| 5790 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5791 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5792 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5793 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5794 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5796 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5797 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5798 | else if (ch == '\t') { |
| 5799 | *p++ = '\\'; |
| 5800 | *p++ = 't'; |
| 5801 | } |
| 5802 | else if (ch == '\n') { |
| 5803 | *p++ = '\\'; |
| 5804 | *p++ = 'n'; |
| 5805 | } |
| 5806 | else if (ch == '\r') { |
| 5807 | *p++ = '\\'; |
| 5808 | *p++ = 'r'; |
| 5809 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5810 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5811 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5812 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5813 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5814 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5815 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5816 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5817 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5819 | /* Copy everything else as-is */ |
| 5820 | else |
| 5821 | *p++ = (char) ch; |
| 5822 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5823 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5824 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5825 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5826 | return NULL; |
| 5827 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5830 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5831 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5832 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5833 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5834 | PyObject *result; |
| 5835 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5836 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5837 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5838 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5839 | Py_DECREF(tmp); |
| 5840 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5844 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5845 | PyObject * |
| 5846 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5847 | Py_ssize_t size, |
| 5848 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5849 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5850 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5851 | Py_ssize_t startinpos; |
| 5852 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5853 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | const char *end; |
| 5855 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5856 | PyObject *errorHandler = NULL; |
| 5857 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5858 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5859 | if (size == 0) |
| 5860 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5861 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5862 | /* Escaped strings will always be longer than the resulting |
| 5863 | 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] | 5864 | length after conversion to the true value. (But decoding error |
| 5865 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5866 | _PyUnicodeWriter_Init(&writer); |
| 5867 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | end = s + size; |
| 5870 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5871 | unsigned char c; |
| 5872 | Py_UCS4 x; |
| 5873 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5874 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5876 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5877 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5878 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5879 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5880 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5881 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5882 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5883 | startinpos = s-starts; |
| 5884 | |
| 5885 | /* \u-escapes are only interpreted iff the number of leading |
| 5886 | backslashes if odd */ |
| 5887 | bs = s; |
| 5888 | for (;s < end;) { |
| 5889 | if (*s != '\\') |
| 5890 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5891 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5892 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5893 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5894 | } |
| 5895 | if (((s - bs) & 1) == 0 || |
| 5896 | s >= end || |
| 5897 | (*s != 'u' && *s != 'U')) { |
| 5898 | continue; |
| 5899 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5900 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5901 | count = *s=='u' ? 4 : 8; |
| 5902 | s++; |
| 5903 | |
| 5904 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5905 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5906 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5907 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5908 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5909 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | errors, &errorHandler, |
| 5911 | "rawunicodeescape", "truncated \\uXXXX", |
| 5912 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5913 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5914 | goto onError; |
| 5915 | goto nextByte; |
| 5916 | } |
| 5917 | x = (x<<4) & ~0xF; |
| 5918 | if (c >= '0' && c <= '9') |
| 5919 | x += c - '0'; |
| 5920 | else if (c >= 'a' && c <= 'f') |
| 5921 | x += 10 + c - 'a'; |
| 5922 | else |
| 5923 | x += 10 + c - 'A'; |
| 5924 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5925 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5926 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5927 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5928 | } |
| 5929 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5930 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5931 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5932 | errors, &errorHandler, |
| 5933 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5934 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5935 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5936 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5937 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5938 | nextByte: |
| 5939 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5940 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5941 | Py_XDECREF(errorHandler); |
| 5942 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5943 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5944 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5946 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5947 | Py_XDECREF(errorHandler); |
| 5948 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5949 | return NULL; |
| 5950 | } |
| 5951 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5952 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5953 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5954 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5955 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5956 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | char *p; |
| 5958 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5959 | Py_ssize_t expandsize, pos; |
| 5960 | int kind; |
| 5961 | void *data; |
| 5962 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5963 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5964 | if (!PyUnicode_Check(unicode)) { |
| 5965 | PyErr_BadArgument(); |
| 5966 | return NULL; |
| 5967 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5968 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5969 | return NULL; |
| 5970 | kind = PyUnicode_KIND(unicode); |
| 5971 | data = PyUnicode_DATA(unicode); |
| 5972 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5973 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 5974 | bytes, and 1 byte characters 4. */ |
| 5975 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5976 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5977 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5978 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5979 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5980 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5981 | if (repr == NULL) |
| 5982 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5983 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5984 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5985 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5986 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5987 | for (pos = 0; pos < len; pos++) { |
| 5988 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5989 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5990 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5991 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5992 | *p++ = '\\'; |
| 5993 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5994 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 5995 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 5996 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 5997 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 5998 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5999 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6000 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6001 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6002 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6003 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6004 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | *p++ = '\\'; |
| 6006 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6007 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6008 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6009 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6010 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6012 | /* Copy everything else as-is */ |
| 6013 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | *p++ = (char) ch; |
| 6015 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6016 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6017 | assert(p > q); |
| 6018 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6019 | return NULL; |
| 6020 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6023 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6024 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6025 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6026 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6027 | PyObject *result; |
| 6028 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6029 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6030 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6031 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6032 | Py_DECREF(tmp); |
| 6033 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6036 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6037 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6038 | PyObject * |
| 6039 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6040 | Py_ssize_t size, |
| 6041 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6042 | { |
| 6043 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6044 | Py_ssize_t startinpos; |
| 6045 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6046 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6047 | const char *end; |
| 6048 | const char *reason; |
| 6049 | PyObject *errorHandler = NULL; |
| 6050 | PyObject *exc = NULL; |
| 6051 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6052 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6053 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6054 | 1)) |
| 6055 | return NULL; |
| 6056 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6057 | if (size == 0) |
| 6058 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6059 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6060 | _PyUnicodeWriter_Init(&writer); |
| 6061 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6062 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6063 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6064 | } |
| 6065 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6066 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6067 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6068 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6069 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6070 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6071 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6072 | endinpos = end-starts; |
| 6073 | reason = "truncated input"; |
| 6074 | goto error; |
| 6075 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6076 | /* We copy the raw representation one byte at a time because the |
| 6077 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6078 | ((char *) &uch)[0] = s[0]; |
| 6079 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6080 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6081 | ((char *) &uch)[2] = s[2]; |
| 6082 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6083 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6084 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6085 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6086 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6087 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6088 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6089 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6090 | reason = "illegal code point (> 0x10FFFF)"; |
| 6091 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6092 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6093 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6094 | s += Py_UNICODE_SIZE; |
| 6095 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6096 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6097 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6098 | Py_UNICODE uch2; |
| 6099 | ((char *) &uch2)[0] = s[0]; |
| 6100 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6101 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6102 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6103 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6104 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6105 | } |
| 6106 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6107 | #endif |
| 6108 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6109 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6110 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6111 | continue; |
| 6112 | |
| 6113 | error: |
| 6114 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6115 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6116 | errors, &errorHandler, |
| 6117 | "unicode_internal", reason, |
| 6118 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6119 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6120 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6123 | Py_XDECREF(errorHandler); |
| 6124 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6125 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6126 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6127 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6128 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6129 | Py_XDECREF(errorHandler); |
| 6130 | Py_XDECREF(exc); |
| 6131 | return NULL; |
| 6132 | } |
| 6133 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6134 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6135 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6136 | PyObject * |
| 6137 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6138 | Py_ssize_t size, |
| 6139 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6140 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6141 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6142 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6145 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6146 | static void |
| 6147 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6148 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6149 | PyObject *unicode, |
| 6150 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6151 | const char *reason) |
| 6152 | { |
| 6153 | if (*exceptionObject == NULL) { |
| 6154 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6155 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6156 | encoding, unicode, startpos, endpos, reason); |
| 6157 | } |
| 6158 | else { |
| 6159 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6160 | goto onError; |
| 6161 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6162 | goto onError; |
| 6163 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6164 | goto onError; |
| 6165 | return; |
| 6166 | onError: |
| 6167 | Py_DECREF(*exceptionObject); |
| 6168 | *exceptionObject = NULL; |
| 6169 | } |
| 6170 | } |
| 6171 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6172 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6173 | static void |
| 6174 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6175 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6176 | PyObject *unicode, |
| 6177 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6178 | const char *reason) |
| 6179 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6180 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6181 | encoding, unicode, startpos, endpos, reason); |
| 6182 | if (*exceptionObject != NULL) |
| 6183 | PyCodec_StrictErrors(*exceptionObject); |
| 6184 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6185 | |
| 6186 | /* error handling callback helper: |
| 6187 | build arguments, call the callback and check the arguments, |
| 6188 | put the result into newpos and return the replacement string, which |
| 6189 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6190 | static PyObject * |
| 6191 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6192 | PyObject **errorHandler, |
| 6193 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6194 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6195 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6196 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6197 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6198 | 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] | 6199 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6200 | PyObject *restuple; |
| 6201 | PyObject *resunicode; |
| 6202 | |
| 6203 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6204 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6205 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6206 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6209 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6210 | return NULL; |
| 6211 | len = PyUnicode_GET_LENGTH(unicode); |
| 6212 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6213 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6214 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6215 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6216 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6217 | |
| 6218 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6219 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6220 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6221 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6222 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6223 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6224 | Py_DECREF(restuple); |
| 6225 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6226 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6227 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | &resunicode, newpos)) { |
| 6229 | Py_DECREF(restuple); |
| 6230 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6231 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6232 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6233 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6234 | Py_DECREF(restuple); |
| 6235 | return NULL; |
| 6236 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6237 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6238 | *newpos = len + *newpos; |
| 6239 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6240 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6241 | Py_DECREF(restuple); |
| 6242 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6243 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6244 | Py_INCREF(resunicode); |
| 6245 | Py_DECREF(restuple); |
| 6246 | return resunicode; |
| 6247 | } |
| 6248 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6249 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6250 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6251 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6252 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6253 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6254 | /* input state */ |
| 6255 | Py_ssize_t pos=0, size; |
| 6256 | int kind; |
| 6257 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6258 | /* output object */ |
| 6259 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6260 | /* pointer into the output */ |
| 6261 | char *str; |
| 6262 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6263 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6264 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6265 | 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] | 6266 | PyObject *errorHandler = NULL; |
| 6267 | PyObject *exc = NULL; |
| 6268 | /* the following variable is used for caching string comparisons |
| 6269 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6270 | int known_errorHandler = -1; |
| 6271 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6272 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6273 | return NULL; |
| 6274 | size = PyUnicode_GET_LENGTH(unicode); |
| 6275 | kind = PyUnicode_KIND(unicode); |
| 6276 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6277 | /* allocate enough for a simple encoding without |
| 6278 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6279 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6280 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6281 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6282 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6283 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6284 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6285 | ressize = size; |
| 6286 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6287 | while (pos < size) { |
| 6288 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6289 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6290 | /* can we encode this? */ |
| 6291 | if (c<limit) { |
| 6292 | /* no overflow check, because we know that the space is enough */ |
| 6293 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6294 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6295 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6297 | Py_ssize_t requiredsize; |
| 6298 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6299 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6301 | Py_ssize_t collstart = pos; |
| 6302 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6303 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6304 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6305 | ++collend; |
| 6306 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6307 | if (known_errorHandler==-1) { |
| 6308 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6309 | known_errorHandler = 1; |
| 6310 | else if (!strcmp(errors, "replace")) |
| 6311 | known_errorHandler = 2; |
| 6312 | else if (!strcmp(errors, "ignore")) |
| 6313 | known_errorHandler = 3; |
| 6314 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6315 | known_errorHandler = 4; |
| 6316 | else |
| 6317 | known_errorHandler = 0; |
| 6318 | } |
| 6319 | switch (known_errorHandler) { |
| 6320 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6321 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6322 | goto onError; |
| 6323 | case 2: /* replace */ |
| 6324 | while (collstart++<collend) |
| 6325 | *str++ = '?'; /* fall through */ |
| 6326 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6327 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6328 | break; |
| 6329 | case 4: /* xmlcharrefreplace */ |
| 6330 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6331 | /* determine replacement size */ |
| 6332 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6333 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6334 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6335 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6336 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6337 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6338 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6339 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6340 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6341 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6342 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6343 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6344 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6346 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6347 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6348 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6349 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6350 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6351 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6352 | if (requiredsize > ressize) { |
| 6353 | if (requiredsize<2*ressize) |
| 6354 | requiredsize = 2*ressize; |
| 6355 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6356 | goto onError; |
| 6357 | str = PyBytes_AS_STRING(res) + respos; |
| 6358 | ressize = requiredsize; |
| 6359 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6360 | /* generate replacement */ |
| 6361 | for (i = collstart; i < collend; ++i) { |
| 6362 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6363 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6364 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6365 | break; |
| 6366 | default: |
| 6367 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6368 | encoding, reason, unicode, &exc, |
| 6369 | collstart, collend, &newpos); |
| 6370 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6371 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6372 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6373 | if (PyBytes_Check(repunicode)) { |
| 6374 | /* Directly copy bytes result to output. */ |
| 6375 | repsize = PyBytes_Size(repunicode); |
| 6376 | if (repsize > 1) { |
| 6377 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6378 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6379 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6380 | Py_DECREF(repunicode); |
| 6381 | goto onError; |
| 6382 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6383 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6384 | ressize += repsize-1; |
| 6385 | } |
| 6386 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6387 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6388 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6389 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6390 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6391 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6392 | /* need more space? (at least enough for what we |
| 6393 | have+the replacement+the rest of the string, so |
| 6394 | we won't have to check space for encodable characters) */ |
| 6395 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6396 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6397 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6398 | if (requiredsize > ressize) { |
| 6399 | if (requiredsize<2*ressize) |
| 6400 | requiredsize = 2*ressize; |
| 6401 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6402 | Py_DECREF(repunicode); |
| 6403 | goto onError; |
| 6404 | } |
| 6405 | str = PyBytes_AS_STRING(res) + respos; |
| 6406 | ressize = requiredsize; |
| 6407 | } |
| 6408 | /* check if there is anything unencodable in the replacement |
| 6409 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6410 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6411 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6412 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6413 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6414 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6415 | Py_DECREF(repunicode); |
| 6416 | goto onError; |
| 6417 | } |
| 6418 | *str = (char)c; |
| 6419 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6420 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6421 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6422 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6423 | } |
| 6424 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6425 | /* Resize if we allocated to much */ |
| 6426 | size = str - PyBytes_AS_STRING(res); |
| 6427 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6428 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6429 | if (_PyBytes_Resize(&res, size) < 0) |
| 6430 | goto onError; |
| 6431 | } |
| 6432 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6433 | Py_XDECREF(errorHandler); |
| 6434 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6435 | return res; |
| 6436 | |
| 6437 | onError: |
| 6438 | Py_XDECREF(res); |
| 6439 | Py_XDECREF(errorHandler); |
| 6440 | Py_XDECREF(exc); |
| 6441 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6442 | } |
| 6443 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6444 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6445 | PyObject * |
| 6446 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6447 | Py_ssize_t size, |
| 6448 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6449 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6450 | PyObject *result; |
| 6451 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6452 | if (unicode == NULL) |
| 6453 | return NULL; |
| 6454 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6455 | Py_DECREF(unicode); |
| 6456 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6457 | } |
| 6458 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6459 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6460 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6461 | { |
| 6462 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6463 | PyErr_BadArgument(); |
| 6464 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6465 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6466 | if (PyUnicode_READY(unicode) == -1) |
| 6467 | return NULL; |
| 6468 | /* Fast path: if it is a one-byte string, construct |
| 6469 | bytes object directly. */ |
| 6470 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6471 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6472 | PyUnicode_GET_LENGTH(unicode)); |
| 6473 | /* Non-Latin-1 characters present. Defer to above function to |
| 6474 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6475 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | PyObject* |
| 6479 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6480 | { |
| 6481 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
| 6484 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6485 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6486 | PyObject * |
| 6487 | PyUnicode_DecodeASCII(const char *s, |
| 6488 | Py_ssize_t size, |
| 6489 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6490 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6491 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6492 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6493 | int kind; |
| 6494 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6495 | Py_ssize_t startinpos; |
| 6496 | Py_ssize_t endinpos; |
| 6497 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6498 | const char *e; |
| 6499 | PyObject *errorHandler = NULL; |
| 6500 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6502 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6503 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6504 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6505 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6506 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6507 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6508 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6509 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6510 | writer.min_length = size; |
| 6511 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6512 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6513 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6514 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6515 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6516 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6517 | writer.pos = outpos; |
| 6518 | if (writer.pos == size) |
| 6519 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6520 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6521 | s += writer.pos; |
| 6522 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6523 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6524 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6525 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6526 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6527 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6528 | ++s; |
| 6529 | } |
| 6530 | else { |
| 6531 | startinpos = s-starts; |
| 6532 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6533 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6534 | errors, &errorHandler, |
| 6535 | "ascii", "ordinal not in range(128)", |
| 6536 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6537 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6538 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6539 | kind = writer.kind; |
| 6540 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6541 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6542 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6543 | Py_XDECREF(errorHandler); |
| 6544 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6545 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6546 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6547 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6548 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6549 | Py_XDECREF(errorHandler); |
| 6550 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | return NULL; |
| 6552 | } |
| 6553 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6554 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6555 | PyObject * |
| 6556 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6557 | Py_ssize_t size, |
| 6558 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6559 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6560 | PyObject *result; |
| 6561 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6562 | if (unicode == NULL) |
| 6563 | return NULL; |
| 6564 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6565 | Py_DECREF(unicode); |
| 6566 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6567 | } |
| 6568 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6569 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6570 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6571 | { |
| 6572 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6573 | PyErr_BadArgument(); |
| 6574 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6575 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6576 | if (PyUnicode_READY(unicode) == -1) |
| 6577 | return NULL; |
| 6578 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6579 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6580 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6581 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6582 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6583 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | PyObject * |
| 6587 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6588 | { |
| 6589 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6590 | } |
| 6591 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6592 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6593 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6594 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6595 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6596 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6597 | #define NEED_RETRY |
| 6598 | #endif |
| 6599 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6600 | #ifndef WC_ERR_INVALID_CHARS |
| 6601 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6602 | #endif |
| 6603 | |
| 6604 | static char* |
| 6605 | code_page_name(UINT code_page, PyObject **obj) |
| 6606 | { |
| 6607 | *obj = NULL; |
| 6608 | if (code_page == CP_ACP) |
| 6609 | return "mbcs"; |
| 6610 | if (code_page == CP_UTF7) |
| 6611 | return "CP_UTF7"; |
| 6612 | if (code_page == CP_UTF8) |
| 6613 | return "CP_UTF8"; |
| 6614 | |
| 6615 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6616 | if (*obj == NULL) |
| 6617 | return NULL; |
| 6618 | return PyBytes_AS_STRING(*obj); |
| 6619 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6620 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6621 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6622 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6623 | { |
| 6624 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6625 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6626 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6627 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6628 | return 0; |
| 6629 | |
| 6630 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6631 | if (prev == curr) |
| 6632 | return 1; |
| 6633 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6634 | as it assumes an incomplete character consists of a single |
| 6635 | byte. */ |
| 6636 | if (curr - prev == 2) |
| 6637 | return 1; |
| 6638 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6639 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6640 | return 0; |
| 6641 | } |
| 6642 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6643 | static DWORD |
| 6644 | decode_code_page_flags(UINT code_page) |
| 6645 | { |
| 6646 | if (code_page == CP_UTF7) { |
| 6647 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6648 | return 0; |
| 6649 | } |
| 6650 | else |
| 6651 | return MB_ERR_INVALID_CHARS; |
| 6652 | } |
| 6653 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6654 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6655 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6656 | * mode. |
| 6657 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6658 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6659 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6660 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6661 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6662 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6663 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6664 | const char *in, |
| 6665 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6666 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6667 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6668 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6669 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6670 | |
| 6671 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6672 | assert(insize > 0); |
| 6673 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6674 | if (outsize <= 0) |
| 6675 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6676 | |
| 6677 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6678 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6679 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6680 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6681 | if (*v == NULL) |
| 6682 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6683 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6684 | } |
| 6685 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6686 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6687 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6688 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6689 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6690 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
| 6693 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6694 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6695 | if (outsize <= 0) |
| 6696 | goto error; |
| 6697 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6698 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6699 | error: |
| 6700 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6701 | return -2; |
| 6702 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6703 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6704 | } |
| 6705 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6706 | /* |
| 6707 | * Decode a byte string from a code page into unicode object with an error |
| 6708 | * handler. |
| 6709 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6710 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6711 | * UnicodeDecodeError exception and returns -1 on error. |
| 6712 | */ |
| 6713 | static int |
| 6714 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6715 | PyObject **v, |
| 6716 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6717 | const char *errors) |
| 6718 | { |
| 6719 | const char *startin = in; |
| 6720 | const char *endin = in + size; |
| 6721 | const DWORD flags = decode_code_page_flags(code_page); |
| 6722 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6723 | 2000 English version of the message. */ |
| 6724 | const char *reason = "No mapping for the Unicode character exists " |
| 6725 | "in the target code page."; |
| 6726 | /* each step cannot decode more than 1 character, but a character can be |
| 6727 | represented as a surrogate pair */ |
| 6728 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6729 | int insize; |
| 6730 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6731 | PyObject *errorHandler = NULL; |
| 6732 | PyObject *exc = NULL; |
| 6733 | PyObject *encoding_obj = NULL; |
| 6734 | char *encoding; |
| 6735 | DWORD err; |
| 6736 | int ret = -1; |
| 6737 | |
| 6738 | assert(size > 0); |
| 6739 | |
| 6740 | encoding = code_page_name(code_page, &encoding_obj); |
| 6741 | if (encoding == NULL) |
| 6742 | return -1; |
| 6743 | |
| 6744 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6745 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6746 | UnicodeDecodeError. */ |
| 6747 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6748 | if (exc != NULL) { |
| 6749 | PyCodec_StrictErrors(exc); |
| 6750 | Py_CLEAR(exc); |
| 6751 | } |
| 6752 | goto error; |
| 6753 | } |
| 6754 | |
| 6755 | if (*v == NULL) { |
| 6756 | /* Create unicode object */ |
| 6757 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6758 | PyErr_NoMemory(); |
| 6759 | goto error; |
| 6760 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6761 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6762 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6763 | if (*v == NULL) |
| 6764 | goto error; |
| 6765 | startout = PyUnicode_AS_UNICODE(*v); |
| 6766 | } |
| 6767 | else { |
| 6768 | /* Extend unicode object */ |
| 6769 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6770 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6771 | PyErr_NoMemory(); |
| 6772 | goto error; |
| 6773 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6774 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6775 | goto error; |
| 6776 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6777 | } |
| 6778 | |
| 6779 | /* Decode the byte string character per character */ |
| 6780 | out = startout; |
| 6781 | while (in < endin) |
| 6782 | { |
| 6783 | /* Decode a character */ |
| 6784 | insize = 1; |
| 6785 | do |
| 6786 | { |
| 6787 | outsize = MultiByteToWideChar(code_page, flags, |
| 6788 | in, insize, |
| 6789 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6790 | if (outsize > 0) |
| 6791 | break; |
| 6792 | err = GetLastError(); |
| 6793 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6794 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6795 | { |
| 6796 | PyErr_SetFromWindowsErr(0); |
| 6797 | goto error; |
| 6798 | } |
| 6799 | insize++; |
| 6800 | } |
| 6801 | /* 4=maximum length of a UTF-8 sequence */ |
| 6802 | while (insize <= 4 && (in + insize) <= endin); |
| 6803 | |
| 6804 | if (outsize <= 0) { |
| 6805 | Py_ssize_t startinpos, endinpos, outpos; |
| 6806 | |
| 6807 | startinpos = in - startin; |
| 6808 | endinpos = startinpos + 1; |
| 6809 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6810 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6811 | errors, &errorHandler, |
| 6812 | encoding, reason, |
| 6813 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6814 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6815 | { |
| 6816 | goto error; |
| 6817 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6818 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6819 | } |
| 6820 | else { |
| 6821 | in += insize; |
| 6822 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6823 | out += outsize; |
| 6824 | } |
| 6825 | } |
| 6826 | |
| 6827 | /* write a NUL character at the end */ |
| 6828 | *out = 0; |
| 6829 | |
| 6830 | /* Extend unicode object */ |
| 6831 | outsize = out - startout; |
| 6832 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6833 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6834 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6835 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6836 | |
| 6837 | error: |
| 6838 | Py_XDECREF(encoding_obj); |
| 6839 | Py_XDECREF(errorHandler); |
| 6840 | Py_XDECREF(exc); |
| 6841 | return ret; |
| 6842 | } |
| 6843 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6844 | static PyObject * |
| 6845 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6846 | const char *s, Py_ssize_t size, |
| 6847 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6848 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6849 | PyObject *v = NULL; |
| 6850 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6851 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6852 | if (code_page < 0) { |
| 6853 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6854 | return NULL; |
| 6855 | } |
| 6856 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6857 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6858 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6859 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6860 | do |
| 6861 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6862 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6863 | if (size > INT_MAX) { |
| 6864 | chunk_size = INT_MAX; |
| 6865 | final = 0; |
| 6866 | done = 0; |
| 6867 | } |
| 6868 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6869 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6870 | { |
| 6871 | chunk_size = (int)size; |
| 6872 | final = (consumed == NULL); |
| 6873 | done = 1; |
| 6874 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6875 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6876 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6877 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6878 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6879 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6880 | if (chunk_size == 0 && done) { |
| 6881 | if (v != NULL) |
| 6882 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6883 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6884 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6885 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6886 | |
| 6887 | converted = decode_code_page_strict(code_page, &v, |
| 6888 | s, chunk_size); |
| 6889 | if (converted == -2) |
| 6890 | converted = decode_code_page_errors(code_page, &v, |
| 6891 | s, chunk_size, |
| 6892 | errors); |
| 6893 | assert(converted != 0); |
| 6894 | |
| 6895 | if (converted < 0) { |
| 6896 | Py_XDECREF(v); |
| 6897 | return NULL; |
| 6898 | } |
| 6899 | |
| 6900 | if (consumed) |
| 6901 | *consumed += converted; |
| 6902 | |
| 6903 | s += converted; |
| 6904 | size -= converted; |
| 6905 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6906 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6907 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6908 | } |
| 6909 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6910 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6911 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6912 | const char *s, |
| 6913 | Py_ssize_t size, |
| 6914 | const char *errors, |
| 6915 | Py_ssize_t *consumed) |
| 6916 | { |
| 6917 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6918 | } |
| 6919 | |
| 6920 | PyObject * |
| 6921 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6922 | Py_ssize_t size, |
| 6923 | const char *errors, |
| 6924 | Py_ssize_t *consumed) |
| 6925 | { |
| 6926 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6927 | } |
| 6928 | |
| 6929 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6930 | PyUnicode_DecodeMBCS(const char *s, |
| 6931 | Py_ssize_t size, |
| 6932 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6933 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6934 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6935 | } |
| 6936 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6937 | static DWORD |
| 6938 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6939 | { |
| 6940 | if (code_page == CP_UTF8) { |
| 6941 | if (winver.dwMajorVersion >= 6) |
| 6942 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6943 | and later */ |
| 6944 | return WC_ERR_INVALID_CHARS; |
| 6945 | else |
| 6946 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 6947 | return 0; |
| 6948 | } |
| 6949 | else if (code_page == CP_UTF7) { |
| 6950 | /* CP_UTF7 only supports flags=0 */ |
| 6951 | return 0; |
| 6952 | } |
| 6953 | else { |
| 6954 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 6955 | return 0; |
| 6956 | else |
| 6957 | return WC_NO_BEST_FIT_CHARS; |
| 6958 | } |
| 6959 | } |
| 6960 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6961 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6962 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 6963 | * mode. |
| 6964 | * |
| 6965 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6966 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6967 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6968 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6969 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6970 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6971 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6972 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6973 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6974 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 6975 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6976 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6977 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6978 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6979 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 6980 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6981 | /* Create a substring so that we can get the UTF-16 representation |
| 6982 | of just the slice under consideration. */ |
| 6983 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6984 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6985 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6986 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6987 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6988 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6989 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6990 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6991 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6992 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 6993 | if (substring == NULL) |
| 6994 | return -1; |
| 6995 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 6996 | if (p == NULL) { |
| 6997 | Py_DECREF(substring); |
| 6998 | return -1; |
| 6999 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7000 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7001 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7002 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7003 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7004 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7005 | NULL, 0, |
| 7006 | NULL, pusedDefaultChar); |
| 7007 | if (outsize <= 0) |
| 7008 | goto error; |
| 7009 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7010 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7011 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7012 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7013 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7014 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7015 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7016 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7017 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7018 | if (*outbytes == NULL) { |
| 7019 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7020 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7021 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7022 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7023 | } |
| 7024 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7025 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7026 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7027 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7028 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7029 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7030 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7031 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7032 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7033 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7034 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7035 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7036 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
| 7039 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7040 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7041 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7042 | out, outsize, |
| 7043 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7044 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7045 | if (outsize <= 0) |
| 7046 | goto error; |
| 7047 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7048 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7049 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7050 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7051 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7052 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7053 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7054 | return -2; |
| 7055 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7056 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7057 | } |
| 7058 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7059 | /* |
| 7060 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7061 | * error handler. |
| 7062 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7063 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7064 | * -1 on other error. |
| 7065 | */ |
| 7066 | static int |
| 7067 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7068 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7069 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7070 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7071 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7072 | Py_ssize_t pos = unicode_offset; |
| 7073 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7074 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7075 | 2000 English version of the message. */ |
| 7076 | const char *reason = "invalid character"; |
| 7077 | /* 4=maximum length of a UTF-8 sequence */ |
| 7078 | char buffer[4]; |
| 7079 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7080 | Py_ssize_t outsize; |
| 7081 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7082 | PyObject *errorHandler = NULL; |
| 7083 | PyObject *exc = NULL; |
| 7084 | PyObject *encoding_obj = NULL; |
| 7085 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7086 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7087 | PyObject *rep; |
| 7088 | int ret = -1; |
| 7089 | |
| 7090 | assert(insize > 0); |
| 7091 | |
| 7092 | encoding = code_page_name(code_page, &encoding_obj); |
| 7093 | if (encoding == NULL) |
| 7094 | return -1; |
| 7095 | |
| 7096 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7097 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7098 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7099 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7100 | if (exc != NULL) { |
| 7101 | PyCodec_StrictErrors(exc); |
| 7102 | Py_DECREF(exc); |
| 7103 | } |
| 7104 | Py_XDECREF(encoding_obj); |
| 7105 | return -1; |
| 7106 | } |
| 7107 | |
| 7108 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7109 | pusedDefaultChar = &usedDefaultChar; |
| 7110 | else |
| 7111 | pusedDefaultChar = NULL; |
| 7112 | |
| 7113 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7114 | PyErr_NoMemory(); |
| 7115 | goto error; |
| 7116 | } |
| 7117 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7118 | |
| 7119 | if (*outbytes == NULL) { |
| 7120 | /* Create string object */ |
| 7121 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7122 | if (*outbytes == NULL) |
| 7123 | goto error; |
| 7124 | out = PyBytes_AS_STRING(*outbytes); |
| 7125 | } |
| 7126 | else { |
| 7127 | /* Extend string object */ |
| 7128 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7129 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7130 | PyErr_NoMemory(); |
| 7131 | goto error; |
| 7132 | } |
| 7133 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7134 | goto error; |
| 7135 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7136 | } |
| 7137 | |
| 7138 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7139 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7140 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7141 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7142 | wchar_t chars[2]; |
| 7143 | int charsize; |
| 7144 | if (ch < 0x10000) { |
| 7145 | chars[0] = (wchar_t)ch; |
| 7146 | charsize = 1; |
| 7147 | } |
| 7148 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7149 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7150 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7151 | charsize = 2; |
| 7152 | } |
| 7153 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7154 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7155 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7156 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7157 | NULL, pusedDefaultChar); |
| 7158 | if (outsize > 0) { |
| 7159 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7160 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7161 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7162 | memcpy(out, buffer, outsize); |
| 7163 | out += outsize; |
| 7164 | continue; |
| 7165 | } |
| 7166 | } |
| 7167 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7168 | PyErr_SetFromWindowsErr(0); |
| 7169 | goto error; |
| 7170 | } |
| 7171 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | rep = unicode_encode_call_errorhandler( |
| 7173 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7174 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7175 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7176 | if (rep == NULL) |
| 7177 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7178 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7179 | |
| 7180 | if (PyBytes_Check(rep)) { |
| 7181 | outsize = PyBytes_GET_SIZE(rep); |
| 7182 | if (outsize != 1) { |
| 7183 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7184 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7185 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7186 | Py_DECREF(rep); |
| 7187 | goto error; |
| 7188 | } |
| 7189 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7190 | } |
| 7191 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7192 | out += outsize; |
| 7193 | } |
| 7194 | else { |
| 7195 | Py_ssize_t i; |
| 7196 | enum PyUnicode_Kind kind; |
| 7197 | void *data; |
| 7198 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7199 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7200 | Py_DECREF(rep); |
| 7201 | goto error; |
| 7202 | } |
| 7203 | |
| 7204 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7205 | if (outsize != 1) { |
| 7206 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7207 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7208 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7209 | Py_DECREF(rep); |
| 7210 | goto error; |
| 7211 | } |
| 7212 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7213 | } |
| 7214 | kind = PyUnicode_KIND(rep); |
| 7215 | data = PyUnicode_DATA(rep); |
| 7216 | for (i=0; i < outsize; i++) { |
| 7217 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7218 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7219 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7220 | encoding, unicode, |
| 7221 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7222 | "unable to encode error handler result to ASCII"); |
| 7223 | Py_DECREF(rep); |
| 7224 | goto error; |
| 7225 | } |
| 7226 | *out = (unsigned char)ch; |
| 7227 | out++; |
| 7228 | } |
| 7229 | } |
| 7230 | Py_DECREF(rep); |
| 7231 | } |
| 7232 | /* write a NUL byte */ |
| 7233 | *out = 0; |
| 7234 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7235 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7236 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7237 | goto error; |
| 7238 | ret = 0; |
| 7239 | |
| 7240 | error: |
| 7241 | Py_XDECREF(encoding_obj); |
| 7242 | Py_XDECREF(errorHandler); |
| 7243 | Py_XDECREF(exc); |
| 7244 | return ret; |
| 7245 | } |
| 7246 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | static PyObject * |
| 7248 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7249 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7250 | const char *errors) |
| 7251 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7252 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7253 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7254 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7255 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7256 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7257 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7258 | return NULL; |
| 7259 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7260 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7261 | if (code_page < 0) { |
| 7262 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7263 | return NULL; |
| 7264 | } |
| 7265 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7266 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7267 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7268 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7269 | offset = 0; |
| 7270 | do |
| 7271 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7272 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7273 | /* 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] | 7274 | chunks. */ |
| 7275 | if (len > INT_MAX/2) { |
| 7276 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7277 | done = 0; |
| 7278 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7279 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7280 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7281 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7282 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7283 | done = 1; |
| 7284 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7285 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7286 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7287 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7288 | errors); |
| 7289 | if (ret == -2) |
| 7290 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7291 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7292 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7293 | if (ret < 0) { |
| 7294 | Py_XDECREF(outbytes); |
| 7295 | return NULL; |
| 7296 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7297 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7298 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7299 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7300 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7301 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7302 | return outbytes; |
| 7303 | } |
| 7304 | |
| 7305 | PyObject * |
| 7306 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7307 | Py_ssize_t size, |
| 7308 | const char *errors) |
| 7309 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7310 | PyObject *unicode, *res; |
| 7311 | unicode = PyUnicode_FromUnicode(p, size); |
| 7312 | if (unicode == NULL) |
| 7313 | return NULL; |
| 7314 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7315 | Py_DECREF(unicode); |
| 7316 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7317 | } |
| 7318 | |
| 7319 | PyObject * |
| 7320 | PyUnicode_EncodeCodePage(int code_page, |
| 7321 | PyObject *unicode, |
| 7322 | const char *errors) |
| 7323 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7324 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7325 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7326 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7327 | PyObject * |
| 7328 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7329 | { |
| 7330 | if (!PyUnicode_Check(unicode)) { |
| 7331 | PyErr_BadArgument(); |
| 7332 | return NULL; |
| 7333 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7334 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7335 | } |
| 7336 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7337 | #undef NEED_RETRY |
| 7338 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7339 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7340 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7341 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7342 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7343 | static int |
| 7344 | charmap_decode_string(const char *s, |
| 7345 | Py_ssize_t size, |
| 7346 | PyObject *mapping, |
| 7347 | const char *errors, |
| 7348 | _PyUnicodeWriter *writer) |
| 7349 | { |
| 7350 | const char *starts = s; |
| 7351 | const char *e; |
| 7352 | Py_ssize_t startinpos, endinpos; |
| 7353 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7354 | Py_ssize_t maplen; |
| 7355 | enum PyUnicode_Kind mapkind; |
| 7356 | void *mapdata; |
| 7357 | Py_UCS4 x; |
| 7358 | unsigned char ch; |
| 7359 | |
| 7360 | if (PyUnicode_READY(mapping) == -1) |
| 7361 | return -1; |
| 7362 | |
| 7363 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7364 | mapdata = PyUnicode_DATA(mapping); |
| 7365 | mapkind = PyUnicode_KIND(mapping); |
| 7366 | |
| 7367 | e = s + size; |
| 7368 | |
| 7369 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7370 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7371 | * is disabled in encoding aliases, latin1 is preferred because |
| 7372 | * its implementation is faster. */ |
| 7373 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7374 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7375 | Py_UCS4 maxchar = writer->maxchar; |
| 7376 | |
| 7377 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7378 | while (s < e) { |
| 7379 | ch = *s; |
| 7380 | x = mapdata_ucs1[ch]; |
| 7381 | if (x > maxchar) { |
| 7382 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7383 | goto onError; |
| 7384 | maxchar = writer->maxchar; |
| 7385 | outdata = (Py_UCS1 *)writer->data; |
| 7386 | } |
| 7387 | outdata[writer->pos] = x; |
| 7388 | writer->pos++; |
| 7389 | ++s; |
| 7390 | } |
| 7391 | return 0; |
| 7392 | } |
| 7393 | |
| 7394 | while (s < e) { |
| 7395 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7396 | enum PyUnicode_Kind outkind = writer->kind; |
| 7397 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7398 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7399 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7400 | Py_UCS4 maxchar = writer->maxchar; |
| 7401 | while (s < e) { |
| 7402 | ch = *s; |
| 7403 | x = mapdata_ucs2[ch]; |
| 7404 | if (x > maxchar) |
| 7405 | goto Error; |
| 7406 | outdata[writer->pos] = x; |
| 7407 | writer->pos++; |
| 7408 | ++s; |
| 7409 | } |
| 7410 | break; |
| 7411 | } |
| 7412 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7413 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7414 | while (s < e) { |
| 7415 | ch = *s; |
| 7416 | x = mapdata_ucs2[ch]; |
| 7417 | if (x == 0xFFFE) |
| 7418 | goto Error; |
| 7419 | outdata[writer->pos] = x; |
| 7420 | writer->pos++; |
| 7421 | ++s; |
| 7422 | } |
| 7423 | break; |
| 7424 | } |
| 7425 | } |
| 7426 | ch = *s; |
| 7427 | |
| 7428 | if (ch < maplen) |
| 7429 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7430 | else |
| 7431 | x = 0xfffe; /* invalid value */ |
| 7432 | Error: |
| 7433 | if (x == 0xfffe) |
| 7434 | { |
| 7435 | /* undefined mapping */ |
| 7436 | startinpos = s-starts; |
| 7437 | endinpos = startinpos+1; |
| 7438 | if (unicode_decode_call_errorhandler_writer( |
| 7439 | errors, &errorHandler, |
| 7440 | "charmap", "character maps to <undefined>", |
| 7441 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7442 | writer)) { |
| 7443 | goto onError; |
| 7444 | } |
| 7445 | continue; |
| 7446 | } |
| 7447 | |
| 7448 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7449 | goto onError; |
| 7450 | ++s; |
| 7451 | } |
| 7452 | Py_XDECREF(errorHandler); |
| 7453 | Py_XDECREF(exc); |
| 7454 | return 0; |
| 7455 | |
| 7456 | onError: |
| 7457 | Py_XDECREF(errorHandler); |
| 7458 | Py_XDECREF(exc); |
| 7459 | return -1; |
| 7460 | } |
| 7461 | |
| 7462 | static int |
| 7463 | charmap_decode_mapping(const char *s, |
| 7464 | Py_ssize_t size, |
| 7465 | PyObject *mapping, |
| 7466 | const char *errors, |
| 7467 | _PyUnicodeWriter *writer) |
| 7468 | { |
| 7469 | const char *starts = s; |
| 7470 | const char *e; |
| 7471 | Py_ssize_t startinpos, endinpos; |
| 7472 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7473 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7474 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7475 | |
| 7476 | e = s + size; |
| 7477 | |
| 7478 | while (s < e) { |
| 7479 | ch = *s; |
| 7480 | |
| 7481 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7482 | key = PyLong_FromLong((long)ch); |
| 7483 | if (key == NULL) |
| 7484 | goto onError; |
| 7485 | |
| 7486 | item = PyObject_GetItem(mapping, key); |
| 7487 | Py_DECREF(key); |
| 7488 | if (item == NULL) { |
| 7489 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7490 | /* No mapping found means: mapping is undefined. */ |
| 7491 | PyErr_Clear(); |
| 7492 | goto Undefined; |
| 7493 | } else |
| 7494 | goto onError; |
| 7495 | } |
| 7496 | |
| 7497 | /* Apply mapping */ |
| 7498 | if (item == Py_None) |
| 7499 | goto Undefined; |
| 7500 | if (PyLong_Check(item)) { |
| 7501 | long value = PyLong_AS_LONG(item); |
| 7502 | if (value == 0xFFFE) |
| 7503 | goto Undefined; |
| 7504 | if (value < 0 || value > MAX_UNICODE) { |
| 7505 | PyErr_Format(PyExc_TypeError, |
| 7506 | "character mapping must be in range(0x%lx)", |
| 7507 | (unsigned long)MAX_UNICODE + 1); |
| 7508 | goto onError; |
| 7509 | } |
| 7510 | |
| 7511 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7512 | goto onError; |
| 7513 | } |
| 7514 | else if (PyUnicode_Check(item)) { |
| 7515 | if (PyUnicode_READY(item) == -1) |
| 7516 | goto onError; |
| 7517 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7518 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7519 | if (value == 0xFFFE) |
| 7520 | goto Undefined; |
| 7521 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7522 | goto onError; |
| 7523 | } |
| 7524 | else { |
| 7525 | writer->overallocate = 1; |
| 7526 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7527 | goto onError; |
| 7528 | } |
| 7529 | } |
| 7530 | else { |
| 7531 | /* wrong return value */ |
| 7532 | PyErr_SetString(PyExc_TypeError, |
| 7533 | "character mapping must return integer, None or str"); |
| 7534 | goto onError; |
| 7535 | } |
| 7536 | Py_CLEAR(item); |
| 7537 | ++s; |
| 7538 | continue; |
| 7539 | |
| 7540 | Undefined: |
| 7541 | /* undefined mapping */ |
| 7542 | Py_CLEAR(item); |
| 7543 | startinpos = s-starts; |
| 7544 | endinpos = startinpos+1; |
| 7545 | if (unicode_decode_call_errorhandler_writer( |
| 7546 | errors, &errorHandler, |
| 7547 | "charmap", "character maps to <undefined>", |
| 7548 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7549 | writer)) { |
| 7550 | goto onError; |
| 7551 | } |
| 7552 | } |
| 7553 | Py_XDECREF(errorHandler); |
| 7554 | Py_XDECREF(exc); |
| 7555 | return 0; |
| 7556 | |
| 7557 | onError: |
| 7558 | Py_XDECREF(item); |
| 7559 | Py_XDECREF(errorHandler); |
| 7560 | Py_XDECREF(exc); |
| 7561 | return -1; |
| 7562 | } |
| 7563 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7564 | PyObject * |
| 7565 | PyUnicode_DecodeCharmap(const char *s, |
| 7566 | Py_ssize_t size, |
| 7567 | PyObject *mapping, |
| 7568 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7570 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | /* Default to Latin-1 */ |
| 7573 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7574 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7575 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7577 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7578 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7579 | writer.min_length = size; |
| 7580 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7582 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7583 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7584 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7585 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7586 | } |
| 7587 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7588 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7589 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7591 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7593 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7594 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7595 | return NULL; |
| 7596 | } |
| 7597 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7598 | /* Charmap encoding: the lookup table */ |
| 7599 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7600 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7601 | PyObject_HEAD |
| 7602 | unsigned char level1[32]; |
| 7603 | int count2, count3; |
| 7604 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7605 | }; |
| 7606 | |
| 7607 | static PyObject* |
| 7608 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7609 | { |
| 7610 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7611 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7612 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7613 | } |
| 7614 | |
| 7615 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7616 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7617 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7618 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7619 | }; |
| 7620 | |
| 7621 | static void |
| 7622 | encoding_map_dealloc(PyObject* o) |
| 7623 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7624 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7625 | } |
| 7626 | |
| 7627 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7628 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7629 | "EncodingMap", /*tp_name*/ |
| 7630 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7631 | 0, /*tp_itemsize*/ |
| 7632 | /* methods */ |
| 7633 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7634 | 0, /*tp_print*/ |
| 7635 | 0, /*tp_getattr*/ |
| 7636 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7637 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7638 | 0, /*tp_repr*/ |
| 7639 | 0, /*tp_as_number*/ |
| 7640 | 0, /*tp_as_sequence*/ |
| 7641 | 0, /*tp_as_mapping*/ |
| 7642 | 0, /*tp_hash*/ |
| 7643 | 0, /*tp_call*/ |
| 7644 | 0, /*tp_str*/ |
| 7645 | 0, /*tp_getattro*/ |
| 7646 | 0, /*tp_setattro*/ |
| 7647 | 0, /*tp_as_buffer*/ |
| 7648 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7649 | 0, /*tp_doc*/ |
| 7650 | 0, /*tp_traverse*/ |
| 7651 | 0, /*tp_clear*/ |
| 7652 | 0, /*tp_richcompare*/ |
| 7653 | 0, /*tp_weaklistoffset*/ |
| 7654 | 0, /*tp_iter*/ |
| 7655 | 0, /*tp_iternext*/ |
| 7656 | encoding_map_methods, /*tp_methods*/ |
| 7657 | 0, /*tp_members*/ |
| 7658 | 0, /*tp_getset*/ |
| 7659 | 0, /*tp_base*/ |
| 7660 | 0, /*tp_dict*/ |
| 7661 | 0, /*tp_descr_get*/ |
| 7662 | 0, /*tp_descr_set*/ |
| 7663 | 0, /*tp_dictoffset*/ |
| 7664 | 0, /*tp_init*/ |
| 7665 | 0, /*tp_alloc*/ |
| 7666 | 0, /*tp_new*/ |
| 7667 | 0, /*tp_free*/ |
| 7668 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7669 | }; |
| 7670 | |
| 7671 | PyObject* |
| 7672 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7673 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7674 | PyObject *result; |
| 7675 | struct encoding_map *mresult; |
| 7676 | int i; |
| 7677 | int need_dict = 0; |
| 7678 | unsigned char level1[32]; |
| 7679 | unsigned char level2[512]; |
| 7680 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7681 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7682 | int kind; |
| 7683 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7684 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7685 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7686 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7687 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7688 | PyErr_BadArgument(); |
| 7689 | return NULL; |
| 7690 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7691 | kind = PyUnicode_KIND(string); |
| 7692 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7693 | length = PyUnicode_GET_LENGTH(string); |
| 7694 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7695 | memset(level1, 0xFF, sizeof level1); |
| 7696 | memset(level2, 0xFF, sizeof level2); |
| 7697 | |
| 7698 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7699 | or if there are non-BMP characters, we need to use |
| 7700 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7701 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7702 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7703 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7704 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7705 | ch = PyUnicode_READ(kind, data, i); |
| 7706 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7707 | need_dict = 1; |
| 7708 | break; |
| 7709 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7710 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7711 | /* unmapped character */ |
| 7712 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7713 | l1 = ch >> 11; |
| 7714 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7715 | if (level1[l1] == 0xFF) |
| 7716 | level1[l1] = count2++; |
| 7717 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7718 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7719 | } |
| 7720 | |
| 7721 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7722 | need_dict = 1; |
| 7723 | |
| 7724 | if (need_dict) { |
| 7725 | PyObject *result = PyDict_New(); |
| 7726 | PyObject *key, *value; |
| 7727 | if (!result) |
| 7728 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7729 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7730 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7731 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7732 | if (!key || !value) |
| 7733 | goto failed1; |
| 7734 | if (PyDict_SetItem(result, key, value) == -1) |
| 7735 | goto failed1; |
| 7736 | Py_DECREF(key); |
| 7737 | Py_DECREF(value); |
| 7738 | } |
| 7739 | return result; |
| 7740 | failed1: |
| 7741 | Py_XDECREF(key); |
| 7742 | Py_XDECREF(value); |
| 7743 | Py_DECREF(result); |
| 7744 | return NULL; |
| 7745 | } |
| 7746 | |
| 7747 | /* Create a three-level trie */ |
| 7748 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7749 | 16*count2 + 128*count3 - 1); |
| 7750 | if (!result) |
| 7751 | return PyErr_NoMemory(); |
| 7752 | PyObject_Init(result, &EncodingMapType); |
| 7753 | mresult = (struct encoding_map*)result; |
| 7754 | mresult->count2 = count2; |
| 7755 | mresult->count3 = count3; |
| 7756 | mlevel1 = mresult->level1; |
| 7757 | mlevel2 = mresult->level23; |
| 7758 | mlevel3 = mresult->level23 + 16*count2; |
| 7759 | memcpy(mlevel1, level1, 32); |
| 7760 | memset(mlevel2, 0xFF, 16*count2); |
| 7761 | memset(mlevel3, 0, 128*count3); |
| 7762 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7763 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7764 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7765 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7766 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7767 | /* unmapped character */ |
| 7768 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7769 | o1 = ch>>11; |
| 7770 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7771 | i2 = 16*mlevel1[o1] + o2; |
| 7772 | if (mlevel2[i2] == 0xFF) |
| 7773 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7774 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7775 | i3 = 128*mlevel2[i2] + o3; |
| 7776 | mlevel3[i3] = i; |
| 7777 | } |
| 7778 | return result; |
| 7779 | } |
| 7780 | |
| 7781 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7782 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7783 | { |
| 7784 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7785 | int l1 = c>>11; |
| 7786 | int l2 = (c>>7) & 0xF; |
| 7787 | int l3 = c & 0x7F; |
| 7788 | int i; |
| 7789 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7790 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7792 | if (c == 0) |
| 7793 | return 0; |
| 7794 | /* level 1*/ |
| 7795 | i = map->level1[l1]; |
| 7796 | if (i == 0xFF) { |
| 7797 | return -1; |
| 7798 | } |
| 7799 | /* level 2*/ |
| 7800 | i = map->level23[16*i+l2]; |
| 7801 | if (i == 0xFF) { |
| 7802 | return -1; |
| 7803 | } |
| 7804 | /* level 3 */ |
| 7805 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7806 | if (i == 0) { |
| 7807 | return -1; |
| 7808 | } |
| 7809 | return i; |
| 7810 | } |
| 7811 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7812 | /* Lookup the character ch in the mapping. If the character |
| 7813 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7814 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7815 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7816 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7818 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7819 | PyObject *x; |
| 7820 | |
| 7821 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7822 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7823 | x = PyObject_GetItem(mapping, w); |
| 7824 | Py_DECREF(w); |
| 7825 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7826 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7827 | /* No mapping found means: mapping is undefined. */ |
| 7828 | PyErr_Clear(); |
| 7829 | x = Py_None; |
| 7830 | Py_INCREF(x); |
| 7831 | return x; |
| 7832 | } else |
| 7833 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7835 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7836 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7837 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7838 | long value = PyLong_AS_LONG(x); |
| 7839 | if (value < 0 || value > 255) { |
| 7840 | PyErr_SetString(PyExc_TypeError, |
| 7841 | "character mapping must be in range(256)"); |
| 7842 | Py_DECREF(x); |
| 7843 | return NULL; |
| 7844 | } |
| 7845 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7846 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7847 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7848 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7850 | /* wrong return value */ |
| 7851 | PyErr_Format(PyExc_TypeError, |
| 7852 | "character mapping must return integer, bytes or None, not %.400s", |
| 7853 | x->ob_type->tp_name); |
| 7854 | Py_DECREF(x); |
| 7855 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7856 | } |
| 7857 | } |
| 7858 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7859 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7860 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7861 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7862 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7863 | /* exponentially overallocate to minimize reallocations */ |
| 7864 | if (requiredsize < 2*outsize) |
| 7865 | requiredsize = 2*outsize; |
| 7866 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7867 | return -1; |
| 7868 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7869 | } |
| 7870 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7871 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7872 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7873 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7874 | /* 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] | 7875 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7876 | space is available. Return a new reference to the object that |
| 7877 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7878 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7879 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7880 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7881 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7882 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7883 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7884 | PyObject *rep; |
| 7885 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7886 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7887 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7888 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7889 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7890 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7891 | if (res == -1) |
| 7892 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7893 | if (outsize<requiredsize) |
| 7894 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7895 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7896 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7897 | outstart[(*outpos)++] = (char)res; |
| 7898 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7899 | } |
| 7900 | |
| 7901 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7902 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7904 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | Py_DECREF(rep); |
| 7906 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7907 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7908 | if (PyLong_Check(rep)) { |
| 7909 | Py_ssize_t requiredsize = *outpos+1; |
| 7910 | if (outsize<requiredsize) |
| 7911 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7912 | Py_DECREF(rep); |
| 7913 | return enc_EXCEPTION; |
| 7914 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7915 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7916 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7917 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7918 | else { |
| 7919 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7920 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7921 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7922 | if (outsize<requiredsize) |
| 7923 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7924 | Py_DECREF(rep); |
| 7925 | return enc_EXCEPTION; |
| 7926 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7927 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7928 | memcpy(outstart + *outpos, repchars, repsize); |
| 7929 | *outpos += repsize; |
| 7930 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7931 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7932 | Py_DECREF(rep); |
| 7933 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7934 | } |
| 7935 | |
| 7936 | /* handle an error in PyUnicode_EncodeCharmap |
| 7937 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7938 | static int |
| 7939 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7940 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7941 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7942 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7943 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7944 | { |
| 7945 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7946 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7947 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7948 | enum PyUnicode_Kind kind; |
| 7949 | void *data; |
| 7950 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7951 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7952 | Py_ssize_t collstartpos = *inpos; |
| 7953 | Py_ssize_t collendpos = *inpos+1; |
| 7954 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7955 | char *encoding = "charmap"; |
| 7956 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7957 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7958 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7959 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7960 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7961 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7962 | return -1; |
| 7963 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7964 | /* find all unencodable characters */ |
| 7965 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7966 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7967 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7968 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7969 | val = encoding_map_lookup(ch, mapping); |
| 7970 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7971 | break; |
| 7972 | ++collendpos; |
| 7973 | continue; |
| 7974 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7975 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7976 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7977 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7978 | if (rep==NULL) |
| 7979 | return -1; |
| 7980 | else if (rep!=Py_None) { |
| 7981 | Py_DECREF(rep); |
| 7982 | break; |
| 7983 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7984 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7986 | } |
| 7987 | /* cache callback name lookup |
| 7988 | * (if not done yet, i.e. it's the first error) */ |
| 7989 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7990 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7991 | *known_errorHandler = 1; |
| 7992 | else if (!strcmp(errors, "replace")) |
| 7993 | *known_errorHandler = 2; |
| 7994 | else if (!strcmp(errors, "ignore")) |
| 7995 | *known_errorHandler = 3; |
| 7996 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7997 | *known_errorHandler = 4; |
| 7998 | else |
| 7999 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8000 | } |
| 8001 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8002 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8003 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8004 | return -1; |
| 8005 | case 2: /* replace */ |
| 8006 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8007 | x = charmapencode_output('?', mapping, res, respos); |
| 8008 | if (x==enc_EXCEPTION) { |
| 8009 | return -1; |
| 8010 | } |
| 8011 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8012 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8013 | return -1; |
| 8014 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8015 | } |
| 8016 | /* fall through */ |
| 8017 | case 3: /* ignore */ |
| 8018 | *inpos = collendpos; |
| 8019 | break; |
| 8020 | case 4: /* xmlcharrefreplace */ |
| 8021 | /* generate replacement (temporarily (mis)uses p) */ |
| 8022 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8023 | char buffer[2+29+1+1]; |
| 8024 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8025 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | for (cp = buffer; *cp; ++cp) { |
| 8027 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8028 | if (x==enc_EXCEPTION) |
| 8029 | return -1; |
| 8030 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8031 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8032 | return -1; |
| 8033 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8034 | } |
| 8035 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8036 | *inpos = collendpos; |
| 8037 | break; |
| 8038 | default: |
| 8039 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8040 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8041 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8042 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8043 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8044 | if (PyBytes_Check(repunicode)) { |
| 8045 | /* Directly copy bytes result to output. */ |
| 8046 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8047 | Py_ssize_t requiredsize; |
| 8048 | repsize = PyBytes_Size(repunicode); |
| 8049 | requiredsize = *respos + repsize; |
| 8050 | if (requiredsize > outsize) |
| 8051 | /* Make room for all additional bytes. */ |
| 8052 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8053 | Py_DECREF(repunicode); |
| 8054 | return -1; |
| 8055 | } |
| 8056 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8057 | PyBytes_AsString(repunicode), repsize); |
| 8058 | *respos += repsize; |
| 8059 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8060 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8061 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8062 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8063 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8064 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8065 | Py_DECREF(repunicode); |
| 8066 | return -1; |
| 8067 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8068 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8069 | data = PyUnicode_DATA(repunicode); |
| 8070 | kind = PyUnicode_KIND(repunicode); |
| 8071 | for (index = 0; index < repsize; index++) { |
| 8072 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8073 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8074 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8075 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8076 | return -1; |
| 8077 | } |
| 8078 | else if (x==enc_FAILED) { |
| 8079 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8080 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8081 | return -1; |
| 8082 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8083 | } |
| 8084 | *inpos = newpos; |
| 8085 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8086 | } |
| 8087 | return 0; |
| 8088 | } |
| 8089 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8090 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8091 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8092 | PyObject *mapping, |
| 8093 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8094 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8095 | /* output object */ |
| 8096 | PyObject *res = NULL; |
| 8097 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8098 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8099 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8100 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8101 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8102 | PyObject *errorHandler = NULL; |
| 8103 | PyObject *exc = NULL; |
| 8104 | /* the following variable is used for caching string comparisons |
| 8105 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8106 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8107 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8108 | void *data; |
| 8109 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8110 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8111 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8112 | return NULL; |
| 8113 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8114 | data = PyUnicode_DATA(unicode); |
| 8115 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8117 | /* Default to Latin-1 */ |
| 8118 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8119 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8120 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8121 | /* allocate enough for a simple encoding without |
| 8122 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8123 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8124 | if (res == NULL) |
| 8125 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8126 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8128 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8129 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8130 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8131 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8132 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8133 | if (x==enc_EXCEPTION) /* error */ |
| 8134 | goto onError; |
| 8135 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8136 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8137 | &exc, |
| 8138 | &known_errorHandler, &errorHandler, errors, |
| 8139 | &res, &respos)) { |
| 8140 | goto onError; |
| 8141 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8142 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8143 | else |
| 8144 | /* done with this character => adjust input position */ |
| 8145 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8146 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8147 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8148 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8149 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8150 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8151 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8152 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8153 | Py_XDECREF(exc); |
| 8154 | Py_XDECREF(errorHandler); |
| 8155 | return res; |
| 8156 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8157 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8158 | Py_XDECREF(res); |
| 8159 | Py_XDECREF(exc); |
| 8160 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8161 | return NULL; |
| 8162 | } |
| 8163 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8164 | /* Deprecated */ |
| 8165 | PyObject * |
| 8166 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8167 | Py_ssize_t size, |
| 8168 | PyObject *mapping, |
| 8169 | const char *errors) |
| 8170 | { |
| 8171 | PyObject *result; |
| 8172 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8173 | if (unicode == NULL) |
| 8174 | return NULL; |
| 8175 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8176 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8177 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8178 | } |
| 8179 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8180 | PyObject * |
| 8181 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8182 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8183 | { |
| 8184 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | PyErr_BadArgument(); |
| 8186 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8188 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8189 | } |
| 8190 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8191 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8192 | static void |
| 8193 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8194 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8195 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8196 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8197 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8198 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8199 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8200 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8201 | } |
| 8202 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8203 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8204 | goto onError; |
| 8205 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8206 | goto onError; |
| 8207 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8208 | goto onError; |
| 8209 | return; |
| 8210 | onError: |
| 8211 | Py_DECREF(*exceptionObject); |
| 8212 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8213 | } |
| 8214 | } |
| 8215 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8216 | /* error handling callback helper: |
| 8217 | build arguments, call the callback and check the arguments, |
| 8218 | put the result into newpos and return the replacement string, which |
| 8219 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8220 | static PyObject * |
| 8221 | unicode_translate_call_errorhandler(const char *errors, |
| 8222 | PyObject **errorHandler, |
| 8223 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8224 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8225 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8226 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8227 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8228 | 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] | 8229 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8230 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8231 | PyObject *restuple; |
| 8232 | PyObject *resunicode; |
| 8233 | |
| 8234 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8235 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8236 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8238 | } |
| 8239 | |
| 8240 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8241 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8242 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8243 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | |
| 8245 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8246 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8247 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8249 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8250 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8251 | Py_DECREF(restuple); |
| 8252 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8253 | } |
| 8254 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8255 | &resunicode, &i_newpos)) { |
| 8256 | Py_DECREF(restuple); |
| 8257 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8258 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8259 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8260 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8261 | else |
| 8262 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8263 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8264 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8265 | Py_DECREF(restuple); |
| 8266 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8267 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8268 | Py_INCREF(resunicode); |
| 8269 | Py_DECREF(restuple); |
| 8270 | return resunicode; |
| 8271 | } |
| 8272 | |
| 8273 | /* Lookup the character ch in the mapping and put the result in result, |
| 8274 | which must be decrefed by the caller. |
| 8275 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8276 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8277 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8278 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8279 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8280 | PyObject *x; |
| 8281 | |
| 8282 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8283 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8284 | x = PyObject_GetItem(mapping, w); |
| 8285 | Py_DECREF(w); |
| 8286 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8288 | /* No mapping found means: use 1:1 mapping. */ |
| 8289 | PyErr_Clear(); |
| 8290 | *result = NULL; |
| 8291 | return 0; |
| 8292 | } else |
| 8293 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8294 | } |
| 8295 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8296 | *result = x; |
| 8297 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8298 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8299 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8300 | long value = PyLong_AS_LONG(x); |
| 8301 | long max = PyUnicode_GetMax(); |
| 8302 | if (value < 0 || value > max) { |
| 8303 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8304 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8305 | Py_DECREF(x); |
| 8306 | return -1; |
| 8307 | } |
| 8308 | *result = x; |
| 8309 | return 0; |
| 8310 | } |
| 8311 | else if (PyUnicode_Check(x)) { |
| 8312 | *result = x; |
| 8313 | return 0; |
| 8314 | } |
| 8315 | else { |
| 8316 | /* wrong return value */ |
| 8317 | PyErr_SetString(PyExc_TypeError, |
| 8318 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8319 | Py_DECREF(x); |
| 8320 | return -1; |
| 8321 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8322 | } |
| 8323 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8324 | if not reallocate and adjust various state variables. |
| 8325 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8326 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8327 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8328 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8329 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8330 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8331 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8332 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | /* exponentially overallocate to minimize reallocations */ |
| 8334 | if (requiredsize < 2 * oldsize) |
| 8335 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8336 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8337 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8338 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8339 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8340 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8341 | } |
| 8342 | return 0; |
| 8343 | } |
| 8344 | /* lookup the character, put the result in the output string and adjust |
| 8345 | various state variables. Return a new reference to the object that |
| 8346 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8347 | undefined (in which case no character was written). |
| 8348 | The called must decref result. |
| 8349 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8350 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8351 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8352 | PyObject *mapping, Py_UCS4 **output, |
| 8353 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8354 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8355 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8356 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8357 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8358 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8359 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8360 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8361 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8362 | } |
| 8363 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8364 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8365 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8366 | /* 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] | 8367 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8368 | } |
| 8369 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8370 | Py_ssize_t repsize; |
| 8371 | if (PyUnicode_READY(*res) == -1) |
| 8372 | return -1; |
| 8373 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8374 | if (repsize==1) { |
| 8375 | /* 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] | 8376 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8377 | } |
| 8378 | else if (repsize!=0) { |
| 8379 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8380 | Py_ssize_t requiredsize = *opos + |
| 8381 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8382 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8383 | Py_ssize_t i; |
| 8384 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8385 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8386 | for(i = 0; i < repsize; i++) |
| 8387 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8389 | } |
| 8390 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8391 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8392 | return 0; |
| 8393 | } |
| 8394 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8395 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8396 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8397 | PyObject *mapping, |
| 8398 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8399 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8400 | /* input object */ |
| 8401 | char *idata; |
| 8402 | Py_ssize_t size, i; |
| 8403 | int kind; |
| 8404 | /* output buffer */ |
| 8405 | Py_UCS4 *output = NULL; |
| 8406 | Py_ssize_t osize; |
| 8407 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8408 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8409 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8410 | char *reason = "character maps to <undefined>"; |
| 8411 | PyObject *errorHandler = NULL; |
| 8412 | PyObject *exc = NULL; |
| 8413 | /* the following variable is used for caching string comparisons |
| 8414 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8415 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8416 | int known_errorHandler = -1; |
| 8417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8418 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8419 | PyErr_BadArgument(); |
| 8420 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8421 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8422 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | if (PyUnicode_READY(input) == -1) |
| 8424 | return NULL; |
| 8425 | idata = (char*)PyUnicode_DATA(input); |
| 8426 | kind = PyUnicode_KIND(input); |
| 8427 | size = PyUnicode_GET_LENGTH(input); |
| 8428 | i = 0; |
| 8429 | |
| 8430 | if (size == 0) { |
| 8431 | Py_INCREF(input); |
| 8432 | return input; |
| 8433 | } |
| 8434 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8435 | /* allocate enough for a simple 1:1 translation without |
| 8436 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8437 | osize = size; |
| 8438 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8439 | opos = 0; |
| 8440 | if (output == NULL) { |
| 8441 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8442 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8443 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8444 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8445 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8446 | /* try to encode it */ |
| 8447 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8448 | if (charmaptranslate_output(input, i, mapping, |
| 8449 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8450 | Py_XDECREF(x); |
| 8451 | goto onError; |
| 8452 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8453 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8454 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8455 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | else { /* untranslatable character */ |
| 8457 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8458 | Py_ssize_t repsize; |
| 8459 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8461 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 | Py_ssize_t collstart = i; |
| 8463 | Py_ssize_t collend = i+1; |
| 8464 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8465 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8466 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8467 | while (collend < size) { |
| 8468 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8469 | goto onError; |
| 8470 | Py_XDECREF(x); |
| 8471 | if (x!=Py_None) |
| 8472 | break; |
| 8473 | ++collend; |
| 8474 | } |
| 8475 | /* cache callback name lookup |
| 8476 | * (if not done yet, i.e. it's the first error) */ |
| 8477 | if (known_errorHandler==-1) { |
| 8478 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8479 | known_errorHandler = 1; |
| 8480 | else if (!strcmp(errors, "replace")) |
| 8481 | known_errorHandler = 2; |
| 8482 | else if (!strcmp(errors, "ignore")) |
| 8483 | known_errorHandler = 3; |
| 8484 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8485 | known_errorHandler = 4; |
| 8486 | else |
| 8487 | known_errorHandler = 0; |
| 8488 | } |
| 8489 | switch (known_errorHandler) { |
| 8490 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8491 | make_translate_exception(&exc, |
| 8492 | input, collstart, collend, reason); |
| 8493 | if (exc != NULL) |
| 8494 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8495 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8496 | case 2: /* replace */ |
| 8497 | /* 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] | 8498 | for (coll = collstart; coll<collend; coll++) |
| 8499 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8500 | /* fall through */ |
| 8501 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8502 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8503 | break; |
| 8504 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8505 | /* generate replacement (temporarily (mis)uses i) */ |
| 8506 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8507 | char buffer[2+29+1+1]; |
| 8508 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8509 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8510 | if (charmaptranslate_makespace(&output, &osize, |
| 8511 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8512 | goto onError; |
| 8513 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8514 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8516 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8517 | break; |
| 8518 | default: |
| 8519 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8520 | reason, input, &exc, |
| 8521 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8522 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8524 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8525 | Py_DECREF(repunicode); |
| 8526 | goto onError; |
| 8527 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8528 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8529 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8530 | if (charmaptranslate_makespace(&output, &osize, |
| 8531 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | Py_DECREF(repunicode); |
| 8533 | goto onError; |
| 8534 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8535 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8536 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8537 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8538 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8539 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8540 | } |
| 8541 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8543 | if (!res) |
| 8544 | goto onError; |
| 8545 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8546 | Py_XDECREF(exc); |
| 8547 | Py_XDECREF(errorHandler); |
| 8548 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8549 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8550 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8551 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8552 | Py_XDECREF(exc); |
| 8553 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | return NULL; |
| 8555 | } |
| 8556 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8557 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8558 | PyObject * |
| 8559 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8560 | Py_ssize_t size, |
| 8561 | PyObject *mapping, |
| 8562 | const char *errors) |
| 8563 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8564 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8565 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8566 | if (!unicode) |
| 8567 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8568 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8569 | Py_DECREF(unicode); |
| 8570 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8571 | } |
| 8572 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8573 | PyObject * |
| 8574 | PyUnicode_Translate(PyObject *str, |
| 8575 | PyObject *mapping, |
| 8576 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | { |
| 8578 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8580 | str = PyUnicode_FromObject(str); |
| 8581 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8582 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8583 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8584 | Py_DECREF(str); |
| 8585 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8586 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8587 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8588 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8589 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8590 | { |
| 8591 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8592 | called as a callback from fixup() which does it already. */ |
| 8593 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8594 | const int kind = PyUnicode_KIND(self); |
| 8595 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8596 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8597 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8598 | Py_ssize_t i; |
| 8599 | |
| 8600 | for (i = 0; i < len; ++i) { |
| 8601 | ch = PyUnicode_READ(kind, data, i); |
| 8602 | fixed = 0; |
| 8603 | if (ch > 127) { |
| 8604 | if (Py_UNICODE_ISSPACE(ch)) |
| 8605 | fixed = ' '; |
| 8606 | else { |
| 8607 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8608 | if (decimal >= 0) |
| 8609 | fixed = '0' + decimal; |
| 8610 | } |
| 8611 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8612 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8613 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8614 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8615 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8616 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8617 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8618 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | } |
| 8620 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8621 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8622 | } |
| 8623 | |
| 8624 | PyObject * |
| 8625 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8626 | { |
| 8627 | if (!PyUnicode_Check(unicode)) { |
| 8628 | PyErr_BadInternalCall(); |
| 8629 | return NULL; |
| 8630 | } |
| 8631 | if (PyUnicode_READY(unicode) == -1) |
| 8632 | return NULL; |
| 8633 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8634 | /* If the string is already ASCII, just return the same string */ |
| 8635 | Py_INCREF(unicode); |
| 8636 | return unicode; |
| 8637 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8638 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8639 | } |
| 8640 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8641 | PyObject * |
| 8642 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8643 | Py_ssize_t length) |
| 8644 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8645 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8646 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8647 | Py_UCS4 maxchar; |
| 8648 | enum PyUnicode_Kind kind; |
| 8649 | void *data; |
| 8650 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8651 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8652 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8653 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8654 | if (ch > 127) { |
| 8655 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8656 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8657 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8658 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8659 | } |
| 8660 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8661 | |
| 8662 | /* Copy to a new string */ |
| 8663 | decimal = PyUnicode_New(length, maxchar); |
| 8664 | if (decimal == NULL) |
| 8665 | return decimal; |
| 8666 | kind = PyUnicode_KIND(decimal); |
| 8667 | data = PyUnicode_DATA(decimal); |
| 8668 | /* Iterate over code points */ |
| 8669 | for (i = 0; i < length; i++) { |
| 8670 | Py_UNICODE ch = s[i]; |
| 8671 | if (ch > 127) { |
| 8672 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8673 | if (decimal >= 0) |
| 8674 | ch = '0' + decimal; |
| 8675 | } |
| 8676 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8678 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8679 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8680 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8681 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8682 | int |
| 8683 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8684 | Py_ssize_t length, |
| 8685 | char *output, |
| 8686 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8687 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8688 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8689 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8690 | enum PyUnicode_Kind kind; |
| 8691 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8692 | |
| 8693 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8694 | PyErr_BadArgument(); |
| 8695 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8696 | } |
| 8697 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8698 | unicode = PyUnicode_FromUnicode(s, length); |
| 8699 | if (unicode == NULL) |
| 8700 | return -1; |
| 8701 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8702 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8703 | Py_DECREF(unicode); |
| 8704 | return -1; |
| 8705 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8706 | kind = PyUnicode_KIND(unicode); |
| 8707 | data = PyUnicode_DATA(unicode); |
| 8708 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8709 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8710 | PyObject *exc; |
| 8711 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8712 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8713 | Py_ssize_t startpos; |
| 8714 | |
| 8715 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8716 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8717 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8718 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8719 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8720 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8721 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8722 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8723 | if (decimal >= 0) { |
| 8724 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8725 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8726 | continue; |
| 8727 | } |
| 8728 | if (0 < ch && ch < 256) { |
| 8729 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8730 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8731 | continue; |
| 8732 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8733 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8734 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8735 | exc = NULL; |
| 8736 | raise_encode_exception(&exc, "decimal", unicode, |
| 8737 | startpos, startpos+1, |
| 8738 | "invalid decimal Unicode string"); |
| 8739 | Py_XDECREF(exc); |
| 8740 | Py_DECREF(unicode); |
| 8741 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8742 | } |
| 8743 | /* 0-terminate the output string */ |
| 8744 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8745 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8746 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8747 | } |
| 8748 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8749 | /* --- Helpers ------------------------------------------------------------ */ |
| 8750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8751 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8752 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8753 | Py_ssize_t start, |
| 8754 | Py_ssize_t end) |
| 8755 | { |
| 8756 | int kind1, kind2, kind; |
| 8757 | void *buf1, *buf2; |
| 8758 | Py_ssize_t len1, len2, result; |
| 8759 | |
| 8760 | kind1 = PyUnicode_KIND(s1); |
| 8761 | kind2 = PyUnicode_KIND(s2); |
| 8762 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8763 | buf1 = PyUnicode_DATA(s1); |
| 8764 | buf2 = PyUnicode_DATA(s2); |
| 8765 | if (kind1 != kind) |
| 8766 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8767 | if (!buf1) |
| 8768 | return -2; |
| 8769 | if (kind2 != kind) |
| 8770 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8771 | if (!buf2) { |
| 8772 | if (kind1 != kind) PyMem_Free(buf1); |
| 8773 | return -2; |
| 8774 | } |
| 8775 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8776 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8777 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8778 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8779 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8780 | case PyUnicode_1BYTE_KIND: |
| 8781 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8782 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8783 | else |
| 8784 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8785 | break; |
| 8786 | case PyUnicode_2BYTE_KIND: |
| 8787 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8788 | break; |
| 8789 | case PyUnicode_4BYTE_KIND: |
| 8790 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8791 | break; |
| 8792 | default: |
| 8793 | assert(0); result = -2; |
| 8794 | } |
| 8795 | } |
| 8796 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8797 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8798 | case PyUnicode_1BYTE_KIND: |
| 8799 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8800 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8801 | else |
| 8802 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8803 | break; |
| 8804 | case PyUnicode_2BYTE_KIND: |
| 8805 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8806 | break; |
| 8807 | case PyUnicode_4BYTE_KIND: |
| 8808 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8809 | break; |
| 8810 | default: |
| 8811 | assert(0); result = -2; |
| 8812 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8813 | } |
| 8814 | |
| 8815 | if (kind1 != kind) |
| 8816 | PyMem_Free(buf1); |
| 8817 | if (kind2 != kind) |
| 8818 | PyMem_Free(buf2); |
| 8819 | |
| 8820 | return result; |
| 8821 | } |
| 8822 | |
| 8823 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8824 | _PyUnicode_InsertThousandsGrouping( |
| 8825 | PyObject *unicode, Py_ssize_t index, |
| 8826 | Py_ssize_t n_buffer, |
| 8827 | void *digits, Py_ssize_t n_digits, |
| 8828 | Py_ssize_t min_width, |
| 8829 | const char *grouping, PyObject *thousands_sep, |
| 8830 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8831 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8832 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8833 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8834 | Py_ssize_t thousands_sep_len; |
| 8835 | Py_ssize_t len; |
| 8836 | |
| 8837 | if (unicode != NULL) { |
| 8838 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8839 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8840 | } |
| 8841 | else { |
| 8842 | kind = PyUnicode_1BYTE_KIND; |
| 8843 | data = NULL; |
| 8844 | } |
| 8845 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8846 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8847 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8848 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8849 | if (thousands_sep_kind < kind) { |
| 8850 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8851 | if (!thousands_sep_data) |
| 8852 | return -1; |
| 8853 | } |
| 8854 | else { |
| 8855 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8856 | if (!data) |
| 8857 | return -1; |
| 8858 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8859 | } |
| 8860 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8861 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8862 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8863 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8864 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8865 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8866 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8867 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8868 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8869 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8870 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8871 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8872 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8873 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8874 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8875 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8876 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8877 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8878 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8879 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8880 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8881 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8882 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8883 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8884 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8885 | break; |
| 8886 | default: |
| 8887 | assert(0); |
| 8888 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8889 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8890 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8891 | if (thousands_sep_kind < kind) |
| 8892 | PyMem_Free(thousands_sep_data); |
| 8893 | else |
| 8894 | PyMem_Free(data); |
| 8895 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8896 | if (unicode == NULL) { |
| 8897 | *maxchar = 127; |
| 8898 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8899 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8900 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8901 | } |
| 8902 | } |
| 8903 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8904 | } |
| 8905 | |
| 8906 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8907 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8908 | #define ADJUST_INDICES(start, end, len) \ |
| 8909 | if (end > len) \ |
| 8910 | end = len; \ |
| 8911 | else if (end < 0) { \ |
| 8912 | end += len; \ |
| 8913 | if (end < 0) \ |
| 8914 | end = 0; \ |
| 8915 | } \ |
| 8916 | if (start < 0) { \ |
| 8917 | start += len; \ |
| 8918 | if (start < 0) \ |
| 8919 | start = 0; \ |
| 8920 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8921 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8922 | Py_ssize_t |
| 8923 | PyUnicode_Count(PyObject *str, |
| 8924 | PyObject *substr, |
| 8925 | Py_ssize_t start, |
| 8926 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8927 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8928 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8929 | PyObject* str_obj; |
| 8930 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8931 | int kind1, kind2, kind; |
| 8932 | void *buf1 = NULL, *buf2 = NULL; |
| 8933 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8934 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8935 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8936 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8937 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8938 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8939 | if (!sub_obj) { |
| 8940 | Py_DECREF(str_obj); |
| 8941 | return -1; |
| 8942 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8943 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8944 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8945 | Py_DECREF(str_obj); |
| 8946 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8947 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8948 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8949 | kind1 = PyUnicode_KIND(str_obj); |
| 8950 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8951 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8952 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8953 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8954 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8955 | if (kind2 > kind) { |
| 8956 | Py_DECREF(sub_obj); |
| 8957 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8958 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8959 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8960 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8961 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8962 | if (!buf2) |
| 8963 | goto onError; |
| 8964 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8965 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8966 | |
| 8967 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8968 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8969 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8970 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8971 | result = asciilib_count( |
| 8972 | ((Py_UCS1*)buf1) + start, end - start, |
| 8973 | buf2, len2, PY_SSIZE_T_MAX |
| 8974 | ); |
| 8975 | else |
| 8976 | result = ucs1lib_count( |
| 8977 | ((Py_UCS1*)buf1) + start, end - start, |
| 8978 | buf2, len2, PY_SSIZE_T_MAX |
| 8979 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8980 | break; |
| 8981 | case PyUnicode_2BYTE_KIND: |
| 8982 | result = ucs2lib_count( |
| 8983 | ((Py_UCS2*)buf1) + start, end - start, |
| 8984 | buf2, len2, PY_SSIZE_T_MAX |
| 8985 | ); |
| 8986 | break; |
| 8987 | case PyUnicode_4BYTE_KIND: |
| 8988 | result = ucs4lib_count( |
| 8989 | ((Py_UCS4*)buf1) + start, end - start, |
| 8990 | buf2, len2, PY_SSIZE_T_MAX |
| 8991 | ); |
| 8992 | break; |
| 8993 | default: |
| 8994 | assert(0); result = 0; |
| 8995 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8996 | |
| 8997 | Py_DECREF(sub_obj); |
| 8998 | Py_DECREF(str_obj); |
| 8999 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9000 | if (kind2 != kind) |
| 9001 | PyMem_Free(buf2); |
| 9002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9003 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9004 | onError: |
| 9005 | Py_DECREF(sub_obj); |
| 9006 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9007 | if (kind2 != kind && buf2) |
| 9008 | PyMem_Free(buf2); |
| 9009 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9010 | } |
| 9011 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9012 | Py_ssize_t |
| 9013 | PyUnicode_Find(PyObject *str, |
| 9014 | PyObject *sub, |
| 9015 | Py_ssize_t start, |
| 9016 | Py_ssize_t end, |
| 9017 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9018 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9019 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9020 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9021 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9022 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9023 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9024 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9025 | if (!sub) { |
| 9026 | Py_DECREF(str); |
| 9027 | return -2; |
| 9028 | } |
| 9029 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9030 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9031 | Py_DECREF(str); |
| 9032 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9033 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9034 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9035 | result = any_find_slice(direction, |
| 9036 | str, sub, start, end |
| 9037 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9039 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9040 | Py_DECREF(sub); |
| 9041 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9042 | return result; |
| 9043 | } |
| 9044 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9045 | Py_ssize_t |
| 9046 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9047 | Py_ssize_t start, Py_ssize_t end, |
| 9048 | int direction) |
| 9049 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9050 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9051 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9052 | if (PyUnicode_READY(str) == -1) |
| 9053 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9054 | if (start < 0 || end < 0) { |
| 9055 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9056 | return -2; |
| 9057 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9058 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9059 | end = PyUnicode_GET_LENGTH(str); |
| 9060 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9061 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9062 | kind, end-start, ch, direction); |
| 9063 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9064 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9065 | else |
| 9066 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9067 | } |
| 9068 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9069 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9070 | tailmatch(PyObject *self, |
| 9071 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9072 | Py_ssize_t start, |
| 9073 | Py_ssize_t end, |
| 9074 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9075 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9076 | int kind_self; |
| 9077 | int kind_sub; |
| 9078 | void *data_self; |
| 9079 | void *data_sub; |
| 9080 | Py_ssize_t offset; |
| 9081 | Py_ssize_t i; |
| 9082 | Py_ssize_t end_sub; |
| 9083 | |
| 9084 | if (PyUnicode_READY(self) == -1 || |
| 9085 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9086 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9087 | |
| 9088 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9089 | return 1; |
| 9090 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9091 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9092 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9093 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9094 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9095 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9096 | kind_self = PyUnicode_KIND(self); |
| 9097 | data_self = PyUnicode_DATA(self); |
| 9098 | kind_sub = PyUnicode_KIND(substring); |
| 9099 | data_sub = PyUnicode_DATA(substring); |
| 9100 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9101 | |
| 9102 | if (direction > 0) |
| 9103 | offset = end; |
| 9104 | else |
| 9105 | offset = start; |
| 9106 | |
| 9107 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9108 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9109 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9110 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9111 | /* If both are of the same kind, memcmp is sufficient */ |
| 9112 | if (kind_self == kind_sub) { |
| 9113 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9114 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9115 | data_sub, |
| 9116 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9117 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9118 | } |
| 9119 | /* otherwise we have to compare each character by first accesing it */ |
| 9120 | else { |
| 9121 | /* We do not need to compare 0 and len(substring)-1 because |
| 9122 | the if statement above ensured already that they are equal |
| 9123 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9124 | for (i = 1; i < end_sub; ++i) { |
| 9125 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9126 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9127 | return 0; |
| 9128 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9129 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9130 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9131 | } |
| 9132 | |
| 9133 | return 0; |
| 9134 | } |
| 9135 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9136 | Py_ssize_t |
| 9137 | PyUnicode_Tailmatch(PyObject *str, |
| 9138 | PyObject *substr, |
| 9139 | Py_ssize_t start, |
| 9140 | Py_ssize_t end, |
| 9141 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9142 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9143 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9144 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9145 | str = PyUnicode_FromObject(str); |
| 9146 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9147 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9148 | substr = PyUnicode_FromObject(substr); |
| 9149 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9150 | Py_DECREF(str); |
| 9151 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9152 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9153 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9154 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9155 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9156 | Py_DECREF(str); |
| 9157 | Py_DECREF(substr); |
| 9158 | return result; |
| 9159 | } |
| 9160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9161 | /* Apply fixfct filter to the Unicode object self and return a |
| 9162 | reference to the modified object */ |
| 9163 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9164 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9165 | fixup(PyObject *self, |
| 9166 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9167 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9168 | PyObject *u; |
| 9169 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9170 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9171 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9172 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9173 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9174 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9175 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9176 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9177 | /* fix functions return the new maximum character in a string, |
| 9178 | if the kind of the resulting unicode object does not change, |
| 9179 | everything is fine. Otherwise we need to change the string kind |
| 9180 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9181 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9182 | |
| 9183 | if (maxchar_new == 0) { |
| 9184 | /* no changes */; |
| 9185 | if (PyUnicode_CheckExact(self)) { |
| 9186 | Py_DECREF(u); |
| 9187 | Py_INCREF(self); |
| 9188 | return self; |
| 9189 | } |
| 9190 | else |
| 9191 | return u; |
| 9192 | } |
| 9193 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9194 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9195 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9196 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9197 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9198 | |
| 9199 | /* In case the maximum character changed, we need to |
| 9200 | convert the string to the new category. */ |
| 9201 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9202 | if (v == NULL) { |
| 9203 | Py_DECREF(u); |
| 9204 | return NULL; |
| 9205 | } |
| 9206 | if (maxchar_new > maxchar_old) { |
| 9207 | /* If the maxchar increased so that the kind changed, not all |
| 9208 | characters are representable anymore and we need to fix the |
| 9209 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9210 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9211 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9212 | maxchar_old = fixfct(v); |
| 9213 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9214 | } |
| 9215 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9216 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9217 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9218 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9219 | Py_DECREF(u); |
| 9220 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9221 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9222 | } |
| 9223 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9224 | static PyObject * |
| 9225 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9226 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9227 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9228 | char *resdata, *data = PyUnicode_DATA(self); |
| 9229 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9230 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9231 | res = PyUnicode_New(len, 127); |
| 9232 | if (res == NULL) |
| 9233 | return NULL; |
| 9234 | resdata = PyUnicode_DATA(res); |
| 9235 | if (lower) |
| 9236 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9237 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9238 | _Py_bytes_upper(resdata, data, len); |
| 9239 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9240 | } |
| 9241 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9242 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9243 | 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] | 9244 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9245 | Py_ssize_t j; |
| 9246 | int final_sigma; |
| 9247 | Py_UCS4 c; |
| 9248 | /* 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] | 9249 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9250 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9251 | |
| 9252 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9253 | */ |
| 9254 | for (j = i - 1; j >= 0; j--) { |
| 9255 | c = PyUnicode_READ(kind, data, j); |
| 9256 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9257 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9258 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9259 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9260 | if (final_sigma) { |
| 9261 | for (j = i + 1; j < length; j++) { |
| 9262 | c = PyUnicode_READ(kind, data, j); |
| 9263 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9264 | break; |
| 9265 | } |
| 9266 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9267 | } |
| 9268 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9269 | } |
| 9270 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9271 | static int |
| 9272 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9273 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9274 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9275 | /* Obscure special case. */ |
| 9276 | if (c == 0x3A3) { |
| 9277 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9278 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9279 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9280 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9281 | } |
| 9282 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9283 | static Py_ssize_t |
| 9284 | 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] | 9285 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9286 | Py_ssize_t i, k = 0; |
| 9287 | int n_res, j; |
| 9288 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9289 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9290 | c = PyUnicode_READ(kind, data, 0); |
| 9291 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9292 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9293 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9294 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9295 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9296 | for (i = 1; i < length; i++) { |
| 9297 | c = PyUnicode_READ(kind, data, i); |
| 9298 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9299 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9300 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9301 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9302 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9303 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9304 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9305 | } |
| 9306 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9307 | static Py_ssize_t |
| 9308 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9309 | Py_ssize_t i, k = 0; |
| 9310 | |
| 9311 | for (i = 0; i < length; i++) { |
| 9312 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9313 | int n_res, j; |
| 9314 | if (Py_UNICODE_ISUPPER(c)) { |
| 9315 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9316 | } |
| 9317 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9318 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9319 | } |
| 9320 | else { |
| 9321 | n_res = 1; |
| 9322 | mapped[0] = c; |
| 9323 | } |
| 9324 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9325 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9326 | res[k++] = mapped[j]; |
| 9327 | } |
| 9328 | } |
| 9329 | return k; |
| 9330 | } |
| 9331 | |
| 9332 | static Py_ssize_t |
| 9333 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9334 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9335 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9336 | Py_ssize_t i, k = 0; |
| 9337 | |
| 9338 | for (i = 0; i < length; i++) { |
| 9339 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9340 | int n_res, j; |
| 9341 | if (lower) |
| 9342 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9343 | else |
| 9344 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9345 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9346 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9347 | res[k++] = mapped[j]; |
| 9348 | } |
| 9349 | } |
| 9350 | return k; |
| 9351 | } |
| 9352 | |
| 9353 | static Py_ssize_t |
| 9354 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9355 | { |
| 9356 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9357 | } |
| 9358 | |
| 9359 | static Py_ssize_t |
| 9360 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9361 | { |
| 9362 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9363 | } |
| 9364 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9365 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9366 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9367 | { |
| 9368 | Py_ssize_t i, k = 0; |
| 9369 | |
| 9370 | for (i = 0; i < length; i++) { |
| 9371 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9372 | Py_UCS4 mapped[3]; |
| 9373 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9374 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9375 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9376 | res[k++] = mapped[j]; |
| 9377 | } |
| 9378 | } |
| 9379 | return k; |
| 9380 | } |
| 9381 | |
| 9382 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9383 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9384 | { |
| 9385 | Py_ssize_t i, k = 0; |
| 9386 | int previous_is_cased; |
| 9387 | |
| 9388 | previous_is_cased = 0; |
| 9389 | for (i = 0; i < length; i++) { |
| 9390 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9391 | Py_UCS4 mapped[3]; |
| 9392 | int n_res, j; |
| 9393 | |
| 9394 | if (previous_is_cased) |
| 9395 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9396 | else |
| 9397 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9398 | |
| 9399 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9400 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9401 | res[k++] = mapped[j]; |
| 9402 | } |
| 9403 | |
| 9404 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9405 | } |
| 9406 | return k; |
| 9407 | } |
| 9408 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9409 | static PyObject * |
| 9410 | case_operation(PyObject *self, |
| 9411 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9412 | { |
| 9413 | PyObject *res = NULL; |
| 9414 | Py_ssize_t length, newlength = 0; |
| 9415 | int kind, outkind; |
| 9416 | void *data, *outdata; |
| 9417 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9418 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9419 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9420 | |
| 9421 | kind = PyUnicode_KIND(self); |
| 9422 | data = PyUnicode_DATA(self); |
| 9423 | length = PyUnicode_GET_LENGTH(self); |
| 9424 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9425 | if (tmp == NULL) |
| 9426 | return PyErr_NoMemory(); |
| 9427 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9428 | res = PyUnicode_New(newlength, maxchar); |
| 9429 | if (res == NULL) |
| 9430 | goto leave; |
| 9431 | tmpend = tmp + newlength; |
| 9432 | outdata = PyUnicode_DATA(res); |
| 9433 | outkind = PyUnicode_KIND(res); |
| 9434 | switch (outkind) { |
| 9435 | case PyUnicode_1BYTE_KIND: |
| 9436 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9437 | break; |
| 9438 | case PyUnicode_2BYTE_KIND: |
| 9439 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9440 | break; |
| 9441 | case PyUnicode_4BYTE_KIND: |
| 9442 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9443 | break; |
| 9444 | default: |
| 9445 | assert(0); |
| 9446 | break; |
| 9447 | } |
| 9448 | leave: |
| 9449 | PyMem_FREE(tmp); |
| 9450 | return res; |
| 9451 | } |
| 9452 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9453 | PyObject * |
| 9454 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9455 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9456 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9457 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9458 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9459 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9460 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9461 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9462 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9463 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9464 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9465 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9466 | int use_memcpy; |
| 9467 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9468 | PyObject *last_obj; |
| 9469 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9470 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9471 | fseq = PySequence_Fast(seq, ""); |
| 9472 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9473 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9474 | } |
| 9475 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9476 | /* NOTE: the following code can't call back into Python code, |
| 9477 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9478 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9479 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9480 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9481 | /* If empty sequence, return u"". */ |
| 9482 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9483 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9484 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9485 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9486 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9487 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9488 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9489 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9490 | if (seqlen == 1) { |
| 9491 | if (PyUnicode_CheckExact(items[0])) { |
| 9492 | res = items[0]; |
| 9493 | Py_INCREF(res); |
| 9494 | Py_DECREF(fseq); |
| 9495 | return res; |
| 9496 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9497 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9498 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9499 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9500 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9501 | /* Set up sep and seplen */ |
| 9502 | if (separator == NULL) { |
| 9503 | /* fall back to a blank space separator */ |
| 9504 | sep = PyUnicode_FromOrdinal(' '); |
| 9505 | if (!sep) |
| 9506 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9507 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9508 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9509 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9510 | else { |
| 9511 | if (!PyUnicode_Check(separator)) { |
| 9512 | PyErr_Format(PyExc_TypeError, |
| 9513 | "separator: expected str instance," |
| 9514 | " %.80s found", |
| 9515 | Py_TYPE(separator)->tp_name); |
| 9516 | goto onError; |
| 9517 | } |
| 9518 | if (PyUnicode_READY(separator)) |
| 9519 | goto onError; |
| 9520 | sep = separator; |
| 9521 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9522 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9523 | /* inc refcount to keep this code path symmetric with the |
| 9524 | above case of a blank separator */ |
| 9525 | Py_INCREF(sep); |
| 9526 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9527 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9528 | } |
| 9529 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9530 | /* There are at least two things to join, or else we have a subclass |
| 9531 | * of str in the sequence. |
| 9532 | * Do a pre-pass to figure out the total amount of space we'll |
| 9533 | * need (sz), and see whether all argument are strings. |
| 9534 | */ |
| 9535 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9536 | #ifdef Py_DEBUG |
| 9537 | use_memcpy = 0; |
| 9538 | #else |
| 9539 | use_memcpy = 1; |
| 9540 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9541 | for (i = 0; i < seqlen; i++) { |
| 9542 | const Py_ssize_t old_sz = sz; |
| 9543 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9544 | if (!PyUnicode_Check(item)) { |
| 9545 | PyErr_Format(PyExc_TypeError, |
| 9546 | "sequence item %zd: expected str instance," |
| 9547 | " %.80s found", |
| 9548 | i, Py_TYPE(item)->tp_name); |
| 9549 | goto onError; |
| 9550 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9551 | if (PyUnicode_READY(item) == -1) |
| 9552 | goto onError; |
| 9553 | sz += PyUnicode_GET_LENGTH(item); |
| 9554 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9555 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9556 | if (i != 0) |
| 9557 | sz += seplen; |
| 9558 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9559 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9560 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9561 | goto onError; |
| 9562 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9563 | if (use_memcpy && last_obj != NULL) { |
| 9564 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9565 | use_memcpy = 0; |
| 9566 | } |
| 9567 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9568 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9569 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9570 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9571 | if (res == NULL) |
| 9572 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9573 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9574 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9575 | #ifdef Py_DEBUG |
| 9576 | use_memcpy = 0; |
| 9577 | #else |
| 9578 | if (use_memcpy) { |
| 9579 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9580 | kind = PyUnicode_KIND(res); |
| 9581 | if (seplen != 0) |
| 9582 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9583 | } |
| 9584 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9585 | if (use_memcpy) { |
| 9586 | for (i = 0; i < seqlen; ++i) { |
| 9587 | Py_ssize_t itemlen; |
| 9588 | item = items[i]; |
| 9589 | |
| 9590 | /* Copy item, and maybe the separator. */ |
| 9591 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9592 | Py_MEMCPY(res_data, |
| 9593 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9594 | kind * seplen); |
| 9595 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9596 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9597 | |
| 9598 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9599 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9600 | Py_MEMCPY(res_data, |
| 9601 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9602 | kind * itemlen); |
| 9603 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9604 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9605 | } |
| 9606 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9607 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9608 | } |
| 9609 | else { |
| 9610 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9611 | Py_ssize_t itemlen; |
| 9612 | item = items[i]; |
| 9613 | |
| 9614 | /* Copy item, and maybe the separator. */ |
| 9615 | if (i && seplen != 0) { |
| 9616 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9617 | res_offset += seplen; |
| 9618 | } |
| 9619 | |
| 9620 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9621 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9622 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9623 | res_offset += itemlen; |
| 9624 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9625 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9626 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9627 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9628 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9629 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9630 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9631 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9632 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9633 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9634 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9635 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9636 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9637 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9638 | return NULL; |
| 9639 | } |
| 9640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9641 | #define FILL(kind, data, value, start, length) \ |
| 9642 | do { \ |
| 9643 | Py_ssize_t i_ = 0; \ |
| 9644 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9645 | switch ((kind)) { \ |
| 9646 | case PyUnicode_1BYTE_KIND: { \ |
| 9647 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9648 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9649 | break; \ |
| 9650 | } \ |
| 9651 | case PyUnicode_2BYTE_KIND: { \ |
| 9652 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9653 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9654 | break; \ |
| 9655 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9656 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9657 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9658 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9659 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9660 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9661 | } \ |
| 9662 | } \ |
| 9663 | } while (0) |
| 9664 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9665 | void |
| 9666 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9667 | Py_UCS4 fill_char) |
| 9668 | { |
| 9669 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9670 | const void *data = PyUnicode_DATA(unicode); |
| 9671 | assert(PyUnicode_IS_READY(unicode)); |
| 9672 | assert(unicode_modifiable(unicode)); |
| 9673 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9674 | assert(start >= 0); |
| 9675 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9676 | FILL(kind, data, fill_char, start, length); |
| 9677 | } |
| 9678 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9679 | Py_ssize_t |
| 9680 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9681 | Py_UCS4 fill_char) |
| 9682 | { |
| 9683 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9684 | |
| 9685 | if (!PyUnicode_Check(unicode)) { |
| 9686 | PyErr_BadInternalCall(); |
| 9687 | return -1; |
| 9688 | } |
| 9689 | if (PyUnicode_READY(unicode) == -1) |
| 9690 | return -1; |
| 9691 | if (unicode_check_modifiable(unicode)) |
| 9692 | return -1; |
| 9693 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9694 | if (start < 0) { |
| 9695 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9696 | return -1; |
| 9697 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9698 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9699 | PyErr_SetString(PyExc_ValueError, |
| 9700 | "fill character is bigger than " |
| 9701 | "the string maximum character"); |
| 9702 | return -1; |
| 9703 | } |
| 9704 | |
| 9705 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9706 | length = Py_MIN(maxlen, length); |
| 9707 | if (length <= 0) |
| 9708 | return 0; |
| 9709 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9710 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9711 | return length; |
| 9712 | } |
| 9713 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9714 | static PyObject * |
| 9715 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9716 | Py_ssize_t left, |
| 9717 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9718 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9719 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9720 | PyObject *u; |
| 9721 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9722 | int kind; |
| 9723 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9724 | |
| 9725 | if (left < 0) |
| 9726 | left = 0; |
| 9727 | if (right < 0) |
| 9728 | right = 0; |
| 9729 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9730 | if (left == 0 && right == 0) |
| 9731 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9733 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9734 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9735 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9736 | return NULL; |
| 9737 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9738 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9739 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9740 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9741 | if (!u) |
| 9742 | return NULL; |
| 9743 | |
| 9744 | kind = PyUnicode_KIND(u); |
| 9745 | data = PyUnicode_DATA(u); |
| 9746 | if (left) |
| 9747 | FILL(kind, data, fill, 0, left); |
| 9748 | if (right) |
| 9749 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9750 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9751 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9752 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9753 | } |
| 9754 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9755 | PyObject * |
| 9756 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9757 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9758 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9759 | |
| 9760 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9761 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9762 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9763 | if (PyUnicode_READY(string) == -1) { |
| 9764 | Py_DECREF(string); |
| 9765 | return NULL; |
| 9766 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9767 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9768 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9769 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9770 | if (PyUnicode_IS_ASCII(string)) |
| 9771 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9772 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9773 | PyUnicode_GET_LENGTH(string), keepends); |
| 9774 | else |
| 9775 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9776 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9777 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9778 | break; |
| 9779 | case PyUnicode_2BYTE_KIND: |
| 9780 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9781 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9782 | PyUnicode_GET_LENGTH(string), keepends); |
| 9783 | break; |
| 9784 | case PyUnicode_4BYTE_KIND: |
| 9785 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9786 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9787 | PyUnicode_GET_LENGTH(string), keepends); |
| 9788 | break; |
| 9789 | default: |
| 9790 | assert(0); |
| 9791 | list = 0; |
| 9792 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9793 | Py_DECREF(string); |
| 9794 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9795 | } |
| 9796 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9797 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9798 | split(PyObject *self, |
| 9799 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9800 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9801 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9802 | int kind1, kind2, kind; |
| 9803 | void *buf1, *buf2; |
| 9804 | Py_ssize_t len1, len2; |
| 9805 | PyObject* out; |
| 9806 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9807 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9808 | maxcount = PY_SSIZE_T_MAX; |
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 (PyUnicode_READY(self) == -1) |
| 9811 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9812 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9813 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9814 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9815 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9816 | if (PyUnicode_IS_ASCII(self)) |
| 9817 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9818 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9819 | PyUnicode_GET_LENGTH(self), maxcount |
| 9820 | ); |
| 9821 | else |
| 9822 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9823 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9824 | PyUnicode_GET_LENGTH(self), maxcount |
| 9825 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9826 | case PyUnicode_2BYTE_KIND: |
| 9827 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9828 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9829 | PyUnicode_GET_LENGTH(self), maxcount |
| 9830 | ); |
| 9831 | case PyUnicode_4BYTE_KIND: |
| 9832 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9833 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9834 | PyUnicode_GET_LENGTH(self), maxcount |
| 9835 | ); |
| 9836 | default: |
| 9837 | assert(0); |
| 9838 | return NULL; |
| 9839 | } |
| 9840 | |
| 9841 | if (PyUnicode_READY(substring) == -1) |
| 9842 | return NULL; |
| 9843 | |
| 9844 | kind1 = PyUnicode_KIND(self); |
| 9845 | kind2 = PyUnicode_KIND(substring); |
| 9846 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9847 | buf1 = PyUnicode_DATA(self); |
| 9848 | buf2 = PyUnicode_DATA(substring); |
| 9849 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9850 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9851 | if (!buf1) |
| 9852 | return NULL; |
| 9853 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9854 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9855 | if (!buf2) { |
| 9856 | if (kind1 != kind) PyMem_Free(buf1); |
| 9857 | return NULL; |
| 9858 | } |
| 9859 | len1 = PyUnicode_GET_LENGTH(self); |
| 9860 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9861 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9862 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9863 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9864 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9865 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9866 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9867 | else |
| 9868 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9869 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9870 | break; |
| 9871 | case PyUnicode_2BYTE_KIND: |
| 9872 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9873 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9874 | break; |
| 9875 | case PyUnicode_4BYTE_KIND: |
| 9876 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9877 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9878 | break; |
| 9879 | default: |
| 9880 | out = NULL; |
| 9881 | } |
| 9882 | if (kind1 != kind) |
| 9883 | PyMem_Free(buf1); |
| 9884 | if (kind2 != kind) |
| 9885 | PyMem_Free(buf2); |
| 9886 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9887 | } |
| 9888 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9889 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9890 | rsplit(PyObject *self, |
| 9891 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9892 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9893 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9894 | int kind1, kind2, kind; |
| 9895 | void *buf1, *buf2; |
| 9896 | Py_ssize_t len1, len2; |
| 9897 | PyObject* out; |
| 9898 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9899 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9900 | maxcount = PY_SSIZE_T_MAX; |
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 (PyUnicode_READY(self) == -1) |
| 9903 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9904 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9905 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9906 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9907 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9908 | if (PyUnicode_IS_ASCII(self)) |
| 9909 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9910 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9911 | PyUnicode_GET_LENGTH(self), maxcount |
| 9912 | ); |
| 9913 | else |
| 9914 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9915 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9916 | PyUnicode_GET_LENGTH(self), maxcount |
| 9917 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9918 | case PyUnicode_2BYTE_KIND: |
| 9919 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9920 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | PyUnicode_GET_LENGTH(self), maxcount |
| 9922 | ); |
| 9923 | case PyUnicode_4BYTE_KIND: |
| 9924 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9925 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9926 | PyUnicode_GET_LENGTH(self), maxcount |
| 9927 | ); |
| 9928 | default: |
| 9929 | assert(0); |
| 9930 | return NULL; |
| 9931 | } |
| 9932 | |
| 9933 | if (PyUnicode_READY(substring) == -1) |
| 9934 | return NULL; |
| 9935 | |
| 9936 | kind1 = PyUnicode_KIND(self); |
| 9937 | kind2 = PyUnicode_KIND(substring); |
| 9938 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9939 | buf1 = PyUnicode_DATA(self); |
| 9940 | buf2 = PyUnicode_DATA(substring); |
| 9941 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9942 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9943 | if (!buf1) |
| 9944 | return NULL; |
| 9945 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9946 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9947 | if (!buf2) { |
| 9948 | if (kind1 != kind) PyMem_Free(buf1); |
| 9949 | return NULL; |
| 9950 | } |
| 9951 | len1 = PyUnicode_GET_LENGTH(self); |
| 9952 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9953 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9954 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9955 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9956 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9957 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9958 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9959 | else |
| 9960 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9961 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9962 | break; |
| 9963 | case PyUnicode_2BYTE_KIND: |
| 9964 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9965 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9966 | break; |
| 9967 | case PyUnicode_4BYTE_KIND: |
| 9968 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9969 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9970 | break; |
| 9971 | default: |
| 9972 | out = NULL; |
| 9973 | } |
| 9974 | if (kind1 != kind) |
| 9975 | PyMem_Free(buf1); |
| 9976 | if (kind2 != kind) |
| 9977 | PyMem_Free(buf2); |
| 9978 | return out; |
| 9979 | } |
| 9980 | |
| 9981 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9982 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9983 | 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] | 9984 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9985 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9986 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9987 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9988 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9989 | else |
| 9990 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9991 | case PyUnicode_2BYTE_KIND: |
| 9992 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9993 | case PyUnicode_4BYTE_KIND: |
| 9994 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9995 | } |
| 9996 | assert(0); |
| 9997 | return -1; |
| 9998 | } |
| 9999 | |
| 10000 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10001 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10002 | 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] | 10003 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10004 | switch (kind) { |
| 10005 | case PyUnicode_1BYTE_KIND: |
| 10006 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10007 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10008 | else |
| 10009 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10010 | case PyUnicode_2BYTE_KIND: |
| 10011 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10012 | case PyUnicode_4BYTE_KIND: |
| 10013 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10014 | } |
| 10015 | assert(0); |
| 10016 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10017 | } |
| 10018 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10019 | static void |
| 10020 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10021 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10022 | { |
| 10023 | int kind = PyUnicode_KIND(u); |
| 10024 | void *data = PyUnicode_DATA(u); |
| 10025 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10026 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10027 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10028 | (Py_UCS1 *)data + len, |
| 10029 | u1, u2, maxcount); |
| 10030 | } |
| 10031 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10032 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10033 | (Py_UCS2 *)data + len, |
| 10034 | u1, u2, maxcount); |
| 10035 | } |
| 10036 | else { |
| 10037 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10038 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10039 | (Py_UCS4 *)data + len, |
| 10040 | u1, u2, maxcount); |
| 10041 | } |
| 10042 | } |
| 10043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10044 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10045 | replace(PyObject *self, PyObject *str1, |
| 10046 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10047 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10048 | PyObject *u; |
| 10049 | char *sbuf = PyUnicode_DATA(self); |
| 10050 | char *buf1 = PyUnicode_DATA(str1); |
| 10051 | char *buf2 = PyUnicode_DATA(str2); |
| 10052 | int srelease = 0, release1 = 0, release2 = 0; |
| 10053 | int skind = PyUnicode_KIND(self); |
| 10054 | int kind1 = PyUnicode_KIND(str1); |
| 10055 | int kind2 = PyUnicode_KIND(str2); |
| 10056 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10057 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10058 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10059 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10060 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10061 | |
| 10062 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10063 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10065 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10066 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10067 | if (str1 == str2) |
| 10068 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10070 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10071 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10072 | if (maxchar < maxchar_str1) |
| 10073 | /* substring too wide to be present */ |
| 10074 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10075 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10076 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10077 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10078 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10079 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10080 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10081 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10082 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10083 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10084 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10086 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10087 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10088 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10089 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10090 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10091 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10092 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10093 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10094 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10095 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10096 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10097 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10098 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10099 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10100 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10101 | } |
| 10102 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10103 | int rkind = skind; |
| 10104 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10105 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10106 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10107 | if (kind1 < rkind) { |
| 10108 | /* widen substring */ |
| 10109 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10110 | if (!buf1) goto error; |
| 10111 | release1 = 1; |
| 10112 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10113 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10114 | if (i < 0) |
| 10115 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10116 | if (rkind > kind2) { |
| 10117 | /* widen replacement */ |
| 10118 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10119 | if (!buf2) goto error; |
| 10120 | release2 = 1; |
| 10121 | } |
| 10122 | else if (rkind < kind2) { |
| 10123 | /* widen self and buf1 */ |
| 10124 | rkind = kind2; |
| 10125 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10126 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10127 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10128 | if (!sbuf) goto error; |
| 10129 | srelease = 1; |
| 10130 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10131 | if (!buf1) goto error; |
| 10132 | release1 = 1; |
| 10133 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10134 | u = PyUnicode_New(slen, maxchar); |
| 10135 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10136 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10137 | assert(PyUnicode_KIND(u) == rkind); |
| 10138 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10139 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10140 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10141 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10142 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10144 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10145 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10146 | |
| 10147 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10148 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10149 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10150 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10151 | if (i == -1) |
| 10152 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10153 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10154 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10155 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10156 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10157 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10158 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10159 | } |
| 10160 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10162 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10163 | int rkind = skind; |
| 10164 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10165 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10167 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10168 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10169 | if (!buf1) goto error; |
| 10170 | release1 = 1; |
| 10171 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10172 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10173 | if (n == 0) |
| 10174 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10175 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10176 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10177 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10178 | if (!buf2) goto error; |
| 10179 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10180 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10181 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10182 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10183 | rkind = kind2; |
| 10184 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10185 | if (!sbuf) goto error; |
| 10186 | srelease = 1; |
| 10187 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10188 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10189 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10190 | if (!buf1) goto error; |
| 10191 | release1 = 1; |
| 10192 | } |
| 10193 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10194 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10195 | 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] | 10196 | PyErr_SetString(PyExc_OverflowError, |
| 10197 | "replace string is too long"); |
| 10198 | goto error; |
| 10199 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10200 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10201 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10202 | _Py_INCREF_UNICODE_EMPTY(); |
| 10203 | if (!unicode_empty) |
| 10204 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10205 | u = unicode_empty; |
| 10206 | goto done; |
| 10207 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10208 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10209 | PyErr_SetString(PyExc_OverflowError, |
| 10210 | "replace string is too long"); |
| 10211 | goto error; |
| 10212 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10213 | u = PyUnicode_New(new_size, maxchar); |
| 10214 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10216 | assert(PyUnicode_KIND(u) == rkind); |
| 10217 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10218 | ires = i = 0; |
| 10219 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10220 | while (n-- > 0) { |
| 10221 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10222 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10223 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10224 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10225 | if (j == -1) |
| 10226 | break; |
| 10227 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10228 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10229 | memcpy(res + rkind * ires, |
| 10230 | sbuf + rkind * i, |
| 10231 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10232 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10233 | } |
| 10234 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10235 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10236 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10237 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10238 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10239 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10240 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10241 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10242 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10243 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10244 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10245 | memcpy(res + rkind * ires, |
| 10246 | sbuf + rkind * i, |
| 10247 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10248 | } |
| 10249 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10250 | /* interleave */ |
| 10251 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10252 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10253 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10254 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10255 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10256 | if (--n <= 0) |
| 10257 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10258 | memcpy(res + rkind * ires, |
| 10259 | sbuf + rkind * i, |
| 10260 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10261 | ires++; |
| 10262 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10263 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10264 | memcpy(res + rkind * ires, |
| 10265 | sbuf + rkind * i, |
| 10266 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10267 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10268 | } |
| 10269 | |
| 10270 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10271 | unicode_adjust_maxchar(&u); |
| 10272 | if (u == NULL) |
| 10273 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10274 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10275 | |
| 10276 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10277 | if (srelease) |
| 10278 | PyMem_FREE(sbuf); |
| 10279 | if (release1) |
| 10280 | PyMem_FREE(buf1); |
| 10281 | if (release2) |
| 10282 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10283 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10284 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10285 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10286 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10287 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 | if (srelease) |
| 10289 | PyMem_FREE(sbuf); |
| 10290 | if (release1) |
| 10291 | PyMem_FREE(buf1); |
| 10292 | if (release2) |
| 10293 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10294 | return unicode_result_unchanged(self); |
| 10295 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10296 | error: |
| 10297 | if (srelease && sbuf) |
| 10298 | PyMem_FREE(sbuf); |
| 10299 | if (release1 && buf1) |
| 10300 | PyMem_FREE(buf1); |
| 10301 | if (release2 && buf2) |
| 10302 | PyMem_FREE(buf2); |
| 10303 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10304 | } |
| 10305 | |
| 10306 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10307 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10308 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10309 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10310 | \n\ |
| 10311 | 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] | 10312 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10313 | |
| 10314 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10315 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10316 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10317 | if (PyUnicode_READY(self) == -1) |
| 10318 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10319 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10320 | } |
| 10321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10322 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10323 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10324 | \n\ |
| 10325 | 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] | 10326 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10327 | |
| 10328 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10329 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10330 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10331 | if (PyUnicode_READY(self) == -1) |
| 10332 | return NULL; |
| 10333 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10334 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10335 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10336 | } |
| 10337 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10338 | PyDoc_STRVAR(casefold__doc__, |
| 10339 | "S.casefold() -> str\n\ |
| 10340 | \n\ |
| 10341 | Return a version of S suitable for caseless comparisons."); |
| 10342 | |
| 10343 | static PyObject * |
| 10344 | unicode_casefold(PyObject *self) |
| 10345 | { |
| 10346 | if (PyUnicode_READY(self) == -1) |
| 10347 | return NULL; |
| 10348 | if (PyUnicode_IS_ASCII(self)) |
| 10349 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10350 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10351 | } |
| 10352 | |
| 10353 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10354 | /* Argument converter. Coerces to a single unicode character */ |
| 10355 | |
| 10356 | static int |
| 10357 | convert_uc(PyObject *obj, void *addr) |
| 10358 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10359 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10360 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10361 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10362 | uniobj = PyUnicode_FromObject(obj); |
| 10363 | if (uniobj == NULL) { |
| 10364 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10365 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10366 | return 0; |
| 10367 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10368 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10369 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10370 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10371 | Py_DECREF(uniobj); |
| 10372 | return 0; |
| 10373 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10374 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10375 | Py_DECREF(uniobj); |
| 10376 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10377 | } |
| 10378 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10379 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10380 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10381 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10382 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10383 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | |
| 10385 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10386 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10387 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10388 | Py_ssize_t marg, left; |
| 10389 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10390 | Py_UCS4 fillchar = ' '; |
| 10391 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10392 | 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] | 10393 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10394 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10395 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10396 | return NULL; |
| 10397 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10398 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10399 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10400 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10401 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10402 | left = marg / 2 + (marg & width & 1); |
| 10403 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10404 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10405 | } |
| 10406 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10407 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10408 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10409 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10410 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10411 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10412 | #define COMPARE(TYPE1, TYPE2) \ |
| 10413 | do { \ |
| 10414 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10415 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10416 | TYPE1* end = p1 + len; \ |
| 10417 | Py_UCS4 c1, c2; \ |
| 10418 | for (; p1 != end; p1++, p2++) { \ |
| 10419 | c1 = *p1; \ |
| 10420 | c2 = *p2; \ |
| 10421 | if (c1 != c2) \ |
| 10422 | return (c1 < c2) ? -1 : 1; \ |
| 10423 | } \ |
| 10424 | } \ |
| 10425 | while (0) |
| 10426 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10427 | int kind1, kind2; |
| 10428 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10429 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10430 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | kind1 = PyUnicode_KIND(str1); |
| 10432 | kind2 = PyUnicode_KIND(str2); |
| 10433 | data1 = PyUnicode_DATA(str1); |
| 10434 | data2 = PyUnicode_DATA(str2); |
| 10435 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10436 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10437 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10438 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10439 | switch(kind1) { |
| 10440 | case PyUnicode_1BYTE_KIND: |
| 10441 | { |
| 10442 | switch(kind2) { |
| 10443 | case PyUnicode_1BYTE_KIND: |
| 10444 | { |
| 10445 | int cmp = memcmp(data1, data2, len); |
| 10446 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10447 | if (cmp < 0) |
| 10448 | return -1; |
| 10449 | if (cmp > 0) |
| 10450 | return 1; |
| 10451 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10452 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10453 | case PyUnicode_2BYTE_KIND: |
| 10454 | COMPARE(Py_UCS1, Py_UCS2); |
| 10455 | break; |
| 10456 | case PyUnicode_4BYTE_KIND: |
| 10457 | COMPARE(Py_UCS1, Py_UCS4); |
| 10458 | break; |
| 10459 | default: |
| 10460 | assert(0); |
| 10461 | } |
| 10462 | break; |
| 10463 | } |
| 10464 | case PyUnicode_2BYTE_KIND: |
| 10465 | { |
| 10466 | switch(kind2) { |
| 10467 | case PyUnicode_1BYTE_KIND: |
| 10468 | COMPARE(Py_UCS2, Py_UCS1); |
| 10469 | break; |
| 10470 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10471 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10472 | COMPARE(Py_UCS2, Py_UCS2); |
| 10473 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10474 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10475 | case PyUnicode_4BYTE_KIND: |
| 10476 | COMPARE(Py_UCS2, Py_UCS4); |
| 10477 | break; |
| 10478 | default: |
| 10479 | assert(0); |
| 10480 | } |
| 10481 | break; |
| 10482 | } |
| 10483 | case PyUnicode_4BYTE_KIND: |
| 10484 | { |
| 10485 | switch(kind2) { |
| 10486 | case PyUnicode_1BYTE_KIND: |
| 10487 | COMPARE(Py_UCS4, Py_UCS1); |
| 10488 | break; |
| 10489 | case PyUnicode_2BYTE_KIND: |
| 10490 | COMPARE(Py_UCS4, Py_UCS2); |
| 10491 | break; |
| 10492 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10493 | { |
| 10494 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10495 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10496 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10497 | if (cmp < 0) |
| 10498 | return -1; |
| 10499 | if (cmp > 0) |
| 10500 | return 1; |
| 10501 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10502 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10503 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10504 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10505 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10506 | default: |
| 10507 | assert(0); |
| 10508 | } |
| 10509 | break; |
| 10510 | } |
| 10511 | default: |
| 10512 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10513 | } |
| 10514 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10515 | if (len1 == len2) |
| 10516 | return 0; |
| 10517 | if (len1 < len2) |
| 10518 | return -1; |
| 10519 | else |
| 10520 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10521 | |
| 10522 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10523 | } |
| 10524 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10525 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10526 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10527 | { |
| 10528 | int kind; |
| 10529 | void *data1, *data2; |
| 10530 | Py_ssize_t len; |
| 10531 | int cmp; |
| 10532 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10533 | len = PyUnicode_GET_LENGTH(str1); |
| 10534 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10535 | return 0; |
| 10536 | kind = PyUnicode_KIND(str1); |
| 10537 | if (PyUnicode_KIND(str2) != kind) |
| 10538 | return 0; |
| 10539 | data1 = PyUnicode_DATA(str1); |
| 10540 | data2 = PyUnicode_DATA(str2); |
| 10541 | |
| 10542 | cmp = memcmp(data1, data2, len * kind); |
| 10543 | return (cmp == 0); |
| 10544 | } |
| 10545 | |
| 10546 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10547 | int |
| 10548 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10549 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10550 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10551 | if (PyUnicode_READY(left) == -1 || |
| 10552 | PyUnicode_READY(right) == -1) |
| 10553 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame^] | 10554 | |
| 10555 | /* a string is equal to itself */ |
| 10556 | if (left == right) |
| 10557 | return 0; |
| 10558 | |
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; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10573 | Py_UCS4 chr; |
| 10574 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10575 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10576 | if (PyUnicode_READY(uni) == -1) |
| 10577 | return -1; |
| 10578 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10579 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10580 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10581 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10582 | size_t len, len2 = strlen(str); |
| 10583 | int cmp; |
| 10584 | |
| 10585 | len = Py_MIN(len1, len2); |
| 10586 | cmp = memcmp(data, str, len); |
| 10587 | if (cmp != 0) |
| 10588 | return cmp; |
| 10589 | if (len1 > len2) |
| 10590 | return 1; /* uni is longer */ |
| 10591 | if (len2 > len1) |
| 10592 | return -1; /* str is longer */ |
| 10593 | return 0; |
| 10594 | } |
| 10595 | else { |
| 10596 | void *data = PyUnicode_DATA(uni); |
| 10597 | /* Compare Unicode string and source character set string */ |
| 10598 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10599 | if (chr != str[i]) |
| 10600 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10601 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10602 | to C strings identical up to that point. */ |
| 10603 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 10604 | return 1; /* uni is longer */ |
| 10605 | if (str[i]) |
| 10606 | return -1; /* str is longer */ |
| 10607 | return 0; |
| 10608 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10609 | } |
| 10610 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10611 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10612 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10613 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10614 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10615 | PyObject * |
| 10616 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10617 | { |
| 10618 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10619 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10620 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10621 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10622 | Py_RETURN_NOTIMPLEMENTED; |
| 10623 | |
| 10624 | if (PyUnicode_READY(left) == -1 || |
| 10625 | PyUnicode_READY(right) == -1) |
| 10626 | return NULL; |
| 10627 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 10628 | if (left == right) { |
| 10629 | switch (op) { |
| 10630 | case Py_EQ: |
| 10631 | case Py_LE: |
| 10632 | case Py_GE: |
| 10633 | /* a string is equal to itself */ |
| 10634 | v = Py_True; |
| 10635 | break; |
| 10636 | case Py_NE: |
| 10637 | case Py_LT: |
| 10638 | case Py_GT: |
| 10639 | v = Py_False; |
| 10640 | break; |
| 10641 | default: |
| 10642 | PyErr_BadArgument(); |
| 10643 | return NULL; |
| 10644 | } |
| 10645 | } |
| 10646 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10647 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10648 | result ^= (op == Py_NE); |
| 10649 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10650 | } |
| 10651 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10652 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10653 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10654 | /* Convert the return value to a Boolean */ |
| 10655 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10656 | case Py_LE: |
| 10657 | v = TEST_COND(result <= 0); |
| 10658 | break; |
| 10659 | case Py_GE: |
| 10660 | v = TEST_COND(result >= 0); |
| 10661 | break; |
| 10662 | case Py_LT: |
| 10663 | v = TEST_COND(result == -1); |
| 10664 | break; |
| 10665 | case Py_GT: |
| 10666 | v = TEST_COND(result == 1); |
| 10667 | break; |
| 10668 | default: |
| 10669 | PyErr_BadArgument(); |
| 10670 | return NULL; |
| 10671 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10672 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10673 | Py_INCREF(v); |
| 10674 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10675 | } |
| 10676 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10677 | int |
| 10678 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10679 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10680 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10681 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10682 | void *buf1, *buf2; |
| 10683 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10684 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10685 | |
| 10686 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10687 | sub = PyUnicode_FromObject(element); |
| 10688 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10689 | PyErr_Format(PyExc_TypeError, |
| 10690 | "'in <string>' requires string as left operand, not %s", |
| 10691 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10692 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10693 | } |
| 10694 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10695 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10696 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10697 | Py_DECREF(sub); |
| 10698 | return -1; |
| 10699 | } |
| 10700 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10701 | kind1 = PyUnicode_KIND(str); |
| 10702 | kind2 = PyUnicode_KIND(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10703 | buf1 = PyUnicode_DATA(str); |
| 10704 | buf2 = PyUnicode_DATA(sub); |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10705 | if (kind2 != kind1) { |
| 10706 | if (kind2 > kind1) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10707 | Py_DECREF(sub); |
| 10708 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10709 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10710 | } |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10711 | buf2 = _PyUnicode_AsKind(sub, kind1); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10712 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | if (!buf2) { |
| 10714 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10715 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10716 | return -1; |
| 10717 | } |
| 10718 | len1 = PyUnicode_GET_LENGTH(str); |
| 10719 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10720 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10721 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10722 | case PyUnicode_1BYTE_KIND: |
| 10723 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10724 | break; |
| 10725 | case PyUnicode_2BYTE_KIND: |
| 10726 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10727 | break; |
| 10728 | case PyUnicode_4BYTE_KIND: |
| 10729 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10730 | break; |
| 10731 | default: |
| 10732 | result = -1; |
| 10733 | assert(0); |
| 10734 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10735 | |
| 10736 | Py_DECREF(str); |
| 10737 | Py_DECREF(sub); |
| 10738 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10739 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10740 | PyMem_Free(buf2); |
| 10741 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10742 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10743 | } |
| 10744 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10745 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10746 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10747 | PyObject * |
| 10748 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10749 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10750 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10751 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10752 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10753 | |
| 10754 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10755 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10756 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10757 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10758 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10759 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10760 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | |
| 10762 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10763 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10764 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10765 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10766 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10767 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10768 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10769 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10770 | } |
| 10771 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10772 | u_len = PyUnicode_GET_LENGTH(u); |
| 10773 | v_len = PyUnicode_GET_LENGTH(v); |
| 10774 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10775 | PyErr_SetString(PyExc_OverflowError, |
| 10776 | "strings are too large to concat"); |
| 10777 | goto onError; |
| 10778 | } |
| 10779 | new_len = u_len + v_len; |
| 10780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10781 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10782 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10783 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10784 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10785 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10786 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10788 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10789 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10790 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10791 | Py_DECREF(u); |
| 10792 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10793 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10794 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10795 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10796 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10797 | Py_XDECREF(u); |
| 10798 | Py_XDECREF(v); |
| 10799 | return NULL; |
| 10800 | } |
| 10801 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10802 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10803 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10804 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10805 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10806 | Py_UCS4 maxchar, maxchar2; |
| 10807 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10808 | |
| 10809 | if (p_left == NULL) { |
| 10810 | if (!PyErr_Occurred()) |
| 10811 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10812 | return; |
| 10813 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10814 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 10815 | if (right == NULL || left == NULL |
| 10816 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10817 | if (!PyErr_Occurred()) |
| 10818 | PyErr_BadInternalCall(); |
| 10819 | goto error; |
| 10820 | } |
| 10821 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10822 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10823 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10824 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10825 | goto error; |
| 10826 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10827 | /* Shortcuts */ |
| 10828 | if (left == unicode_empty) { |
| 10829 | Py_DECREF(left); |
| 10830 | Py_INCREF(right); |
| 10831 | *p_left = right; |
| 10832 | return; |
| 10833 | } |
| 10834 | if (right == unicode_empty) |
| 10835 | return; |
| 10836 | |
| 10837 | left_len = PyUnicode_GET_LENGTH(left); |
| 10838 | right_len = PyUnicode_GET_LENGTH(right); |
| 10839 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10840 | PyErr_SetString(PyExc_OverflowError, |
| 10841 | "strings are too large to concat"); |
| 10842 | goto error; |
| 10843 | } |
| 10844 | new_len = left_len + right_len; |
| 10845 | |
| 10846 | if (unicode_modifiable(left) |
| 10847 | && PyUnicode_CheckExact(right) |
| 10848 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10849 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10850 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10851 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10852 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10853 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10854 | { |
| 10855 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10856 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10857 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 10858 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10859 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10860 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10861 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10862 | else { |
| 10863 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10864 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10865 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10866 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10867 | /* Concat the two Unicode strings */ |
| 10868 | res = PyUnicode_New(new_len, maxchar); |
| 10869 | if (res == NULL) |
| 10870 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10871 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10872 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10873 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 10874 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10875 | } |
| 10876 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10877 | return; |
| 10878 | |
| 10879 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10880 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10881 | } |
| 10882 | |
| 10883 | void |
| 10884 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10885 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10886 | PyUnicode_Append(pleft, right); |
| 10887 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10888 | } |
| 10889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10890 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10891 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10892 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10893 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10894 | 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] | 10895 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10896 | |
| 10897 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10898 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10900 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10901 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10902 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10903 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10904 | int kind1, kind2, kind; |
| 10905 | void *buf1, *buf2; |
| 10906 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10907 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10908 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10909 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10910 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10911 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10912 | kind1 = PyUnicode_KIND(self); |
| 10913 | kind2 = PyUnicode_KIND(substring); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 10914 | if (kind2 > kind1) { |
| 10915 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10916 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 10917 | } |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10918 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10919 | buf1 = PyUnicode_DATA(self); |
| 10920 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10921 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10922 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10923 | if (!buf2) { |
| 10924 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10925 | return NULL; |
| 10926 | } |
| 10927 | len1 = PyUnicode_GET_LENGTH(self); |
| 10928 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10929 | |
| 10930 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10931 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10932 | case PyUnicode_1BYTE_KIND: |
| 10933 | iresult = ucs1lib_count( |
| 10934 | ((Py_UCS1*)buf1) + start, end - start, |
| 10935 | buf2, len2, PY_SSIZE_T_MAX |
| 10936 | ); |
| 10937 | break; |
| 10938 | case PyUnicode_2BYTE_KIND: |
| 10939 | iresult = ucs2lib_count( |
| 10940 | ((Py_UCS2*)buf1) + start, end - start, |
| 10941 | buf2, len2, PY_SSIZE_T_MAX |
| 10942 | ); |
| 10943 | break; |
| 10944 | case PyUnicode_4BYTE_KIND: |
| 10945 | iresult = ucs4lib_count( |
| 10946 | ((Py_UCS4*)buf1) + start, end - start, |
| 10947 | buf2, len2, PY_SSIZE_T_MAX |
| 10948 | ); |
| 10949 | break; |
| 10950 | default: |
| 10951 | assert(0); iresult = 0; |
| 10952 | } |
| 10953 | |
| 10954 | result = PyLong_FromSsize_t(iresult); |
| 10955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10956 | if (kind2 != kind) |
| 10957 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10958 | |
| 10959 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10960 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10961 | return result; |
| 10962 | } |
| 10963 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10964 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10965 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10967 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10968 | 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] | 10969 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10970 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10971 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10972 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10973 | |
| 10974 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10975 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10976 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10977 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | char *encoding = NULL; |
| 10979 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10980 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10981 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10982 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10983 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10984 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10985 | } |
| 10986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10987 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10988 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10989 | \n\ |
| 10990 | 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] | 10991 | 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] | 10992 | |
| 10993 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10994 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10995 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10996 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10997 | Py_UCS4 ch; |
| 10998 | PyObject *u; |
| 10999 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11000 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11001 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11002 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 | |
| 11004 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11005 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11006 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11007 | if (PyUnicode_READY(self) == -1) |
| 11008 | return NULL; |
| 11009 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11010 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11011 | src_len = PyUnicode_GET_LENGTH(self); |
| 11012 | i = j = line_pos = 0; |
| 11013 | kind = PyUnicode_KIND(self); |
| 11014 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11015 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11016 | for (; i < src_len; i++) { |
| 11017 | ch = PyUnicode_READ(kind, src_data, i); |
| 11018 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11019 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11020 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11021 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11022 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11023 | goto overflow; |
| 11024 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11025 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11026 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11027 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11029 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11030 | goto overflow; |
| 11031 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11032 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11033 | if (ch == '\n' || ch == '\r') |
| 11034 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11035 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11036 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11037 | if (!found) |
| 11038 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11040 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11041 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11042 | if (!u) |
| 11043 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11044 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11045 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11046 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11047 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11048 | for (; i < src_len; i++) { |
| 11049 | ch = PyUnicode_READ(kind, src_data, i); |
| 11050 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11051 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11052 | incr = tabsize - (line_pos % tabsize); |
| 11053 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11054 | FILL(kind, dest_data, ' ', j, incr); |
| 11055 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11056 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11057 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11058 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11059 | line_pos++; |
| 11060 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11061 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11062 | if (ch == '\n' || ch == '\r') |
| 11063 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11065 | } |
| 11066 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11067 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11068 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11069 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11070 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11071 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11072 | } |
| 11073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11074 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11075 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11076 | \n\ |
| 11077 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11078 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11079 | arguments start and end are interpreted as in slice notation.\n\ |
| 11080 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11081 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11082 | |
| 11083 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11084 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11085 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11086 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11087 | Py_ssize_t start; |
| 11088 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11089 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11090 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11091 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11092 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11093 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11094 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11095 | if (PyUnicode_READY(self) == -1) { |
| 11096 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11097 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11098 | } |
| 11099 | if (PyUnicode_READY(substring) == -1) { |
| 11100 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11101 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11102 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11103 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11104 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11105 | |
| 11106 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11107 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11108 | if (result == -2) |
| 11109 | return NULL; |
| 11110 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11111 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11112 | } |
| 11113 | |
| 11114 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11115 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11116 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11117 | void *data; |
| 11118 | enum PyUnicode_Kind kind; |
| 11119 | Py_UCS4 ch; |
| 11120 | PyObject *res; |
| 11121 | |
| 11122 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11123 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11124 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11125 | } |
| 11126 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11127 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11128 | return NULL; |
| 11129 | } |
| 11130 | kind = PyUnicode_KIND(self); |
| 11131 | data = PyUnicode_DATA(self); |
| 11132 | ch = PyUnicode_READ(kind, data, index); |
| 11133 | if (ch < 256) |
| 11134 | return get_latin1_char(ch); |
| 11135 | |
| 11136 | res = PyUnicode_New(1, ch); |
| 11137 | if (res == NULL) |
| 11138 | return NULL; |
| 11139 | kind = PyUnicode_KIND(res); |
| 11140 | data = PyUnicode_DATA(res); |
| 11141 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11142 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11143 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11144 | } |
| 11145 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11146 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11147 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11148 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11149 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11150 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11151 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11152 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11153 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11154 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11155 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11156 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11157 | if (_PyUnicode_HASH(self) != -1) |
| 11158 | return _PyUnicode_HASH(self); |
| 11159 | if (PyUnicode_READY(self) == -1) |
| 11160 | return -1; |
| 11161 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11162 | /* |
| 11163 | We make the hash of the empty string be 0, rather than using |
| 11164 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11165 | */ |
| 11166 | if (len == 0) { |
| 11167 | _PyUnicode_HASH(self) = 0; |
| 11168 | return 0; |
| 11169 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11170 | |
| 11171 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11172 | #define HASH(P) \ |
| 11173 | x ^= (Py_uhash_t) *P << 7; \ |
| 11174 | while (--len >= 0) \ |
| 11175 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11176 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11177 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11178 | switch (PyUnicode_KIND(self)) { |
| 11179 | case PyUnicode_1BYTE_KIND: { |
| 11180 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11181 | HASH(c); |
| 11182 | break; |
| 11183 | } |
| 11184 | case PyUnicode_2BYTE_KIND: { |
| 11185 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11186 | HASH(s); |
| 11187 | break; |
| 11188 | } |
| 11189 | default: { |
| 11190 | Py_UCS4 *l; |
| 11191 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11192 | "Impossible switch case in unicode_hash"); |
| 11193 | l = PyUnicode_4BYTE_DATA(self); |
| 11194 | HASH(l); |
| 11195 | break; |
| 11196 | } |
| 11197 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11198 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11199 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11200 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11201 | if (x == -1) |
| 11202 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11203 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11204 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11205 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11206 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11208 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11209 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11211 | 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] | 11212 | |
| 11213 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11214 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11215 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11216 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11217 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11218 | Py_ssize_t start; |
| 11219 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11220 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11221 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11222 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11223 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11224 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11225 | if (PyUnicode_READY(self) == -1) { |
| 11226 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11227 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11228 | } |
| 11229 | if (PyUnicode_READY(substring) == -1) { |
| 11230 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11231 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11232 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11233 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11234 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 | |
| 11236 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11237 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11238 | if (result == -2) |
| 11239 | return NULL; |
| 11240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11241 | if (result < 0) { |
| 11242 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11243 | return NULL; |
| 11244 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11245 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11246 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11247 | } |
| 11248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11249 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11250 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11252 | 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] | 11253 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11254 | |
| 11255 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11256 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11258 | Py_ssize_t i, length; |
| 11259 | int kind; |
| 11260 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11261 | int cased; |
| 11262 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11263 | if (PyUnicode_READY(self) == -1) |
| 11264 | return NULL; |
| 11265 | length = PyUnicode_GET_LENGTH(self); |
| 11266 | kind = PyUnicode_KIND(self); |
| 11267 | data = PyUnicode_DATA(self); |
| 11268 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11269 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11270 | if (length == 1) |
| 11271 | return PyBool_FromLong( |
| 11272 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11274 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11275 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11276 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11277 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11278 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11279 | for (i = 0; i < length; i++) { |
| 11280 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11281 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11282 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11283 | return PyBool_FromLong(0); |
| 11284 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11285 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11286 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11287 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11288 | } |
| 11289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11290 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11291 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11292 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11293 | 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] | 11294 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11295 | |
| 11296 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11297 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11299 | Py_ssize_t i, length; |
| 11300 | int kind; |
| 11301 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11302 | int cased; |
| 11303 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11304 | if (PyUnicode_READY(self) == -1) |
| 11305 | return NULL; |
| 11306 | length = PyUnicode_GET_LENGTH(self); |
| 11307 | kind = PyUnicode_KIND(self); |
| 11308 | data = PyUnicode_DATA(self); |
| 11309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11310 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11311 | if (length == 1) |
| 11312 | return PyBool_FromLong( |
| 11313 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11314 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11315 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11316 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11317 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11318 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11319 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11320 | for (i = 0; i < length; i++) { |
| 11321 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11322 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11323 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11324 | return PyBool_FromLong(0); |
| 11325 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11326 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11327 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11328 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 | } |
| 11330 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11331 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11332 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11333 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11334 | Return True if S is a titlecased string and there is at least one\n\ |
| 11335 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11336 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11337 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11338 | |
| 11339 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11340 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11341 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11342 | Py_ssize_t i, length; |
| 11343 | int kind; |
| 11344 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 | int cased, previous_is_cased; |
| 11346 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11347 | if (PyUnicode_READY(self) == -1) |
| 11348 | return NULL; |
| 11349 | length = PyUnicode_GET_LENGTH(self); |
| 11350 | kind = PyUnicode_KIND(self); |
| 11351 | data = PyUnicode_DATA(self); |
| 11352 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11353 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11354 | if (length == 1) { |
| 11355 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11356 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11357 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11358 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11359 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11360 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11361 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11362 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11363 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11364 | cased = 0; |
| 11365 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11366 | for (i = 0; i < length; i++) { |
| 11367 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11368 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11369 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11370 | if (previous_is_cased) |
| 11371 | return PyBool_FromLong(0); |
| 11372 | previous_is_cased = 1; |
| 11373 | cased = 1; |
| 11374 | } |
| 11375 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11376 | if (!previous_is_cased) |
| 11377 | return PyBool_FromLong(0); |
| 11378 | previous_is_cased = 1; |
| 11379 | cased = 1; |
| 11380 | } |
| 11381 | else |
| 11382 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11383 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11384 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11385 | } |
| 11386 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11387 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11388 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11389 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11390 | Return True if all characters in S are whitespace\n\ |
| 11391 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11392 | |
| 11393 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11394 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11395 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11396 | Py_ssize_t i, length; |
| 11397 | int kind; |
| 11398 | void *data; |
| 11399 | |
| 11400 | if (PyUnicode_READY(self) == -1) |
| 11401 | return NULL; |
| 11402 | length = PyUnicode_GET_LENGTH(self); |
| 11403 | kind = PyUnicode_KIND(self); |
| 11404 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11405 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11407 | if (length == 1) |
| 11408 | return PyBool_FromLong( |
| 11409 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11410 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11411 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11412 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11413 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11415 | for (i = 0; i < length; i++) { |
| 11416 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11417 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11418 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11419 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11420 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | } |
| 11422 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11423 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11424 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11425 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11426 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11427 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11428 | |
| 11429 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11430 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11431 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11432 | Py_ssize_t i, length; |
| 11433 | int kind; |
| 11434 | void *data; |
| 11435 | |
| 11436 | if (PyUnicode_READY(self) == -1) |
| 11437 | return NULL; |
| 11438 | length = PyUnicode_GET_LENGTH(self); |
| 11439 | kind = PyUnicode_KIND(self); |
| 11440 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11441 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11442 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11443 | if (length == 1) |
| 11444 | return PyBool_FromLong( |
| 11445 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11446 | |
| 11447 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11448 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11449 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11450 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11451 | for (i = 0; i < length; i++) { |
| 11452 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
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 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11455 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11456 | } |
| 11457 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11458 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11459 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11460 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11461 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11462 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11463 | |
| 11464 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11465 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11466 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11467 | int kind; |
| 11468 | void *data; |
| 11469 | Py_ssize_t len, i; |
| 11470 | |
| 11471 | if (PyUnicode_READY(self) == -1) |
| 11472 | return NULL; |
| 11473 | |
| 11474 | kind = PyUnicode_KIND(self); |
| 11475 | data = PyUnicode_DATA(self); |
| 11476 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11477 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11478 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11479 | if (len == 1) { |
| 11480 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11481 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11482 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11483 | |
| 11484 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11485 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11486 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11487 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11488 | for (i = 0; i < len; i++) { |
| 11489 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11490 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11491 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11492 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11493 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11494 | } |
| 11495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11496 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11497 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11499 | 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] | 11500 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | |
| 11502 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11503 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 | Py_ssize_t i, length; |
| 11506 | int kind; |
| 11507 | void *data; |
| 11508 | |
| 11509 | if (PyUnicode_READY(self) == -1) |
| 11510 | return NULL; |
| 11511 | length = PyUnicode_GET_LENGTH(self); |
| 11512 | kind = PyUnicode_KIND(self); |
| 11513 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11514 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11515 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11516 | if (length == 1) |
| 11517 | return PyBool_FromLong( |
| 11518 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11519 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11520 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11521 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11522 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11523 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | for (i = 0; i < length; i++) { |
| 11525 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11526 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11528 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 | } |
| 11530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11531 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11532 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11534 | Return True if all characters in S are digits\n\ |
| 11535 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11536 | |
| 11537 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11538 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11540 | Py_ssize_t i, length; |
| 11541 | int kind; |
| 11542 | void *data; |
| 11543 | |
| 11544 | if (PyUnicode_READY(self) == -1) |
| 11545 | return NULL; |
| 11546 | length = PyUnicode_GET_LENGTH(self); |
| 11547 | kind = PyUnicode_KIND(self); |
| 11548 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11550 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11551 | if (length == 1) { |
| 11552 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11553 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11554 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11555 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11556 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11557 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11558 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11559 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11560 | for (i = 0; i < length; i++) { |
| 11561 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11562 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11563 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11564 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | } |
| 11566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11567 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11568 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11569 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11570 | 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] | 11571 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11572 | |
| 11573 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11574 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11575 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11576 | Py_ssize_t i, length; |
| 11577 | int kind; |
| 11578 | void *data; |
| 11579 | |
| 11580 | if (PyUnicode_READY(self) == -1) |
| 11581 | return NULL; |
| 11582 | length = PyUnicode_GET_LENGTH(self); |
| 11583 | kind = PyUnicode_KIND(self); |
| 11584 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11586 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11587 | if (length == 1) |
| 11588 | return PyBool_FromLong( |
| 11589 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11590 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11591 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11592 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11593 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11594 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11595 | for (i = 0; i < length; i++) { |
| 11596 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11597 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11598 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11599 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11600 | } |
| 11601 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11602 | int |
| 11603 | PyUnicode_IsIdentifier(PyObject *self) |
| 11604 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11605 | int kind; |
| 11606 | void *data; |
| 11607 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11608 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11610 | if (PyUnicode_READY(self) == -1) { |
| 11611 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11612 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11613 | } |
| 11614 | |
| 11615 | /* Special case for empty strings */ |
| 11616 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11617 | return 0; |
| 11618 | kind = PyUnicode_KIND(self); |
| 11619 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11620 | |
| 11621 | /* PEP 3131 says that the first character must be in |
| 11622 | XID_Start and subsequent characters in XID_Continue, |
| 11623 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11624 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11625 | letters, digits, underscore). However, given the current |
| 11626 | definition of XID_Start and XID_Continue, it is sufficient |
| 11627 | to check just for these, except that _ must be allowed |
| 11628 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11629 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11630 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11631 | return 0; |
| 11632 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11633 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11634 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11635 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11636 | return 1; |
| 11637 | } |
| 11638 | |
| 11639 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11640 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11641 | \n\ |
| 11642 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11643 | to the language definition.\n\ |
| 11644 | \n\ |
| 11645 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11646 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11647 | |
| 11648 | static PyObject* |
| 11649 | unicode_isidentifier(PyObject *self) |
| 11650 | { |
| 11651 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11652 | } |
| 11653 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11654 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11655 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11656 | \n\ |
| 11657 | Return True if all characters in S are considered\n\ |
| 11658 | printable in repr() or S is empty, False otherwise."); |
| 11659 | |
| 11660 | static PyObject* |
| 11661 | unicode_isprintable(PyObject *self) |
| 11662 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11663 | Py_ssize_t i, length; |
| 11664 | int kind; |
| 11665 | void *data; |
| 11666 | |
| 11667 | if (PyUnicode_READY(self) == -1) |
| 11668 | return NULL; |
| 11669 | length = PyUnicode_GET_LENGTH(self); |
| 11670 | kind = PyUnicode_KIND(self); |
| 11671 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11672 | |
| 11673 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11674 | if (length == 1) |
| 11675 | return PyBool_FromLong( |
| 11676 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11678 | for (i = 0; i < length; i++) { |
| 11679 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11680 | Py_RETURN_FALSE; |
| 11681 | } |
| 11682 | } |
| 11683 | Py_RETURN_TRUE; |
| 11684 | } |
| 11685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11686 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11687 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11688 | \n\ |
| 11689 | 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] | 11690 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11691 | |
| 11692 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11693 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11694 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11695 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11696 | } |
| 11697 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11698 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11699 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11700 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11701 | if (PyUnicode_READY(self) == -1) |
| 11702 | return -1; |
| 11703 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11704 | } |
| 11705 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11706 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11707 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11708 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11709 | 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] | 11710 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11711 | |
| 11712 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11713 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11714 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11715 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11716 | Py_UCS4 fillchar = ' '; |
| 11717 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11718 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11719 | return NULL; |
| 11720 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11721 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11722 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11723 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11724 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11725 | return unicode_result_unchanged(self); |
| 11726 | |
| 11727 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11728 | } |
| 11729 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11730 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11731 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11732 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11733 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11734 | |
| 11735 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11736 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11737 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11738 | if (PyUnicode_READY(self) == -1) |
| 11739 | return NULL; |
| 11740 | if (PyUnicode_IS_ASCII(self)) |
| 11741 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11742 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | } |
| 11744 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11745 | #define LEFTSTRIP 0 |
| 11746 | #define RIGHTSTRIP 1 |
| 11747 | #define BOTHSTRIP 2 |
| 11748 | |
| 11749 | /* Arrays indexed by above */ |
| 11750 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11751 | |
| 11752 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11753 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11754 | /* externally visible for str.strip(unicode) */ |
| 11755 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11756 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11757 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11758 | void *data; |
| 11759 | int kind; |
| 11760 | Py_ssize_t i, j, len; |
| 11761 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11762 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11763 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11764 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11765 | return NULL; |
| 11766 | |
| 11767 | kind = PyUnicode_KIND(self); |
| 11768 | data = PyUnicode_DATA(self); |
| 11769 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11770 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11771 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11772 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11773 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11774 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11775 | i = 0; |
| 11776 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11777 | while (i < len) { |
| 11778 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11779 | if (!BLOOM(sepmask, ch)) |
| 11780 | break; |
| 11781 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11782 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11783 | i++; |
| 11784 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11785 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11786 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11787 | j = len; |
| 11788 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11789 | j--; |
| 11790 | while (j >= i) { |
| 11791 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11792 | if (!BLOOM(sepmask, ch)) |
| 11793 | break; |
| 11794 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11795 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11796 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11797 | } |
| 11798 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11799 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11800 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11801 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11802 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11803 | } |
| 11804 | |
| 11805 | PyObject* |
| 11806 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11807 | { |
| 11808 | unsigned char *data; |
| 11809 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11810 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11811 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11812 | if (PyUnicode_READY(self) == -1) |
| 11813 | return NULL; |
| 11814 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11815 | length = PyUnicode_GET_LENGTH(self); |
| 11816 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11817 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11818 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11819 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11820 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11821 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11822 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11823 | return NULL; |
| 11824 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11825 | if (start >= length || end < start) |
| 11826 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11827 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11828 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11829 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11830 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11831 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11832 | } |
| 11833 | else { |
| 11834 | kind = PyUnicode_KIND(self); |
| 11835 | data = PyUnicode_1BYTE_DATA(self); |
| 11836 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11837 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11838 | length); |
| 11839 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11840 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11841 | |
| 11842 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11843 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11844 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11845 | Py_ssize_t len, i, j; |
| 11846 | |
| 11847 | if (PyUnicode_READY(self) == -1) |
| 11848 | return NULL; |
| 11849 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11850 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11851 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11852 | if (PyUnicode_IS_ASCII(self)) { |
| 11853 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 11854 | |
| 11855 | i = 0; |
| 11856 | if (striptype != RIGHTSTRIP) { |
| 11857 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 11858 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11859 | if (!_Py_ascii_whitespace[ch]) |
| 11860 | break; |
| 11861 | i++; |
| 11862 | } |
| 11863 | } |
| 11864 | |
| 11865 | j = len; |
| 11866 | if (striptype != LEFTSTRIP) { |
| 11867 | j--; |
| 11868 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 11869 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11870 | if (!_Py_ascii_whitespace[ch]) |
| 11871 | break; |
| 11872 | j--; |
| 11873 | } |
| 11874 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11875 | } |
| 11876 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11877 | else { |
| 11878 | int kind = PyUnicode_KIND(self); |
| 11879 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11880 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11881 | i = 0; |
| 11882 | if (striptype != RIGHTSTRIP) { |
| 11883 | while (i < len) { |
| 11884 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11885 | if (!Py_UNICODE_ISSPACE(ch)) |
| 11886 | break; |
| 11887 | i++; |
| 11888 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 11889 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11890 | |
| 11891 | j = len; |
| 11892 | if (striptype != LEFTSTRIP) { |
| 11893 | j--; |
| 11894 | while (j >= i) { |
| 11895 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11896 | if (!Py_UNICODE_ISSPACE(ch)) |
| 11897 | break; |
| 11898 | j--; |
| 11899 | } |
| 11900 | j++; |
| 11901 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11902 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11903 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11904 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11905 | } |
| 11906 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11907 | |
| 11908 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11909 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11910 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11911 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11912 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 11913 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11914 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11915 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11916 | if (sep != NULL && sep != Py_None) { |
| 11917 | if (PyUnicode_Check(sep)) |
| 11918 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11919 | else { |
| 11920 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11921 | "%s arg must be None or str", |
| 11922 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11923 | return NULL; |
| 11924 | } |
| 11925 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11926 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11927 | return do_strip(self, striptype); |
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(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11932 | "S.strip([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 leading and trailing\n\ |
| 11935 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11936 | 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] | 11937 | |
| 11938 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11939 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11940 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11941 | if (PyTuple_GET_SIZE(args) == 0) |
| 11942 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11943 | else |
| 11944 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11945 | } |
| 11946 | |
| 11947 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11948 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11949 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11950 | \n\ |
| 11951 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11952 | 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] | 11953 | |
| 11954 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11955 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11956 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11957 | if (PyTuple_GET_SIZE(args) == 0) |
| 11958 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11959 | else |
| 11960 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11961 | } |
| 11962 | |
| 11963 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11964 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11965 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11966 | \n\ |
| 11967 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11968 | 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] | 11969 | |
| 11970 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11971 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11972 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11973 | if (PyTuple_GET_SIZE(args) == 0) |
| 11974 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11975 | else |
| 11976 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11977 | } |
| 11978 | |
| 11979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11980 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11981 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11982 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11983 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11984 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11985 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 11986 | if (len < 1) |
| 11987 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11988 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11989 | /* no repeat, return original string */ |
| 11990 | if (len == 1) |
| 11991 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11992 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11993 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11994 | return NULL; |
| 11995 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11996 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11997 | PyErr_SetString(PyExc_OverflowError, |
| 11998 | "repeated string is too long"); |
| 11999 | return NULL; |
| 12000 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12001 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12002 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12003 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12004 | if (!u) |
| 12005 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12006 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12007 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12008 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12009 | const int kind = PyUnicode_KIND(str); |
| 12010 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12011 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12012 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12013 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12014 | } |
| 12015 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12016 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12017 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12018 | ucs2[n] = fill_char; |
| 12019 | } else { |
| 12020 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12021 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12022 | for (n = 0; n < len; ++n) |
| 12023 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12024 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12025 | } |
| 12026 | else { |
| 12027 | /* number of characters copied this far */ |
| 12028 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12029 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12030 | char *to = (char *) PyUnicode_DATA(u); |
| 12031 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12032 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12033 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12034 | n = (done <= nchars-done) ? done : nchars-done; |
| 12035 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12036 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12037 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12038 | } |
| 12039 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12040 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12041 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12042 | } |
| 12043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12044 | PyObject * |
| 12045 | PyUnicode_Replace(PyObject *obj, |
| 12046 | PyObject *subobj, |
| 12047 | PyObject *replobj, |
| 12048 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12049 | { |
| 12050 | PyObject *self; |
| 12051 | PyObject *str1; |
| 12052 | PyObject *str2; |
| 12053 | PyObject *result; |
| 12054 | |
| 12055 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12056 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12057 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12058 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12059 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12060 | Py_DECREF(self); |
| 12061 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12062 | } |
| 12063 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12064 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12065 | Py_DECREF(self); |
| 12066 | Py_DECREF(str1); |
| 12067 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12068 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12069 | if (PyUnicode_READY(self) == -1 || |
| 12070 | PyUnicode_READY(str1) == -1 || |
| 12071 | PyUnicode_READY(str2) == -1) |
| 12072 | result = NULL; |
| 12073 | else |
| 12074 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12075 | Py_DECREF(self); |
| 12076 | Py_DECREF(str1); |
| 12077 | Py_DECREF(str2); |
| 12078 | return result; |
| 12079 | } |
| 12080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12081 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12082 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12083 | \n\ |
| 12084 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12085 | old replaced by new. If the optional argument count is\n\ |
| 12086 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12087 | |
| 12088 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12089 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12090 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12091 | PyObject *str1; |
| 12092 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12093 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12094 | PyObject *result; |
| 12095 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12096 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12097 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12098 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12099 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12100 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12101 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12102 | return NULL; |
| 12103 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12104 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12105 | Py_DECREF(str1); |
| 12106 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12107 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12108 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12109 | result = NULL; |
| 12110 | else |
| 12111 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12112 | |
| 12113 | Py_DECREF(str1); |
| 12114 | Py_DECREF(str2); |
| 12115 | return result; |
| 12116 | } |
| 12117 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12118 | static PyObject * |
| 12119 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12120 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12121 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12122 | Py_ssize_t isize; |
| 12123 | Py_ssize_t osize, squote, dquote, i, o; |
| 12124 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12125 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12126 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12128 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12129 | return NULL; |
| 12130 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12131 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12132 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12133 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12134 | /* Compute length of output, quote characters, and |
| 12135 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12136 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12137 | max = 127; |
| 12138 | squote = dquote = 0; |
| 12139 | ikind = PyUnicode_KIND(unicode); |
| 12140 | for (i = 0; i < isize; i++) { |
| 12141 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12142 | switch (ch) { |
| 12143 | case '\'': squote++; osize++; break; |
| 12144 | case '"': dquote++; osize++; break; |
| 12145 | case '\\': case '\t': case '\r': case '\n': |
| 12146 | osize += 2; break; |
| 12147 | default: |
| 12148 | /* Fast-path ASCII */ |
| 12149 | if (ch < ' ' || ch == 0x7f) |
| 12150 | osize += 4; /* \xHH */ |
| 12151 | else if (ch < 0x7f) |
| 12152 | osize++; |
| 12153 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12154 | osize++; |
| 12155 | max = ch > max ? ch : max; |
| 12156 | } |
| 12157 | else if (ch < 0x100) |
| 12158 | osize += 4; /* \xHH */ |
| 12159 | else if (ch < 0x10000) |
| 12160 | osize += 6; /* \uHHHH */ |
| 12161 | else |
| 12162 | osize += 10; /* \uHHHHHHHH */ |
| 12163 | } |
| 12164 | } |
| 12165 | |
| 12166 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12167 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12168 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12169 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12170 | if (dquote) |
| 12171 | /* Both squote and dquote present. Use squote, |
| 12172 | and escape them */ |
| 12173 | osize += squote; |
| 12174 | else |
| 12175 | quote = '"'; |
| 12176 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12177 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12178 | |
| 12179 | repr = PyUnicode_New(osize, max); |
| 12180 | if (repr == NULL) |
| 12181 | return NULL; |
| 12182 | okind = PyUnicode_KIND(repr); |
| 12183 | odata = PyUnicode_DATA(repr); |
| 12184 | |
| 12185 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12186 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12187 | if (unchanged) { |
| 12188 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12189 | unicode, 0, |
| 12190 | isize); |
| 12191 | } |
| 12192 | else { |
| 12193 | for (i = 0, o = 1; i < isize; i++) { |
| 12194 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12195 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12196 | /* Escape quotes and backslashes */ |
| 12197 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12198 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12199 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12200 | continue; |
| 12201 | } |
| 12202 | |
| 12203 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12204 | if (ch == '\t') { |
| 12205 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12206 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12207 | } |
| 12208 | else if (ch == '\n') { |
| 12209 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12210 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12211 | } |
| 12212 | else if (ch == '\r') { |
| 12213 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12214 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12215 | } |
| 12216 | |
| 12217 | /* Map non-printable US ASCII to '\xhh' */ |
| 12218 | else if (ch < ' ' || ch == 0x7F) { |
| 12219 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12220 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12221 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12222 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12223 | } |
| 12224 | |
| 12225 | /* Copy ASCII characters as-is */ |
| 12226 | else if (ch < 0x7F) { |
| 12227 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12228 | } |
| 12229 | |
| 12230 | /* Non-ASCII characters */ |
| 12231 | else { |
| 12232 | /* Map Unicode whitespace and control characters |
| 12233 | (categories Z* and C* except ASCII space) |
| 12234 | */ |
| 12235 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12236 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12237 | /* Map 8-bit characters to '\xhh' */ |
| 12238 | if (ch <= 0xff) { |
| 12239 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12240 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12241 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12242 | } |
| 12243 | /* Map 16-bit characters to '\uxxxx' */ |
| 12244 | else if (ch <= 0xffff) { |
| 12245 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12246 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12247 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12248 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12249 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12250 | } |
| 12251 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12252 | else { |
| 12253 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12254 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12255 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12256 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12257 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12258 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12259 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12260 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12261 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12262 | } |
| 12263 | } |
| 12264 | /* Copy characters as-is */ |
| 12265 | else { |
| 12266 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12267 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12268 | } |
| 12269 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12270 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12271 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12272 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12273 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12274 | } |
| 12275 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12276 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12277 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12278 | \n\ |
| 12279 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12280 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | arguments start and end are interpreted as in slice notation.\n\ |
| 12282 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12283 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12284 | |
| 12285 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12286 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12287 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12288 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12289 | Py_ssize_t start; |
| 12290 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12291 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12292 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12293 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12294 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12295 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12296 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12297 | if (PyUnicode_READY(self) == -1) { |
| 12298 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12299 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12300 | } |
| 12301 | if (PyUnicode_READY(substring) == -1) { |
| 12302 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12303 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12304 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12305 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12306 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12307 | |
| 12308 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12309 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12310 | if (result == -2) |
| 12311 | return NULL; |
| 12312 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12313 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12314 | } |
| 12315 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12316 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12317 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12318 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12319 | 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] | 12320 | |
| 12321 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12322 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12323 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12324 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12325 | Py_ssize_t start; |
| 12326 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12327 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12328 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12329 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12330 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12331 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12332 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12333 | if (PyUnicode_READY(self) == -1) { |
| 12334 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12335 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12336 | } |
| 12337 | if (PyUnicode_READY(substring) == -1) { |
| 12338 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12339 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12340 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12341 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12342 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12343 | |
| 12344 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12345 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12346 | if (result == -2) |
| 12347 | return NULL; |
| 12348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12349 | if (result < 0) { |
| 12350 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12351 | return NULL; |
| 12352 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12353 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12354 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12355 | } |
| 12356 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12357 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12358 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12359 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12360 | 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] | 12361 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12362 | |
| 12363 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12364 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12365 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12366 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12367 | Py_UCS4 fillchar = ' '; |
| 12368 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12369 | 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] | 12370 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12371 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12372 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12373 | return NULL; |
| 12374 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12375 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12376 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12377 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12378 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12379 | } |
| 12380 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12381 | PyObject * |
| 12382 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12383 | { |
| 12384 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12385 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | s = PyUnicode_FromObject(s); |
| 12387 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12388 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12389 | if (sep != NULL) { |
| 12390 | sep = PyUnicode_FromObject(sep); |
| 12391 | if (sep == NULL) { |
| 12392 | Py_DECREF(s); |
| 12393 | return NULL; |
| 12394 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12395 | } |
| 12396 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12397 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12398 | |
| 12399 | Py_DECREF(s); |
| 12400 | Py_XDECREF(sep); |
| 12401 | return result; |
| 12402 | } |
| 12403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12404 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12405 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12406 | \n\ |
| 12407 | Return a list of the words in S, using sep as the\n\ |
| 12408 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12409 | 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] | 12410 | whitespace string is a separator and empty strings are\n\ |
| 12411 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12412 | |
| 12413 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12414 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12415 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12416 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12417 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12418 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12419 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12420 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12421 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12422 | return NULL; |
| 12423 | |
| 12424 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12425 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12426 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12427 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12428 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12429 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12430 | } |
| 12431 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12432 | PyObject * |
| 12433 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12434 | { |
| 12435 | PyObject* str_obj; |
| 12436 | PyObject* sep_obj; |
| 12437 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12438 | int kind1, kind2, kind; |
| 12439 | void *buf1 = NULL, *buf2 = NULL; |
| 12440 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12441 | |
| 12442 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12443 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12444 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12445 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12446 | if (!sep_obj) { |
| 12447 | Py_DECREF(str_obj); |
| 12448 | return NULL; |
| 12449 | } |
| 12450 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12451 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12452 | Py_DECREF(str_obj); |
| 12453 | return NULL; |
| 12454 | } |
| 12455 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12456 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12457 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12458 | kind = Py_MAX(kind1, kind2); |
| 12459 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12460 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12461 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12462 | if (!buf1) |
| 12463 | goto onError; |
| 12464 | buf2 = PyUnicode_DATA(sep_obj); |
| 12465 | if (kind2 != kind) |
| 12466 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12467 | if (!buf2) |
| 12468 | goto onError; |
| 12469 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12470 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12471 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12472 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12473 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12474 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12475 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12476 | else |
| 12477 | 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] | 12478 | break; |
| 12479 | case PyUnicode_2BYTE_KIND: |
| 12480 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12481 | break; |
| 12482 | case PyUnicode_4BYTE_KIND: |
| 12483 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12484 | break; |
| 12485 | default: |
| 12486 | assert(0); |
| 12487 | out = 0; |
| 12488 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12489 | |
| 12490 | Py_DECREF(sep_obj); |
| 12491 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12492 | if (kind1 != kind) |
| 12493 | PyMem_Free(buf1); |
| 12494 | if (kind2 != kind) |
| 12495 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12496 | |
| 12497 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12498 | onError: |
| 12499 | Py_DECREF(sep_obj); |
| 12500 | Py_DECREF(str_obj); |
| 12501 | if (kind1 != kind && buf1) |
| 12502 | PyMem_Free(buf1); |
| 12503 | if (kind2 != kind && buf2) |
| 12504 | PyMem_Free(buf2); |
| 12505 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12506 | } |
| 12507 | |
| 12508 | |
| 12509 | PyObject * |
| 12510 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12511 | { |
| 12512 | PyObject* str_obj; |
| 12513 | PyObject* sep_obj; |
| 12514 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12515 | int kind1, kind2, kind; |
| 12516 | void *buf1 = NULL, *buf2 = NULL; |
| 12517 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12518 | |
| 12519 | str_obj = PyUnicode_FromObject(str_in); |
| 12520 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12521 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12522 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12523 | if (!sep_obj) { |
| 12524 | Py_DECREF(str_obj); |
| 12525 | return NULL; |
| 12526 | } |
| 12527 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12528 | kind1 = PyUnicode_KIND(str_in); |
| 12529 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12530 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12531 | buf1 = PyUnicode_DATA(str_in); |
| 12532 | if (kind1 != kind) |
| 12533 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12534 | if (!buf1) |
| 12535 | goto onError; |
| 12536 | buf2 = PyUnicode_DATA(sep_obj); |
| 12537 | if (kind2 != kind) |
| 12538 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12539 | if (!buf2) |
| 12540 | goto onError; |
| 12541 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12542 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12543 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12544 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12545 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12546 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12547 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12548 | else |
| 12549 | 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] | 12550 | break; |
| 12551 | case PyUnicode_2BYTE_KIND: |
| 12552 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12553 | break; |
| 12554 | case PyUnicode_4BYTE_KIND: |
| 12555 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12556 | break; |
| 12557 | default: |
| 12558 | assert(0); |
| 12559 | out = 0; |
| 12560 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12561 | |
| 12562 | Py_DECREF(sep_obj); |
| 12563 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12564 | if (kind1 != kind) |
| 12565 | PyMem_Free(buf1); |
| 12566 | if (kind2 != kind) |
| 12567 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12568 | |
| 12569 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12570 | onError: |
| 12571 | Py_DECREF(sep_obj); |
| 12572 | Py_DECREF(str_obj); |
| 12573 | if (kind1 != kind && buf1) |
| 12574 | PyMem_Free(buf1); |
| 12575 | if (kind2 != kind && buf2) |
| 12576 | PyMem_Free(buf2); |
| 12577 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12578 | } |
| 12579 | |
| 12580 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12581 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12582 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12583 | 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] | 12584 | 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] | 12585 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12586 | |
| 12587 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12588 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12589 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12590 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12591 | } |
| 12592 | |
| 12593 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12594 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12595 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12596 | 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] | 12597 | 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] | 12598 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12599 | |
| 12600 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12601 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12602 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12603 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12604 | } |
| 12605 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12606 | PyObject * |
| 12607 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12608 | { |
| 12609 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12610 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12611 | s = PyUnicode_FromObject(s); |
| 12612 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12613 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12614 | if (sep != NULL) { |
| 12615 | sep = PyUnicode_FromObject(sep); |
| 12616 | if (sep == NULL) { |
| 12617 | Py_DECREF(s); |
| 12618 | return NULL; |
| 12619 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12620 | } |
| 12621 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12622 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12623 | |
| 12624 | Py_DECREF(s); |
| 12625 | Py_XDECREF(sep); |
| 12626 | return result; |
| 12627 | } |
| 12628 | |
| 12629 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12630 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12631 | \n\ |
| 12632 | Return a list of the words in S, using sep as the\n\ |
| 12633 | delimiter string, starting at the end of the string and\n\ |
| 12634 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12635 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12636 | is a separator."); |
| 12637 | |
| 12638 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12639 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12640 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12641 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12642 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12643 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12644 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12645 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12646 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12647 | return NULL; |
| 12648 | |
| 12649 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12650 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12651 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12652 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12653 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12654 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12655 | } |
| 12656 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12657 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12658 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12659 | \n\ |
| 12660 | 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] | 12661 | 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] | 12662 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12663 | |
| 12664 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12665 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12666 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12667 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12668 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12669 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12670 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12671 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12672 | return NULL; |
| 12673 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12674 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12675 | } |
| 12676 | |
| 12677 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12678 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12679 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12680 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12681 | } |
| 12682 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12683 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12684 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | \n\ |
| 12686 | 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] | 12687 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12688 | |
| 12689 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12690 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12691 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12692 | if (PyUnicode_READY(self) == -1) |
| 12693 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12694 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12695 | } |
| 12696 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12697 | /*[clinic] |
| 12698 | module str |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12699 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12700 | @staticmethod |
| 12701 | str.maketrans as unicode_maketrans |
| 12702 | |
| 12703 | x: object |
| 12704 | |
| 12705 | y: unicode=NULL |
| 12706 | |
| 12707 | z: unicode=NULL |
| 12708 | |
| 12709 | / |
| 12710 | |
| 12711 | Return a translation table usable for str.translate(). |
| 12712 | |
| 12713 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12714 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12715 | Character keys will be then converted to ordinals. |
| 12716 | If there are two arguments, they must be strings of equal length, and |
| 12717 | in the resulting dictionary, each character in x will be mapped to the |
| 12718 | character at the same position in y. If there is a third argument, it |
| 12719 | must be a string, whose characters will be mapped to None in the result. |
| 12720 | [clinic]*/ |
| 12721 | |
| 12722 | PyDoc_STRVAR(unicode_maketrans__doc__, |
| 12723 | "Return a translation table usable for str.translate().\n" |
| 12724 | "\n" |
| 12725 | "str.maketrans(x, y=None, z=None)\n" |
| 12726 | "\n" |
| 12727 | "If there is only one argument, it must be a dictionary mapping Unicode\n" |
| 12728 | "ordinals (integers) or characters to Unicode ordinals, strings or None.\n" |
| 12729 | "Character keys will be then converted to ordinals.\n" |
| 12730 | "If there are two arguments, they must be strings of equal length, and\n" |
| 12731 | "in the resulting dictionary, each character in x will be mapped to the\n" |
| 12732 | "character at the same position in y. If there is a third argument, it\n" |
| 12733 | "must be a string, whose characters will be mapped to None in the result."); |
| 12734 | |
| 12735 | #define UNICODE_MAKETRANS_METHODDEF \ |
| 12736 | {"maketrans", (PyCFunction)unicode_maketrans, METH_VARARGS|METH_STATIC, unicode_maketrans__doc__}, |
| 12737 | |
| 12738 | static PyObject * |
| 12739 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z); |
| 12740 | |
| 12741 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12742 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12743 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12744 | PyObject *return_value = NULL; |
| 12745 | PyObject *x; |
| 12746 | PyObject *y = NULL; |
| 12747 | PyObject *z = NULL; |
| 12748 | |
| 12749 | if (!PyArg_ParseTuple(args, |
| 12750 | "O|UU:maketrans", |
| 12751 | &x, &y, &z)) |
| 12752 | goto exit; |
| 12753 | return_value = unicode_maketrans_impl(x, y, z); |
| 12754 | |
| 12755 | exit: |
| 12756 | return return_value; |
| 12757 | } |
| 12758 | |
| 12759 | static PyObject * |
| 12760 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
| 12761 | /*[clinic checksum: 137db9c3199e7906b7967009f511c24fa3235b5f]*/ |
| 12762 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12763 | PyObject *new = NULL, *key, *value; |
| 12764 | Py_ssize_t i = 0; |
| 12765 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12766 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12767 | new = PyDict_New(); |
| 12768 | if (!new) |
| 12769 | return NULL; |
| 12770 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12771 | int x_kind, y_kind, z_kind; |
| 12772 | void *x_data, *y_data, *z_data; |
| 12773 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12774 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12775 | if (!PyUnicode_Check(x)) { |
| 12776 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12777 | "be a string if there is a second argument"); |
| 12778 | goto err; |
| 12779 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12780 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12781 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12782 | "arguments must have equal length"); |
| 12783 | goto err; |
| 12784 | } |
| 12785 | /* 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] | 12786 | x_kind = PyUnicode_KIND(x); |
| 12787 | y_kind = PyUnicode_KIND(y); |
| 12788 | x_data = PyUnicode_DATA(x); |
| 12789 | y_data = PyUnicode_DATA(y); |
| 12790 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12791 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12792 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12793 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12794 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12795 | if (!value) { |
| 12796 | Py_DECREF(key); |
| 12797 | goto err; |
| 12798 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12799 | res = PyDict_SetItem(new, key, value); |
| 12800 | Py_DECREF(key); |
| 12801 | Py_DECREF(value); |
| 12802 | if (res < 0) |
| 12803 | goto err; |
| 12804 | } |
| 12805 | /* create entries for deleting chars in z */ |
| 12806 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12807 | z_kind = PyUnicode_KIND(z); |
| 12808 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12809 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12810 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12811 | if (!key) |
| 12812 | goto err; |
| 12813 | res = PyDict_SetItem(new, key, Py_None); |
| 12814 | Py_DECREF(key); |
| 12815 | if (res < 0) |
| 12816 | goto err; |
| 12817 | } |
| 12818 | } |
| 12819 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12820 | int kind; |
| 12821 | void *data; |
| 12822 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12823 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12824 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12825 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12826 | "to maketrans it must be a dict"); |
| 12827 | goto err; |
| 12828 | } |
| 12829 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12830 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12831 | if (PyUnicode_Check(key)) { |
| 12832 | /* convert string keys to integer keys */ |
| 12833 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12834 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12835 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12836 | "table must be of length 1"); |
| 12837 | goto err; |
| 12838 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12839 | kind = PyUnicode_KIND(key); |
| 12840 | data = PyUnicode_DATA(key); |
| 12841 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12842 | if (!newkey) |
| 12843 | goto err; |
| 12844 | res = PyDict_SetItem(new, newkey, value); |
| 12845 | Py_DECREF(newkey); |
| 12846 | if (res < 0) |
| 12847 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12848 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12849 | /* just keep integer keys */ |
| 12850 | if (PyDict_SetItem(new, key, value) < 0) |
| 12851 | goto err; |
| 12852 | } else { |
| 12853 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12854 | "be strings or integers"); |
| 12855 | goto err; |
| 12856 | } |
| 12857 | } |
| 12858 | } |
| 12859 | return new; |
| 12860 | err: |
| 12861 | Py_DECREF(new); |
| 12862 | return NULL; |
| 12863 | } |
| 12864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12865 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12866 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12867 | \n\ |
| 12868 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12869 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12870 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12871 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12872 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12873 | |
| 12874 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12875 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12876 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12877 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12878 | } |
| 12879 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12880 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12881 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12882 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12883 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12884 | |
| 12885 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12886 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12887 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12888 | if (PyUnicode_READY(self) == -1) |
| 12889 | return NULL; |
| 12890 | if (PyUnicode_IS_ASCII(self)) |
| 12891 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12892 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12893 | } |
| 12894 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12895 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12896 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12897 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12898 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12899 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12900 | |
| 12901 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12902 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12903 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12904 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12905 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12906 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12907 | int kind; |
| 12908 | void *data; |
| 12909 | Py_UCS4 chr; |
| 12910 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12911 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12912 | return NULL; |
| 12913 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12914 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12915 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12916 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12917 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12918 | return unicode_result_unchanged(self); |
| 12919 | |
| 12920 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12921 | |
| 12922 | u = pad(self, fill, 0, '0'); |
| 12923 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12924 | if (u == NULL) |
| 12925 | return NULL; |
| 12926 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12927 | kind = PyUnicode_KIND(u); |
| 12928 | data = PyUnicode_DATA(u); |
| 12929 | chr = PyUnicode_READ(kind, data, fill); |
| 12930 | |
| 12931 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12932 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12933 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12934 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12935 | } |
| 12936 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12937 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12938 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12939 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12940 | |
| 12941 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12942 | static PyObject * |
| 12943 | unicode__decimal2ascii(PyObject *self) |
| 12944 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12945 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12946 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12947 | #endif |
| 12948 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12949 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12950 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12951 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12952 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12953 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12954 | With optional end, stop comparing S at that position.\n\ |
| 12955 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12956 | |
| 12957 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12958 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12959 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12960 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12961 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12962 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12963 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12964 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12965 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12966 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12967 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12968 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12969 | if (PyTuple_Check(subobj)) { |
| 12970 | Py_ssize_t i; |
| 12971 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12972 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12973 | if (substring == NULL) |
| 12974 | return NULL; |
| 12975 | result = tailmatch(self, substring, start, end, -1); |
| 12976 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12977 | if (result == -1) |
| 12978 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12979 | if (result) { |
| 12980 | Py_RETURN_TRUE; |
| 12981 | } |
| 12982 | } |
| 12983 | /* nothing matched */ |
| 12984 | Py_RETURN_FALSE; |
| 12985 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12986 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12987 | if (substring == NULL) { |
| 12988 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12989 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12990 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12991 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12992 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12993 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12994 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12995 | if (result == -1) |
| 12996 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12997 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12998 | } |
| 12999 | |
| 13000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13001 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13002 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13003 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13004 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13005 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13006 | With optional end, stop comparing S at that position.\n\ |
| 13007 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13008 | |
| 13009 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13010 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13011 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13012 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13013 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13014 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13015 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13016 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13017 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13018 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13019 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13020 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13021 | if (PyTuple_Check(subobj)) { |
| 13022 | Py_ssize_t i; |
| 13023 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13024 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13025 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13026 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13027 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13028 | result = tailmatch(self, substring, start, end, +1); |
| 13029 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13030 | if (result == -1) |
| 13031 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13032 | if (result) { |
| 13033 | Py_RETURN_TRUE; |
| 13034 | } |
| 13035 | } |
| 13036 | Py_RETURN_FALSE; |
| 13037 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13038 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13039 | if (substring == NULL) { |
| 13040 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13041 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13042 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13043 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13044 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13045 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13046 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13047 | if (result == -1) |
| 13048 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13049 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13050 | } |
| 13051 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13052 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13053 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13054 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13055 | if (!writer->readonly) |
| 13056 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 13057 | else { |
| 13058 | /* Copy-on-write mode: set buffer size to 0 so |
| 13059 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13060 | * next write. */ |
| 13061 | writer->size = 0; |
| 13062 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13063 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13064 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13065 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13066 | } |
| 13067 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13068 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13069 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13070 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13071 | memset(writer, 0, sizeof(*writer)); |
| 13072 | #ifdef Py_DEBUG |
| 13073 | writer->kind = 5; /* invalid kind */ |
| 13074 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13075 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13076 | } |
| 13077 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13078 | int |
| 13079 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13080 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13081 | { |
| 13082 | Py_ssize_t newlen; |
| 13083 | PyObject *newbuffer; |
| 13084 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13085 | assert(length > 0); |
| 13086 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13087 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13088 | PyErr_NoMemory(); |
| 13089 | return -1; |
| 13090 | } |
| 13091 | newlen = writer->pos + length; |
| 13092 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13093 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13094 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13095 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13096 | assert(!writer->readonly); |
| 13097 | if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13098 | /* overallocate 25% to limit the number of resize */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13099 | newlen += newlen / 4; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13100 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13101 | if (newlen < writer->min_length) |
| 13102 | newlen = writer->min_length; |
| 13103 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13104 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13105 | if (writer->buffer == NULL) |
| 13106 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13107 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13108 | else if (newlen > writer->size) { |
| 13109 | if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13110 | /* overallocate 25% to limit the number of resize */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13111 | newlen += newlen / 4; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13112 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13113 | if (newlen < writer->min_length) |
| 13114 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13115 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13116 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13117 | /* resize + widen */ |
| 13118 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13119 | if (newbuffer == NULL) |
| 13120 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13121 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13122 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13123 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13124 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13125 | } |
| 13126 | else { |
| 13127 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13128 | if (newbuffer == NULL) |
| 13129 | return -1; |
| 13130 | } |
| 13131 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13132 | } |
| 13133 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13134 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13135 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13136 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13137 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13138 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13139 | writer->buffer, 0, writer->pos); |
| 13140 | Py_DECREF(writer->buffer); |
| 13141 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13142 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13143 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13144 | return 0; |
| 13145 | } |
| 13146 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13147 | Py_LOCAL_INLINE(int) |
| 13148 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13149 | { |
| 13150 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13151 | return -1; |
| 13152 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13153 | writer->pos++; |
| 13154 | return 0; |
| 13155 | } |
| 13156 | |
| 13157 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13158 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13159 | { |
| 13160 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13161 | } |
| 13162 | |
| 13163 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13164 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13165 | { |
| 13166 | Py_UCS4 maxchar; |
| 13167 | Py_ssize_t len; |
| 13168 | |
| 13169 | if (PyUnicode_READY(str) == -1) |
| 13170 | return -1; |
| 13171 | len = PyUnicode_GET_LENGTH(str); |
| 13172 | if (len == 0) |
| 13173 | return 0; |
| 13174 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13175 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13176 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13177 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13178 | Py_INCREF(str); |
| 13179 | writer->buffer = str; |
| 13180 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13181 | writer->pos += len; |
| 13182 | return 0; |
| 13183 | } |
| 13184 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13185 | return -1; |
| 13186 | } |
| 13187 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13188 | str, 0, len); |
| 13189 | writer->pos += len; |
| 13190 | return 0; |
| 13191 | } |
| 13192 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13193 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13194 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13195 | Py_ssize_t start, Py_ssize_t end) |
| 13196 | { |
| 13197 | Py_UCS4 maxchar; |
| 13198 | Py_ssize_t len; |
| 13199 | |
| 13200 | if (PyUnicode_READY(str) == -1) |
| 13201 | return -1; |
| 13202 | |
| 13203 | assert(0 <= start); |
| 13204 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13205 | assert(start <= end); |
| 13206 | |
| 13207 | if (end == 0) |
| 13208 | return 0; |
| 13209 | |
| 13210 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13211 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13212 | |
| 13213 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13214 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13215 | else |
| 13216 | maxchar = writer->maxchar; |
| 13217 | len = end - start; |
| 13218 | |
| 13219 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13220 | return -1; |
| 13221 | |
| 13222 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13223 | str, start, len); |
| 13224 | writer->pos += len; |
| 13225 | return 0; |
| 13226 | } |
| 13227 | |
| 13228 | int |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13229 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) |
| 13230 | { |
| 13231 | Py_UCS4 maxchar; |
| 13232 | |
| 13233 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13234 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13235 | return -1; |
| 13236 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13237 | writer->pos += len; |
| 13238 | return 0; |
| 13239 | } |
| 13240 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13241 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13242 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13243 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13244 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13245 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13246 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13247 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13248 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13249 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13250 | str = writer->buffer; |
| 13251 | writer->buffer = NULL; |
| 13252 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13253 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13254 | } |
| 13255 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13256 | PyObject *newbuffer; |
| 13257 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13258 | if (newbuffer == NULL) { |
| 13259 | Py_DECREF(writer->buffer); |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13260 | writer->buffer = NULL; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13261 | return NULL; |
| 13262 | } |
| 13263 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13264 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13265 | str = writer->buffer; |
| 13266 | writer->buffer = NULL; |
| 13267 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13268 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13269 | } |
| 13270 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13271 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13272 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13273 | { |
| 13274 | Py_CLEAR(writer->buffer); |
| 13275 | } |
| 13276 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13277 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13278 | |
| 13279 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13280 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13281 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13282 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13283 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13284 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13285 | PyDoc_STRVAR(format_map__doc__, |
| 13286 | "S.format_map(mapping) -> str\n\ |
| 13287 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13288 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13289 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13290 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13291 | static PyObject * |
| 13292 | unicode__format__(PyObject* self, PyObject* args) |
| 13293 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13294 | PyObject *format_spec; |
| 13295 | _PyUnicodeWriter writer; |
| 13296 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13297 | |
| 13298 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13299 | return NULL; |
| 13300 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13301 | if (PyUnicode_READY(self) == -1) |
| 13302 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13303 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13304 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13305 | self, format_spec, 0, |
| 13306 | PyUnicode_GET_LENGTH(format_spec)); |
| 13307 | if (ret == -1) { |
| 13308 | _PyUnicodeWriter_Dealloc(&writer); |
| 13309 | return NULL; |
| 13310 | } |
| 13311 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13312 | } |
| 13313 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13314 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13315 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13316 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13317 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13318 | |
| 13319 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13320 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13321 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13322 | Py_ssize_t size; |
| 13323 | |
| 13324 | /* If it's a compact object, account for base structure + |
| 13325 | character data. */ |
| 13326 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13327 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13328 | else if (PyUnicode_IS_COMPACT(v)) |
| 13329 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13330 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13331 | else { |
| 13332 | /* If it is a two-block object, account for base object, and |
| 13333 | for character block if present. */ |
| 13334 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13335 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13336 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13337 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13338 | } |
| 13339 | /* 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] | 13340 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13341 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13342 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13343 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13344 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13345 | |
| 13346 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13347 | } |
| 13348 | |
| 13349 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13350 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13351 | |
| 13352 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13353 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13354 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13355 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13356 | if (!copy) |
| 13357 | return NULL; |
| 13358 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13359 | } |
| 13360 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13361 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13362 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13363 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13364 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13365 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13366 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13367 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13368 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13369 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13370 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13371 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13372 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13373 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13374 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13375 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13376 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13377 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13378 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13379 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13380 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13381 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13382 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13383 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13384 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13385 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13386 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13387 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13388 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13389 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13390 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13391 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13392 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13393 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13394 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13395 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13396 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13397 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13398 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13399 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13400 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13401 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13402 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13403 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13404 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13405 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13406 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13407 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13408 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13409 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13410 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13411 | #endif |
| 13412 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13413 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13414 | {NULL, NULL} |
| 13415 | }; |
| 13416 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13417 | static PyObject * |
| 13418 | unicode_mod(PyObject *v, PyObject *w) |
| 13419 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13420 | if (!PyUnicode_Check(v)) |
| 13421 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13422 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13423 | } |
| 13424 | |
| 13425 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13426 | 0, /*nb_add*/ |
| 13427 | 0, /*nb_subtract*/ |
| 13428 | 0, /*nb_multiply*/ |
| 13429 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13430 | }; |
| 13431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13432 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13433 | (lenfunc) unicode_length, /* sq_length */ |
| 13434 | PyUnicode_Concat, /* sq_concat */ |
| 13435 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13436 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13437 | 0, /* sq_slice */ |
| 13438 | 0, /* sq_ass_item */ |
| 13439 | 0, /* sq_ass_slice */ |
| 13440 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13441 | }; |
| 13442 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13443 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13444 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13445 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13446 | if (PyUnicode_READY(self) == -1) |
| 13447 | return NULL; |
| 13448 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13449 | if (PyIndex_Check(item)) { |
| 13450 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13451 | if (i == -1 && PyErr_Occurred()) |
| 13452 | return NULL; |
| 13453 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13454 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13455 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13456 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13457 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13458 | PyObject *result; |
| 13459 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13460 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13461 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13462 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13463 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13464 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13465 | return NULL; |
| 13466 | } |
| 13467 | |
| 13468 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13469 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13470 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13471 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13472 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13473 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13474 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13475 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13476 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13477 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13478 | src_kind = PyUnicode_KIND(self); |
| 13479 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13480 | if (!PyUnicode_IS_ASCII(self)) { |
| 13481 | kind_limit = kind_maxchar_limit(src_kind); |
| 13482 | max_char = 0; |
| 13483 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13484 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13485 | if (ch > max_char) { |
| 13486 | max_char = ch; |
| 13487 | if (max_char >= kind_limit) |
| 13488 | break; |
| 13489 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13490 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13491 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13492 | else |
| 13493 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13494 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13495 | if (result == NULL) |
| 13496 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13497 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13498 | dest_data = PyUnicode_DATA(result); |
| 13499 | |
| 13500 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13501 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13502 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13503 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13504 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13505 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13506 | } else { |
| 13507 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13508 | return NULL; |
| 13509 | } |
| 13510 | } |
| 13511 | |
| 13512 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13513 | (lenfunc)unicode_length, /* mp_length */ |
| 13514 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13515 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13516 | }; |
| 13517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13518 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13519 | /* Helpers for PyUnicode_Format() */ |
| 13520 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13521 | struct unicode_formatter_t { |
| 13522 | PyObject *args; |
| 13523 | int args_owned; |
| 13524 | Py_ssize_t arglen, argidx; |
| 13525 | PyObject *dict; |
| 13526 | |
| 13527 | enum PyUnicode_Kind fmtkind; |
| 13528 | Py_ssize_t fmtcnt, fmtpos; |
| 13529 | void *fmtdata; |
| 13530 | PyObject *fmtstr; |
| 13531 | |
| 13532 | _PyUnicodeWriter writer; |
| 13533 | }; |
| 13534 | |
| 13535 | struct unicode_format_arg_t { |
| 13536 | Py_UCS4 ch; |
| 13537 | int flags; |
| 13538 | Py_ssize_t width; |
| 13539 | int prec; |
| 13540 | int sign; |
| 13541 | }; |
| 13542 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13543 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13544 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13545 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13546 | Py_ssize_t argidx = ctx->argidx; |
| 13547 | |
| 13548 | if (argidx < ctx->arglen) { |
| 13549 | ctx->argidx++; |
| 13550 | if (ctx->arglen < 0) |
| 13551 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13552 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13553 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13554 | } |
| 13555 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13556 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13557 | return NULL; |
| 13558 | } |
| 13559 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13560 | /* 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] | 13561 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13562 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13563 | otherwise. |
| 13564 | |
| 13565 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13566 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13567 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13568 | PyObject **p_output, |
| 13569 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13570 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13571 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13572 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13573 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13574 | int prec; |
| 13575 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13577 | x = PyFloat_AsDouble(v); |
| 13578 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13579 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13580 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13581 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13582 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13583 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13584 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13585 | if (arg->flags & F_ALT) |
| 13586 | dtoa_flags = Py_DTSF_ALT; |
| 13587 | else |
| 13588 | dtoa_flags = 0; |
| 13589 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13590 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13591 | return -1; |
| 13592 | len = strlen(p); |
| 13593 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13594 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13595 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13596 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13597 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13598 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13599 | writer->pos += len; |
| 13600 | } |
| 13601 | else |
| 13602 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13603 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13604 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13605 | } |
| 13606 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13607 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13608 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13609 | * Python's regular ints. |
| 13610 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13611 | * The output string is of the form |
| 13612 | * "-"? ("0x" | "0X")? digit+ |
| 13613 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13614 | * set in flags. The case of hex digits will be correct, |
| 13615 | * There will be at least prec digits, zero-filled on the left if |
| 13616 | * necessary to get that many. |
| 13617 | * val object to be converted |
| 13618 | * flags bitmask of format flags; only F_ALT is looked at |
| 13619 | * prec minimum number of digits; 0-fill on left if needed |
| 13620 | * type a character in [duoxX]; u acts the same as d |
| 13621 | * |
| 13622 | * CAUTION: o, x and X conversions on regular ints can never |
| 13623 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13624 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13625 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13626 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13627 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13628 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13629 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13630 | Py_ssize_t i; |
| 13631 | int sign; /* 1 if '-', else 0 */ |
| 13632 | int len; /* number of characters */ |
| 13633 | Py_ssize_t llen; |
| 13634 | int numdigits; /* len == numnondigits + numdigits */ |
| 13635 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13636 | int prec = arg->prec; |
| 13637 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13638 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13639 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13640 | if (prec > INT_MAX-3) { |
| 13641 | PyErr_SetString(PyExc_OverflowError, |
| 13642 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13643 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13644 | } |
| 13645 | |
| 13646 | assert(PyLong_Check(val)); |
| 13647 | |
| 13648 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13649 | default: |
| 13650 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13651 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13652 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13653 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13654 | /* int and int subclasses should print numerically when a numeric */ |
| 13655 | /* format code is used (see issue18780) */ |
| 13656 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13657 | break; |
| 13658 | case 'o': |
| 13659 | numnondigits = 2; |
| 13660 | result = PyNumber_ToBase(val, 8); |
| 13661 | break; |
| 13662 | case 'x': |
| 13663 | case 'X': |
| 13664 | numnondigits = 2; |
| 13665 | result = PyNumber_ToBase(val, 16); |
| 13666 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13667 | } |
| 13668 | if (!result) |
| 13669 | return NULL; |
| 13670 | |
| 13671 | assert(unicode_modifiable(result)); |
| 13672 | assert(PyUnicode_IS_READY(result)); |
| 13673 | assert(PyUnicode_IS_ASCII(result)); |
| 13674 | |
| 13675 | /* To modify the string in-place, there can only be one reference. */ |
| 13676 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13677 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13678 | PyErr_BadInternalCall(); |
| 13679 | return NULL; |
| 13680 | } |
| 13681 | buf = PyUnicode_DATA(result); |
| 13682 | llen = PyUnicode_GET_LENGTH(result); |
| 13683 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13684 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13685 | PyErr_SetString(PyExc_ValueError, |
| 13686 | "string too large in _PyBytes_FormatLong"); |
| 13687 | return NULL; |
| 13688 | } |
| 13689 | len = (int)llen; |
| 13690 | sign = buf[0] == '-'; |
| 13691 | numnondigits += sign; |
| 13692 | numdigits = len - numnondigits; |
| 13693 | assert(numdigits > 0); |
| 13694 | |
| 13695 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13696 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13697 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13698 | assert(buf[sign] == '0'); |
| 13699 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13700 | buf[sign+1] == 'o'); |
| 13701 | numnondigits -= 2; |
| 13702 | buf += 2; |
| 13703 | len -= 2; |
| 13704 | if (sign) |
| 13705 | buf[0] = '-'; |
| 13706 | assert(len == numnondigits + numdigits); |
| 13707 | assert(numdigits > 0); |
| 13708 | } |
| 13709 | |
| 13710 | /* Fill with leading zeroes to meet minimum width. */ |
| 13711 | if (prec > numdigits) { |
| 13712 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13713 | numnondigits + prec); |
| 13714 | char *b1; |
| 13715 | if (!r1) { |
| 13716 | Py_DECREF(result); |
| 13717 | return NULL; |
| 13718 | } |
| 13719 | b1 = PyBytes_AS_STRING(r1); |
| 13720 | for (i = 0; i < numnondigits; ++i) |
| 13721 | *b1++ = *buf++; |
| 13722 | for (i = 0; i < prec - numdigits; i++) |
| 13723 | *b1++ = '0'; |
| 13724 | for (i = 0; i < numdigits; i++) |
| 13725 | *b1++ = *buf++; |
| 13726 | *b1 = '\0'; |
| 13727 | Py_DECREF(result); |
| 13728 | result = r1; |
| 13729 | buf = PyBytes_AS_STRING(result); |
| 13730 | len = numnondigits + prec; |
| 13731 | } |
| 13732 | |
| 13733 | /* Fix up case for hex conversions. */ |
| 13734 | if (type == 'X') { |
| 13735 | /* Need to convert all lower case letters to upper case. |
| 13736 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13737 | for (i = 0; i < len; i++) |
| 13738 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13739 | buf[i] -= 'a'-'A'; |
| 13740 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13741 | if (!PyUnicode_Check(result) |
| 13742 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13743 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13744 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13745 | Py_DECREF(result); |
| 13746 | result = unicode; |
| 13747 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13748 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13749 | if (PyUnicode_Resize(&result, len) < 0) |
| 13750 | Py_CLEAR(result); |
| 13751 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13752 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13753 | } |
| 13754 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13755 | /* Format an integer. |
| 13756 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13757 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13758 | * -1 and raise an exception on error */ |
| 13759 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13760 | mainformatlong(PyObject *v, |
| 13761 | struct unicode_format_arg_t *arg, |
| 13762 | PyObject **p_output, |
| 13763 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13764 | { |
| 13765 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13766 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13767 | |
| 13768 | if (!PyNumber_Check(v)) |
| 13769 | goto wrongtype; |
| 13770 | |
| 13771 | if (!PyLong_Check(v)) { |
| 13772 | iobj = PyNumber_Long(v); |
| 13773 | if (iobj == NULL) { |
| 13774 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13775 | goto wrongtype; |
| 13776 | return -1; |
| 13777 | } |
| 13778 | assert(PyLong_Check(iobj)); |
| 13779 | } |
| 13780 | else { |
| 13781 | iobj = v; |
| 13782 | Py_INCREF(iobj); |
| 13783 | } |
| 13784 | |
| 13785 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13786 | && arg->width == -1 && arg->prec == -1 |
| 13787 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 13788 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13789 | { |
| 13790 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13791 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13792 | int base; |
| 13793 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13794 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13795 | { |
| 13796 | default: |
| 13797 | assert(0 && "'type' not in [diuoxX]"); |
| 13798 | case 'd': |
| 13799 | case 'i': |
| 13800 | case 'u': |
| 13801 | base = 10; |
| 13802 | break; |
| 13803 | case 'o': |
| 13804 | base = 8; |
| 13805 | break; |
| 13806 | case 'x': |
| 13807 | case 'X': |
| 13808 | base = 16; |
| 13809 | break; |
| 13810 | } |
| 13811 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13812 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 13813 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13814 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13815 | } |
| 13816 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13817 | return 1; |
| 13818 | } |
| 13819 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13820 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13821 | Py_DECREF(iobj); |
| 13822 | if (res == NULL) |
| 13823 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13824 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13825 | return 0; |
| 13826 | |
| 13827 | wrongtype: |
| 13828 | PyErr_Format(PyExc_TypeError, |
| 13829 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13830 | "not %.200s", |
| 13831 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13832 | return -1; |
| 13833 | } |
| 13834 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13835 | static Py_UCS4 |
| 13836 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13837 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13838 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13839 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13840 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13841 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13842 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13843 | goto onError; |
| 13844 | } |
| 13845 | else { |
| 13846 | /* Integer input truncated to a character */ |
| 13847 | long x; |
| 13848 | x = PyLong_AsLong(v); |
| 13849 | if (x == -1 && PyErr_Occurred()) |
| 13850 | goto onError; |
| 13851 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13852 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13853 | PyErr_SetString(PyExc_OverflowError, |
| 13854 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13855 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13856 | } |
| 13857 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13858 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13859 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13860 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13861 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13862 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13863 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13864 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13865 | } |
| 13866 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13867 | /* Parse options of an argument: flags, width, precision. |
| 13868 | Handle also "%(name)" syntax. |
| 13869 | |
| 13870 | Return 0 if the argument has been formatted into arg->str. |
| 13871 | Return 1 if the argument has been written into ctx->writer, |
| 13872 | Raise an exception and return -1 on error. */ |
| 13873 | static int |
| 13874 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 13875 | struct unicode_format_arg_t *arg) |
| 13876 | { |
| 13877 | #define FORMAT_READ(ctx) \ |
| 13878 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 13879 | |
| 13880 | PyObject *v; |
| 13881 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13882 | if (arg->ch == '(') { |
| 13883 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 13884 | Py_ssize_t keystart; |
| 13885 | Py_ssize_t keylen; |
| 13886 | PyObject *key; |
| 13887 | int pcount = 1; |
| 13888 | |
| 13889 | if (ctx->dict == NULL) { |
| 13890 | PyErr_SetString(PyExc_TypeError, |
| 13891 | "format requires a mapping"); |
| 13892 | return -1; |
| 13893 | } |
| 13894 | ++ctx->fmtpos; |
| 13895 | --ctx->fmtcnt; |
| 13896 | keystart = ctx->fmtpos; |
| 13897 | /* Skip over balanced parentheses */ |
| 13898 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 13899 | arg->ch = FORMAT_READ(ctx); |
| 13900 | if (arg->ch == ')') |
| 13901 | --pcount; |
| 13902 | else if (arg->ch == '(') |
| 13903 | ++pcount; |
| 13904 | ctx->fmtpos++; |
| 13905 | } |
| 13906 | keylen = ctx->fmtpos - keystart - 1; |
| 13907 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 13908 | PyErr_SetString(PyExc_ValueError, |
| 13909 | "incomplete format key"); |
| 13910 | return -1; |
| 13911 | } |
| 13912 | key = PyUnicode_Substring(ctx->fmtstr, |
| 13913 | keystart, keystart + keylen); |
| 13914 | if (key == NULL) |
| 13915 | return -1; |
| 13916 | if (ctx->args_owned) { |
| 13917 | Py_DECREF(ctx->args); |
| 13918 | ctx->args_owned = 0; |
| 13919 | } |
| 13920 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 13921 | Py_DECREF(key); |
| 13922 | if (ctx->args == NULL) |
| 13923 | return -1; |
| 13924 | ctx->args_owned = 1; |
| 13925 | ctx->arglen = -1; |
| 13926 | ctx->argidx = -2; |
| 13927 | } |
| 13928 | |
| 13929 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13930 | while (--ctx->fmtcnt >= 0) { |
| 13931 | arg->ch = FORMAT_READ(ctx); |
| 13932 | ctx->fmtpos++; |
| 13933 | switch (arg->ch) { |
| 13934 | case '-': arg->flags |= F_LJUST; continue; |
| 13935 | case '+': arg->flags |= F_SIGN; continue; |
| 13936 | case ' ': arg->flags |= F_BLANK; continue; |
| 13937 | case '#': arg->flags |= F_ALT; continue; |
| 13938 | case '0': arg->flags |= F_ZERO; continue; |
| 13939 | } |
| 13940 | break; |
| 13941 | } |
| 13942 | |
| 13943 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13944 | if (arg->ch == '*') { |
| 13945 | v = unicode_format_getnextarg(ctx); |
| 13946 | if (v == NULL) |
| 13947 | return -1; |
| 13948 | if (!PyLong_Check(v)) { |
| 13949 | PyErr_SetString(PyExc_TypeError, |
| 13950 | "* wants int"); |
| 13951 | return -1; |
| 13952 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13953 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13954 | if (arg->width == -1 && PyErr_Occurred()) |
| 13955 | return -1; |
| 13956 | if (arg->width < 0) { |
| 13957 | arg->flags |= F_LJUST; |
| 13958 | arg->width = -arg->width; |
| 13959 | } |
| 13960 | if (--ctx->fmtcnt >= 0) { |
| 13961 | arg->ch = FORMAT_READ(ctx); |
| 13962 | ctx->fmtpos++; |
| 13963 | } |
| 13964 | } |
| 13965 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13966 | arg->width = arg->ch - '0'; |
| 13967 | while (--ctx->fmtcnt >= 0) { |
| 13968 | arg->ch = FORMAT_READ(ctx); |
| 13969 | ctx->fmtpos++; |
| 13970 | if (arg->ch < '0' || arg->ch > '9') |
| 13971 | break; |
| 13972 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 13973 | mixing signed and unsigned comparison. Since arg->ch is between |
| 13974 | '0' and '9', casting to int is safe. */ |
| 13975 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 13976 | PyErr_SetString(PyExc_ValueError, |
| 13977 | "width too big"); |
| 13978 | return -1; |
| 13979 | } |
| 13980 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 13981 | } |
| 13982 | } |
| 13983 | |
| 13984 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13985 | if (arg->ch == '.') { |
| 13986 | arg->prec = 0; |
| 13987 | if (--ctx->fmtcnt >= 0) { |
| 13988 | arg->ch = FORMAT_READ(ctx); |
| 13989 | ctx->fmtpos++; |
| 13990 | } |
| 13991 | if (arg->ch == '*') { |
| 13992 | v = unicode_format_getnextarg(ctx); |
| 13993 | if (v == NULL) |
| 13994 | return -1; |
| 13995 | if (!PyLong_Check(v)) { |
| 13996 | PyErr_SetString(PyExc_TypeError, |
| 13997 | "* wants int"); |
| 13998 | return -1; |
| 13999 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14000 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14001 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14002 | return -1; |
| 14003 | if (arg->prec < 0) |
| 14004 | arg->prec = 0; |
| 14005 | if (--ctx->fmtcnt >= 0) { |
| 14006 | arg->ch = FORMAT_READ(ctx); |
| 14007 | ctx->fmtpos++; |
| 14008 | } |
| 14009 | } |
| 14010 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14011 | arg->prec = arg->ch - '0'; |
| 14012 | while (--ctx->fmtcnt >= 0) { |
| 14013 | arg->ch = FORMAT_READ(ctx); |
| 14014 | ctx->fmtpos++; |
| 14015 | if (arg->ch < '0' || arg->ch > '9') |
| 14016 | break; |
| 14017 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14018 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14019 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14020 | return -1; |
| 14021 | } |
| 14022 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14023 | } |
| 14024 | } |
| 14025 | } |
| 14026 | |
| 14027 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14028 | if (ctx->fmtcnt >= 0) { |
| 14029 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14030 | if (--ctx->fmtcnt >= 0) { |
| 14031 | arg->ch = FORMAT_READ(ctx); |
| 14032 | ctx->fmtpos++; |
| 14033 | } |
| 14034 | } |
| 14035 | } |
| 14036 | if (ctx->fmtcnt < 0) { |
| 14037 | PyErr_SetString(PyExc_ValueError, |
| 14038 | "incomplete format"); |
| 14039 | return -1; |
| 14040 | } |
| 14041 | return 0; |
| 14042 | |
| 14043 | #undef FORMAT_READ |
| 14044 | } |
| 14045 | |
| 14046 | /* Format one argument. Supported conversion specifiers: |
| 14047 | |
| 14048 | - "s", "r", "a": any type |
| 14049 | - "i", "d", "u", "o", "x", "X": int |
| 14050 | - "e", "E", "f", "F", "g", "G": float |
| 14051 | - "c": int or str (1 character) |
| 14052 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14053 | When possible, the output is written directly into the Unicode writer |
| 14054 | (ctx->writer). A string is created when padding is required. |
| 14055 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14056 | Return 0 if the argument has been formatted into *p_str, |
| 14057 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14058 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14059 | static int |
| 14060 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14061 | struct unicode_format_arg_t *arg, |
| 14062 | PyObject **p_str) |
| 14063 | { |
| 14064 | PyObject *v; |
| 14065 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14066 | |
| 14067 | if (ctx->fmtcnt == 0) |
| 14068 | ctx->writer.overallocate = 0; |
| 14069 | |
| 14070 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14071 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14072 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14073 | return 1; |
| 14074 | } |
| 14075 | |
| 14076 | v = unicode_format_getnextarg(ctx); |
| 14077 | if (v == NULL) |
| 14078 | return -1; |
| 14079 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14080 | |
| 14081 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14082 | case 's': |
| 14083 | case 'r': |
| 14084 | case 'a': |
| 14085 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14086 | /* Fast path */ |
| 14087 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14088 | return -1; |
| 14089 | return 1; |
| 14090 | } |
| 14091 | |
| 14092 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14093 | *p_str = v; |
| 14094 | Py_INCREF(*p_str); |
| 14095 | } |
| 14096 | else { |
| 14097 | if (arg->ch == 's') |
| 14098 | *p_str = PyObject_Str(v); |
| 14099 | else if (arg->ch == 'r') |
| 14100 | *p_str = PyObject_Repr(v); |
| 14101 | else |
| 14102 | *p_str = PyObject_ASCII(v); |
| 14103 | } |
| 14104 | break; |
| 14105 | |
| 14106 | case 'i': |
| 14107 | case 'd': |
| 14108 | case 'u': |
| 14109 | case 'o': |
| 14110 | case 'x': |
| 14111 | case 'X': |
| 14112 | { |
| 14113 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14114 | if (ret != 0) |
| 14115 | return ret; |
| 14116 | arg->sign = 1; |
| 14117 | break; |
| 14118 | } |
| 14119 | |
| 14120 | case 'e': |
| 14121 | case 'E': |
| 14122 | case 'f': |
| 14123 | case 'F': |
| 14124 | case 'g': |
| 14125 | case 'G': |
| 14126 | if (arg->width == -1 && arg->prec == -1 |
| 14127 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14128 | { |
| 14129 | /* Fast path */ |
| 14130 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14131 | return -1; |
| 14132 | return 1; |
| 14133 | } |
| 14134 | |
| 14135 | arg->sign = 1; |
| 14136 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14137 | return -1; |
| 14138 | break; |
| 14139 | |
| 14140 | case 'c': |
| 14141 | { |
| 14142 | Py_UCS4 ch = formatchar(v); |
| 14143 | if (ch == (Py_UCS4) -1) |
| 14144 | return -1; |
| 14145 | if (arg->width == -1 && arg->prec == -1) { |
| 14146 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14147 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14148 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14149 | return 1; |
| 14150 | } |
| 14151 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14152 | break; |
| 14153 | } |
| 14154 | |
| 14155 | default: |
| 14156 | PyErr_Format(PyExc_ValueError, |
| 14157 | "unsupported format character '%c' (0x%x) " |
| 14158 | "at index %zd", |
| 14159 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14160 | (int)arg->ch, |
| 14161 | ctx->fmtpos - 1); |
| 14162 | return -1; |
| 14163 | } |
| 14164 | if (*p_str == NULL) |
| 14165 | return -1; |
| 14166 | assert (PyUnicode_Check(*p_str)); |
| 14167 | return 0; |
| 14168 | } |
| 14169 | |
| 14170 | static int |
| 14171 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14172 | struct unicode_format_arg_t *arg, |
| 14173 | PyObject *str) |
| 14174 | { |
| 14175 | Py_ssize_t len; |
| 14176 | enum PyUnicode_Kind kind; |
| 14177 | void *pbuf; |
| 14178 | Py_ssize_t pindex; |
| 14179 | Py_UCS4 signchar; |
| 14180 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14181 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14182 | Py_ssize_t sublen; |
| 14183 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14184 | Py_UCS4 fill; |
| 14185 | |
| 14186 | fill = ' '; |
| 14187 | if (arg->sign && arg->flags & F_ZERO) |
| 14188 | fill = '0'; |
| 14189 | |
| 14190 | if (PyUnicode_READY(str) == -1) |
| 14191 | return -1; |
| 14192 | |
| 14193 | len = PyUnicode_GET_LENGTH(str); |
| 14194 | if ((arg->width == -1 || arg->width <= len) |
| 14195 | && (arg->prec == -1 || arg->prec >= len) |
| 14196 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14197 | { |
| 14198 | /* Fast path */ |
| 14199 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14200 | return -1; |
| 14201 | return 0; |
| 14202 | } |
| 14203 | |
| 14204 | /* Truncate the string for "s", "r" and "a" formats |
| 14205 | if the precision is set */ |
| 14206 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14207 | if (arg->prec >= 0 && len > arg->prec) |
| 14208 | len = arg->prec; |
| 14209 | } |
| 14210 | |
| 14211 | /* Adjust sign and width */ |
| 14212 | kind = PyUnicode_KIND(str); |
| 14213 | pbuf = PyUnicode_DATA(str); |
| 14214 | pindex = 0; |
| 14215 | signchar = '\0'; |
| 14216 | if (arg->sign) { |
| 14217 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14218 | if (ch == '-' || ch == '+') { |
| 14219 | signchar = ch; |
| 14220 | len--; |
| 14221 | pindex++; |
| 14222 | } |
| 14223 | else if (arg->flags & F_SIGN) |
| 14224 | signchar = '+'; |
| 14225 | else if (arg->flags & F_BLANK) |
| 14226 | signchar = ' '; |
| 14227 | else |
| 14228 | arg->sign = 0; |
| 14229 | } |
| 14230 | if (arg->width < len) |
| 14231 | arg->width = len; |
| 14232 | |
| 14233 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14234 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14235 | if (!(arg->flags & F_LJUST)) { |
| 14236 | if (arg->sign) { |
| 14237 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14238 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14239 | } |
| 14240 | else { |
| 14241 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14242 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14243 | } |
| 14244 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14245 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14246 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14247 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14248 | } |
| 14249 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14250 | buflen = arg->width; |
| 14251 | if (arg->sign && len == arg->width) |
| 14252 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14253 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14254 | return -1; |
| 14255 | |
| 14256 | /* Write the sign if needed */ |
| 14257 | if (arg->sign) { |
| 14258 | if (fill != ' ') { |
| 14259 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14260 | writer->pos += 1; |
| 14261 | } |
| 14262 | if (arg->width > len) |
| 14263 | arg->width--; |
| 14264 | } |
| 14265 | |
| 14266 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14267 | if the alternate form is used. |
| 14268 | For example, write "0x" for the "%#x" format. */ |
| 14269 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14270 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14271 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14272 | if (fill != ' ') { |
| 14273 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14274 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14275 | writer->pos += 2; |
| 14276 | pindex += 2; |
| 14277 | } |
| 14278 | arg->width -= 2; |
| 14279 | if (arg->width < 0) |
| 14280 | arg->width = 0; |
| 14281 | len -= 2; |
| 14282 | } |
| 14283 | |
| 14284 | /* Pad left with the fill character if needed */ |
| 14285 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14286 | sublen = arg->width - len; |
| 14287 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14288 | writer->pos += sublen; |
| 14289 | arg->width = len; |
| 14290 | } |
| 14291 | |
| 14292 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14293 | the alternate form is used */ |
| 14294 | if (fill == ' ') { |
| 14295 | if (arg->sign) { |
| 14296 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14297 | writer->pos += 1; |
| 14298 | } |
| 14299 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14300 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14301 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14302 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14303 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14304 | writer->pos += 2; |
| 14305 | pindex += 2; |
| 14306 | } |
| 14307 | } |
| 14308 | |
| 14309 | /* Write characters */ |
| 14310 | if (len) { |
| 14311 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14312 | str, pindex, len); |
| 14313 | writer->pos += len; |
| 14314 | } |
| 14315 | |
| 14316 | /* Pad right with the fill character if needed */ |
| 14317 | if (arg->width > len) { |
| 14318 | sublen = arg->width - len; |
| 14319 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14320 | writer->pos += sublen; |
| 14321 | } |
| 14322 | return 0; |
| 14323 | } |
| 14324 | |
| 14325 | /* Helper of PyUnicode_Format(): format one arg. |
| 14326 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14327 | static int |
| 14328 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14329 | { |
| 14330 | struct unicode_format_arg_t arg; |
| 14331 | PyObject *str; |
| 14332 | int ret; |
| 14333 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14334 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14335 | arg.flags = 0; |
| 14336 | arg.width = -1; |
| 14337 | arg.prec = -1; |
| 14338 | arg.sign = 0; |
| 14339 | str = NULL; |
| 14340 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14341 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14342 | if (ret == -1) |
| 14343 | return -1; |
| 14344 | |
| 14345 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14346 | if (ret == -1) |
| 14347 | return -1; |
| 14348 | |
| 14349 | if (ret != 1) { |
| 14350 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14351 | Py_DECREF(str); |
| 14352 | if (ret == -1) |
| 14353 | return -1; |
| 14354 | } |
| 14355 | |
| 14356 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14357 | PyErr_SetString(PyExc_TypeError, |
| 14358 | "not all arguments converted during string formatting"); |
| 14359 | return -1; |
| 14360 | } |
| 14361 | return 0; |
| 14362 | } |
| 14363 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14364 | PyObject * |
| 14365 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14366 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14367 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14368 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14369 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14370 | PyErr_BadInternalCall(); |
| 14371 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14372 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14373 | |
| 14374 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14375 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14376 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14377 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14378 | Py_DECREF(ctx.fmtstr); |
| 14379 | return NULL; |
| 14380 | } |
| 14381 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14382 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14383 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14384 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14385 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14386 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14387 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14388 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14389 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14390 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14391 | ctx.arglen = PyTuple_Size(args); |
| 14392 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14393 | } |
| 14394 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14395 | ctx.arglen = -1; |
| 14396 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14397 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14398 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14399 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14400 | ctx.dict = args; |
| 14401 | else |
| 14402 | ctx.dict = NULL; |
| 14403 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14404 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14405 | while (--ctx.fmtcnt >= 0) { |
| 14406 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14407 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14408 | |
| 14409 | nonfmtpos = ctx.fmtpos++; |
| 14410 | while (ctx.fmtcnt >= 0 && |
| 14411 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14412 | ctx.fmtpos++; |
| 14413 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14414 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14415 | if (ctx.fmtcnt < 0) { |
| 14416 | ctx.fmtpos--; |
| 14417 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14418 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14419 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14420 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14421 | nonfmtpos, ctx.fmtpos) < 0) |
| 14422 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14423 | } |
| 14424 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14425 | ctx.fmtpos++; |
| 14426 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14427 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14428 | } |
| 14429 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14430 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14431 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14432 | PyErr_SetString(PyExc_TypeError, |
| 14433 | "not all arguments converted during string formatting"); |
| 14434 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14435 | } |
| 14436 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14437 | if (ctx.args_owned) { |
| 14438 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14439 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14440 | Py_DECREF(ctx.fmtstr); |
| 14441 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14442 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14443 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14444 | Py_DECREF(ctx.fmtstr); |
| 14445 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14446 | if (ctx.args_owned) { |
| 14447 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14448 | } |
| 14449 | return NULL; |
| 14450 | } |
| 14451 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14452 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14453 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14454 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14455 | static PyObject * |
| 14456 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14457 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14458 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14459 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14460 | char *encoding = NULL; |
| 14461 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14462 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14463 | if (type != &PyUnicode_Type) |
| 14464 | return unicode_subtype_new(type, args, kwds); |
| 14465 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14466 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14467 | return NULL; |
| 14468 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14469 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14470 | if (encoding == NULL && errors == NULL) |
| 14471 | return PyObject_Str(x); |
| 14472 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14473 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14474 | } |
| 14475 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14476 | static PyObject * |
| 14477 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14478 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14479 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14480 | Py_ssize_t length, char_size; |
| 14481 | int share_wstr, share_utf8; |
| 14482 | unsigned int kind; |
| 14483 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14484 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14485 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14486 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14487 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14488 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14489 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14490 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14491 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14492 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14493 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14494 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14495 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14496 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14497 | if (self == NULL) { |
| 14498 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14499 | return NULL; |
| 14500 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14501 | kind = PyUnicode_KIND(unicode); |
| 14502 | length = PyUnicode_GET_LENGTH(unicode); |
| 14503 | |
| 14504 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14505 | #ifdef Py_DEBUG |
| 14506 | _PyUnicode_HASH(self) = -1; |
| 14507 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14508 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14509 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14510 | _PyUnicode_STATE(self).interned = 0; |
| 14511 | _PyUnicode_STATE(self).kind = kind; |
| 14512 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14513 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14514 | _PyUnicode_STATE(self).ready = 1; |
| 14515 | _PyUnicode_WSTR(self) = NULL; |
| 14516 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14517 | _PyUnicode_UTF8(self) = NULL; |
| 14518 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14519 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14520 | |
| 14521 | share_utf8 = 0; |
| 14522 | share_wstr = 0; |
| 14523 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14524 | char_size = 1; |
| 14525 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14526 | share_utf8 = 1; |
| 14527 | } |
| 14528 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14529 | char_size = 2; |
| 14530 | if (sizeof(wchar_t) == 2) |
| 14531 | share_wstr = 1; |
| 14532 | } |
| 14533 | else { |
| 14534 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14535 | char_size = 4; |
| 14536 | if (sizeof(wchar_t) == 4) |
| 14537 | share_wstr = 1; |
| 14538 | } |
| 14539 | |
| 14540 | /* Ensure we won't overflow the length. */ |
| 14541 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14542 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14543 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14544 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14545 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14546 | if (data == NULL) { |
| 14547 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14548 | goto onError; |
| 14549 | } |
| 14550 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14551 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14552 | if (share_utf8) { |
| 14553 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14554 | _PyUnicode_UTF8(self) = data; |
| 14555 | } |
| 14556 | if (share_wstr) { |
| 14557 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14558 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14559 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14560 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14561 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14562 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14563 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14564 | #ifdef Py_DEBUG |
| 14565 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14566 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14567 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14568 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14569 | |
| 14570 | onError: |
| 14571 | Py_DECREF(unicode); |
| 14572 | Py_DECREF(self); |
| 14573 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14574 | } |
| 14575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14576 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14577 | "str(object='') -> str\n\ |
| 14578 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14579 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14580 | Create a new string object from the given object. If encoding or\n\ |
| 14581 | errors is specified, then the object must expose a data buffer\n\ |
| 14582 | that will be decoded using the given encoding and error handler.\n\ |
| 14583 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14584 | or repr(object).\n\ |
| 14585 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14586 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14587 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14588 | static PyObject *unicode_iter(PyObject *seq); |
| 14589 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14590 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14591 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14592 | "str", /* tp_name */ |
| 14593 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14594 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14595 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14596 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14597 | 0, /* tp_print */ |
| 14598 | 0, /* tp_getattr */ |
| 14599 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14600 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14601 | unicode_repr, /* tp_repr */ |
| 14602 | &unicode_as_number, /* tp_as_number */ |
| 14603 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14604 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14605 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14606 | 0, /* tp_call*/ |
| 14607 | (reprfunc) unicode_str, /* tp_str */ |
| 14608 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14609 | 0, /* tp_setattro */ |
| 14610 | 0, /* tp_as_buffer */ |
| 14611 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14612 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14613 | unicode_doc, /* tp_doc */ |
| 14614 | 0, /* tp_traverse */ |
| 14615 | 0, /* tp_clear */ |
| 14616 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14617 | 0, /* tp_weaklistoffset */ |
| 14618 | unicode_iter, /* tp_iter */ |
| 14619 | 0, /* tp_iternext */ |
| 14620 | unicode_methods, /* tp_methods */ |
| 14621 | 0, /* tp_members */ |
| 14622 | 0, /* tp_getset */ |
| 14623 | &PyBaseObject_Type, /* tp_base */ |
| 14624 | 0, /* tp_dict */ |
| 14625 | 0, /* tp_descr_get */ |
| 14626 | 0, /* tp_descr_set */ |
| 14627 | 0, /* tp_dictoffset */ |
| 14628 | 0, /* tp_init */ |
| 14629 | 0, /* tp_alloc */ |
| 14630 | unicode_new, /* tp_new */ |
| 14631 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14632 | }; |
| 14633 | |
| 14634 | /* Initialize the Unicode implementation */ |
| 14635 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14636 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14637 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14638 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14639 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14640 | 0x000A, /* LINE FEED */ |
| 14641 | 0x000D, /* CARRIAGE RETURN */ |
| 14642 | 0x001C, /* FILE SEPARATOR */ |
| 14643 | 0x001D, /* GROUP SEPARATOR */ |
| 14644 | 0x001E, /* RECORD SEPARATOR */ |
| 14645 | 0x0085, /* NEXT LINE */ |
| 14646 | 0x2028, /* LINE SEPARATOR */ |
| 14647 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14648 | }; |
| 14649 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14650 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14651 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14652 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14653 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14654 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14655 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14656 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14657 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14658 | |
| 14659 | /* initialize the linebreak bloom filter */ |
| 14660 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14661 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14662 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14663 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14664 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14665 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14666 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14667 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14668 | Py_FatalError("Can't initialize field name iterator type"); |
| 14669 | |
| 14670 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14671 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14672 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14673 | #ifdef HAVE_MBCS |
| 14674 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14675 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14676 | PyErr_SetFromWindowsErr(0); |
| 14677 | return -1; |
| 14678 | } |
| 14679 | #endif |
| 14680 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14681 | } |
| 14682 | |
| 14683 | /* Finalize the Unicode implementation */ |
| 14684 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14685 | int |
| 14686 | PyUnicode_ClearFreeList(void) |
| 14687 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14688 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14689 | } |
| 14690 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14691 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14692 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14693 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14694 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14695 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14696 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14697 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14698 | for (i = 0; i < 256; i++) |
| 14699 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14700 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14701 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14702 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14703 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14704 | void |
| 14705 | PyUnicode_InternInPlace(PyObject **p) |
| 14706 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 14707 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14708 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14709 | #ifdef Py_DEBUG |
| 14710 | assert(s != NULL); |
| 14711 | assert(_PyUnicode_CHECK(s)); |
| 14712 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14713 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14714 | return; |
| 14715 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14716 | /* If it's a subclass, we don't really know what putting |
| 14717 | it in the interned dict might do. */ |
| 14718 | if (!PyUnicode_CheckExact(s)) |
| 14719 | return; |
| 14720 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14721 | return; |
| 14722 | if (interned == NULL) { |
| 14723 | interned = PyDict_New(); |
| 14724 | if (interned == NULL) { |
| 14725 | PyErr_Clear(); /* Don't leave an exception */ |
| 14726 | return; |
| 14727 | } |
| 14728 | } |
| 14729 | /* It might be that the GetItem call fails even |
| 14730 | though the key is present in the dictionary, |
| 14731 | namely when this happens during a stack overflow. */ |
| 14732 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14733 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14734 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14735 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 14736 | if (t) { |
| 14737 | Py_INCREF(t); |
| 14738 | Py_DECREF(*p); |
| 14739 | *p = t; |
| 14740 | return; |
| 14741 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14742 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14743 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14744 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14745 | PyErr_Clear(); |
| 14746 | PyThreadState_GET()->recursion_critical = 0; |
| 14747 | return; |
| 14748 | } |
| 14749 | PyThreadState_GET()->recursion_critical = 0; |
| 14750 | /* The two references in interned are not counted by refcnt. |
| 14751 | The deallocator will take care of this */ |
| 14752 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14753 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14754 | } |
| 14755 | |
| 14756 | void |
| 14757 | PyUnicode_InternImmortal(PyObject **p) |
| 14758 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14759 | PyUnicode_InternInPlace(p); |
| 14760 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14761 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14762 | Py_INCREF(*p); |
| 14763 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14764 | } |
| 14765 | |
| 14766 | PyObject * |
| 14767 | PyUnicode_InternFromString(const char *cp) |
| 14768 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14769 | PyObject *s = PyUnicode_FromString(cp); |
| 14770 | if (s == NULL) |
| 14771 | return NULL; |
| 14772 | PyUnicode_InternInPlace(&s); |
| 14773 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14774 | } |
| 14775 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14776 | void |
| 14777 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14778 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14779 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14780 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14781 | Py_ssize_t i, n; |
| 14782 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14783 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14784 | if (interned == NULL || !PyDict_Check(interned)) |
| 14785 | return; |
| 14786 | keys = PyDict_Keys(interned); |
| 14787 | if (keys == NULL || !PyList_Check(keys)) { |
| 14788 | PyErr_Clear(); |
| 14789 | return; |
| 14790 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14791 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14792 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14793 | detector, interned unicode strings are not forcibly deallocated; |
| 14794 | rather, we give them their stolen references back, and then clear |
| 14795 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14796 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14797 | n = PyList_GET_SIZE(keys); |
| 14798 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14799 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14800 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14801 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14802 | if (PyUnicode_READY(s) == -1) { |
| 14803 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14804 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14805 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14806 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14807 | case SSTATE_NOT_INTERNED: |
| 14808 | /* XXX Shouldn't happen */ |
| 14809 | break; |
| 14810 | case SSTATE_INTERNED_IMMORTAL: |
| 14811 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14812 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14813 | break; |
| 14814 | case SSTATE_INTERNED_MORTAL: |
| 14815 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14816 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14817 | break; |
| 14818 | default: |
| 14819 | Py_FatalError("Inconsistent interned string state."); |
| 14820 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14821 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14822 | } |
| 14823 | fprintf(stderr, "total size of all interned strings: " |
| 14824 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14825 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14826 | Py_DECREF(keys); |
| 14827 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14828 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14829 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14830 | |
| 14831 | |
| 14832 | /********************* Unicode Iterator **************************/ |
| 14833 | |
| 14834 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14835 | PyObject_HEAD |
| 14836 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14837 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14838 | } unicodeiterobject; |
| 14839 | |
| 14840 | static void |
| 14841 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14842 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14843 | _PyObject_GC_UNTRACK(it); |
| 14844 | Py_XDECREF(it->it_seq); |
| 14845 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14846 | } |
| 14847 | |
| 14848 | static int |
| 14849 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14850 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14851 | Py_VISIT(it->it_seq); |
| 14852 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14853 | } |
| 14854 | |
| 14855 | static PyObject * |
| 14856 | unicodeiter_next(unicodeiterobject *it) |
| 14857 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14858 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14859 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14860 | assert(it != NULL); |
| 14861 | seq = it->it_seq; |
| 14862 | if (seq == NULL) |
| 14863 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14864 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14866 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14867 | int kind = PyUnicode_KIND(seq); |
| 14868 | void *data = PyUnicode_DATA(seq); |
| 14869 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14870 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14871 | if (item != NULL) |
| 14872 | ++it->it_index; |
| 14873 | return item; |
| 14874 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14875 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14876 | Py_DECREF(seq); |
| 14877 | it->it_seq = NULL; |
| 14878 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14879 | } |
| 14880 | |
| 14881 | static PyObject * |
| 14882 | unicodeiter_len(unicodeiterobject *it) |
| 14883 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14884 | Py_ssize_t len = 0; |
| 14885 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14886 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14887 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14888 | } |
| 14889 | |
| 14890 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14891 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14892 | static PyObject * |
| 14893 | unicodeiter_reduce(unicodeiterobject *it) |
| 14894 | { |
| 14895 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14896 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14897 | it->it_seq, it->it_index); |
| 14898 | } else { |
| 14899 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14900 | if (u == NULL) |
| 14901 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14902 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14903 | } |
| 14904 | } |
| 14905 | |
| 14906 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14907 | |
| 14908 | static PyObject * |
| 14909 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14910 | { |
| 14911 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14912 | if (index == -1 && PyErr_Occurred()) |
| 14913 | return NULL; |
| 14914 | if (index < 0) |
| 14915 | index = 0; |
| 14916 | it->it_index = index; |
| 14917 | Py_RETURN_NONE; |
| 14918 | } |
| 14919 | |
| 14920 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14921 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14922 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14923 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14924 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14925 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14926 | reduce_doc}, |
| 14927 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14928 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14929 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14930 | }; |
| 14931 | |
| 14932 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14933 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14934 | "str_iterator", /* tp_name */ |
| 14935 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14936 | 0, /* tp_itemsize */ |
| 14937 | /* methods */ |
| 14938 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14939 | 0, /* tp_print */ |
| 14940 | 0, /* tp_getattr */ |
| 14941 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14942 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14943 | 0, /* tp_repr */ |
| 14944 | 0, /* tp_as_number */ |
| 14945 | 0, /* tp_as_sequence */ |
| 14946 | 0, /* tp_as_mapping */ |
| 14947 | 0, /* tp_hash */ |
| 14948 | 0, /* tp_call */ |
| 14949 | 0, /* tp_str */ |
| 14950 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14951 | 0, /* tp_setattro */ |
| 14952 | 0, /* tp_as_buffer */ |
| 14953 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14954 | 0, /* tp_doc */ |
| 14955 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14956 | 0, /* tp_clear */ |
| 14957 | 0, /* tp_richcompare */ |
| 14958 | 0, /* tp_weaklistoffset */ |
| 14959 | PyObject_SelfIter, /* tp_iter */ |
| 14960 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14961 | unicodeiter_methods, /* tp_methods */ |
| 14962 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14963 | }; |
| 14964 | |
| 14965 | static PyObject * |
| 14966 | unicode_iter(PyObject *seq) |
| 14967 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14968 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14969 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14970 | if (!PyUnicode_Check(seq)) { |
| 14971 | PyErr_BadInternalCall(); |
| 14972 | return NULL; |
| 14973 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14974 | if (PyUnicode_READY(seq) == -1) |
| 14975 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14976 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14977 | if (it == NULL) |
| 14978 | return NULL; |
| 14979 | it->it_index = 0; |
| 14980 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14981 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14982 | _PyObject_GC_TRACK(it); |
| 14983 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14984 | } |
| 14985 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14986 | |
| 14987 | size_t |
| 14988 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14989 | { |
| 14990 | int res = 0; |
| 14991 | while(*u++) |
| 14992 | res++; |
| 14993 | return res; |
| 14994 | } |
| 14995 | |
| 14996 | Py_UNICODE* |
| 14997 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14998 | { |
| 14999 | Py_UNICODE *u = s1; |
| 15000 | while ((*u++ = *s2++)); |
| 15001 | return s1; |
| 15002 | } |
| 15003 | |
| 15004 | Py_UNICODE* |
| 15005 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15006 | { |
| 15007 | Py_UNICODE *u = s1; |
| 15008 | while ((*u++ = *s2++)) |
| 15009 | if (n-- == 0) |
| 15010 | break; |
| 15011 | return s1; |
| 15012 | } |
| 15013 | |
| 15014 | Py_UNICODE* |
| 15015 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15016 | { |
| 15017 | Py_UNICODE *u1 = s1; |
| 15018 | u1 += Py_UNICODE_strlen(u1); |
| 15019 | Py_UNICODE_strcpy(u1, s2); |
| 15020 | return s1; |
| 15021 | } |
| 15022 | |
| 15023 | int |
| 15024 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15025 | { |
| 15026 | while (*s1 && *s2 && *s1 == *s2) |
| 15027 | s1++, s2++; |
| 15028 | if (*s1 && *s2) |
| 15029 | return (*s1 < *s2) ? -1 : +1; |
| 15030 | if (*s1) |
| 15031 | return 1; |
| 15032 | if (*s2) |
| 15033 | return -1; |
| 15034 | return 0; |
| 15035 | } |
| 15036 | |
| 15037 | int |
| 15038 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15039 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15040 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15041 | for (; n != 0; n--) { |
| 15042 | u1 = *s1; |
| 15043 | u2 = *s2; |
| 15044 | if (u1 != u2) |
| 15045 | return (u1 < u2) ? -1 : +1; |
| 15046 | if (u1 == '\0') |
| 15047 | return 0; |
| 15048 | s1++; |
| 15049 | s2++; |
| 15050 | } |
| 15051 | return 0; |
| 15052 | } |
| 15053 | |
| 15054 | Py_UNICODE* |
| 15055 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15056 | { |
| 15057 | const Py_UNICODE *p; |
| 15058 | for (p = s; *p; p++) |
| 15059 | if (*p == c) |
| 15060 | return (Py_UNICODE*)p; |
| 15061 | return NULL; |
| 15062 | } |
| 15063 | |
| 15064 | Py_UNICODE* |
| 15065 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15066 | { |
| 15067 | const Py_UNICODE *p; |
| 15068 | p = s + Py_UNICODE_strlen(s); |
| 15069 | while (p != s) { |
| 15070 | p--; |
| 15071 | if (*p == c) |
| 15072 | return (Py_UNICODE*)p; |
| 15073 | } |
| 15074 | return NULL; |
| 15075 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15076 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15077 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15078 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15079 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15080 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15081 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15082 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15083 | if (!PyUnicode_Check(unicode)) { |
| 15084 | PyErr_BadArgument(); |
| 15085 | return NULL; |
| 15086 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15087 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15088 | if (u == NULL) |
| 15089 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15090 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15091 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15092 | PyErr_NoMemory(); |
| 15093 | return NULL; |
| 15094 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15095 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15096 | size *= sizeof(Py_UNICODE); |
| 15097 | copy = PyMem_Malloc(size); |
| 15098 | if (copy == NULL) { |
| 15099 | PyErr_NoMemory(); |
| 15100 | return NULL; |
| 15101 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15102 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15103 | return copy; |
| 15104 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15105 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15106 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15107 | to the string.Formatter class implemented in Python. */ |
| 15108 | |
| 15109 | static PyMethodDef _string_methods[] = { |
| 15110 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15111 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15112 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15113 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15114 | {NULL, NULL} |
| 15115 | }; |
| 15116 | |
| 15117 | static struct PyModuleDef _string_module = { |
| 15118 | PyModuleDef_HEAD_INIT, |
| 15119 | "_string", |
| 15120 | PyDoc_STR("string helper module"), |
| 15121 | 0, |
| 15122 | _string_methods, |
| 15123 | NULL, |
| 15124 | NULL, |
| 15125 | NULL, |
| 15126 | NULL |
| 15127 | }; |
| 15128 | |
| 15129 | PyMODINIT_FUNC |
| 15130 | PyInit__string(void) |
| 15131 | { |
| 15132 | return PyModule_Create(&_string_module); |
| 15133 | } |
| 15134 | |
| 15135 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15136 | #ifdef __cplusplus |
| 15137 | } |
| 15138 | #endif |