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 | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 107 | /* Optimized version of Py_MAX() to compute the maximum character: |
| 108 | use it when your are computing the second argument of PyUnicode_New() */ |
| 109 | #define MAX_MAXCHAR(maxchar1, maxchar2) \ |
| 110 | ((maxchar1) | (maxchar2)) |
| 111 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 112 | #undef PyUnicode_READY |
| 113 | #define PyUnicode_READY(op) \ |
| 114 | (assert(_PyUnicode_CHECK(op)), \ |
| 115 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 116 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 117 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 118 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 119 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 120 | (assert(_PyUnicode_CHECK(op)), \ |
| 121 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 122 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 123 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 124 | (assert(_PyUnicode_CHECK(op)), \ |
| 125 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 126 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 127 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 128 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 129 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 130 | (assert(_PyUnicode_CHECK(op)), \ |
| 131 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 132 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 133 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 134 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 135 | /* true if the Unicode object has an allocated wstr memory block |
| 136 | (not shared with other data) */ |
| 137 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 138 | (assert(_PyUnicode_CHECK(op)), \ |
| 139 | (_PyUnicode_WSTR(op) && \ |
| 140 | (!PyUnicode_IS_READY(op) || \ |
| 141 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 142 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 143 | /* Generic helper macro to convert characters of different types. |
| 144 | from_type and to_type have to be valid type names, begin and end |
| 145 | are pointers to the source characters which should be of type |
| 146 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 147 | buffer where the result characters are written to. */ |
| 148 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 149 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 150 | to_type *_to = (to_type *) to; \ |
| 151 | const from_type *_iter = (begin); \ |
| 152 | const from_type *_end = (end); \ |
| 153 | Py_ssize_t n = (_end) - (_iter); \ |
| 154 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 155 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 156 | while (_iter < (_unrolled_end)) { \ |
| 157 | _to[0] = (to_type) _iter[0]; \ |
| 158 | _to[1] = (to_type) _iter[1]; \ |
| 159 | _to[2] = (to_type) _iter[2]; \ |
| 160 | _to[3] = (to_type) _iter[3]; \ |
| 161 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 162 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 163 | while (_iter < (_end)) \ |
| 164 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 165 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 166 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 167 | /* This dictionary holds all interned unicode strings. Note that references |
| 168 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 169 | When the interned string reaches a refcnt of 0 the string deallocation |
| 170 | function will delete the reference from this dictionary. |
| 171 | |
| 172 | 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] | 173 | 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] | 174 | */ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 175 | static PyObject *interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 176 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 177 | /* The empty Unicode object is shared to improve performance. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 178 | static PyObject *unicode_empty = NULL; |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 179 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 180 | #define _Py_INCREF_UNICODE_EMPTY() \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 181 | do { \ |
| 182 | if (unicode_empty != NULL) \ |
| 183 | Py_INCREF(unicode_empty); \ |
| 184 | else { \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 185 | unicode_empty = PyUnicode_New(0, 0); \ |
| 186 | if (unicode_empty != NULL) { \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 187 | Py_INCREF(unicode_empty); \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 188 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); \ |
| 189 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 190 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 191 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 192 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 193 | #define _Py_RETURN_UNICODE_EMPTY() \ |
| 194 | do { \ |
| 195 | _Py_INCREF_UNICODE_EMPTY(); \ |
| 196 | return unicode_empty; \ |
| 197 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 198 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 199 | /* Forward declaration */ |
| 200 | Py_LOCAL_INLINE(int) |
| 201 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch); |
| 202 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 203 | /* List of static strings. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 204 | static _Py_Identifier *static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 205 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 206 | /* Single character Unicode strings in the Latin-1 range are being |
| 207 | shared as well. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 208 | static PyObject *unicode_latin1[256] = {NULL}; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 209 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 210 | /* Fast detection of the most frequent whitespace characters */ |
| 211 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 212 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 213 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 214 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 215 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 216 | /* case 0x000C: * FORM FEED */ |
| 217 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 218 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 219 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 220 | /* case 0x001C: * FILE SEPARATOR */ |
| 221 | /* case 0x001D: * GROUP SEPARATOR */ |
| 222 | /* case 0x001E: * RECORD SEPARATOR */ |
| 223 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 224 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 225 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 226 | 1, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 230 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 231 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 232 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 233 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 234 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 235 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 236 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 237 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 238 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 241 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 242 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 243 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 244 | static int unicode_modifiable(PyObject *unicode); |
| 245 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 246 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 247 | static PyObject * |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 248 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 249 | static PyObject * |
| 250 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 251 | static PyObject * |
| 252 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 253 | |
| 254 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 255 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 256 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 257 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 258 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 259 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 260 | static void |
| 261 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 262 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 263 | PyObject *unicode, |
| 264 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 265 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 266 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 267 | /* Same for linebreaks */ |
| 268 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 269 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 270 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 271 | /* 0x000B, * LINE TABULATION */ |
| 272 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 273 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 274 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 275 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 276 | /* 0x001C, * FILE SEPARATOR */ |
| 277 | /* 0x001D, * GROUP SEPARATOR */ |
| 278 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 279 | 0, 0, 0, 0, 1, 1, 1, 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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 284 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 286 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 287 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 288 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 289 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 290 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 291 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 292 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 295 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 296 | 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] | 297 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 298 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 299 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 300 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 301 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 302 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 303 | /* This is actually an illegal character, so it should |
| 304 | not be passed to unichr. */ |
| 305 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 306 | #endif |
| 307 | } |
| 308 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 309 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 310 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 311 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 312 | { |
| 313 | PyASCIIObject *ascii; |
| 314 | unsigned int kind; |
| 315 | |
| 316 | assert(PyUnicode_Check(op)); |
| 317 | |
| 318 | ascii = (PyASCIIObject *)op; |
| 319 | kind = ascii->state.kind; |
| 320 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 321 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 322 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 323 | assert(ascii->state.ready == 1); |
| 324 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 325 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 326 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 327 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 328 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 | if (ascii->state.compact == 1) { |
| 330 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 331 | assert(kind == PyUnicode_1BYTE_KIND |
| 332 | || kind == PyUnicode_2BYTE_KIND |
| 333 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 334 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 335 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 336 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 337 | } |
| 338 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 339 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 340 | |
| 341 | data = unicode->data.any; |
| 342 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 343 | assert(ascii->length == 0); |
| 344 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 345 | assert(ascii->state.compact == 0); |
| 346 | assert(ascii->state.ascii == 0); |
| 347 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 348 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 349 | assert(ascii->wstr != NULL); |
| 350 | assert(data == NULL); |
| 351 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 352 | } |
| 353 | else { |
| 354 | assert(kind == PyUnicode_1BYTE_KIND |
| 355 | || kind == PyUnicode_2BYTE_KIND |
| 356 | || kind == PyUnicode_4BYTE_KIND); |
| 357 | assert(ascii->state.compact == 0); |
| 358 | assert(ascii->state.ready == 1); |
| 359 | assert(data != NULL); |
| 360 | if (ascii->state.ascii) { |
| 361 | assert (compact->utf8 == data); |
| 362 | assert (compact->utf8_length == ascii->length); |
| 363 | } |
| 364 | else |
| 365 | assert (compact->utf8 != data); |
| 366 | } |
| 367 | } |
| 368 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 369 | if ( |
| 370 | #if SIZEOF_WCHAR_T == 2 |
| 371 | kind == PyUnicode_2BYTE_KIND |
| 372 | #else |
| 373 | kind == PyUnicode_4BYTE_KIND |
| 374 | #endif |
| 375 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 376 | { |
| 377 | assert(ascii->wstr == data); |
| 378 | assert(compact->wstr_length == ascii->length); |
| 379 | } else |
| 380 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 381 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 382 | |
| 383 | if (compact->utf8 == NULL) |
| 384 | assert(compact->utf8_length == 0); |
| 385 | if (ascii->wstr == NULL) |
| 386 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 387 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 388 | /* check that the best kind is used */ |
| 389 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 390 | { |
| 391 | Py_ssize_t i; |
| 392 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 393 | void *data; |
| 394 | Py_UCS4 ch; |
| 395 | |
| 396 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 397 | for (i=0; i < ascii->length; i++) |
| 398 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 399 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 400 | if (ch > maxchar) |
| 401 | maxchar = ch; |
| 402 | } |
| 403 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 404 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 405 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 406 | assert(maxchar <= 255); |
| 407 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 408 | else |
| 409 | assert(maxchar < 128); |
| 410 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 411 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 412 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 413 | assert(maxchar <= 0xFFFF); |
| 414 | } |
| 415 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 416 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 417 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 418 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 419 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 420 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 421 | return 1; |
| 422 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 423 | #endif |
| 424 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 425 | static PyObject* |
| 426 | unicode_result_wchar(PyObject *unicode) |
| 427 | { |
| 428 | #ifndef Py_DEBUG |
| 429 | Py_ssize_t len; |
| 430 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 431 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 432 | if (len == 0) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 433 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 434 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (len == 1) { |
| 438 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 439 | if ((Py_UCS4)ch < 256) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 440 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 441 | Py_DECREF(unicode); |
| 442 | return latin1_char; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (_PyUnicode_Ready(unicode) < 0) { |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 447 | Py_DECREF(unicode); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 448 | return NULL; |
| 449 | } |
| 450 | #else |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 451 | assert(Py_REFCNT(unicode) == 1); |
| 452 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 453 | /* don't make the result ready in debug mode to ensure that the caller |
| 454 | makes the string ready before using it */ |
| 455 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 456 | #endif |
| 457 | return unicode; |
| 458 | } |
| 459 | |
| 460 | static PyObject* |
| 461 | unicode_result_ready(PyObject *unicode) |
| 462 | { |
| 463 | Py_ssize_t length; |
| 464 | |
| 465 | length = PyUnicode_GET_LENGTH(unicode); |
| 466 | if (length == 0) { |
| 467 | if (unicode != unicode_empty) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 468 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 469 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 470 | } |
| 471 | return unicode_empty; |
| 472 | } |
| 473 | |
| 474 | if (length == 1) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 475 | void *data = PyUnicode_DATA(unicode); |
| 476 | int kind = PyUnicode_KIND(unicode); |
| 477 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 478 | if (ch < 256) { |
| 479 | PyObject *latin1_char = unicode_latin1[ch]; |
| 480 | if (latin1_char != NULL) { |
| 481 | if (unicode != latin1_char) { |
| 482 | Py_INCREF(latin1_char); |
| 483 | Py_DECREF(unicode); |
| 484 | } |
| 485 | return latin1_char; |
| 486 | } |
| 487 | else { |
| 488 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 489 | Py_INCREF(unicode); |
| 490 | unicode_latin1[ch] = unicode; |
| 491 | return unicode; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 497 | return unicode; |
| 498 | } |
| 499 | |
| 500 | static PyObject* |
| 501 | unicode_result(PyObject *unicode) |
| 502 | { |
| 503 | assert(_PyUnicode_CHECK(unicode)); |
| 504 | if (PyUnicode_IS_READY(unicode)) |
| 505 | return unicode_result_ready(unicode); |
| 506 | else |
| 507 | return unicode_result_wchar(unicode); |
| 508 | } |
| 509 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 510 | static PyObject* |
| 511 | unicode_result_unchanged(PyObject *unicode) |
| 512 | { |
| 513 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 514 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 515 | return NULL; |
| 516 | Py_INCREF(unicode); |
| 517 | return unicode; |
| 518 | } |
| 519 | else |
| 520 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 521 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 524 | #ifdef HAVE_MBCS |
| 525 | static OSVERSIONINFOEX winver; |
| 526 | #endif |
| 527 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 528 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 529 | |
| 530 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 531 | to keep things simple, we use a single bitmask, using the least 5 |
| 532 | bits from each unicode characters as the bit index. */ |
| 533 | |
| 534 | /* the linebreak mask is set up by Unicode_Init below */ |
| 535 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 536 | #if LONG_BIT >= 128 |
| 537 | #define BLOOM_WIDTH 128 |
| 538 | #elif LONG_BIT >= 64 |
| 539 | #define BLOOM_WIDTH 64 |
| 540 | #elif LONG_BIT >= 32 |
| 541 | #define BLOOM_WIDTH 32 |
| 542 | #else |
| 543 | #error "LONG_BIT is smaller than 32" |
| 544 | #endif |
| 545 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 546 | #define BLOOM_MASK unsigned long |
| 547 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 548 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 549 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 550 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 551 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 552 | #define BLOOM_LINEBREAK(ch) \ |
| 553 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 554 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 555 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 556 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 557 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 558 | { |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 559 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN) \ |
| 560 | do { \ |
| 561 | TYPE *data = (TYPE *)PTR; \ |
| 562 | TYPE *end = data + LEN; \ |
| 563 | Py_UCS4 ch; \ |
| 564 | for (; data != end; data++) { \ |
| 565 | ch = *data; \ |
| 566 | MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ |
| 567 | } \ |
| 568 | break; \ |
| 569 | } while (0) |
| 570 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 571 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 572 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 573 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 574 | |
| 575 | mask = 0; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 576 | switch (kind) { |
| 577 | case PyUnicode_1BYTE_KIND: |
| 578 | BLOOM_UPDATE(Py_UCS1, mask, ptr, len); |
| 579 | break; |
| 580 | case PyUnicode_2BYTE_KIND: |
| 581 | BLOOM_UPDATE(Py_UCS2, mask, ptr, len); |
| 582 | break; |
| 583 | case PyUnicode_4BYTE_KIND: |
| 584 | BLOOM_UPDATE(Py_UCS4, mask, ptr, len); |
| 585 | break; |
| 586 | default: |
| 587 | assert(0); |
| 588 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 589 | return mask; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 590 | |
| 591 | #undef BLOOM_UPDATE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 594 | /* Compilation of templated routines */ |
| 595 | |
| 596 | #include "stringlib/asciilib.h" |
| 597 | #include "stringlib/fastsearch.h" |
| 598 | #include "stringlib/partition.h" |
| 599 | #include "stringlib/split.h" |
| 600 | #include "stringlib/count.h" |
| 601 | #include "stringlib/find.h" |
| 602 | #include "stringlib/find_max_char.h" |
| 603 | #include "stringlib/localeutil.h" |
| 604 | #include "stringlib/undef.h" |
| 605 | |
| 606 | #include "stringlib/ucs1lib.h" |
| 607 | #include "stringlib/fastsearch.h" |
| 608 | #include "stringlib/partition.h" |
| 609 | #include "stringlib/split.h" |
| 610 | #include "stringlib/count.h" |
| 611 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 612 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 613 | #include "stringlib/find_max_char.h" |
| 614 | #include "stringlib/localeutil.h" |
| 615 | #include "stringlib/undef.h" |
| 616 | |
| 617 | #include "stringlib/ucs2lib.h" |
| 618 | #include "stringlib/fastsearch.h" |
| 619 | #include "stringlib/partition.h" |
| 620 | #include "stringlib/split.h" |
| 621 | #include "stringlib/count.h" |
| 622 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 623 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 624 | #include "stringlib/find_max_char.h" |
| 625 | #include "stringlib/localeutil.h" |
| 626 | #include "stringlib/undef.h" |
| 627 | |
| 628 | #include "stringlib/ucs4lib.h" |
| 629 | #include "stringlib/fastsearch.h" |
| 630 | #include "stringlib/partition.h" |
| 631 | #include "stringlib/split.h" |
| 632 | #include "stringlib/count.h" |
| 633 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 634 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 635 | #include "stringlib/find_max_char.h" |
| 636 | #include "stringlib/localeutil.h" |
| 637 | #include "stringlib/undef.h" |
| 638 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 639 | #include "stringlib/unicodedefs.h" |
| 640 | #include "stringlib/fastsearch.h" |
| 641 | #include "stringlib/count.h" |
| 642 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 643 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 645 | /* --- Unicode Object ----------------------------------------------------- */ |
| 646 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 647 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 648 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 649 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 650 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 651 | Py_ssize_t size, Py_UCS4 ch, |
| 652 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 653 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 654 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 655 | |
| 656 | switch (kind) { |
| 657 | case PyUnicode_1BYTE_KIND: |
| 658 | { |
| 659 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 660 | if (ch1 == ch) |
| 661 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 662 | else |
| 663 | return -1; |
| 664 | } |
| 665 | case PyUnicode_2BYTE_KIND: |
| 666 | { |
| 667 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 668 | if (ch2 == ch) |
| 669 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 670 | else |
| 671 | return -1; |
| 672 | } |
| 673 | case PyUnicode_4BYTE_KIND: |
| 674 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 675 | default: |
| 676 | assert(0); |
| 677 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 678 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 681 | #ifdef Py_DEBUG |
| 682 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 683 | earlier. |
| 684 | |
| 685 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 686 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 687 | invalid character in Unicode 6.0. */ |
| 688 | static void |
| 689 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 690 | { |
| 691 | int kind = PyUnicode_KIND(unicode); |
| 692 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 693 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 694 | if (length <= old_length) |
| 695 | return; |
| 696 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 697 | } |
| 698 | #endif |
| 699 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 700 | static PyObject* |
| 701 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 702 | { |
| 703 | Py_ssize_t char_size; |
| 704 | Py_ssize_t struct_size; |
| 705 | Py_ssize_t new_size; |
| 706 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 707 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 708 | #ifdef Py_DEBUG |
| 709 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 710 | #endif |
| 711 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 712 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 713 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 714 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 715 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 716 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 717 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 718 | struct_size = sizeof(PyASCIIObject); |
| 719 | else |
| 720 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 721 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 722 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 723 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 724 | PyErr_NoMemory(); |
| 725 | return NULL; |
| 726 | } |
| 727 | new_size = (struct_size + (length + 1) * char_size); |
| 728 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 729 | _Py_DEC_REFTOTAL; |
| 730 | _Py_ForgetReference(unicode); |
| 731 | |
| 732 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 733 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 734 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 735 | PyErr_NoMemory(); |
| 736 | return NULL; |
| 737 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 738 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 739 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 740 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 741 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 742 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 743 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 744 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 745 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 746 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 747 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 748 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 749 | _PyUnicode_WSTR(unicode) = NULL; |
| 750 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 751 | #ifdef Py_DEBUG |
| 752 | unicode_fill_invalid(unicode, old_length); |
| 753 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 754 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 755 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 756 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 757 | return unicode; |
| 758 | } |
| 759 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 760 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 761 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 762 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 763 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 764 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 765 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 766 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 767 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 768 | if (PyUnicode_IS_READY(unicode)) { |
| 769 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 770 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 771 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 772 | #ifdef Py_DEBUG |
| 773 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 774 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 775 | |
| 776 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 777 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 778 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 779 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 780 | |
| 781 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 782 | PyErr_NoMemory(); |
| 783 | return -1; |
| 784 | } |
| 785 | new_size = (length + 1) * char_size; |
| 786 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 787 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 788 | { |
| 789 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 790 | _PyUnicode_UTF8(unicode) = NULL; |
| 791 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 792 | } |
| 793 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 794 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 795 | if (data == NULL) { |
| 796 | PyErr_NoMemory(); |
| 797 | return -1; |
| 798 | } |
| 799 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 800 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 801 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 802 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 803 | } |
| 804 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 805 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 806 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 807 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 808 | _PyUnicode_LENGTH(unicode) = length; |
| 809 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 810 | #ifdef Py_DEBUG |
| 811 | unicode_fill_invalid(unicode, old_length); |
| 812 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 813 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 814 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 815 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 816 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 817 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 818 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 819 | |
| 820 | /* check for integer overflow */ |
| 821 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 822 | PyErr_NoMemory(); |
| 823 | return -1; |
| 824 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 825 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 826 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 827 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 828 | if (!wstr) { |
| 829 | PyErr_NoMemory(); |
| 830 | return -1; |
| 831 | } |
| 832 | _PyUnicode_WSTR(unicode) = wstr; |
| 833 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 834 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 835 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 836 | return 0; |
| 837 | } |
| 838 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 839 | static PyObject* |
| 840 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 841 | { |
| 842 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 843 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 844 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 845 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 846 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 847 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 848 | |
| 849 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 850 | if (copy == NULL) |
| 851 | return NULL; |
| 852 | |
| 853 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 854 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 855 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 856 | } |
| 857 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 858 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 859 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 860 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 861 | if (w == NULL) |
| 862 | return NULL; |
| 863 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 864 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 865 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 866 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 867 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 871 | /* 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] | 872 | Ux0000 terminated; some code (e.g. new_identifier) |
| 873 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 874 | |
| 875 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 876 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 877 | |
| 878 | */ |
| 879 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 880 | static PyUnicodeObject * |
| 881 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 882 | { |
| 883 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 884 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 885 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 886 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 887 | if (length == 0 && unicode_empty != NULL) { |
| 888 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 889 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 892 | /* Ensure we won't overflow the size. */ |
| 893 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 894 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 895 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 896 | if (length < 0) { |
| 897 | PyErr_SetString(PyExc_SystemError, |
| 898 | "Negative size passed to _PyUnicode_New"); |
| 899 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 902 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 903 | if (unicode == NULL) |
| 904 | return NULL; |
| 905 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 906 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 907 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 908 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 909 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 910 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 911 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 913 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 914 | * the caller fails before initializing str -- unicode_resize() |
| 915 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 916 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 917 | * We don't want unicode_resize to read uninitialized memory in |
| 918 | * that case. |
| 919 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 920 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 921 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 922 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 923 | _PyUnicode_HASH(unicode) = -1; |
| 924 | _PyUnicode_STATE(unicode).interned = 0; |
| 925 | _PyUnicode_STATE(unicode).kind = 0; |
| 926 | _PyUnicode_STATE(unicode).compact = 0; |
| 927 | _PyUnicode_STATE(unicode).ready = 0; |
| 928 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 929 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 930 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 931 | _PyUnicode_UTF8(unicode) = NULL; |
| 932 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 933 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 934 | return unicode; |
| 935 | } |
| 936 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 937 | static const char* |
| 938 | unicode_kind_name(PyObject *unicode) |
| 939 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 940 | /* don't check consistency: unicode_kind_name() is called from |
| 941 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 942 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 943 | { |
| 944 | if (!PyUnicode_IS_READY(unicode)) |
| 945 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 946 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 947 | { |
| 948 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 949 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 950 | return "legacy ascii"; |
| 951 | else |
| 952 | return "legacy latin1"; |
| 953 | case PyUnicode_2BYTE_KIND: |
| 954 | return "legacy UCS2"; |
| 955 | case PyUnicode_4BYTE_KIND: |
| 956 | return "legacy UCS4"; |
| 957 | default: |
| 958 | return "<legacy invalid kind>"; |
| 959 | } |
| 960 | } |
| 961 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 962 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 963 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 964 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 965 | return "ascii"; |
| 966 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 967 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 968 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 969 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 970 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 971 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 972 | default: |
| 973 | return "<invalid compact kind>"; |
| 974 | } |
| 975 | } |
| 976 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 977 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 978 | /* Functions wrapping macros for use in debugger */ |
| 979 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 980 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | void *_PyUnicode_compact_data(void *unicode) { |
| 984 | return _PyUnicode_COMPACT_DATA(unicode); |
| 985 | } |
| 986 | void *_PyUnicode_data(void *unicode){ |
| 987 | printf("obj %p\n", unicode); |
| 988 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 989 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 990 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 991 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 992 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 993 | return PyUnicode_DATA(unicode); |
| 994 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 995 | |
| 996 | void |
| 997 | _PyUnicode_Dump(PyObject *op) |
| 998 | { |
| 999 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1000 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 1001 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 1002 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1003 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1004 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1005 | { |
| 1006 | if (ascii->state.ascii) |
| 1007 | data = (ascii + 1); |
| 1008 | else |
| 1009 | data = (compact + 1); |
| 1010 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1011 | else |
| 1012 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1013 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 1014 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1015 | if (ascii->wstr == data) |
| 1016 | printf("shared "); |
| 1017 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1018 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1019 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1020 | printf(" (%zu), ", compact->wstr_length); |
| 1021 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1022 | printf("shared "); |
| 1023 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1024 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1025 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1026 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1027 | #endif |
| 1028 | |
| 1029 | PyObject * |
| 1030 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1031 | { |
| 1032 | PyObject *obj; |
| 1033 | PyCompactUnicodeObject *unicode; |
| 1034 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1035 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1036 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1037 | Py_ssize_t char_size; |
| 1038 | Py_ssize_t struct_size; |
| 1039 | |
| 1040 | /* Optimization for empty strings */ |
| 1041 | if (size == 0 && unicode_empty != NULL) { |
| 1042 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1043 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1046 | is_ascii = 0; |
| 1047 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1049 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1050 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1051 | char_size = 1; |
| 1052 | is_ascii = 1; |
| 1053 | struct_size = sizeof(PyASCIIObject); |
| 1054 | } |
| 1055 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1056 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1057 | char_size = 1; |
| 1058 | } |
| 1059 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1060 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1061 | char_size = 2; |
| 1062 | if (sizeof(wchar_t) == 2) |
| 1063 | is_sharing = 1; |
| 1064 | } |
| 1065 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1066 | if (maxchar > MAX_UNICODE) { |
| 1067 | PyErr_SetString(PyExc_SystemError, |
| 1068 | "invalid maximum character passed to PyUnicode_New"); |
| 1069 | return NULL; |
| 1070 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1071 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1072 | char_size = 4; |
| 1073 | if (sizeof(wchar_t) == 4) |
| 1074 | is_sharing = 1; |
| 1075 | } |
| 1076 | |
| 1077 | /* Ensure we won't overflow the size. */ |
| 1078 | if (size < 0) { |
| 1079 | PyErr_SetString(PyExc_SystemError, |
| 1080 | "Negative size passed to PyUnicode_New"); |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1084 | return PyErr_NoMemory(); |
| 1085 | |
| 1086 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1087 | * PyObject_New() so we are able to allocate space for the object and |
| 1088 | * it's data buffer. |
| 1089 | */ |
| 1090 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1091 | if (obj == NULL) |
| 1092 | return PyErr_NoMemory(); |
| 1093 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1094 | if (obj == NULL) |
| 1095 | return NULL; |
| 1096 | |
| 1097 | unicode = (PyCompactUnicodeObject *)obj; |
| 1098 | if (is_ascii) |
| 1099 | data = ((PyASCIIObject*)obj) + 1; |
| 1100 | else |
| 1101 | data = unicode + 1; |
| 1102 | _PyUnicode_LENGTH(unicode) = size; |
| 1103 | _PyUnicode_HASH(unicode) = -1; |
| 1104 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1105 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1106 | _PyUnicode_STATE(unicode).compact = 1; |
| 1107 | _PyUnicode_STATE(unicode).ready = 1; |
| 1108 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1109 | if (is_ascii) { |
| 1110 | ((char*)data)[size] = 0; |
| 1111 | _PyUnicode_WSTR(unicode) = NULL; |
| 1112 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1113 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1114 | ((char*)data)[size] = 0; |
| 1115 | _PyUnicode_WSTR(unicode) = NULL; |
| 1116 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 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 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | else { |
| 1121 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1122 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1123 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1124 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1125 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1126 | ((Py_UCS4*)data)[size] = 0; |
| 1127 | if (is_sharing) { |
| 1128 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1129 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1130 | } |
| 1131 | else { |
| 1132 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1133 | _PyUnicode_WSTR(unicode) = NULL; |
| 1134 | } |
| 1135 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1136 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1137 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1138 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1139 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1140 | return obj; |
| 1141 | } |
| 1142 | |
| 1143 | #if SIZEOF_WCHAR_T == 2 |
| 1144 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1145 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1146 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | |
| 1148 | This function assumes that unicode can hold one more code point than wstr |
| 1149 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1150 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1151 | 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] | 1152 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1153 | { |
| 1154 | const wchar_t *iter; |
| 1155 | Py_UCS4 *ucs4_out; |
| 1156 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1157 | assert(unicode != NULL); |
| 1158 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1159 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1160 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1161 | |
| 1162 | for (iter = begin; iter < end; ) { |
| 1163 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1164 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1165 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1166 | && (iter+1) < end |
| 1167 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1168 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1169 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1170 | iter += 2; |
| 1171 | } |
| 1172 | else { |
| 1173 | *ucs4_out++ = *iter; |
| 1174 | iter++; |
| 1175 | } |
| 1176 | } |
| 1177 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1178 | _PyUnicode_GET_LENGTH(unicode))); |
| 1179 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1180 | } |
| 1181 | #endif |
| 1182 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1183 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1184 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1185 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1186 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1187 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1188 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1189 | return -1; |
| 1190 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
| 1193 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1194 | static int |
| 1195 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1196 | PyObject *from, Py_ssize_t from_start, |
| 1197 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1198 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1199 | unsigned int from_kind, to_kind; |
| 1200 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1201 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1202 | assert(0 <= how_many); |
| 1203 | assert(0 <= from_start); |
| 1204 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1205 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1206 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1207 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1208 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1209 | assert(PyUnicode_Check(to)); |
| 1210 | assert(PyUnicode_IS_READY(to)); |
| 1211 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1212 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1213 | if (how_many == 0) |
| 1214 | return 0; |
| 1215 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1217 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1218 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1219 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1220 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1221 | #ifdef Py_DEBUG |
| 1222 | if (!check_maxchar |
| 1223 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1224 | { |
| 1225 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1226 | Py_UCS4 ch; |
| 1227 | Py_ssize_t i; |
| 1228 | for (i=0; i < how_many; i++) { |
| 1229 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1230 | assert(ch <= to_maxchar); |
| 1231 | } |
| 1232 | } |
| 1233 | #endif |
| 1234 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1235 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1236 | if (check_maxchar |
| 1237 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1238 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1239 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1240 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1241 | Py_UCS4 max_char; |
| 1242 | max_char = ucs1lib_find_max_char(from_data, |
| 1243 | (Py_UCS1*)from_data + how_many); |
| 1244 | if (max_char >= 128) |
| 1245 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1246 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1247 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1248 | (char*)from_data + from_kind * from_start, |
| 1249 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1250 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1251 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1252 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1253 | { |
| 1254 | _PyUnicode_CONVERT_BYTES( |
| 1255 | Py_UCS1, Py_UCS2, |
| 1256 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1257 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1258 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1259 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1260 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1261 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1262 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1263 | { |
| 1264 | _PyUnicode_CONVERT_BYTES( |
| 1265 | Py_UCS1, Py_UCS4, |
| 1266 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1267 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1268 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1269 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1270 | } |
| 1271 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1272 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1273 | { |
| 1274 | _PyUnicode_CONVERT_BYTES( |
| 1275 | Py_UCS2, Py_UCS4, |
| 1276 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1277 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1278 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1279 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1280 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1281 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1282 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1283 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1284 | if (!check_maxchar) { |
| 1285 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1286 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1287 | { |
| 1288 | _PyUnicode_CONVERT_BYTES( |
| 1289 | Py_UCS2, Py_UCS1, |
| 1290 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1291 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1292 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1293 | ); |
| 1294 | } |
| 1295 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1296 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1297 | { |
| 1298 | _PyUnicode_CONVERT_BYTES( |
| 1299 | Py_UCS4, Py_UCS1, |
| 1300 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1301 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1302 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1303 | ); |
| 1304 | } |
| 1305 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1306 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1307 | { |
| 1308 | _PyUnicode_CONVERT_BYTES( |
| 1309 | Py_UCS4, Py_UCS2, |
| 1310 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1311 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1312 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1313 | ); |
| 1314 | } |
| 1315 | else { |
| 1316 | assert(0); |
| 1317 | return -1; |
| 1318 | } |
| 1319 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1320 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1321 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1322 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1323 | Py_ssize_t i; |
| 1324 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1325 | for (i=0; i < how_many; i++) { |
| 1326 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1327 | if (ch > to_maxchar) |
| 1328 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1329 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1330 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1331 | } |
| 1332 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1333 | return 0; |
| 1334 | } |
| 1335 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1336 | void |
| 1337 | _PyUnicode_FastCopyCharacters( |
| 1338 | PyObject *to, Py_ssize_t to_start, |
| 1339 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1340 | { |
| 1341 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1342 | } |
| 1343 | |
| 1344 | Py_ssize_t |
| 1345 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1346 | PyObject *from, Py_ssize_t from_start, |
| 1347 | Py_ssize_t how_many) |
| 1348 | { |
| 1349 | int err; |
| 1350 | |
| 1351 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1352 | PyErr_BadInternalCall(); |
| 1353 | return -1; |
| 1354 | } |
| 1355 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1356 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1357 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1358 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1359 | return -1; |
| 1360 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1361 | if (from_start < 0) { |
| 1362 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1363 | return -1; |
| 1364 | } |
| 1365 | if (to_start < 0) { |
| 1366 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1367 | return -1; |
| 1368 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1369 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1370 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1371 | PyErr_Format(PyExc_SystemError, |
| 1372 | "Cannot write %zi characters at %zi " |
| 1373 | "in a string of %zi characters", |
| 1374 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1375 | return -1; |
| 1376 | } |
| 1377 | |
| 1378 | if (how_many == 0) |
| 1379 | return 0; |
| 1380 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1381 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1382 | return -1; |
| 1383 | |
| 1384 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1385 | if (err) { |
| 1386 | PyErr_Format(PyExc_SystemError, |
| 1387 | "Cannot copy %s characters " |
| 1388 | "into a string of %s characters", |
| 1389 | unicode_kind_name(from), |
| 1390 | unicode_kind_name(to)); |
| 1391 | return -1; |
| 1392 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1393 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1396 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1397 | correct string length can be computed before converting a string to UCS4. |
| 1398 | This function counts single surrogates as a character and not as a pair. |
| 1399 | |
| 1400 | Return 0 on success, or -1 on error. */ |
| 1401 | static int |
| 1402 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1403 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1404 | { |
| 1405 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1406 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1407 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1408 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1409 | *num_surrogates = 0; |
| 1410 | *maxchar = 0; |
| 1411 | |
| 1412 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1413 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1414 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1415 | && (iter+1) < end |
| 1416 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1417 | { |
| 1418 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1419 | ++(*num_surrogates); |
| 1420 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1421 | } |
| 1422 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1423 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1424 | { |
| 1425 | ch = *iter; |
| 1426 | iter++; |
| 1427 | } |
| 1428 | if (ch > *maxchar) { |
| 1429 | *maxchar = ch; |
| 1430 | if (*maxchar > MAX_UNICODE) { |
| 1431 | PyErr_Format(PyExc_ValueError, |
| 1432 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1433 | ch); |
| 1434 | return -1; |
| 1435 | } |
| 1436 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1437 | } |
| 1438 | return 0; |
| 1439 | } |
| 1440 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1441 | int |
| 1442 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1443 | { |
| 1444 | wchar_t *end; |
| 1445 | Py_UCS4 maxchar = 0; |
| 1446 | Py_ssize_t num_surrogates; |
| 1447 | #if SIZEOF_WCHAR_T == 2 |
| 1448 | Py_ssize_t length_wo_surrogates; |
| 1449 | #endif |
| 1450 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1451 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1452 | strings were created using _PyObject_New() and where no canonical |
| 1453 | representation (the str field) has been set yet aka strings |
| 1454 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1455 | assert(_PyUnicode_CHECK(unicode)); |
| 1456 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1457 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1458 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1459 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1460 | /* Actually, it should neither be interned nor be anything else: */ |
| 1461 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1462 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1464 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1465 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1466 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | |
| 1468 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1469 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1470 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1471 | PyErr_NoMemory(); |
| 1472 | return -1; |
| 1473 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1474 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1475 | _PyUnicode_WSTR(unicode), end, |
| 1476 | PyUnicode_1BYTE_DATA(unicode)); |
| 1477 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1478 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1479 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1480 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1481 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1482 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1483 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1484 | } |
| 1485 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1486 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1487 | _PyUnicode_UTF8(unicode) = NULL; |
| 1488 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1489 | } |
| 1490 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1491 | _PyUnicode_WSTR(unicode) = NULL; |
| 1492 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1493 | } |
| 1494 | /* In this case we might have to convert down from 4-byte native |
| 1495 | wchar_t to 2-byte unicode. */ |
| 1496 | else if (maxchar < 65536) { |
| 1497 | assert(num_surrogates == 0 && |
| 1498 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1499 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1500 | #if SIZEOF_WCHAR_T == 2 |
| 1501 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1502 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1503 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1504 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1505 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1506 | _PyUnicode_UTF8(unicode) = NULL; |
| 1507 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1508 | #else |
| 1509 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1510 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1511 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1512 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1513 | PyErr_NoMemory(); |
| 1514 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1515 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1516 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1517 | _PyUnicode_WSTR(unicode), end, |
| 1518 | PyUnicode_2BYTE_DATA(unicode)); |
| 1519 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1520 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1521 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1522 | _PyUnicode_UTF8(unicode) = NULL; |
| 1523 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1524 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1525 | _PyUnicode_WSTR(unicode) = NULL; |
| 1526 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1527 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1528 | } |
| 1529 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1530 | else { |
| 1531 | #if SIZEOF_WCHAR_T == 2 |
| 1532 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1533 | new normalized 4-byte version. */ |
| 1534 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1535 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1536 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1537 | PyErr_NoMemory(); |
| 1538 | return -1; |
| 1539 | } |
| 1540 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1541 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1542 | _PyUnicode_UTF8(unicode) = NULL; |
| 1543 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1544 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1545 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1546 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1547 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1548 | _PyUnicode_WSTR(unicode) = NULL; |
| 1549 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1550 | #else |
| 1551 | assert(num_surrogates == 0); |
| 1552 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1553 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1554 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1555 | _PyUnicode_UTF8(unicode) = NULL; |
| 1556 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1557 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1558 | #endif |
| 1559 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1560 | } |
| 1561 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1562 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1563 | return 0; |
| 1564 | } |
| 1565 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1566 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1567 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1568 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1569 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1570 | case SSTATE_NOT_INTERNED: |
| 1571 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1572 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1573 | case SSTATE_INTERNED_MORTAL: |
| 1574 | /* revive dead object temporarily for DelItem */ |
| 1575 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1576 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1577 | Py_FatalError( |
| 1578 | "deletion of interned string failed"); |
| 1579 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1580 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1581 | case SSTATE_INTERNED_IMMORTAL: |
| 1582 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1583 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1584 | default: |
| 1585 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1588 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1589 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1590 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1591 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1592 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1593 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1594 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1595 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1598 | #ifdef Py_DEBUG |
| 1599 | static int |
| 1600 | unicode_is_singleton(PyObject *unicode) |
| 1601 | { |
| 1602 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1603 | if (unicode == unicode_empty) |
| 1604 | return 1; |
| 1605 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1606 | { |
| 1607 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1608 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1609 | return 1; |
| 1610 | } |
| 1611 | return 0; |
| 1612 | } |
| 1613 | #endif |
| 1614 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1615 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1616 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1617 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1618 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1619 | if (Py_REFCNT(unicode) != 1) |
| 1620 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1621 | if (_PyUnicode_HASH(unicode) != -1) |
| 1622 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1623 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1624 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1625 | if (!PyUnicode_CheckExact(unicode)) |
| 1626 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1627 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1628 | /* singleton refcount is greater than 1 */ |
| 1629 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1630 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1631 | return 1; |
| 1632 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1633 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1634 | static int |
| 1635 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1636 | { |
| 1637 | PyObject *unicode; |
| 1638 | Py_ssize_t old_length; |
| 1639 | |
| 1640 | assert(p_unicode != NULL); |
| 1641 | unicode = *p_unicode; |
| 1642 | |
| 1643 | assert(unicode != NULL); |
| 1644 | assert(PyUnicode_Check(unicode)); |
| 1645 | assert(0 <= length); |
| 1646 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1647 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1648 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1649 | else |
| 1650 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1651 | if (old_length == length) |
| 1652 | return 0; |
| 1653 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1654 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1655 | _Py_INCREF_UNICODE_EMPTY(); |
| 1656 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1657 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1658 | Py_DECREF(*p_unicode); |
| 1659 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1660 | return 0; |
| 1661 | } |
| 1662 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1663 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1664 | PyObject *copy = resize_copy(unicode, length); |
| 1665 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1666 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1667 | Py_DECREF(*p_unicode); |
| 1668 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1669 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1672 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1673 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1674 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1675 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1676 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1677 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1678 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1679 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1682 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1683 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1684 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1685 | PyObject *unicode; |
| 1686 | if (p_unicode == NULL) { |
| 1687 | PyErr_BadInternalCall(); |
| 1688 | return -1; |
| 1689 | } |
| 1690 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1691 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1692 | { |
| 1693 | PyErr_BadInternalCall(); |
| 1694 | return -1; |
| 1695 | } |
| 1696 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1697 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1698 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1699 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1700 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1701 | WARNING: The function doesn't copy the terminating null character and |
| 1702 | doesn't check the maximum character (may write a latin1 character in an |
| 1703 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1704 | static void |
| 1705 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1706 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1707 | { |
| 1708 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1709 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1710 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1711 | |
| 1712 | switch (kind) { |
| 1713 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1714 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1715 | #ifdef Py_DEBUG |
| 1716 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1717 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1718 | (const Py_UCS1*)str, |
| 1719 | (const Py_UCS1*)str + len); |
| 1720 | assert(maxchar < 128); |
| 1721 | } |
| 1722 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1723 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1724 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1725 | } |
| 1726 | case PyUnicode_2BYTE_KIND: { |
| 1727 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1728 | Py_UCS2 *ucs2 = start; |
| 1729 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1730 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1731 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1732 | *ucs2 = (Py_UCS2)*str; |
| 1733 | |
| 1734 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1735 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1736 | } |
| 1737 | default: { |
| 1738 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1739 | Py_UCS4 *ucs4 = start; |
| 1740 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1741 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1742 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1743 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1744 | *ucs4 = (Py_UCS4)*str; |
| 1745 | |
| 1746 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1752 | static PyObject* |
| 1753 | get_latin1_char(unsigned char ch) |
| 1754 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1755 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1756 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1757 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | if (!unicode) |
| 1759 | return NULL; |
| 1760 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1761 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | unicode_latin1[ch] = unicode; |
| 1763 | } |
| 1764 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1765 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1766 | } |
| 1767 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1768 | PyObject * |
| 1769 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1770 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1771 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1772 | Py_UCS4 maxchar = 0; |
| 1773 | Py_ssize_t num_surrogates; |
| 1774 | |
| 1775 | if (u == NULL) |
| 1776 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1777 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1778 | /* If the Unicode data is known at construction time, we can apply |
| 1779 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1781 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1782 | if (size == 0) |
| 1783 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1784 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1785 | /* Single character Unicode objects in the Latin-1 range are |
| 1786 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1787 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1788 | return get_latin1_char((unsigned char)*u); |
| 1789 | |
| 1790 | /* If not empty and not single character, copy the Unicode data |
| 1791 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1792 | if (find_maxchar_surrogates(u, u + size, |
| 1793 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1794 | return NULL; |
| 1795 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1796 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1797 | if (!unicode) |
| 1798 | return NULL; |
| 1799 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1800 | switch (PyUnicode_KIND(unicode)) { |
| 1801 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1802 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1803 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1804 | break; |
| 1805 | case PyUnicode_2BYTE_KIND: |
| 1806 | #if Py_UNICODE_SIZE == 2 |
| 1807 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1808 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1809 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1810 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1811 | #endif |
| 1812 | break; |
| 1813 | case PyUnicode_4BYTE_KIND: |
| 1814 | #if SIZEOF_WCHAR_T == 2 |
| 1815 | /* This is the only case which has to process surrogates, thus |
| 1816 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1817 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1818 | #else |
| 1819 | assert(num_surrogates == 0); |
| 1820 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1821 | #endif |
| 1822 | break; |
| 1823 | default: |
| 1824 | assert(0 && "Impossible state"); |
| 1825 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1826 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1827 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1830 | PyObject * |
| 1831 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1832 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1833 | if (size < 0) { |
| 1834 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1835 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1836 | return NULL; |
| 1837 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1838 | if (u != NULL) |
| 1839 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1840 | else |
| 1841 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1844 | PyObject * |
| 1845 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1846 | { |
| 1847 | size_t size = strlen(u); |
| 1848 | if (size > PY_SSIZE_T_MAX) { |
| 1849 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1850 | return NULL; |
| 1851 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1852 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1855 | PyObject * |
| 1856 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1857 | { |
| 1858 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1859 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1860 | strlen(id->string), |
| 1861 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1862 | if (!id->object) |
| 1863 | return NULL; |
| 1864 | PyUnicode_InternInPlace(&id->object); |
| 1865 | assert(!id->next); |
| 1866 | id->next = static_strings; |
| 1867 | static_strings = id; |
| 1868 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1869 | return id->object; |
| 1870 | } |
| 1871 | |
| 1872 | void |
| 1873 | _PyUnicode_ClearStaticStrings() |
| 1874 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1875 | _Py_Identifier *tmp, *s = static_strings; |
| 1876 | while (s) { |
| 1877 | Py_DECREF(s->object); |
| 1878 | s->object = NULL; |
| 1879 | tmp = s->next; |
| 1880 | s->next = NULL; |
| 1881 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1882 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1883 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1884 | } |
| 1885 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1886 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1887 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1888 | PyObject* |
| 1889 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1890 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1891 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1892 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1893 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1894 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1895 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1896 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1897 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1898 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1899 | unicode = PyUnicode_New(size, 127); |
| 1900 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1901 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1902 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1903 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1904 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1907 | static Py_UCS4 |
| 1908 | kind_maxchar_limit(unsigned int kind) |
| 1909 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1910 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1911 | case PyUnicode_1BYTE_KIND: |
| 1912 | return 0x80; |
| 1913 | case PyUnicode_2BYTE_KIND: |
| 1914 | return 0x100; |
| 1915 | case PyUnicode_4BYTE_KIND: |
| 1916 | return 0x10000; |
| 1917 | default: |
| 1918 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1919 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1923 | Py_LOCAL_INLINE(Py_UCS4) |
| 1924 | align_maxchar(Py_UCS4 maxchar) |
| 1925 | { |
| 1926 | if (maxchar <= 127) |
| 1927 | return 127; |
| 1928 | else if (maxchar <= 255) |
| 1929 | return 255; |
| 1930 | else if (maxchar <= 65535) |
| 1931 | return 65535; |
| 1932 | else |
| 1933 | return MAX_UNICODE; |
| 1934 | } |
| 1935 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1936 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1937 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1938 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1939 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1940 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1941 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1942 | if (size == 0) |
| 1943 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1944 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1945 | if (size == 1) |
| 1946 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1947 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1948 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1949 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1950 | if (!res) |
| 1951 | return NULL; |
| 1952 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1953 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1954 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1957 | static PyObject* |
| 1958 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1959 | { |
| 1960 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1961 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1962 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1963 | if (size == 0) |
| 1964 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1965 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1966 | if (size == 1) { |
| 1967 | Py_UCS4 ch = u[0]; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1968 | int kind; |
| 1969 | void *data; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1970 | if (ch < 256) |
| 1971 | return get_latin1_char((unsigned char)ch); |
| 1972 | |
| 1973 | res = PyUnicode_New(1, ch); |
| 1974 | if (res == NULL) |
| 1975 | return NULL; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 1976 | kind = PyUnicode_KIND(res); |
| 1977 | data = PyUnicode_DATA(res); |
| 1978 | PyUnicode_WRITE(kind, data, 0, ch); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1979 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1980 | return res; |
| 1981 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1982 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1983 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1984 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1985 | if (!res) |
| 1986 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1987 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1988 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1989 | else { |
| 1990 | _PyUnicode_CONVERT_BYTES( |
| 1991 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1992 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1993 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1994 | return res; |
| 1995 | } |
| 1996 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1997 | static PyObject* |
| 1998 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | { |
| 2000 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2001 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2002 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2003 | if (size == 0) |
| 2004 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2005 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2006 | if (size == 1) { |
| 2007 | Py_UCS4 ch = u[0]; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2008 | int kind; |
| 2009 | void *data; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2010 | if (ch < 256) |
| 2011 | return get_latin1_char((unsigned char)ch); |
| 2012 | |
| 2013 | res = PyUnicode_New(1, ch); |
| 2014 | if (res == NULL) |
| 2015 | return NULL; |
Victor Stinner | f50a4e9 | 2013-04-09 22:38:52 +0200 | [diff] [blame] | 2016 | kind = PyUnicode_KIND(res); |
| 2017 | data = PyUnicode_DATA(res); |
| 2018 | PyUnicode_WRITE(kind, data, 0, ch); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 2019 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 2020 | return res; |
| 2021 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2022 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2023 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2024 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2025 | if (!res) |
| 2026 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2027 | if (max_char < 256) |
| 2028 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2029 | PyUnicode_1BYTE_DATA(res)); |
| 2030 | else if (max_char < 0x10000) |
| 2031 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2032 | PyUnicode_2BYTE_DATA(res)); |
| 2033 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2034 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2035 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2036 | return res; |
| 2037 | } |
| 2038 | |
| 2039 | PyObject* |
| 2040 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2041 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2042 | if (size < 0) { |
| 2043 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2044 | return NULL; |
| 2045 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2046 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2047 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2048 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2049 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2050 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2051 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2052 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2053 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2054 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2055 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2056 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2057 | } |
| 2058 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2059 | Py_UCS4 |
| 2060 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2061 | { |
| 2062 | enum PyUnicode_Kind kind; |
| 2063 | void *startptr, *endptr; |
| 2064 | |
| 2065 | assert(PyUnicode_IS_READY(unicode)); |
| 2066 | assert(0 <= start); |
| 2067 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2068 | assert(start <= end); |
| 2069 | |
| 2070 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2071 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2072 | |
| 2073 | if (start == end) |
| 2074 | return 127; |
| 2075 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2076 | if (PyUnicode_IS_ASCII(unicode)) |
| 2077 | return 127; |
| 2078 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2079 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2080 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2081 | endptr = (char *)startptr + end * kind; |
| 2082 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2083 | switch(kind) { |
| 2084 | case PyUnicode_1BYTE_KIND: |
| 2085 | return ucs1lib_find_max_char(startptr, endptr); |
| 2086 | case PyUnicode_2BYTE_KIND: |
| 2087 | return ucs2lib_find_max_char(startptr, endptr); |
| 2088 | case PyUnicode_4BYTE_KIND: |
| 2089 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2090 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2091 | assert(0); |
| 2092 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2096 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2097 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2098 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2099 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2100 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2101 | { |
| 2102 | PyObject *unicode, *copy; |
| 2103 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2104 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2105 | unsigned int kind; |
| 2106 | |
| 2107 | assert(p_unicode != NULL); |
| 2108 | unicode = *p_unicode; |
| 2109 | assert(PyUnicode_IS_READY(unicode)); |
| 2110 | if (PyUnicode_IS_ASCII(unicode)) |
| 2111 | return; |
| 2112 | |
| 2113 | len = PyUnicode_GET_LENGTH(unicode); |
| 2114 | kind = PyUnicode_KIND(unicode); |
| 2115 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2116 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2117 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2118 | if (max_char >= 128) |
| 2119 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2120 | } |
| 2121 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2122 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2123 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2124 | if (max_char >= 256) |
| 2125 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2126 | } |
| 2127 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2128 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2129 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2130 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2131 | if (max_char >= 0x10000) |
| 2132 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2133 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2134 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2135 | if (copy != NULL) |
| 2136 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2137 | Py_DECREF(unicode); |
| 2138 | *p_unicode = copy; |
| 2139 | } |
| 2140 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2141 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2142 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2143 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2144 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2145 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2146 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2147 | if (!PyUnicode_Check(unicode)) { |
| 2148 | PyErr_BadInternalCall(); |
| 2149 | return NULL; |
| 2150 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2151 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2152 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2153 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2154 | length = PyUnicode_GET_LENGTH(unicode); |
| 2155 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2156 | if (!copy) |
| 2157 | return NULL; |
| 2158 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2159 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2160 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2161 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2162 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2163 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2164 | } |
| 2165 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2166 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2167 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2168 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2169 | |
| 2170 | void* |
| 2171 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2172 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2173 | Py_ssize_t len; |
| 2174 | void *result; |
| 2175 | unsigned int skind; |
| 2176 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2177 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2178 | return NULL; |
| 2179 | |
| 2180 | len = PyUnicode_GET_LENGTH(s); |
| 2181 | skind = PyUnicode_KIND(s); |
| 2182 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2183 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2184 | return NULL; |
| 2185 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2186 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2187 | case PyUnicode_2BYTE_KIND: |
| 2188 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2189 | if (!result) |
| 2190 | return PyErr_NoMemory(); |
| 2191 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2192 | _PyUnicode_CONVERT_BYTES( |
| 2193 | Py_UCS1, Py_UCS2, |
| 2194 | PyUnicode_1BYTE_DATA(s), |
| 2195 | PyUnicode_1BYTE_DATA(s) + len, |
| 2196 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2197 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2198 | case PyUnicode_4BYTE_KIND: |
| 2199 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2200 | if (!result) |
| 2201 | return PyErr_NoMemory(); |
| 2202 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2203 | _PyUnicode_CONVERT_BYTES( |
| 2204 | Py_UCS2, Py_UCS4, |
| 2205 | PyUnicode_2BYTE_DATA(s), |
| 2206 | PyUnicode_2BYTE_DATA(s) + len, |
| 2207 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2208 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2209 | else { |
| 2210 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2211 | _PyUnicode_CONVERT_BYTES( |
| 2212 | Py_UCS1, Py_UCS4, |
| 2213 | PyUnicode_1BYTE_DATA(s), |
| 2214 | PyUnicode_1BYTE_DATA(s) + len, |
| 2215 | result); |
| 2216 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2217 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2218 | default: |
| 2219 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2220 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2221 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2222 | return NULL; |
| 2223 | } |
| 2224 | |
| 2225 | static Py_UCS4* |
| 2226 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2227 | int copy_null) |
| 2228 | { |
| 2229 | int kind; |
| 2230 | void *data; |
| 2231 | Py_ssize_t len, targetlen; |
| 2232 | if (PyUnicode_READY(string) == -1) |
| 2233 | return NULL; |
| 2234 | kind = PyUnicode_KIND(string); |
| 2235 | data = PyUnicode_DATA(string); |
| 2236 | len = PyUnicode_GET_LENGTH(string); |
| 2237 | targetlen = len; |
| 2238 | if (copy_null) |
| 2239 | targetlen++; |
| 2240 | if (!target) { |
| 2241 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2242 | PyErr_NoMemory(); |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2246 | if (!target) { |
| 2247 | PyErr_NoMemory(); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | } |
| 2251 | else { |
| 2252 | if (targetsize < targetlen) { |
| 2253 | PyErr_Format(PyExc_SystemError, |
| 2254 | "string is longer than the buffer"); |
| 2255 | if (copy_null && 0 < targetsize) |
| 2256 | target[0] = 0; |
| 2257 | return NULL; |
| 2258 | } |
| 2259 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2260 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2261 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2262 | _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] | 2263 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2264 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2265 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2266 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2267 | } |
| 2268 | else { |
| 2269 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2270 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2271 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2272 | if (copy_null) |
| 2273 | target[len] = 0; |
| 2274 | return target; |
| 2275 | } |
| 2276 | |
| 2277 | Py_UCS4* |
| 2278 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2279 | int copy_null) |
| 2280 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2281 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2282 | PyErr_BadInternalCall(); |
| 2283 | return NULL; |
| 2284 | } |
| 2285 | return as_ucs4(string, target, targetsize, copy_null); |
| 2286 | } |
| 2287 | |
| 2288 | Py_UCS4* |
| 2289 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2290 | { |
| 2291 | return as_ucs4(string, NULL, 0, 1); |
| 2292 | } |
| 2293 | |
| 2294 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2295 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2296 | PyObject * |
| 2297 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2298 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2299 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2301 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2302 | PyErr_BadInternalCall(); |
| 2303 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2306 | if (size == -1) { |
| 2307 | size = wcslen(w); |
| 2308 | } |
| 2309 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2310 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2313 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2314 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2315 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2316 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2317 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2318 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2319 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2320 | if (longflag) |
| 2321 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2322 | else if (longlongflag) { |
| 2323 | /* longlongflag should only ever be nonzero on machines with |
| 2324 | HAVE_LONG_LONG defined */ |
| 2325 | #ifdef HAVE_LONG_LONG |
| 2326 | char *f = PY_FORMAT_LONG_LONG; |
| 2327 | while (*f) |
| 2328 | *fmt++ = *f++; |
| 2329 | #else |
| 2330 | /* we shouldn't ever get here */ |
| 2331 | assert(0); |
| 2332 | *fmt++ = 'l'; |
| 2333 | #endif |
| 2334 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2335 | else if (size_tflag) { |
| 2336 | char *f = PY_FORMAT_SIZE_T; |
| 2337 | while (*f) |
| 2338 | *fmt++ = *f++; |
| 2339 | } |
| 2340 | *fmt++ = c; |
| 2341 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2344 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2345 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2346 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2347 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2348 | |
| 2349 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2350 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2351 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2352 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2353 | const char *p; |
| 2354 | Py_ssize_t len; |
| 2355 | int zeropad; |
| 2356 | int width; |
| 2357 | int precision; |
| 2358 | int longflag; |
| 2359 | int longlongflag; |
| 2360 | int size_tflag; |
| 2361 | int fill; |
| 2362 | |
| 2363 | p = f; |
| 2364 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2365 | zeropad = 0; |
| 2366 | if (*f == '0') { |
| 2367 | zeropad = 1; |
| 2368 | f++; |
| 2369 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2370 | |
| 2371 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2372 | width = 0; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2373 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2374 | if (width > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2375 | PyErr_SetString(PyExc_ValueError, |
| 2376 | "width too big"); |
| 2377 | return NULL; |
| 2378 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2379 | width = (width*10) + (*f - '0'); |
| 2380 | f++; |
| 2381 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2382 | precision = 0; |
| 2383 | if (*f == '.') { |
| 2384 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2385 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2386 | if (precision > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2387 | PyErr_SetString(PyExc_ValueError, |
| 2388 | "precision too big"); |
| 2389 | return NULL; |
| 2390 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2391 | precision = (precision*10) + (*f - '0'); |
| 2392 | f++; |
| 2393 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2394 | if (*f == '%') { |
| 2395 | /* "%.3%s" => f points to "3" */ |
| 2396 | f--; |
| 2397 | } |
| 2398 | } |
| 2399 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2400 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2401 | f--; |
| 2402 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2403 | |
| 2404 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2405 | longflag = 0; |
| 2406 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2407 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2408 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2409 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2410 | longflag = 1; |
| 2411 | ++f; |
| 2412 | } |
| 2413 | #ifdef HAVE_LONG_LONG |
| 2414 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2415 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2416 | longlongflag = 1; |
| 2417 | f += 2; |
| 2418 | } |
| 2419 | #endif |
| 2420 | } |
| 2421 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2422 | 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] | 2423 | size_tflag = 1; |
| 2424 | ++f; |
| 2425 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2426 | |
| 2427 | if (f[1] == '\0') |
| 2428 | writer->overallocate = 0; |
| 2429 | |
| 2430 | switch (*f) { |
| 2431 | case 'c': |
| 2432 | { |
| 2433 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2434 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
| 2435 | PyErr_SetString(PyExc_ValueError, |
| 2436 | "character argument not in range(0x110000)"); |
| 2437 | return NULL; |
| 2438 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2439 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2440 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2441 | break; |
| 2442 | } |
| 2443 | |
| 2444 | case 'i': |
| 2445 | case 'd': |
| 2446 | case 'u': |
| 2447 | case 'x': |
| 2448 | { |
| 2449 | /* used by sprintf */ |
| 2450 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2451 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2452 | |
| 2453 | if (*f == 'u') { |
| 2454 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2455 | |
| 2456 | if (longflag) |
| 2457 | len = sprintf(buffer, fmt, |
| 2458 | va_arg(*vargs, unsigned long)); |
| 2459 | #ifdef HAVE_LONG_LONG |
| 2460 | else if (longlongflag) |
| 2461 | len = sprintf(buffer, fmt, |
| 2462 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2463 | #endif |
| 2464 | else if (size_tflag) |
| 2465 | len = sprintf(buffer, fmt, |
| 2466 | va_arg(*vargs, size_t)); |
| 2467 | else |
| 2468 | len = sprintf(buffer, fmt, |
| 2469 | va_arg(*vargs, unsigned int)); |
| 2470 | } |
| 2471 | else if (*f == 'x') { |
| 2472 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2473 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2474 | } |
| 2475 | else { |
| 2476 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2477 | |
| 2478 | if (longflag) |
| 2479 | len = sprintf(buffer, fmt, |
| 2480 | va_arg(*vargs, long)); |
| 2481 | #ifdef HAVE_LONG_LONG |
| 2482 | else if (longlongflag) |
| 2483 | len = sprintf(buffer, fmt, |
| 2484 | va_arg(*vargs, PY_LONG_LONG)); |
| 2485 | #endif |
| 2486 | else if (size_tflag) |
| 2487 | len = sprintf(buffer, fmt, |
| 2488 | va_arg(*vargs, Py_ssize_t)); |
| 2489 | else |
| 2490 | len = sprintf(buffer, fmt, |
| 2491 | va_arg(*vargs, int)); |
| 2492 | } |
| 2493 | assert(len >= 0); |
| 2494 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2495 | if (precision < len) |
| 2496 | precision = len; |
| 2497 | if (width > precision) { |
| 2498 | Py_UCS4 fillchar; |
| 2499 | fill = width - precision; |
| 2500 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2501 | if (_PyUnicodeWriter_Prepare(writer, fill, fillchar) == -1) |
| 2502 | return NULL; |
| 2503 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2504 | return NULL; |
| 2505 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2506 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2507 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2508 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2509 | if (_PyUnicodeWriter_Prepare(writer, fill, '0') == -1) |
| 2510 | return NULL; |
| 2511 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2512 | return NULL; |
| 2513 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2514 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2515 | if (_PyUnicodeWriter_WriteCstr(writer, buffer, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2516 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2517 | break; |
| 2518 | } |
| 2519 | |
| 2520 | case 'p': |
| 2521 | { |
| 2522 | char number[MAX_LONG_LONG_CHARS]; |
| 2523 | |
| 2524 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2525 | assert(len >= 0); |
| 2526 | |
| 2527 | /* %p is ill-defined: ensure leading 0x. */ |
| 2528 | if (number[1] == 'X') |
| 2529 | number[1] = 'x'; |
| 2530 | else if (number[1] != 'x') { |
| 2531 | memmove(number + 2, number, |
| 2532 | strlen(number) + 1); |
| 2533 | number[0] = '0'; |
| 2534 | number[1] = 'x'; |
| 2535 | len += 2; |
| 2536 | } |
| 2537 | |
| 2538 | if (_PyUnicodeWriter_WriteCstr(writer, number, len) == -1) |
| 2539 | return NULL; |
| 2540 | break; |
| 2541 | } |
| 2542 | |
| 2543 | case 's': |
| 2544 | { |
| 2545 | /* UTF-8 */ |
| 2546 | const char *s = va_arg(*vargs, const char*); |
| 2547 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
| 2548 | if (!str) |
| 2549 | return NULL; |
| 2550 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2551 | Py_DECREF(str); |
| 2552 | return NULL; |
| 2553 | } |
| 2554 | Py_DECREF(str); |
| 2555 | break; |
| 2556 | } |
| 2557 | |
| 2558 | case 'U': |
| 2559 | { |
| 2560 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2561 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2562 | |
| 2563 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2564 | return NULL; |
| 2565 | break; |
| 2566 | } |
| 2567 | |
| 2568 | case 'V': |
| 2569 | { |
| 2570 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2571 | const char *str = va_arg(*vargs, const char *); |
| 2572 | PyObject *str_obj; |
| 2573 | assert(obj || str); |
| 2574 | if (obj) { |
| 2575 | assert(_PyUnicode_CHECK(obj)); |
| 2576 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2577 | return NULL; |
| 2578 | } |
| 2579 | else { |
| 2580 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
| 2581 | if (!str_obj) |
| 2582 | return NULL; |
| 2583 | if (_PyUnicodeWriter_WriteStr(writer, str_obj) == -1) { |
| 2584 | Py_DECREF(str_obj); |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | Py_DECREF(str_obj); |
| 2588 | } |
| 2589 | break; |
| 2590 | } |
| 2591 | |
| 2592 | case 'S': |
| 2593 | { |
| 2594 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2595 | PyObject *str; |
| 2596 | assert(obj); |
| 2597 | str = PyObject_Str(obj); |
| 2598 | if (!str) |
| 2599 | return NULL; |
| 2600 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2601 | Py_DECREF(str); |
| 2602 | return NULL; |
| 2603 | } |
| 2604 | Py_DECREF(str); |
| 2605 | break; |
| 2606 | } |
| 2607 | |
| 2608 | case 'R': |
| 2609 | { |
| 2610 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2611 | PyObject *repr; |
| 2612 | assert(obj); |
| 2613 | repr = PyObject_Repr(obj); |
| 2614 | if (!repr) |
| 2615 | return NULL; |
| 2616 | if (_PyUnicodeWriter_WriteStr(writer, repr) == -1) { |
| 2617 | Py_DECREF(repr); |
| 2618 | return NULL; |
| 2619 | } |
| 2620 | Py_DECREF(repr); |
| 2621 | break; |
| 2622 | } |
| 2623 | |
| 2624 | case 'A': |
| 2625 | { |
| 2626 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2627 | PyObject *ascii; |
| 2628 | assert(obj); |
| 2629 | ascii = PyObject_ASCII(obj); |
| 2630 | if (!ascii) |
| 2631 | return NULL; |
| 2632 | if (_PyUnicodeWriter_WriteStr(writer, ascii) == -1) { |
| 2633 | Py_DECREF(ascii); |
| 2634 | return NULL; |
| 2635 | } |
| 2636 | Py_DECREF(ascii); |
| 2637 | break; |
| 2638 | } |
| 2639 | |
| 2640 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2641 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2642 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2643 | break; |
| 2644 | |
| 2645 | default: |
| 2646 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2647 | of the format string to the output string. (we cannot just |
| 2648 | skip the code, since there's no way to know what's in the |
| 2649 | argument list) */ |
| 2650 | len = strlen(p); |
| 2651 | if (_PyUnicodeWriter_WriteCstr(writer, p, len) == -1) |
| 2652 | return NULL; |
| 2653 | f = p+len; |
| 2654 | return f; |
| 2655 | } |
| 2656 | |
| 2657 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2658 | return f; |
| 2659 | } |
| 2660 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2661 | PyObject * |
| 2662 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2663 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2664 | va_list vargs2; |
| 2665 | const char *f; |
| 2666 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2667 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2668 | _PyUnicodeWriter_Init(&writer); |
| 2669 | writer.min_length = strlen(format) + 100; |
| 2670 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2671 | |
| 2672 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2673 | Copy it to be able to pass a reference to a subfunction. */ |
| 2674 | Py_VA_COPY(vargs2, vargs); |
| 2675 | |
| 2676 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2677 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2678 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2679 | if (f == NULL) |
| 2680 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2681 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2682 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2683 | const char *p; |
| 2684 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2685 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2686 | p = f; |
| 2687 | do |
| 2688 | { |
| 2689 | if ((unsigned char)*p > 127) { |
| 2690 | PyErr_Format(PyExc_ValueError, |
| 2691 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2692 | "string, got a non-ASCII byte: 0x%02x", |
| 2693 | (unsigned char)*p); |
| 2694 | return NULL; |
| 2695 | } |
| 2696 | p++; |
| 2697 | } |
| 2698 | while (*p != '\0' && *p != '%'); |
| 2699 | len = p - f; |
| 2700 | |
| 2701 | if (*p == '\0') |
| 2702 | writer.overallocate = 0; |
| 2703 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
| 2704 | goto fail; |
| 2705 | unicode_write_cstr(writer.buffer, writer.pos, f, len); |
| 2706 | writer.pos += len; |
| 2707 | |
| 2708 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2709 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2710 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2711 | return _PyUnicodeWriter_Finish(&writer); |
| 2712 | |
| 2713 | fail: |
| 2714 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2715 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2718 | PyObject * |
| 2719 | PyUnicode_FromFormat(const char *format, ...) |
| 2720 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2721 | PyObject* ret; |
| 2722 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2723 | |
| 2724 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2725 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2726 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2727 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2728 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2729 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2730 | va_end(vargs); |
| 2731 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2734 | #ifdef HAVE_WCHAR_H |
| 2735 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2736 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2737 | convert a Unicode object to a wide character string. |
| 2738 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2739 | - 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] | 2740 | character) required to convert the unicode object. Ignore size argument. |
| 2741 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2742 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2743 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2744 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2745 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2746 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2747 | wchar_t *w, |
| 2748 | Py_ssize_t size) |
| 2749 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2750 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2751 | const wchar_t *wstr; |
| 2752 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2753 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2754 | if (wstr == NULL) |
| 2755 | return -1; |
| 2756 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2757 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2758 | if (size > res) |
| 2759 | size = res + 1; |
| 2760 | else |
| 2761 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2762 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2763 | return res; |
| 2764 | } |
| 2765 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2766 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2770 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2771 | wchar_t *w, |
| 2772 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2773 | { |
| 2774 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2775 | PyErr_BadInternalCall(); |
| 2776 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2777 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2778 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2781 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2782 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2783 | Py_ssize_t *size) |
| 2784 | { |
| 2785 | wchar_t* buffer; |
| 2786 | Py_ssize_t buflen; |
| 2787 | |
| 2788 | if (unicode == NULL) { |
| 2789 | PyErr_BadInternalCall(); |
| 2790 | return NULL; |
| 2791 | } |
| 2792 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2793 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2794 | if (buflen == -1) |
| 2795 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2796 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2797 | PyErr_NoMemory(); |
| 2798 | return NULL; |
| 2799 | } |
| 2800 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2801 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2802 | if (buffer == NULL) { |
| 2803 | PyErr_NoMemory(); |
| 2804 | return NULL; |
| 2805 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2806 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2807 | if (buflen == -1) { |
| 2808 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2809 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2810 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2811 | if (size != NULL) |
| 2812 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2813 | return buffer; |
| 2814 | } |
| 2815 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2816 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2817 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2818 | PyObject * |
| 2819 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2820 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2821 | PyObject *v; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2822 | void *data; |
| 2823 | int kind; |
| 2824 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2825 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2826 | PyErr_SetString(PyExc_ValueError, |
| 2827 | "chr() arg not in range(0x110000)"); |
| 2828 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2829 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2830 | |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2831 | if ((Py_UCS4)ordinal < 256) |
| 2832 | return get_latin1_char((unsigned char)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2833 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2834 | v = PyUnicode_New(1, ordinal); |
| 2835 | if (v == NULL) |
| 2836 | return NULL; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2837 | kind = PyUnicode_KIND(v); |
| 2838 | data = PyUnicode_DATA(v); |
| 2839 | PyUnicode_WRITE(kind, data, 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2840 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2841 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2844 | PyObject * |
| 2845 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2846 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2847 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2848 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2849 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2850 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2851 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2852 | Py_INCREF(obj); |
| 2853 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2854 | } |
| 2855 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2856 | /* For a Unicode subtype that's not a Unicode object, |
| 2857 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2858 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2859 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2860 | PyErr_Format(PyExc_TypeError, |
| 2861 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2862 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2863 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2866 | PyObject * |
| 2867 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2868 | const char *encoding, |
| 2869 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2870 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2871 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2872 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2873 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2874 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2875 | PyErr_BadInternalCall(); |
| 2876 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2877 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2878 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2879 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2880 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2881 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2882 | _Py_RETURN_UNICODE_EMPTY(); |
| 2883 | v = PyUnicode_Decode( |
| 2884 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2885 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2886 | return v; |
| 2887 | } |
| 2888 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2889 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2890 | PyErr_SetString(PyExc_TypeError, |
| 2891 | "decoding str is not supported"); |
| 2892 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2893 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2894 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2895 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2896 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2897 | PyErr_Format(PyExc_TypeError, |
| 2898 | "coercing to str: need bytes, bytearray " |
| 2899 | "or buffer-like object, %.80s found", |
| 2900 | Py_TYPE(obj)->tp_name); |
| 2901 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2902 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2903 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2904 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2905 | PyBuffer_Release(&buffer); |
| 2906 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2907 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2908 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2909 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2910 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2911 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2914 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2915 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2916 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2917 | int |
| 2918 | _Py_normalize_encoding(const char *encoding, |
| 2919 | char *lower, |
| 2920 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2921 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2922 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2923 | char *l; |
| 2924 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2925 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2926 | if (encoding == NULL) { |
| 2927 | strcpy(lower, "utf-8"); |
| 2928 | return 1; |
| 2929 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2930 | e = encoding; |
| 2931 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2932 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2933 | while (*e) { |
| 2934 | if (l == l_end) |
| 2935 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2936 | if (Py_ISUPPER(*e)) { |
| 2937 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2938 | } |
| 2939 | else if (*e == '_') { |
| 2940 | *l++ = '-'; |
| 2941 | e++; |
| 2942 | } |
| 2943 | else { |
| 2944 | *l++ = *e++; |
| 2945 | } |
| 2946 | } |
| 2947 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2948 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2951 | PyObject * |
| 2952 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2953 | Py_ssize_t size, |
| 2954 | const char *encoding, |
| 2955 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2956 | { |
| 2957 | PyObject *buffer = NULL, *unicode; |
| 2958 | Py_buffer info; |
| 2959 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2960 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2961 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2962 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2963 | if ((strcmp(lower, "utf-8") == 0) || |
| 2964 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2965 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2966 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2967 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2968 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2969 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2970 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2971 | else if (strcmp(lower, "mbcs") == 0) |
| 2972 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2973 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2974 | else if (strcmp(lower, "ascii") == 0) |
| 2975 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2976 | else if (strcmp(lower, "utf-16") == 0) |
| 2977 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2978 | else if (strcmp(lower, "utf-32") == 0) |
| 2979 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2980 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2981 | |
| 2982 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2983 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2984 | 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] | 2985 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2986 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2987 | if (buffer == NULL) |
| 2988 | goto onError; |
| 2989 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2990 | if (unicode == NULL) |
| 2991 | goto onError; |
| 2992 | if (!PyUnicode_Check(unicode)) { |
| 2993 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2994 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2995 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2996 | Py_DECREF(unicode); |
| 2997 | goto onError; |
| 2998 | } |
| 2999 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3000 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3001 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3002 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3003 | Py_XDECREF(buffer); |
| 3004 | return NULL; |
| 3005 | } |
| 3006 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3007 | PyObject * |
| 3008 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3009 | const char *encoding, |
| 3010 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3011 | { |
| 3012 | PyObject *v; |
| 3013 | |
| 3014 | if (!PyUnicode_Check(unicode)) { |
| 3015 | PyErr_BadArgument(); |
| 3016 | goto onError; |
| 3017 | } |
| 3018 | |
| 3019 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3020 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3021 | |
| 3022 | /* Decode via the codec registry */ |
| 3023 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3024 | if (v == NULL) |
| 3025 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3026 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3027 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3028 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3029 | return NULL; |
| 3030 | } |
| 3031 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3032 | PyObject * |
| 3033 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3034 | const char *encoding, |
| 3035 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3036 | { |
| 3037 | PyObject *v; |
| 3038 | |
| 3039 | if (!PyUnicode_Check(unicode)) { |
| 3040 | PyErr_BadArgument(); |
| 3041 | goto onError; |
| 3042 | } |
| 3043 | |
| 3044 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3045 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3046 | |
| 3047 | /* Decode via the codec registry */ |
| 3048 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3049 | if (v == NULL) |
| 3050 | goto onError; |
| 3051 | if (!PyUnicode_Check(v)) { |
| 3052 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3053 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3054 | Py_TYPE(v)->tp_name); |
| 3055 | Py_DECREF(v); |
| 3056 | goto onError; |
| 3057 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3058 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3059 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3060 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3061 | return NULL; |
| 3062 | } |
| 3063 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3064 | PyObject * |
| 3065 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3066 | Py_ssize_t size, |
| 3067 | const char *encoding, |
| 3068 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3069 | { |
| 3070 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3072 | unicode = PyUnicode_FromUnicode(s, size); |
| 3073 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3074 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3075 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3076 | Py_DECREF(unicode); |
| 3077 | return v; |
| 3078 | } |
| 3079 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3080 | PyObject * |
| 3081 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3082 | const char *encoding, |
| 3083 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3084 | { |
| 3085 | PyObject *v; |
| 3086 | |
| 3087 | if (!PyUnicode_Check(unicode)) { |
| 3088 | PyErr_BadArgument(); |
| 3089 | goto onError; |
| 3090 | } |
| 3091 | |
| 3092 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3093 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3094 | |
| 3095 | /* Encode via the codec registry */ |
| 3096 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3097 | if (v == NULL) |
| 3098 | goto onError; |
| 3099 | return v; |
| 3100 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3101 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3102 | return NULL; |
| 3103 | } |
| 3104 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3105 | static size_t |
| 3106 | wcstombs_errorpos(const wchar_t *wstr) |
| 3107 | { |
| 3108 | size_t len; |
| 3109 | #if SIZEOF_WCHAR_T == 2 |
| 3110 | wchar_t buf[3]; |
| 3111 | #else |
| 3112 | wchar_t buf[2]; |
| 3113 | #endif |
| 3114 | char outbuf[MB_LEN_MAX]; |
| 3115 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3116 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3117 | #if SIZEOF_WCHAR_T == 2 |
| 3118 | buf[2] = 0; |
| 3119 | #else |
| 3120 | buf[1] = 0; |
| 3121 | #endif |
| 3122 | start = wstr; |
| 3123 | while (*wstr != L'\0') |
| 3124 | { |
| 3125 | previous = wstr; |
| 3126 | #if SIZEOF_WCHAR_T == 2 |
| 3127 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3128 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3129 | { |
| 3130 | buf[0] = wstr[0]; |
| 3131 | buf[1] = wstr[1]; |
| 3132 | wstr += 2; |
| 3133 | } |
| 3134 | else { |
| 3135 | buf[0] = *wstr; |
| 3136 | buf[1] = 0; |
| 3137 | wstr++; |
| 3138 | } |
| 3139 | #else |
| 3140 | buf[0] = *wstr; |
| 3141 | wstr++; |
| 3142 | #endif |
| 3143 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3144 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3145 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3149 | return 0; |
| 3150 | } |
| 3151 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3152 | static int |
| 3153 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3154 | { |
| 3155 | if (errors == NULL) { |
| 3156 | *surrogateescape = 0; |
| 3157 | return 0; |
| 3158 | } |
| 3159 | |
| 3160 | if (strcmp(errors, "strict") == 0) { |
| 3161 | *surrogateescape = 0; |
| 3162 | return 0; |
| 3163 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3164 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3165 | *surrogateescape = 1; |
| 3166 | return 0; |
| 3167 | } |
| 3168 | PyErr_Format(PyExc_ValueError, |
| 3169 | "only 'strict' and 'surrogateescape' error handlers " |
| 3170 | "are supported, not '%s'", |
| 3171 | errors); |
| 3172 | return -1; |
| 3173 | } |
| 3174 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3175 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3176 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3177 | { |
| 3178 | Py_ssize_t wlen, wlen2; |
| 3179 | wchar_t *wstr; |
| 3180 | PyObject *bytes = NULL; |
| 3181 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3182 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3183 | PyObject *exc; |
| 3184 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3185 | int surrogateescape; |
| 3186 | |
| 3187 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3188 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3189 | |
| 3190 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3191 | if (wstr == NULL) |
| 3192 | return NULL; |
| 3193 | |
| 3194 | wlen2 = wcslen(wstr); |
| 3195 | if (wlen2 != wlen) { |
| 3196 | PyMem_Free(wstr); |
| 3197 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3198 | return NULL; |
| 3199 | } |
| 3200 | |
| 3201 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3202 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3203 | char *str; |
| 3204 | |
| 3205 | str = _Py_wchar2char(wstr, &error_pos); |
| 3206 | if (str == NULL) { |
| 3207 | if (error_pos == (size_t)-1) { |
| 3208 | PyErr_NoMemory(); |
| 3209 | PyMem_Free(wstr); |
| 3210 | return NULL; |
| 3211 | } |
| 3212 | else { |
| 3213 | goto encode_error; |
| 3214 | } |
| 3215 | } |
| 3216 | PyMem_Free(wstr); |
| 3217 | |
| 3218 | bytes = PyBytes_FromString(str); |
| 3219 | PyMem_Free(str); |
| 3220 | } |
| 3221 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3222 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3223 | size_t len, len2; |
| 3224 | |
| 3225 | len = wcstombs(NULL, wstr, 0); |
| 3226 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3227 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3228 | goto encode_error; |
| 3229 | } |
| 3230 | |
| 3231 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3232 | if (bytes == NULL) { |
| 3233 | PyMem_Free(wstr); |
| 3234 | return NULL; |
| 3235 | } |
| 3236 | |
| 3237 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3238 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3239 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3240 | goto encode_error; |
| 3241 | } |
| 3242 | PyMem_Free(wstr); |
| 3243 | } |
| 3244 | return bytes; |
| 3245 | |
| 3246 | encode_error: |
| 3247 | errmsg = strerror(errno); |
| 3248 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3249 | |
| 3250 | if (error_pos == (size_t)-1) |
| 3251 | error_pos = wcstombs_errorpos(wstr); |
| 3252 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3253 | PyMem_Free(wstr); |
| 3254 | Py_XDECREF(bytes); |
| 3255 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3256 | if (errmsg != NULL) { |
| 3257 | size_t errlen; |
| 3258 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3259 | if (wstr != NULL) { |
| 3260 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3261 | PyMem_Free(wstr); |
| 3262 | } else |
| 3263 | errmsg = NULL; |
| 3264 | } |
| 3265 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3266 | reason = PyUnicode_FromString( |
| 3267 | "wcstombs() encountered an unencodable " |
| 3268 | "wide character"); |
| 3269 | if (reason == NULL) |
| 3270 | return NULL; |
| 3271 | |
| 3272 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3273 | "locale", unicode, |
| 3274 | (Py_ssize_t)error_pos, |
| 3275 | (Py_ssize_t)(error_pos+1), |
| 3276 | reason); |
| 3277 | Py_DECREF(reason); |
| 3278 | if (exc != NULL) { |
| 3279 | PyCodec_StrictErrors(exc); |
| 3280 | Py_XDECREF(exc); |
| 3281 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3282 | return NULL; |
| 3283 | } |
| 3284 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3285 | PyObject * |
| 3286 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3287 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3288 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3289 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3290 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3291 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3292 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3293 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3294 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3295 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3296 | the Python codec requires to encode at least its own filename. Use the C |
| 3297 | version of the locale codec until the codec registry is initialized and |
| 3298 | the Python codec is loaded. |
| 3299 | |
| 3300 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3301 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3302 | subinterpreters. */ |
| 3303 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3304 | return PyUnicode_AsEncodedString(unicode, |
| 3305 | Py_FileSystemDefaultEncoding, |
| 3306 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3307 | } |
| 3308 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3309 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3310 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3311 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3314 | PyObject * |
| 3315 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3316 | const char *encoding, |
| 3317 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3318 | { |
| 3319 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3320 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3321 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3322 | if (!PyUnicode_Check(unicode)) { |
| 3323 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3324 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3325 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3326 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3327 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3328 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3329 | if ((strcmp(lower, "utf-8") == 0) || |
| 3330 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3331 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3332 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3333 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3334 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3335 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3336 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3337 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3338 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3339 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3340 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3341 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3342 | else if (strcmp(lower, "mbcs") == 0) |
| 3343 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3344 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3345 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3346 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3347 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3348 | |
| 3349 | /* Encode via the codec registry */ |
| 3350 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3351 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3352 | return NULL; |
| 3353 | |
| 3354 | /* The normal path */ |
| 3355 | if (PyBytes_Check(v)) |
| 3356 | return v; |
| 3357 | |
| 3358 | /* 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] | 3359 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3360 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3361 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3362 | |
| 3363 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3364 | "encoder %s returned bytearray instead of bytes", |
| 3365 | encoding); |
| 3366 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3367 | Py_DECREF(v); |
| 3368 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3369 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3370 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3371 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3372 | Py_DECREF(v); |
| 3373 | return b; |
| 3374 | } |
| 3375 | |
| 3376 | PyErr_Format(PyExc_TypeError, |
| 3377 | "encoder did not return a bytes object (type=%.400s)", |
| 3378 | Py_TYPE(v)->tp_name); |
| 3379 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3380 | return NULL; |
| 3381 | } |
| 3382 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3383 | PyObject * |
| 3384 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3385 | const char *encoding, |
| 3386 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3387 | { |
| 3388 | PyObject *v; |
| 3389 | |
| 3390 | if (!PyUnicode_Check(unicode)) { |
| 3391 | PyErr_BadArgument(); |
| 3392 | goto onError; |
| 3393 | } |
| 3394 | |
| 3395 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3396 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3397 | |
| 3398 | /* Encode via the codec registry */ |
| 3399 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3400 | if (v == NULL) |
| 3401 | goto onError; |
| 3402 | if (!PyUnicode_Check(v)) { |
| 3403 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3404 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3405 | Py_TYPE(v)->tp_name); |
| 3406 | Py_DECREF(v); |
| 3407 | goto onError; |
| 3408 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3409 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3410 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3411 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3412 | return NULL; |
| 3413 | } |
| 3414 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3415 | static size_t |
| 3416 | mbstowcs_errorpos(const char *str, size_t len) |
| 3417 | { |
| 3418 | #ifdef HAVE_MBRTOWC |
| 3419 | const char *start = str; |
| 3420 | mbstate_t mbs; |
| 3421 | size_t converted; |
| 3422 | wchar_t ch; |
| 3423 | |
| 3424 | memset(&mbs, 0, sizeof mbs); |
| 3425 | while (len) |
| 3426 | { |
| 3427 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3428 | if (converted == 0) |
| 3429 | /* Reached end of string */ |
| 3430 | break; |
| 3431 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3432 | /* Conversion error or incomplete character */ |
| 3433 | return str - start; |
| 3434 | } |
| 3435 | else { |
| 3436 | str += converted; |
| 3437 | len -= converted; |
| 3438 | } |
| 3439 | } |
| 3440 | /* failed to find the undecodable byte sequence */ |
| 3441 | return 0; |
| 3442 | #endif |
| 3443 | return 0; |
| 3444 | } |
| 3445 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3446 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3447 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3448 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3449 | { |
| 3450 | wchar_t smallbuf[256]; |
| 3451 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3452 | wchar_t *wstr; |
| 3453 | size_t wlen, wlen2; |
| 3454 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3455 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3456 | size_t error_pos; |
| 3457 | char *errmsg; |
| 3458 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3459 | |
| 3460 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3461 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3462 | |
| 3463 | if (str[len] != '\0' || len != strlen(str)) { |
| 3464 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3465 | return NULL; |
| 3466 | } |
| 3467 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3468 | if (surrogateescape) { |
| 3469 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3470 | wstr = _Py_char2wchar(str, &wlen); |
| 3471 | if (wstr == NULL) { |
| 3472 | if (wlen == (size_t)-1) |
| 3473 | PyErr_NoMemory(); |
| 3474 | else |
| 3475 | PyErr_SetFromErrno(PyExc_OSError); |
| 3476 | return NULL; |
| 3477 | } |
| 3478 | |
| 3479 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3480 | PyMem_Free(wstr); |
| 3481 | } |
| 3482 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3483 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3484 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3485 | wlen = mbstowcs(NULL, str, 0); |
| 3486 | #else |
| 3487 | wlen = len; |
| 3488 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3489 | if (wlen == (size_t)-1) |
| 3490 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3491 | if (wlen+1 <= smallbuf_len) { |
| 3492 | wstr = smallbuf; |
| 3493 | } |
| 3494 | else { |
| 3495 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3496 | return PyErr_NoMemory(); |
| 3497 | |
| 3498 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3499 | if (!wstr) |
| 3500 | return PyErr_NoMemory(); |
| 3501 | } |
| 3502 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3503 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3504 | if (wlen2 == (size_t)-1) { |
| 3505 | if (wstr != smallbuf) |
| 3506 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3507 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3508 | } |
| 3509 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3510 | assert(wlen2 == wlen); |
| 3511 | #endif |
| 3512 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3513 | if (wstr != smallbuf) |
| 3514 | PyMem_Free(wstr); |
| 3515 | } |
| 3516 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3517 | |
| 3518 | decode_error: |
| 3519 | errmsg = strerror(errno); |
| 3520 | assert(errmsg != NULL); |
| 3521 | |
| 3522 | error_pos = mbstowcs_errorpos(str, len); |
| 3523 | if (errmsg != NULL) { |
| 3524 | size_t errlen; |
| 3525 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3526 | if (wstr != NULL) { |
| 3527 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3528 | PyMem_Free(wstr); |
| 3529 | } else |
| 3530 | errmsg = NULL; |
| 3531 | } |
| 3532 | if (errmsg == NULL) |
| 3533 | reason = PyUnicode_FromString( |
| 3534 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3535 | if (reason == NULL) |
| 3536 | return NULL; |
| 3537 | |
| 3538 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3539 | "locale", str, len, |
| 3540 | (Py_ssize_t)error_pos, |
| 3541 | (Py_ssize_t)(error_pos+1), |
| 3542 | reason); |
| 3543 | Py_DECREF(reason); |
| 3544 | if (exc != NULL) { |
| 3545 | PyCodec_StrictErrors(exc); |
| 3546 | Py_XDECREF(exc); |
| 3547 | } |
| 3548 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3552 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3553 | { |
| 3554 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3555 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | |
| 3559 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3560 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3561 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3562 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3563 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3564 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3565 | PyObject* |
| 3566 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3567 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3568 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3569 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3570 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3571 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3572 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3573 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3574 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3575 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3576 | the Python codec requires to encode at least its own filename. Use the C |
| 3577 | version of the locale codec until the codec registry is initialized and |
| 3578 | the Python codec is loaded. |
| 3579 | |
| 3580 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3581 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3582 | subinterpreters. */ |
| 3583 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3584 | return PyUnicode_Decode(s, size, |
| 3585 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3586 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3587 | } |
| 3588 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3589 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3590 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3591 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3594 | |
| 3595 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3596 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3597 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3598 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3599 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3600 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3601 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3602 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3603 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3604 | if (pos == -1) |
| 3605 | return 0; |
| 3606 | else |
| 3607 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3608 | } |
| 3609 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3610 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3611 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3612 | { |
| 3613 | PyObject *output = NULL; |
| 3614 | Py_ssize_t size; |
| 3615 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3616 | if (arg == NULL) { |
| 3617 | Py_DECREF(*(PyObject**)addr); |
| 3618 | return 1; |
| 3619 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3620 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3621 | output = arg; |
| 3622 | Py_INCREF(output); |
| 3623 | } |
| 3624 | else { |
| 3625 | arg = PyUnicode_FromObject(arg); |
| 3626 | if (!arg) |
| 3627 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3628 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3629 | Py_DECREF(arg); |
| 3630 | if (!output) |
| 3631 | return 0; |
| 3632 | if (!PyBytes_Check(output)) { |
| 3633 | Py_DECREF(output); |
| 3634 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3635 | return 0; |
| 3636 | } |
| 3637 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3638 | size = PyBytes_GET_SIZE(output); |
| 3639 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3640 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3641 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3642 | Py_DECREF(output); |
| 3643 | return 0; |
| 3644 | } |
| 3645 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3646 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3650 | int |
| 3651 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3652 | { |
| 3653 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3654 | if (arg == NULL) { |
| 3655 | Py_DECREF(*(PyObject**)addr); |
| 3656 | return 1; |
| 3657 | } |
| 3658 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3659 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3660 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3661 | output = arg; |
| 3662 | Py_INCREF(output); |
| 3663 | } |
| 3664 | else { |
| 3665 | arg = PyBytes_FromObject(arg); |
| 3666 | if (!arg) |
| 3667 | return 0; |
| 3668 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3669 | PyBytes_GET_SIZE(arg)); |
| 3670 | Py_DECREF(arg); |
| 3671 | if (!output) |
| 3672 | return 0; |
| 3673 | if (!PyUnicode_Check(output)) { |
| 3674 | Py_DECREF(output); |
| 3675 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3676 | return 0; |
| 3677 | } |
| 3678 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3679 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3680 | Py_DECREF(output); |
| 3681 | return 0; |
| 3682 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3683 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3684 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3685 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3686 | Py_DECREF(output); |
| 3687 | return 0; |
| 3688 | } |
| 3689 | *(PyObject**)addr = output; |
| 3690 | return Py_CLEANUP_SUPPORTED; |
| 3691 | } |
| 3692 | |
| 3693 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3694 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3695 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3696 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3697 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3698 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3699 | if (!PyUnicode_Check(unicode)) { |
| 3700 | PyErr_BadArgument(); |
| 3701 | return NULL; |
| 3702 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3703 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3704 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3705 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3706 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3707 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3708 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3709 | if (bytes == NULL) |
| 3710 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3711 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3712 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3713 | Py_DECREF(bytes); |
| 3714 | return NULL; |
| 3715 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3716 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3717 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3718 | PyBytes_AS_STRING(bytes), |
| 3719 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3720 | Py_DECREF(bytes); |
| 3721 | } |
| 3722 | |
| 3723 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3724 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3725 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3729 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3730 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3731 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3732 | } |
| 3733 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3734 | Py_UNICODE * |
| 3735 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3736 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3737 | const unsigned char *one_byte; |
| 3738 | #if SIZEOF_WCHAR_T == 4 |
| 3739 | const Py_UCS2 *two_bytes; |
| 3740 | #else |
| 3741 | const Py_UCS4 *four_bytes; |
| 3742 | const Py_UCS4 *ucs4_end; |
| 3743 | Py_ssize_t num_surrogates; |
| 3744 | #endif |
| 3745 | wchar_t *w; |
| 3746 | wchar_t *wchar_end; |
| 3747 | |
| 3748 | if (!PyUnicode_Check(unicode)) { |
| 3749 | PyErr_BadArgument(); |
| 3750 | return NULL; |
| 3751 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3752 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3753 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3754 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3755 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3756 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3757 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3758 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3759 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3760 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3761 | num_surrogates = 0; |
| 3762 | |
| 3763 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3764 | if (*four_bytes > 0xFFFF) |
| 3765 | ++num_surrogates; |
| 3766 | } |
| 3767 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3768 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3769 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3770 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3771 | PyErr_NoMemory(); |
| 3772 | return NULL; |
| 3773 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3774 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3775 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3776 | w = _PyUnicode_WSTR(unicode); |
| 3777 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3778 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3779 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3780 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3781 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3782 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3783 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3784 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3785 | } |
| 3786 | else |
| 3787 | *w = *four_bytes; |
| 3788 | |
| 3789 | if (w > wchar_end) { |
| 3790 | assert(0 && "Miscalculated string end"); |
| 3791 | } |
| 3792 | } |
| 3793 | *w = 0; |
| 3794 | #else |
| 3795 | /* sizeof(wchar_t) == 4 */ |
| 3796 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3797 | "should share memory already."); |
| 3798 | return NULL; |
| 3799 | #endif |
| 3800 | } |
| 3801 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3802 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3803 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3804 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3805 | PyErr_NoMemory(); |
| 3806 | return NULL; |
| 3807 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3808 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3809 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3810 | w = _PyUnicode_WSTR(unicode); |
| 3811 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3813 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3814 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3815 | for (; w < wchar_end; ++one_byte, ++w) |
| 3816 | *w = *one_byte; |
| 3817 | /* null-terminate the wstr */ |
| 3818 | *w = 0; |
| 3819 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3820 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3821 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3822 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3823 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3824 | *w = *two_bytes; |
| 3825 | /* null-terminate the wstr */ |
| 3826 | *w = 0; |
| 3827 | #else |
| 3828 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3829 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3830 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3831 | Py_FatalError("Impossible unicode object state, wstr " |
| 3832 | "and str should share memory already."); |
| 3833 | return NULL; |
| 3834 | #endif |
| 3835 | } |
| 3836 | else { |
| 3837 | assert(0 && "This should never happen."); |
| 3838 | } |
| 3839 | } |
| 3840 | } |
| 3841 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3842 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3843 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3846 | Py_UNICODE * |
| 3847 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3848 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3849 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3852 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3853 | Py_ssize_t |
| 3854 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | { |
| 3856 | if (!PyUnicode_Check(unicode)) { |
| 3857 | PyErr_BadArgument(); |
| 3858 | goto onError; |
| 3859 | } |
| 3860 | return PyUnicode_GET_SIZE(unicode); |
| 3861 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3862 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3863 | return -1; |
| 3864 | } |
| 3865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3866 | Py_ssize_t |
| 3867 | PyUnicode_GetLength(PyObject *unicode) |
| 3868 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3869 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3870 | PyErr_BadArgument(); |
| 3871 | return -1; |
| 3872 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3873 | if (PyUnicode_READY(unicode) == -1) |
| 3874 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3875 | return PyUnicode_GET_LENGTH(unicode); |
| 3876 | } |
| 3877 | |
| 3878 | Py_UCS4 |
| 3879 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3880 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3881 | void *data; |
| 3882 | int kind; |
| 3883 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3884 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3885 | PyErr_BadArgument(); |
| 3886 | return (Py_UCS4)-1; |
| 3887 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3888 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3889 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3890 | return (Py_UCS4)-1; |
| 3891 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3892 | data = PyUnicode_DATA(unicode); |
| 3893 | kind = PyUnicode_KIND(unicode); |
| 3894 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | int |
| 3898 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3899 | { |
| 3900 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3901 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3902 | return -1; |
| 3903 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3904 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3905 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3906 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3907 | return -1; |
| 3908 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3909 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3910 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3911 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3912 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3913 | return -1; |
| 3914 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3915 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3916 | index, ch); |
| 3917 | return 0; |
| 3918 | } |
| 3919 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3920 | const char * |
| 3921 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3922 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3923 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3926 | /* create or adjust a UnicodeDecodeError */ |
| 3927 | static void |
| 3928 | make_decode_exception(PyObject **exceptionObject, |
| 3929 | const char *encoding, |
| 3930 | const char *input, Py_ssize_t length, |
| 3931 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3932 | const char *reason) |
| 3933 | { |
| 3934 | if (*exceptionObject == NULL) { |
| 3935 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3936 | encoding, input, length, startpos, endpos, reason); |
| 3937 | } |
| 3938 | else { |
| 3939 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3940 | goto onError; |
| 3941 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3942 | goto onError; |
| 3943 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3944 | goto onError; |
| 3945 | } |
| 3946 | return; |
| 3947 | |
| 3948 | onError: |
| 3949 | Py_DECREF(*exceptionObject); |
| 3950 | *exceptionObject = NULL; |
| 3951 | } |
| 3952 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3953 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3954 | /* error handling callback helper: |
| 3955 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3956 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3957 | and adjust various state variables. |
| 3958 | return 0 on success, -1 on error |
| 3959 | */ |
| 3960 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3961 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3962 | unicode_decode_call_errorhandler_wchar( |
| 3963 | const char *errors, PyObject **errorHandler, |
| 3964 | const char *encoding, const char *reason, |
| 3965 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3966 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3967 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3968 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3969 | 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] | 3970 | |
| 3971 | PyObject *restuple = NULL; |
| 3972 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3973 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3974 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3975 | Py_ssize_t requiredsize; |
| 3976 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3977 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3978 | wchar_t *repwstr; |
| 3979 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3980 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3981 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 3982 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3983 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3984 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3985 | *errorHandler = PyCodec_LookupError(errors); |
| 3986 | if (*errorHandler == NULL) |
| 3987 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3990 | make_decode_exception(exceptionObject, |
| 3991 | encoding, |
| 3992 | *input, *inend - *input, |
| 3993 | *startinpos, *endinpos, |
| 3994 | reason); |
| 3995 | if (*exceptionObject == NULL) |
| 3996 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3997 | |
| 3998 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3999 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4000 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4001 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4002 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4003 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4004 | } |
| 4005 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4006 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4007 | |
| 4008 | /* Copy back the bytes variables, which might have been modified by the |
| 4009 | callback */ |
| 4010 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4011 | if (!inputobj) |
| 4012 | goto onError; |
| 4013 | if (!PyBytes_Check(inputobj)) { |
| 4014 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4015 | } |
| 4016 | *input = PyBytes_AS_STRING(inputobj); |
| 4017 | insize = PyBytes_GET_SIZE(inputobj); |
| 4018 | *inend = *input + insize; |
| 4019 | /* we can DECREF safely, as the exception has another reference, |
| 4020 | so the object won't go away. */ |
| 4021 | Py_DECREF(inputobj); |
| 4022 | |
| 4023 | if (newpos<0) |
| 4024 | newpos = insize+newpos; |
| 4025 | if (newpos<0 || newpos>insize) { |
| 4026 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4027 | goto onError; |
| 4028 | } |
| 4029 | |
| 4030 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4031 | if (repwstr == NULL) |
| 4032 | goto onError; |
| 4033 | /* need more space? (at least enough for what we |
| 4034 | have+the replacement+the rest of the string (starting |
| 4035 | at the new input position), so we won't have to check space |
| 4036 | when there are no errors in the rest of the string) */ |
| 4037 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4038 | if (requiredsize > outsize) { |
| 4039 | if (requiredsize < 2*outsize) |
| 4040 | requiredsize = 2*outsize; |
| 4041 | if (unicode_resize(output, requiredsize) < 0) |
| 4042 | goto onError; |
| 4043 | } |
| 4044 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4045 | *outpos += repwlen; |
| 4046 | |
| 4047 | *endinpos = newpos; |
| 4048 | *inptr = *input + newpos; |
| 4049 | |
| 4050 | /* we made it! */ |
| 4051 | Py_XDECREF(restuple); |
| 4052 | return 0; |
| 4053 | |
| 4054 | onError: |
| 4055 | Py_XDECREF(restuple); |
| 4056 | return -1; |
| 4057 | } |
| 4058 | #endif /* HAVE_MBCS */ |
| 4059 | |
| 4060 | static int |
| 4061 | unicode_decode_call_errorhandler_writer( |
| 4062 | const char *errors, PyObject **errorHandler, |
| 4063 | const char *encoding, const char *reason, |
| 4064 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4065 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4066 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4067 | { |
| 4068 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4069 | |
| 4070 | PyObject *restuple = NULL; |
| 4071 | PyObject *repunicode = NULL; |
| 4072 | Py_ssize_t insize; |
| 4073 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4074 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4075 | PyObject *inputobj = NULL; |
| 4076 | |
| 4077 | if (*errorHandler == NULL) { |
| 4078 | *errorHandler = PyCodec_LookupError(errors); |
| 4079 | if (*errorHandler == NULL) |
| 4080 | goto onError; |
| 4081 | } |
| 4082 | |
| 4083 | make_decode_exception(exceptionObject, |
| 4084 | encoding, |
| 4085 | *input, *inend - *input, |
| 4086 | *startinpos, *endinpos, |
| 4087 | reason); |
| 4088 | if (*exceptionObject == NULL) |
| 4089 | goto onError; |
| 4090 | |
| 4091 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4092 | if (restuple == NULL) |
| 4093 | goto onError; |
| 4094 | if (!PyTuple_Check(restuple)) { |
| 4095 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4096 | goto onError; |
| 4097 | } |
| 4098 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4099 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4100 | |
| 4101 | /* Copy back the bytes variables, which might have been modified by the |
| 4102 | callback */ |
| 4103 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4104 | if (!inputobj) |
| 4105 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4106 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4107 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4108 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4109 | *input = PyBytes_AS_STRING(inputobj); |
| 4110 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4111 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4112 | /* we can DECREF safely, as the exception has another reference, |
| 4113 | so the object won't go away. */ |
| 4114 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4115 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4116 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4117 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4118 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4119 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4120 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4121 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4122 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4123 | if (PyUnicode_READY(repunicode) < 0) |
| 4124 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4125 | replen = PyUnicode_GET_LENGTH(repunicode); |
| 4126 | writer->min_length += replen; |
| 4127 | if (replen > 1) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4128 | writer->overallocate = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4129 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4130 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4131 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4132 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4133 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4134 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4135 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4136 | Py_XDECREF(restuple); |
| 4137 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4138 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4139 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4140 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4141 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4144 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4145 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4146 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4147 | |
| 4148 | /* Three simple macros defining base-64. */ |
| 4149 | |
| 4150 | /* Is c a base-64 character? */ |
| 4151 | |
| 4152 | #define IS_BASE64(c) \ |
| 4153 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4154 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4155 | ((c) >= '0' && (c) <= '9') || \ |
| 4156 | (c) == '+' || (c) == '/') |
| 4157 | |
| 4158 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4159 | |
| 4160 | #define FROM_BASE64(c) \ |
| 4161 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4162 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4163 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4164 | (c) == '+' ? 62 : 63) |
| 4165 | |
| 4166 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4167 | |
| 4168 | #define TO_BASE64(n) \ |
| 4169 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4170 | |
| 4171 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4172 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4173 | * byte not decoding to itself is the + which begins a base64 |
| 4174 | * string. */ |
| 4175 | |
| 4176 | #define DECODE_DIRECT(c) \ |
| 4177 | ((c) <= 127 && (c) != '+') |
| 4178 | |
| 4179 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4180 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4181 | * the above). See RFC2152. This array identifies these different |
| 4182 | * sets: |
| 4183 | * 0 : "Set D" |
| 4184 | * alphanumeric and '(),-./:? |
| 4185 | * 1 : "Set O" |
| 4186 | * !"#$%&*;<=>@[]^_`{|} |
| 4187 | * 2 : "whitespace" |
| 4188 | * ht nl cr sp |
| 4189 | * 3 : special (must be base64 encoded) |
| 4190 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4191 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4192 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4193 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4194 | char utf7_category[128] = { |
| 4195 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4196 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4197 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4198 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4199 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4200 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4201 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4202 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4203 | /* @ A B C D E F G H I J K L M N O */ |
| 4204 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4205 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4206 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4207 | /* ` a b c d e f g h i j k l m n o */ |
| 4208 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4209 | /* p q r s t u v w x y z { | } ~ del */ |
| 4210 | 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] | 4211 | }; |
| 4212 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4213 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4214 | * answer depends on whether we are encoding set O as itself, and also |
| 4215 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4216 | * clear that the answers to these questions vary between |
| 4217 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4218 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4219 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4220 | ((c) < 128 && (c) > 0 && \ |
| 4221 | ((utf7_category[(c)] == 0) || \ |
| 4222 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4223 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4225 | PyObject * |
| 4226 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4227 | Py_ssize_t size, |
| 4228 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4229 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4230 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4231 | } |
| 4232 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4233 | /* The decoder. The only state we preserve is our read position, |
| 4234 | * i.e. how many characters we have consumed. So if we end in the |
| 4235 | * middle of a shift sequence we have to back off the read position |
| 4236 | * and the output to the beginning of the sequence, otherwise we lose |
| 4237 | * all the shift state (seen bits, number of bits seen, high |
| 4238 | * surrogate). */ |
| 4239 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4240 | PyObject * |
| 4241 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4242 | Py_ssize_t size, |
| 4243 | const char *errors, |
| 4244 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4245 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4246 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4247 | Py_ssize_t startinpos; |
| 4248 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4249 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4250 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4251 | const char *errmsg = ""; |
| 4252 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4253 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4254 | unsigned int base64bits = 0; |
| 4255 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4256 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4257 | PyObject *errorHandler = NULL; |
| 4258 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4259 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4260 | if (size == 0) { |
| 4261 | if (consumed) |
| 4262 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4263 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4264 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4265 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4266 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4267 | _PyUnicodeWriter_Init(&writer); |
| 4268 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4269 | |
| 4270 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4271 | e = s + size; |
| 4272 | |
| 4273 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4274 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4275 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4276 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4277 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4278 | if (inShift) { /* in a base-64 section */ |
| 4279 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4280 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4281 | base64bits += 6; |
| 4282 | s++; |
| 4283 | if (base64bits >= 16) { |
| 4284 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4285 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4286 | base64bits -= 16; |
| 4287 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4288 | if (surrogate) { |
| 4289 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4290 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4291 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4292 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4293 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4294 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4295 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4296 | } |
| 4297 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4298 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4299 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4300 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4301 | } |
| 4302 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4303 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4304 | /* first surrogate */ |
| 4305 | surrogate = outCh; |
| 4306 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4307 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4308 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4309 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4310 | } |
| 4311 | } |
| 4312 | } |
| 4313 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4314 | inShift = 0; |
| 4315 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4316 | if (surrogate) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4317 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4318 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4319 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4320 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4321 | if (base64bits > 0) { /* left-over bits */ |
| 4322 | if (base64bits >= 6) { |
| 4323 | /* We've seen at least one base-64 character */ |
| 4324 | errmsg = "partial character in shift sequence"; |
| 4325 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4326 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4327 | else { |
| 4328 | /* Some bits remain; they should be zero */ |
| 4329 | if (base64buffer != 0) { |
| 4330 | errmsg = "non-zero padding bits in shift sequence"; |
| 4331 | goto utf7Error; |
| 4332 | } |
| 4333 | } |
| 4334 | } |
| 4335 | if (ch != '-') { |
| 4336 | /* '-' is absorbed; other terminating |
| 4337 | characters are preserved */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4338 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4339 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4340 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4341 | } |
| 4342 | } |
| 4343 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4344 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4345 | s++; /* consume '+' */ |
| 4346 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4347 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4348 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4349 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4350 | } |
| 4351 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4352 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4353 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4355 | } |
| 4356 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4357 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4358 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4359 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4360 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4361 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4362 | else { |
| 4363 | startinpos = s-starts; |
| 4364 | s++; |
| 4365 | errmsg = "unexpected special character"; |
| 4366 | goto utf7Error; |
| 4367 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4368 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4370 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4371 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4372 | errors, &errorHandler, |
| 4373 | "utf7", errmsg, |
| 4374 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4375 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4376 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4379 | /* end of string */ |
| 4380 | |
| 4381 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4382 | /* if we're in an inconsistent state, that's an error */ |
| 4383 | if (surrogate || |
| 4384 | (base64bits >= 6) || |
| 4385 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4386 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4387 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4388 | errors, &errorHandler, |
| 4389 | "utf7", "unterminated shift sequence", |
| 4390 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4391 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4392 | goto onError; |
| 4393 | if (s < e) |
| 4394 | goto restart; |
| 4395 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4396 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4397 | |
| 4398 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4399 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4400 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4401 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4402 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4403 | } |
| 4404 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4405 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4406 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4407 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4408 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4409 | Py_XDECREF(errorHandler); |
| 4410 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4411 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4412 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4413 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4414 | Py_XDECREF(errorHandler); |
| 4415 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4416 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 | return NULL; |
| 4418 | } |
| 4419 | |
| 4420 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4421 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4422 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4423 | int base64SetO, |
| 4424 | int base64WhiteSpace, |
| 4425 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4426 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4427 | int kind; |
| 4428 | void *data; |
| 4429 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4430 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4431 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4432 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | unsigned int base64bits = 0; |
| 4434 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4435 | char * out; |
| 4436 | char * start; |
| 4437 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4438 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4439 | return NULL; |
| 4440 | kind = PyUnicode_KIND(str); |
| 4441 | data = PyUnicode_DATA(str); |
| 4442 | len = PyUnicode_GET_LENGTH(str); |
| 4443 | |
| 4444 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4446 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4447 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4448 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4449 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4450 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4451 | if (v == NULL) |
| 4452 | return NULL; |
| 4453 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4454 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4455 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4456 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4457 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4458 | if (inShift) { |
| 4459 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4460 | /* shifting out */ |
| 4461 | if (base64bits) { /* output remaining bits */ |
| 4462 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4463 | base64buffer = 0; |
| 4464 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4465 | } |
| 4466 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4468 | so no '-' is required, except if the character is itself a '-' */ |
| 4469 | if (IS_BASE64(ch) || ch == '-') { |
| 4470 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4471 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4472 | *out++ = (char) ch; |
| 4473 | } |
| 4474 | else { |
| 4475 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4476 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4477 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4478 | else { /* not in a shift sequence */ |
| 4479 | if (ch == '+') { |
| 4480 | *out++ = '+'; |
| 4481 | *out++ = '-'; |
| 4482 | } |
| 4483 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4484 | *out++ = (char) ch; |
| 4485 | } |
| 4486 | else { |
| 4487 | *out++ = '+'; |
| 4488 | inShift = 1; |
| 4489 | goto encode_char; |
| 4490 | } |
| 4491 | } |
| 4492 | continue; |
| 4493 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4494 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4495 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4496 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4497 | /* code first surrogate */ |
| 4498 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4499 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4500 | while (base64bits >= 6) { |
| 4501 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4502 | base64bits -= 6; |
| 4503 | } |
| 4504 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4505 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4506 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4507 | base64bits += 16; |
| 4508 | base64buffer = (base64buffer << 16) | ch; |
| 4509 | while (base64bits >= 6) { |
| 4510 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4511 | base64bits -= 6; |
| 4512 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4513 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4514 | if (base64bits) |
| 4515 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4516 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4517 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4518 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4519 | return NULL; |
| 4520 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4521 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4522 | PyObject * |
| 4523 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4524 | Py_ssize_t size, |
| 4525 | int base64SetO, |
| 4526 | int base64WhiteSpace, |
| 4527 | const char *errors) |
| 4528 | { |
| 4529 | PyObject *result; |
| 4530 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4531 | if (tmp == NULL) |
| 4532 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4533 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4534 | base64WhiteSpace, errors); |
| 4535 | Py_DECREF(tmp); |
| 4536 | return result; |
| 4537 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4538 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4539 | #undef IS_BASE64 |
| 4540 | #undef FROM_BASE64 |
| 4541 | #undef TO_BASE64 |
| 4542 | #undef DECODE_DIRECT |
| 4543 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4545 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4546 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4547 | PyObject * |
| 4548 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4549 | Py_ssize_t size, |
| 4550 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4551 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4552 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4553 | } |
| 4554 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4555 | #include "stringlib/asciilib.h" |
| 4556 | #include "stringlib/codecs.h" |
| 4557 | #include "stringlib/undef.h" |
| 4558 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4559 | #include "stringlib/ucs1lib.h" |
| 4560 | #include "stringlib/codecs.h" |
| 4561 | #include "stringlib/undef.h" |
| 4562 | |
| 4563 | #include "stringlib/ucs2lib.h" |
| 4564 | #include "stringlib/codecs.h" |
| 4565 | #include "stringlib/undef.h" |
| 4566 | |
| 4567 | #include "stringlib/ucs4lib.h" |
| 4568 | #include "stringlib/codecs.h" |
| 4569 | #include "stringlib/undef.h" |
| 4570 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4571 | /* Mask to quickly check whether a C 'long' contains a |
| 4572 | non-ASCII, UTF8-encoded char. */ |
| 4573 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4574 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4575 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4576 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4577 | #else |
| 4578 | # error C 'long' size should be either 4 or 8! |
| 4579 | #endif |
| 4580 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4581 | static Py_ssize_t |
| 4582 | 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] | 4583 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4584 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4585 | 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] | 4586 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4587 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4588 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4589 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4590 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4591 | an explanation. */ |
| 4592 | /* Help register allocation */ |
| 4593 | register const char *_p = p; |
| 4594 | register Py_UCS1 * q = dest; |
| 4595 | while (_p < aligned_end) { |
| 4596 | unsigned long value = *(const unsigned long *) _p; |
| 4597 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4598 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4599 | *((unsigned long *)q) = value; |
| 4600 | _p += SIZEOF_LONG; |
| 4601 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4602 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4603 | p = _p; |
| 4604 | while (p < end) { |
| 4605 | if ((unsigned char)*p & 0x80) |
| 4606 | break; |
| 4607 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4608 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4609 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4610 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4611 | #endif |
| 4612 | while (p < end) { |
| 4613 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4614 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4615 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4616 | /* Help register allocation */ |
| 4617 | register const char *_p = p; |
| 4618 | while (_p < aligned_end) { |
| 4619 | unsigned long value = *(unsigned long *) _p; |
| 4620 | if (value & ASCII_CHAR_MASK) |
| 4621 | break; |
| 4622 | _p += SIZEOF_LONG; |
| 4623 | } |
| 4624 | p = _p; |
| 4625 | if (_p == end) |
| 4626 | break; |
| 4627 | } |
| 4628 | if ((unsigned char)*p & 0x80) |
| 4629 | break; |
| 4630 | ++p; |
| 4631 | } |
| 4632 | memcpy(dest, start, p - start); |
| 4633 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4634 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4635 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4636 | PyObject * |
| 4637 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4638 | Py_ssize_t size, |
| 4639 | const char *errors, |
| 4640 | Py_ssize_t *consumed) |
| 4641 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4642 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4643 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4644 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4645 | |
| 4646 | Py_ssize_t startinpos; |
| 4647 | Py_ssize_t endinpos; |
| 4648 | const char *errmsg = ""; |
| 4649 | PyObject *errorHandler = NULL; |
| 4650 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4651 | |
| 4652 | if (size == 0) { |
| 4653 | if (consumed) |
| 4654 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4655 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4656 | } |
| 4657 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4658 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4659 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4660 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4661 | *consumed = 1; |
| 4662 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4663 | } |
| 4664 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4665 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4666 | writer.min_length = size; |
| 4667 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4668 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4669 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4670 | writer.pos = ascii_decode(s, end, writer.data); |
| 4671 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4672 | while (s < end) { |
| 4673 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4674 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4675 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4676 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4677 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4678 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4679 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4680 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4681 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4682 | } else { |
| 4683 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4684 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | switch (ch) { |
| 4688 | case 0: |
| 4689 | if (s == end || consumed) |
| 4690 | goto End; |
| 4691 | errmsg = "unexpected end of data"; |
| 4692 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4693 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4694 | break; |
| 4695 | case 1: |
| 4696 | errmsg = "invalid start byte"; |
| 4697 | startinpos = s - starts; |
| 4698 | endinpos = startinpos + 1; |
| 4699 | break; |
| 4700 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4701 | case 3: |
| 4702 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4703 | errmsg = "invalid continuation byte"; |
| 4704 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4705 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4706 | break; |
| 4707 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4708 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4709 | goto onError; |
| 4710 | continue; |
| 4711 | } |
| 4712 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4713 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4714 | errors, &errorHandler, |
| 4715 | "utf-8", errmsg, |
| 4716 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4717 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4718 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4719 | } |
| 4720 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4721 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4722 | if (consumed) |
| 4723 | *consumed = s - starts; |
| 4724 | |
| 4725 | Py_XDECREF(errorHandler); |
| 4726 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4727 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4728 | |
| 4729 | onError: |
| 4730 | Py_XDECREF(errorHandler); |
| 4731 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4732 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4733 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4734 | } |
| 4735 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4736 | #ifdef __APPLE__ |
| 4737 | |
| 4738 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4739 | used to decode the command line arguments on Mac OS X. |
| 4740 | |
| 4741 | Return a pointer to a newly allocated wide character string (use |
| 4742 | PyMem_Free() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4743 | |
| 4744 | wchar_t* |
| 4745 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4746 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4747 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4748 | wchar_t *unicode; |
| 4749 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4750 | |
| 4751 | /* Note: size will always be longer than the resulting Unicode |
| 4752 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4753 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4754 | return NULL; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4755 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4756 | if (!unicode) |
| 4757 | return NULL; |
| 4758 | |
| 4759 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4760 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4761 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4762 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4763 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4764 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4765 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4766 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4767 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4768 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4769 | if (ch > 0xFF) { |
| 4770 | #if SIZEOF_WCHAR_T == 4 |
| 4771 | assert(0); |
| 4772 | #else |
| 4773 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4774 | /* compute and append the two surrogates: */ |
| 4775 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4776 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4777 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4778 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4779 | else { |
| 4780 | if (!ch && s == e) |
| 4781 | break; |
| 4782 | /* surrogateescape */ |
| 4783 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4784 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4785 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4786 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4787 | return unicode; |
| 4788 | } |
| 4789 | |
| 4790 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4792 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4793 | |
| 4794 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4795 | and allocate exactly as much space needed at the end. Else allocate the |
| 4796 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4797 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4798 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4799 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4800 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4801 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4802 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4803 | void *data; |
| 4804 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4806 | if (!PyUnicode_Check(unicode)) { |
| 4807 | PyErr_BadArgument(); |
| 4808 | return NULL; |
| 4809 | } |
| 4810 | |
| 4811 | if (PyUnicode_READY(unicode) == -1) |
| 4812 | return NULL; |
| 4813 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4814 | if (PyUnicode_UTF8(unicode)) |
| 4815 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4816 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4817 | |
| 4818 | kind = PyUnicode_KIND(unicode); |
| 4819 | data = PyUnicode_DATA(unicode); |
| 4820 | size = PyUnicode_GET_LENGTH(unicode); |
| 4821 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4822 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4823 | default: |
| 4824 | assert(0); |
| 4825 | case PyUnicode_1BYTE_KIND: |
| 4826 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4827 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4828 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4829 | case PyUnicode_2BYTE_KIND: |
| 4830 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4831 | case PyUnicode_4BYTE_KIND: |
| 4832 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4833 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4836 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4837 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4838 | Py_ssize_t size, |
| 4839 | const char *errors) |
| 4840 | { |
| 4841 | PyObject *v, *unicode; |
| 4842 | |
| 4843 | unicode = PyUnicode_FromUnicode(s, size); |
| 4844 | if (unicode == NULL) |
| 4845 | return NULL; |
| 4846 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4847 | Py_DECREF(unicode); |
| 4848 | return v; |
| 4849 | } |
| 4850 | |
| 4851 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4852 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4853 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4854 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4857 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4858 | |
| 4859 | PyObject * |
| 4860 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4861 | Py_ssize_t size, |
| 4862 | const char *errors, |
| 4863 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4864 | { |
| 4865 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4866 | } |
| 4867 | |
| 4868 | PyObject * |
| 4869 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4870 | Py_ssize_t size, |
| 4871 | const char *errors, |
| 4872 | int *byteorder, |
| 4873 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4874 | { |
| 4875 | const char *starts = s; |
| 4876 | Py_ssize_t startinpos; |
| 4877 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4878 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4879 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4880 | int le, bo = 0; /* assume native ordering by default */ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4881 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4882 | PyObject *errorHandler = NULL; |
| 4883 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4884 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4885 | q = (unsigned char *)s; |
| 4886 | e = q + size; |
| 4887 | |
| 4888 | if (byteorder) |
| 4889 | bo = *byteorder; |
| 4890 | |
| 4891 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4892 | byte order setting accordingly. In native mode, the leading BOM |
| 4893 | mark is skipped, in all other modes, it is copied to the output |
| 4894 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4895 | if (bo == 0 && size >= 4) { |
| 4896 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4897 | if (bom == 0x0000FEFF) { |
| 4898 | bo = -1; |
| 4899 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4900 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4901 | else if (bom == 0xFFFE0000) { |
| 4902 | bo = 1; |
| 4903 | q += 4; |
| 4904 | } |
| 4905 | if (byteorder) |
| 4906 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4909 | if (q == e) { |
| 4910 | if (consumed) |
| 4911 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4912 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4915 | #ifdef WORDS_BIGENDIAN |
| 4916 | le = bo < 0; |
| 4917 | #else |
| 4918 | le = bo <= 0; |
| 4919 | #endif |
| 4920 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4921 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4922 | writer.min_length = (e - q + 3) / 4; |
| 4923 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4924 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4925 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4926 | while (1) { |
| 4927 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4928 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4929 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4930 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4931 | enum PyUnicode_Kind kind = writer.kind; |
| 4932 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4933 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4934 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4935 | if (le) { |
| 4936 | do { |
| 4937 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4938 | if (ch > maxch) |
| 4939 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4940 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4941 | q += 4; |
| 4942 | } while (q <= last); |
| 4943 | } |
| 4944 | else { |
| 4945 | do { |
| 4946 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 4947 | if (ch > maxch) |
| 4948 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4949 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4950 | q += 4; |
| 4951 | } while (q <= last); |
| 4952 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4953 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | if (ch <= maxch) { |
| 4957 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4958 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4959 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4960 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4961 | startinpos = ((const char *)q) - starts; |
| 4962 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4963 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4964 | else { |
| 4965 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4966 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4967 | goto onError; |
| 4968 | q += 4; |
| 4969 | continue; |
| 4970 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4971 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4972 | startinpos = ((const char *)q) - starts; |
| 4973 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4974 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4975 | |
| 4976 | /* The remaining input chars are ignored if the callback |
| 4977 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4978 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4979 | errors, &errorHandler, |
| 4980 | "utf32", errmsg, |
| 4981 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4982 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4983 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4986 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4987 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4988 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4989 | Py_XDECREF(errorHandler); |
| 4990 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4991 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4992 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4993 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4994 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4995 | Py_XDECREF(errorHandler); |
| 4996 | Py_XDECREF(exc); |
| 4997 | return NULL; |
| 4998 | } |
| 4999 | |
| 5000 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5001 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5002 | const char *errors, |
| 5003 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5004 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5005 | int kind; |
| 5006 | void *data; |
| 5007 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5008 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5009 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5010 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5011 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5012 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5013 | int iorder[] = {0, 1, 2, 3}; |
| 5014 | #else |
| 5015 | int iorder[] = {3, 2, 1, 0}; |
| 5016 | #endif |
| 5017 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5018 | #define STORECHAR(CH) \ |
| 5019 | do { \ |
| 5020 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5021 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5022 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5023 | p[iorder[0]] = (CH) & 0xff; \ |
| 5024 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5025 | } while(0) |
| 5026 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5027 | if (!PyUnicode_Check(str)) { |
| 5028 | PyErr_BadArgument(); |
| 5029 | return NULL; |
| 5030 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5031 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5032 | return NULL; |
| 5033 | kind = PyUnicode_KIND(str); |
| 5034 | data = PyUnicode_DATA(str); |
| 5035 | len = PyUnicode_GET_LENGTH(str); |
| 5036 | |
| 5037 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5038 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5039 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5040 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5041 | if (v == NULL) |
| 5042 | return NULL; |
| 5043 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5044 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5045 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5046 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5047 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5048 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5049 | |
| 5050 | if (byteorder == -1) { |
| 5051 | /* force LE */ |
| 5052 | iorder[0] = 0; |
| 5053 | iorder[1] = 1; |
| 5054 | iorder[2] = 2; |
| 5055 | iorder[3] = 3; |
| 5056 | } |
| 5057 | else if (byteorder == 1) { |
| 5058 | /* force BE */ |
| 5059 | iorder[0] = 3; |
| 5060 | iorder[1] = 2; |
| 5061 | iorder[2] = 1; |
| 5062 | iorder[3] = 0; |
| 5063 | } |
| 5064 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5065 | for (i = 0; i < len; i++) |
| 5066 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5067 | |
| 5068 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5069 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5070 | #undef STORECHAR |
| 5071 | } |
| 5072 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5073 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5074 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5075 | Py_ssize_t size, |
| 5076 | const char *errors, |
| 5077 | int byteorder) |
| 5078 | { |
| 5079 | PyObject *result; |
| 5080 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5081 | if (tmp == NULL) |
| 5082 | return NULL; |
| 5083 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5084 | Py_DECREF(tmp); |
| 5085 | return result; |
| 5086 | } |
| 5087 | |
| 5088 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5089 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5090 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5091 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5094 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5095 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5096 | PyObject * |
| 5097 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5098 | Py_ssize_t size, |
| 5099 | const char *errors, |
| 5100 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5101 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5102 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5103 | } |
| 5104 | |
| 5105 | PyObject * |
| 5106 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5107 | Py_ssize_t size, |
| 5108 | const char *errors, |
| 5109 | int *byteorder, |
| 5110 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5111 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5112 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5113 | Py_ssize_t startinpos; |
| 5114 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5115 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5116 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5117 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5118 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5119 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5120 | PyObject *errorHandler = NULL; |
| 5121 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5122 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5123 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5124 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5125 | |
| 5126 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5127 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5128 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5129 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5130 | byte order setting accordingly. In native mode, the leading BOM |
| 5131 | mark is skipped, in all other modes, it is copied to the output |
| 5132 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5133 | if (bo == 0 && size >= 2) { |
| 5134 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5135 | if (bom == 0xFEFF) { |
| 5136 | q += 2; |
| 5137 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5138 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5139 | else if (bom == 0xFFFE) { |
| 5140 | q += 2; |
| 5141 | bo = 1; |
| 5142 | } |
| 5143 | if (byteorder) |
| 5144 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5145 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5146 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5147 | if (q == e) { |
| 5148 | if (consumed) |
| 5149 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5150 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5151 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5152 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5153 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5154 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5155 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5156 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5157 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5158 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5159 | /* Note: size will always be longer than the resulting Unicode |
| 5160 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5161 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5162 | writer.min_length = (e - q + 1) / 2; |
| 5163 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5164 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5165 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5166 | while (1) { |
| 5167 | Py_UCS4 ch = 0; |
| 5168 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5169 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5170 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5171 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5172 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5173 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5174 | native_ordering); |
| 5175 | else |
| 5176 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5177 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5178 | native_ordering); |
| 5179 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5180 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5181 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5182 | native_ordering); |
| 5183 | } else { |
| 5184 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5185 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5186 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5187 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5188 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5189 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5190 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5191 | switch (ch) |
| 5192 | { |
| 5193 | case 0: |
| 5194 | /* remaining byte at the end? (size should be even) */ |
| 5195 | if (q == e || consumed) |
| 5196 | goto End; |
| 5197 | errmsg = "truncated data"; |
| 5198 | startinpos = ((const char *)q) - starts; |
| 5199 | endinpos = ((const char *)e) - starts; |
| 5200 | break; |
| 5201 | /* The remaining input chars are ignored if the callback |
| 5202 | chooses to skip the input */ |
| 5203 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5204 | q -= 2; |
| 5205 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5206 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5207 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5208 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5209 | endinpos = ((const char *)e) - starts; |
| 5210 | break; |
| 5211 | case 2: |
| 5212 | errmsg = "illegal encoding"; |
| 5213 | startinpos = ((const char *)q) - 2 - starts; |
| 5214 | endinpos = startinpos + 2; |
| 5215 | break; |
| 5216 | case 3: |
| 5217 | errmsg = "illegal UTF-16 surrogate"; |
| 5218 | startinpos = ((const char *)q) - 4 - starts; |
| 5219 | endinpos = startinpos + 2; |
| 5220 | break; |
| 5221 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5222 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5223 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5224 | continue; |
| 5225 | } |
| 5226 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5227 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5228 | errors, |
| 5229 | &errorHandler, |
| 5230 | "utf16", errmsg, |
| 5231 | &starts, |
| 5232 | (const char **)&e, |
| 5233 | &startinpos, |
| 5234 | &endinpos, |
| 5235 | &exc, |
| 5236 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5237 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5238 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5241 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5242 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5243 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5244 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5245 | Py_XDECREF(errorHandler); |
| 5246 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5247 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5248 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5249 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5250 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5251 | Py_XDECREF(errorHandler); |
| 5252 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5253 | return NULL; |
| 5254 | } |
| 5255 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5256 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5257 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5258 | const char *errors, |
| 5259 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5260 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5261 | enum PyUnicode_Kind kind; |
| 5262 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5263 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5264 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5265 | unsigned short *out; |
| 5266 | Py_ssize_t bytesize; |
| 5267 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5268 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5269 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5270 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5271 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5272 | #endif |
| 5273 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5274 | if (!PyUnicode_Check(str)) { |
| 5275 | PyErr_BadArgument(); |
| 5276 | return NULL; |
| 5277 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5278 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5279 | return NULL; |
| 5280 | kind = PyUnicode_KIND(str); |
| 5281 | data = PyUnicode_DATA(str); |
| 5282 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5283 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5284 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5285 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5286 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5287 | const Py_UCS4 *end = in + len; |
| 5288 | while (in < end) |
| 5289 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5290 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5291 | } |
| 5292 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5293 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5294 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5295 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5296 | if (v == NULL) |
| 5297 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5298 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5299 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5300 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5301 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5302 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5303 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5304 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5305 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5306 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5307 | switch (kind) { |
| 5308 | case PyUnicode_1BYTE_KIND: { |
| 5309 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5310 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5311 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5312 | case PyUnicode_2BYTE_KIND: { |
| 5313 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5314 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5315 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5316 | case PyUnicode_4BYTE_KIND: { |
| 5317 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5318 | break; |
| 5319 | } |
| 5320 | default: |
| 5321 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5322 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5323 | |
| 5324 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5325 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5328 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5329 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5330 | Py_ssize_t size, |
| 5331 | const char *errors, |
| 5332 | int byteorder) |
| 5333 | { |
| 5334 | PyObject *result; |
| 5335 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5336 | if (tmp == NULL) |
| 5337 | return NULL; |
| 5338 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5339 | Py_DECREF(tmp); |
| 5340 | return result; |
| 5341 | } |
| 5342 | |
| 5343 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5344 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5345 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5346 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5350 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5351 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5352 | if all the escapes in the string make it still a valid ASCII string. |
| 5353 | Returns -1 if any escapes were found which cause the string to |
| 5354 | pop out of ASCII range. Otherwise returns the length of the |
| 5355 | required buffer to hold the string. |
| 5356 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5357 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5358 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5359 | { |
| 5360 | const unsigned char *p = (const unsigned char *)s; |
| 5361 | const unsigned char *end = p + size; |
| 5362 | Py_ssize_t length = 0; |
| 5363 | |
| 5364 | if (size < 0) |
| 5365 | return -1; |
| 5366 | |
| 5367 | for (; p < end; ++p) { |
| 5368 | if (*p > 127) { |
| 5369 | /* Non-ASCII */ |
| 5370 | return -1; |
| 5371 | } |
| 5372 | else if (*p != '\\') { |
| 5373 | /* Normal character */ |
| 5374 | ++length; |
| 5375 | } |
| 5376 | else { |
| 5377 | /* Backslash-escape, check next char */ |
| 5378 | ++p; |
| 5379 | /* Escape sequence reaches till end of string or |
| 5380 | non-ASCII follow-up. */ |
| 5381 | if (p >= end || *p > 127) |
| 5382 | return -1; |
| 5383 | switch (*p) { |
| 5384 | case '\n': |
| 5385 | /* backslash + \n result in zero characters */ |
| 5386 | break; |
| 5387 | case '\\': case '\'': case '\"': |
| 5388 | case 'b': case 'f': case 't': |
| 5389 | case 'n': case 'r': case 'v': case 'a': |
| 5390 | ++length; |
| 5391 | break; |
| 5392 | case '0': case '1': case '2': case '3': |
| 5393 | case '4': case '5': case '6': case '7': |
| 5394 | case 'x': case 'u': case 'U': case 'N': |
| 5395 | /* these do not guarantee ASCII characters */ |
| 5396 | return -1; |
| 5397 | default: |
| 5398 | /* count the backslash + the other character */ |
| 5399 | length += 2; |
| 5400 | } |
| 5401 | } |
| 5402 | } |
| 5403 | return length; |
| 5404 | } |
| 5405 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5406 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5407 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5408 | PyObject * |
| 5409 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5410 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5411 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5413 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5414 | Py_ssize_t startinpos; |
| 5415 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5416 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5418 | char* message; |
| 5419 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5420 | PyObject *errorHandler = NULL; |
| 5421 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5422 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5423 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5424 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5425 | if (len == 0) |
| 5426 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5427 | |
| 5428 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5429 | either the string is pure ASCII with named escapes like \n, etc. |
| 5430 | and we determined it's exact size (common case) |
| 5431 | or it contains \x, \u, ... escape sequences. then we create a |
| 5432 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5433 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5434 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5435 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5436 | } |
| 5437 | else { |
| 5438 | /* Escaped strings will always be longer than the resulting |
| 5439 | Unicode string, so we start with size here and then reduce the |
| 5440 | length after conversion to the true value. |
| 5441 | (but if the error callback returns a long replacement string |
| 5442 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5443 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5444 | } |
| 5445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5447 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5450 | while (s < end) { |
| 5451 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5452 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5453 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | |
| 5455 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5456 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5457 | x = (unsigned char)*s; |
| 5458 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5459 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5460 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 | continue; |
| 5462 | } |
| 5463 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5464 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5465 | /* \ - Escapes */ |
| 5466 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5467 | c = *s++; |
| 5468 | if (s > end) |
| 5469 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5470 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5471 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5472 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5473 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5474 | #define WRITECHAR(ch) \ |
| 5475 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5476 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5477 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5478 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5479 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5481 | case '\\': WRITECHAR('\\'); break; |
| 5482 | case '\'': WRITECHAR('\''); break; |
| 5483 | case '\"': WRITECHAR('\"'); break; |
| 5484 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5485 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5486 | case 'f': WRITECHAR('\014'); break; |
| 5487 | case 't': WRITECHAR('\t'); break; |
| 5488 | case 'n': WRITECHAR('\n'); break; |
| 5489 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5490 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5491 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5492 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5493 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5494 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5495 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | case '0': case '1': case '2': case '3': |
| 5497 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5498 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5499 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5500 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5501 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5502 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5503 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5504 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5505 | break; |
| 5506 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5507 | /* hex escapes */ |
| 5508 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5510 | digits = 2; |
| 5511 | message = "truncated \\xXX escape"; |
| 5512 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5513 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5514 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5515 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5516 | digits = 4; |
| 5517 | message = "truncated \\uXXXX escape"; |
| 5518 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5520 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5521 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5522 | digits = 8; |
| 5523 | message = "truncated \\UXXXXXXXX escape"; |
| 5524 | hexescape: |
| 5525 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5526 | if (end - s < digits) { |
| 5527 | /* count only hex digits */ |
| 5528 | for (; s < end; ++s) { |
| 5529 | c = (unsigned char)*s; |
| 5530 | if (!Py_ISXDIGIT(c)) |
| 5531 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5532 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5533 | goto error; |
| 5534 | } |
| 5535 | for (; digits--; ++s) { |
| 5536 | c = (unsigned char)*s; |
| 5537 | if (!Py_ISXDIGIT(c)) |
| 5538 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5539 | chr = (chr<<4) & ~0xF; |
| 5540 | if (c >= '0' && c <= '9') |
| 5541 | chr += c - '0'; |
| 5542 | else if (c >= 'a' && c <= 'f') |
| 5543 | chr += 10 + c - 'a'; |
| 5544 | else |
| 5545 | chr += 10 + c - 'A'; |
| 5546 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5547 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5548 | /* _decoding_error will have already written into the |
| 5549 | target buffer. */ |
| 5550 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5551 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5552 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5553 | message = "illegal Unicode character"; |
| 5554 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5555 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5556 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5557 | break; |
| 5558 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5559 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5560 | case 'N': |
| 5561 | message = "malformed \\N character escape"; |
| 5562 | if (ucnhash_CAPI == NULL) { |
| 5563 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5564 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5565 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5566 | if (ucnhash_CAPI == NULL) |
| 5567 | goto ucnhashError; |
| 5568 | } |
| 5569 | if (*s == '{') { |
| 5570 | const char *start = s+1; |
| 5571 | /* look for the closing brace */ |
| 5572 | while (*s != '}' && s < end) |
| 5573 | s++; |
| 5574 | if (s > start && s < end && *s == '}') { |
| 5575 | /* found a name. look it up in the unicode database */ |
| 5576 | message = "unknown Unicode character name"; |
| 5577 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5578 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5579 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5580 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5581 | goto store; |
| 5582 | } |
| 5583 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5584 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5585 | |
| 5586 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5587 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5588 | message = "\\ at end of string"; |
| 5589 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5590 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5591 | } |
| 5592 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5593 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5594 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5595 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5596 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5598 | continue; |
| 5599 | |
| 5600 | error: |
| 5601 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5602 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5603 | errors, &errorHandler, |
| 5604 | "unicodeescape", message, |
| 5605 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5606 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5607 | goto onError; |
| 5608 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5609 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5610 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5611 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5612 | Py_XDECREF(errorHandler); |
| 5613 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5614 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5615 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5616 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5617 | PyErr_SetString( |
| 5618 | PyExc_UnicodeError, |
| 5619 | "\\N escapes not supported (can't load unicodedata module)" |
| 5620 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5621 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5622 | Py_XDECREF(errorHandler); |
| 5623 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5624 | return NULL; |
| 5625 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5626 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5627 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5628 | Py_XDECREF(errorHandler); |
| 5629 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | return NULL; |
| 5631 | } |
| 5632 | |
| 5633 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5634 | |
| 5635 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5636 | appropriate. |
| 5637 | |
| 5638 | */ |
| 5639 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5640 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5641 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5643 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5644 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5646 | int kind; |
| 5647 | void *data; |
| 5648 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5649 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5650 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5651 | escape. |
| 5652 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5653 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5654 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5655 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5656 | */ |
| 5657 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5658 | if (!PyUnicode_Check(unicode)) { |
| 5659 | PyErr_BadArgument(); |
| 5660 | return NULL; |
| 5661 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5662 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5663 | return NULL; |
| 5664 | len = PyUnicode_GET_LENGTH(unicode); |
| 5665 | kind = PyUnicode_KIND(unicode); |
| 5666 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5667 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5668 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5669 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5670 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5671 | } |
| 5672 | |
| 5673 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5674 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5675 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5676 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5677 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5678 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5679 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5680 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5681 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5682 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5683 | if (repr == NULL) |
| 5684 | return NULL; |
| 5685 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5686 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5688 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5689 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5690 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5691 | /* Escape backslashes */ |
| 5692 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5693 | *p++ = '\\'; |
| 5694 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5695 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5696 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5697 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5698 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5699 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5700 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5701 | *p++ = '\\'; |
| 5702 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5703 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5704 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5705 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5706 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5707 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5708 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5709 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5710 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5711 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5712 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5713 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5715 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5716 | *p++ = '\\'; |
| 5717 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5718 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5719 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5720 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5721 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5722 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5723 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5724 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5725 | else if (ch == '\t') { |
| 5726 | *p++ = '\\'; |
| 5727 | *p++ = 't'; |
| 5728 | } |
| 5729 | else if (ch == '\n') { |
| 5730 | *p++ = '\\'; |
| 5731 | *p++ = 'n'; |
| 5732 | } |
| 5733 | else if (ch == '\r') { |
| 5734 | *p++ = '\\'; |
| 5735 | *p++ = 'r'; |
| 5736 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5737 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5738 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5739 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5741 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5742 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5743 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5744 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5745 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | /* Copy everything else as-is */ |
| 5747 | else |
| 5748 | *p++ = (char) ch; |
| 5749 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5750 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5751 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5752 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5753 | return NULL; |
| 5754 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 | } |
| 5756 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5757 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5758 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5759 | Py_ssize_t size) |
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 | PyObject *result; |
| 5762 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5763 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5765 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5766 | Py_DECREF(tmp); |
| 5767 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
| 5770 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5771 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5772 | PyObject * |
| 5773 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5774 | Py_ssize_t size, |
| 5775 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5776 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5777 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5778 | Py_ssize_t startinpos; |
| 5779 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5780 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5781 | const char *end; |
| 5782 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5783 | PyObject *errorHandler = NULL; |
| 5784 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5785 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5786 | if (size == 0) |
| 5787 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5788 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 | /* Escaped strings will always be longer than the resulting |
| 5790 | 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] | 5791 | length after conversion to the true value. (But decoding error |
| 5792 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5793 | _PyUnicodeWriter_Init(&writer); |
| 5794 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | end = s + size; |
| 5797 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5798 | unsigned char c; |
| 5799 | Py_UCS4 x; |
| 5800 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5801 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5803 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5804 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5805 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5806 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5807 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5808 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5809 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5810 | startinpos = s-starts; |
| 5811 | |
| 5812 | /* \u-escapes are only interpreted iff the number of leading |
| 5813 | backslashes if odd */ |
| 5814 | bs = s; |
| 5815 | for (;s < end;) { |
| 5816 | if (*s != '\\') |
| 5817 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5818 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5819 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5820 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5821 | } |
| 5822 | if (((s - bs) & 1) == 0 || |
| 5823 | s >= end || |
| 5824 | (*s != 'u' && *s != 'U')) { |
| 5825 | continue; |
| 5826 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5827 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5828 | count = *s=='u' ? 4 : 8; |
| 5829 | s++; |
| 5830 | |
| 5831 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5833 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5834 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5835 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5836 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5837 | errors, &errorHandler, |
| 5838 | "rawunicodeescape", "truncated \\uXXXX", |
| 5839 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5840 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5841 | goto onError; |
| 5842 | goto nextByte; |
| 5843 | } |
| 5844 | x = (x<<4) & ~0xF; |
| 5845 | if (c >= '0' && c <= '9') |
| 5846 | x += c - '0'; |
| 5847 | else if (c >= 'a' && c <= 'f') |
| 5848 | x += 10 + c - 'a'; |
| 5849 | else |
| 5850 | x += 10 + c - 'A'; |
| 5851 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5852 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5853 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5854 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5855 | } |
| 5856 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5857 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5858 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5859 | errors, &errorHandler, |
| 5860 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5861 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5862 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5863 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5864 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5865 | nextByte: |
| 5866 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5868 | Py_XDECREF(errorHandler); |
| 5869 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5870 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5871 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5872 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5873 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5874 | Py_XDECREF(errorHandler); |
| 5875 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | return NULL; |
| 5877 | } |
| 5878 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5879 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5880 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5881 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5882 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5883 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | char *p; |
| 5885 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5886 | Py_ssize_t expandsize, pos; |
| 5887 | int kind; |
| 5888 | void *data; |
| 5889 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5891 | if (!PyUnicode_Check(unicode)) { |
| 5892 | PyErr_BadArgument(); |
| 5893 | return NULL; |
| 5894 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5895 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5896 | return NULL; |
| 5897 | kind = PyUnicode_KIND(unicode); |
| 5898 | data = PyUnicode_DATA(unicode); |
| 5899 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5900 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 5901 | bytes, and 1 byte characters 4. */ |
| 5902 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5903 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5904 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5905 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5906 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5907 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | if (repr == NULL) |
| 5909 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5910 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5911 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5913 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5914 | for (pos = 0; pos < len; pos++) { |
| 5915 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5916 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5917 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5918 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5919 | *p++ = '\\'; |
| 5920 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5921 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 5922 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 5923 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 5924 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 5925 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5926 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5927 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5928 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5929 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5930 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5931 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | *p++ = '\\'; |
| 5933 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5934 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5935 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5936 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5937 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5939 | /* Copy everything else as-is */ |
| 5940 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5941 | *p++ = (char) ch; |
| 5942 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5943 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5944 | assert(p > q); |
| 5945 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5946 | return NULL; |
| 5947 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5950 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5951 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 5952 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5954 | PyObject *result; |
| 5955 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5956 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5957 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5958 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 5959 | Py_DECREF(tmp); |
| 5960 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5963 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5964 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5965 | PyObject * |
| 5966 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5967 | Py_ssize_t size, |
| 5968 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5969 | { |
| 5970 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5971 | Py_ssize_t startinpos; |
| 5972 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5973 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5974 | const char *end; |
| 5975 | const char *reason; |
| 5976 | PyObject *errorHandler = NULL; |
| 5977 | PyObject *exc = NULL; |
| 5978 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5979 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 5980 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5981 | 1)) |
| 5982 | return NULL; |
| 5983 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5984 | if (size == 0) |
| 5985 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5986 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5987 | _PyUnicodeWriter_Init(&writer); |
| 5988 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 5989 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5990 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5991 | } |
| 5992 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5993 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5994 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5995 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 5996 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 5997 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 5998 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 5999 | endinpos = end-starts; |
| 6000 | reason = "truncated input"; |
| 6001 | goto error; |
| 6002 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6003 | /* We copy the raw representation one byte at a time because the |
| 6004 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6005 | ((char *) &uch)[0] = s[0]; |
| 6006 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6007 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6008 | ((char *) &uch)[2] = s[2]; |
| 6009 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6010 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6011 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6012 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6013 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6014 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6015 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6016 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6017 | reason = "illegal code point (> 0x10FFFF)"; |
| 6018 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6019 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6020 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6021 | s += Py_UNICODE_SIZE; |
| 6022 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6023 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6024 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6025 | Py_UNICODE uch2; |
| 6026 | ((char *) &uch2)[0] = s[0]; |
| 6027 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6028 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6029 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6030 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6031 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6032 | } |
| 6033 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6034 | #endif |
| 6035 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6036 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6037 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6038 | continue; |
| 6039 | |
| 6040 | error: |
| 6041 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6042 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6043 | errors, &errorHandler, |
| 6044 | "unicode_internal", reason, |
| 6045 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6046 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6047 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6050 | Py_XDECREF(errorHandler); |
| 6051 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6052 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6053 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6055 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6056 | Py_XDECREF(errorHandler); |
| 6057 | Py_XDECREF(exc); |
| 6058 | return NULL; |
| 6059 | } |
| 6060 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6062 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6063 | PyObject * |
| 6064 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6065 | Py_ssize_t size, |
| 6066 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6069 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6072 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6073 | static void |
| 6074 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6075 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6076 | PyObject *unicode, |
| 6077 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6078 | const char *reason) |
| 6079 | { |
| 6080 | if (*exceptionObject == NULL) { |
| 6081 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6082 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6083 | encoding, unicode, startpos, endpos, reason); |
| 6084 | } |
| 6085 | else { |
| 6086 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6087 | goto onError; |
| 6088 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6089 | goto onError; |
| 6090 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6091 | goto onError; |
| 6092 | return; |
| 6093 | onError: |
| 6094 | Py_DECREF(*exceptionObject); |
| 6095 | *exceptionObject = NULL; |
| 6096 | } |
| 6097 | } |
| 6098 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6099 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6100 | static void |
| 6101 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6102 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6103 | PyObject *unicode, |
| 6104 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6105 | const char *reason) |
| 6106 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6107 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6108 | encoding, unicode, startpos, endpos, reason); |
| 6109 | if (*exceptionObject != NULL) |
| 6110 | PyCodec_StrictErrors(*exceptionObject); |
| 6111 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6112 | |
| 6113 | /* error handling callback helper: |
| 6114 | build arguments, call the callback and check the arguments, |
| 6115 | put the result into newpos and return the replacement string, which |
| 6116 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6117 | static PyObject * |
| 6118 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6119 | PyObject **errorHandler, |
| 6120 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6121 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6122 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6123 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6124 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6125 | 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] | 6126 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6127 | PyObject *restuple; |
| 6128 | PyObject *resunicode; |
| 6129 | |
| 6130 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6131 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6132 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6133 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6136 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6137 | return NULL; |
| 6138 | len = PyUnicode_GET_LENGTH(unicode); |
| 6139 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6140 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6141 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6142 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6143 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6144 | |
| 6145 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6146 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6147 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6148 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6149 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6150 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6151 | Py_DECREF(restuple); |
| 6152 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6153 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6154 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6155 | &resunicode, newpos)) { |
| 6156 | Py_DECREF(restuple); |
| 6157 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6158 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6159 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6160 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6161 | Py_DECREF(restuple); |
| 6162 | return NULL; |
| 6163 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6164 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6165 | *newpos = len + *newpos; |
| 6166 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6168 | Py_DECREF(restuple); |
| 6169 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6170 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6171 | Py_INCREF(resunicode); |
| 6172 | Py_DECREF(restuple); |
| 6173 | return resunicode; |
| 6174 | } |
| 6175 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6176 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6177 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6178 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6179 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6180 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6181 | /* input state */ |
| 6182 | Py_ssize_t pos=0, size; |
| 6183 | int kind; |
| 6184 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6185 | /* output object */ |
| 6186 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6187 | /* pointer into the output */ |
| 6188 | char *str; |
| 6189 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6190 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6191 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6192 | 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] | 6193 | PyObject *errorHandler = NULL; |
| 6194 | PyObject *exc = NULL; |
| 6195 | /* the following variable is used for caching string comparisons |
| 6196 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6197 | int known_errorHandler = -1; |
| 6198 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6199 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6200 | return NULL; |
| 6201 | size = PyUnicode_GET_LENGTH(unicode); |
| 6202 | kind = PyUnicode_KIND(unicode); |
| 6203 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6204 | /* allocate enough for a simple encoding without |
| 6205 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6206 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6207 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6208 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6209 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6210 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6211 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6212 | ressize = size; |
| 6213 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6214 | while (pos < size) { |
| 6215 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6216 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6217 | /* can we encode this? */ |
| 6218 | if (c<limit) { |
| 6219 | /* no overflow check, because we know that the space is enough */ |
| 6220 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6221 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6222 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6223 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6224 | Py_ssize_t requiredsize; |
| 6225 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6226 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6228 | Py_ssize_t collstart = pos; |
| 6229 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6230 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6231 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6232 | ++collend; |
| 6233 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6234 | if (known_errorHandler==-1) { |
| 6235 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6236 | known_errorHandler = 1; |
| 6237 | else if (!strcmp(errors, "replace")) |
| 6238 | known_errorHandler = 2; |
| 6239 | else if (!strcmp(errors, "ignore")) |
| 6240 | known_errorHandler = 3; |
| 6241 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6242 | known_errorHandler = 4; |
| 6243 | else |
| 6244 | known_errorHandler = 0; |
| 6245 | } |
| 6246 | switch (known_errorHandler) { |
| 6247 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6248 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6249 | goto onError; |
| 6250 | case 2: /* replace */ |
| 6251 | while (collstart++<collend) |
| 6252 | *str++ = '?'; /* fall through */ |
| 6253 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6254 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6255 | break; |
| 6256 | case 4: /* xmlcharrefreplace */ |
| 6257 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6258 | /* determine replacement size */ |
| 6259 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6260 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6261 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6263 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6264 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6265 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6266 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6267 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6268 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6269 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6270 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6271 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6272 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6273 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6274 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6275 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6276 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6278 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6279 | if (requiredsize > ressize) { |
| 6280 | if (requiredsize<2*ressize) |
| 6281 | requiredsize = 2*ressize; |
| 6282 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6283 | goto onError; |
| 6284 | str = PyBytes_AS_STRING(res) + respos; |
| 6285 | ressize = requiredsize; |
| 6286 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6287 | /* generate replacement */ |
| 6288 | for (i = collstart; i < collend; ++i) { |
| 6289 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6290 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6291 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6292 | break; |
| 6293 | default: |
| 6294 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6295 | encoding, reason, unicode, &exc, |
| 6296 | collstart, collend, &newpos); |
| 6297 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6298 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6299 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6300 | if (PyBytes_Check(repunicode)) { |
| 6301 | /* Directly copy bytes result to output. */ |
| 6302 | repsize = PyBytes_Size(repunicode); |
| 6303 | if (repsize > 1) { |
| 6304 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6305 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6306 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6307 | Py_DECREF(repunicode); |
| 6308 | goto onError; |
| 6309 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6310 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6311 | ressize += repsize-1; |
| 6312 | } |
| 6313 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6314 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6315 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6316 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6317 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6318 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6319 | /* need more space? (at least enough for what we |
| 6320 | have+the replacement+the rest of the string, so |
| 6321 | we won't have to check space for encodable characters) */ |
| 6322 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6323 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6324 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6325 | if (requiredsize > ressize) { |
| 6326 | if (requiredsize<2*ressize) |
| 6327 | requiredsize = 2*ressize; |
| 6328 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6329 | Py_DECREF(repunicode); |
| 6330 | goto onError; |
| 6331 | } |
| 6332 | str = PyBytes_AS_STRING(res) + respos; |
| 6333 | ressize = requiredsize; |
| 6334 | } |
| 6335 | /* check if there is anything unencodable in the replacement |
| 6336 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6337 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6338 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6339 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6340 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6341 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6342 | Py_DECREF(repunicode); |
| 6343 | goto onError; |
| 6344 | } |
| 6345 | *str = (char)c; |
| 6346 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6347 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6348 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6349 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6350 | } |
| 6351 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6352 | /* Resize if we allocated to much */ |
| 6353 | size = str - PyBytes_AS_STRING(res); |
| 6354 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6355 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6356 | if (_PyBytes_Resize(&res, size) < 0) |
| 6357 | goto onError; |
| 6358 | } |
| 6359 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6360 | Py_XDECREF(errorHandler); |
| 6361 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6362 | return res; |
| 6363 | |
| 6364 | onError: |
| 6365 | Py_XDECREF(res); |
| 6366 | Py_XDECREF(errorHandler); |
| 6367 | Py_XDECREF(exc); |
| 6368 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6371 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6372 | PyObject * |
| 6373 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6374 | Py_ssize_t size, |
| 6375 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6376 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6377 | PyObject *result; |
| 6378 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6379 | if (unicode == NULL) |
| 6380 | return NULL; |
| 6381 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6382 | Py_DECREF(unicode); |
| 6383 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6386 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6387 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6388 | { |
| 6389 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6390 | PyErr_BadArgument(); |
| 6391 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6392 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6393 | if (PyUnicode_READY(unicode) == -1) |
| 6394 | return NULL; |
| 6395 | /* Fast path: if it is a one-byte string, construct |
| 6396 | bytes object directly. */ |
| 6397 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6398 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6399 | PyUnicode_GET_LENGTH(unicode)); |
| 6400 | /* Non-Latin-1 characters present. Defer to above function to |
| 6401 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6402 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6403 | } |
| 6404 | |
| 6405 | PyObject* |
| 6406 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6407 | { |
| 6408 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6412 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6413 | PyObject * |
| 6414 | PyUnicode_DecodeASCII(const char *s, |
| 6415 | Py_ssize_t size, |
| 6416 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6418 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6419 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6420 | int kind; |
| 6421 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6422 | Py_ssize_t startinpos; |
| 6423 | Py_ssize_t endinpos; |
| 6424 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6425 | const char *e; |
| 6426 | PyObject *errorHandler = NULL; |
| 6427 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6430 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6433 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6434 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6435 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6436 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6437 | writer.min_length = size; |
| 6438 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6439 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6440 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6441 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6442 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6443 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6444 | writer.pos = outpos; |
| 6445 | if (writer.pos == size) |
| 6446 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6447 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6448 | s += writer.pos; |
| 6449 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6450 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6451 | register unsigned char c = (unsigned char)*s; |
| 6452 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6453 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6454 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6455 | ++s; |
| 6456 | } |
| 6457 | else { |
| 6458 | startinpos = s-starts; |
| 6459 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6460 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6461 | errors, &errorHandler, |
| 6462 | "ascii", "ordinal not in range(128)", |
| 6463 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6464 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6465 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6466 | kind = writer.kind; |
| 6467 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6468 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6470 | Py_XDECREF(errorHandler); |
| 6471 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6472 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6473 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6474 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6475 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6476 | Py_XDECREF(errorHandler); |
| 6477 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6478 | return NULL; |
| 6479 | } |
| 6480 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6481 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6482 | PyObject * |
| 6483 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6484 | Py_ssize_t size, |
| 6485 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6487 | PyObject *result; |
| 6488 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6489 | if (unicode == NULL) |
| 6490 | return NULL; |
| 6491 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6492 | Py_DECREF(unicode); |
| 6493 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6494 | } |
| 6495 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6496 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6497 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | { |
| 6499 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6500 | PyErr_BadArgument(); |
| 6501 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6502 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6503 | if (PyUnicode_READY(unicode) == -1) |
| 6504 | return NULL; |
| 6505 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6506 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6507 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6508 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6509 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6510 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6511 | } |
| 6512 | |
| 6513 | PyObject * |
| 6514 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6515 | { |
| 6516 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6517 | } |
| 6518 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6519 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6520 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6521 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6522 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6523 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6524 | #define NEED_RETRY |
| 6525 | #endif |
| 6526 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6527 | #ifndef WC_ERR_INVALID_CHARS |
| 6528 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6529 | #endif |
| 6530 | |
| 6531 | static char* |
| 6532 | code_page_name(UINT code_page, PyObject **obj) |
| 6533 | { |
| 6534 | *obj = NULL; |
| 6535 | if (code_page == CP_ACP) |
| 6536 | return "mbcs"; |
| 6537 | if (code_page == CP_UTF7) |
| 6538 | return "CP_UTF7"; |
| 6539 | if (code_page == CP_UTF8) |
| 6540 | return "CP_UTF8"; |
| 6541 | |
| 6542 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6543 | if (*obj == NULL) |
| 6544 | return NULL; |
| 6545 | return PyBytes_AS_STRING(*obj); |
| 6546 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6547 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6548 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6549 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6550 | { |
| 6551 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6552 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6553 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6554 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6555 | return 0; |
| 6556 | |
| 6557 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6558 | if (prev == curr) |
| 6559 | return 1; |
| 6560 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6561 | as it assumes an incomplete character consists of a single |
| 6562 | byte. */ |
| 6563 | if (curr - prev == 2) |
| 6564 | return 1; |
| 6565 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6566 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6567 | return 0; |
| 6568 | } |
| 6569 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6570 | static DWORD |
| 6571 | decode_code_page_flags(UINT code_page) |
| 6572 | { |
| 6573 | if (code_page == CP_UTF7) { |
| 6574 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6575 | return 0; |
| 6576 | } |
| 6577 | else |
| 6578 | return MB_ERR_INVALID_CHARS; |
| 6579 | } |
| 6580 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6581 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6582 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6583 | * mode. |
| 6584 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6585 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6586 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6587 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6588 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6589 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6590 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6591 | const char *in, |
| 6592 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6593 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6594 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6595 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6596 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6597 | |
| 6598 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6599 | assert(insize > 0); |
| 6600 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6601 | if (outsize <= 0) |
| 6602 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6603 | |
| 6604 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6605 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6606 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6607 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6608 | if (*v == NULL) |
| 6609 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6610 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6611 | } |
| 6612 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6613 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6614 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6615 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6616 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6617 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6618 | } |
| 6619 | |
| 6620 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6621 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6622 | if (outsize <= 0) |
| 6623 | goto error; |
| 6624 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6625 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6626 | error: |
| 6627 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6628 | return -2; |
| 6629 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6630 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6631 | } |
| 6632 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6633 | /* |
| 6634 | * Decode a byte string from a code page into unicode object with an error |
| 6635 | * handler. |
| 6636 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6637 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6638 | * UnicodeDecodeError exception and returns -1 on error. |
| 6639 | */ |
| 6640 | static int |
| 6641 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6642 | PyObject **v, |
| 6643 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6644 | const char *errors) |
| 6645 | { |
| 6646 | const char *startin = in; |
| 6647 | const char *endin = in + size; |
| 6648 | const DWORD flags = decode_code_page_flags(code_page); |
| 6649 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6650 | 2000 English version of the message. */ |
| 6651 | const char *reason = "No mapping for the Unicode character exists " |
| 6652 | "in the target code page."; |
| 6653 | /* each step cannot decode more than 1 character, but a character can be |
| 6654 | represented as a surrogate pair */ |
| 6655 | wchar_t buffer[2], *startout, *out; |
| 6656 | int insize, outsize; |
| 6657 | PyObject *errorHandler = NULL; |
| 6658 | PyObject *exc = NULL; |
| 6659 | PyObject *encoding_obj = NULL; |
| 6660 | char *encoding; |
| 6661 | DWORD err; |
| 6662 | int ret = -1; |
| 6663 | |
| 6664 | assert(size > 0); |
| 6665 | |
| 6666 | encoding = code_page_name(code_page, &encoding_obj); |
| 6667 | if (encoding == NULL) |
| 6668 | return -1; |
| 6669 | |
| 6670 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6671 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6672 | UnicodeDecodeError. */ |
| 6673 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6674 | if (exc != NULL) { |
| 6675 | PyCodec_StrictErrors(exc); |
| 6676 | Py_CLEAR(exc); |
| 6677 | } |
| 6678 | goto error; |
| 6679 | } |
| 6680 | |
| 6681 | if (*v == NULL) { |
| 6682 | /* Create unicode object */ |
| 6683 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6684 | PyErr_NoMemory(); |
| 6685 | goto error; |
| 6686 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6687 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6688 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6689 | if (*v == NULL) |
| 6690 | goto error; |
| 6691 | startout = PyUnicode_AS_UNICODE(*v); |
| 6692 | } |
| 6693 | else { |
| 6694 | /* Extend unicode object */ |
| 6695 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6696 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6697 | PyErr_NoMemory(); |
| 6698 | goto error; |
| 6699 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6700 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6701 | goto error; |
| 6702 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6703 | } |
| 6704 | |
| 6705 | /* Decode the byte string character per character */ |
| 6706 | out = startout; |
| 6707 | while (in < endin) |
| 6708 | { |
| 6709 | /* Decode a character */ |
| 6710 | insize = 1; |
| 6711 | do |
| 6712 | { |
| 6713 | outsize = MultiByteToWideChar(code_page, flags, |
| 6714 | in, insize, |
| 6715 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6716 | if (outsize > 0) |
| 6717 | break; |
| 6718 | err = GetLastError(); |
| 6719 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6720 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6721 | { |
| 6722 | PyErr_SetFromWindowsErr(0); |
| 6723 | goto error; |
| 6724 | } |
| 6725 | insize++; |
| 6726 | } |
| 6727 | /* 4=maximum length of a UTF-8 sequence */ |
| 6728 | while (insize <= 4 && (in + insize) <= endin); |
| 6729 | |
| 6730 | if (outsize <= 0) { |
| 6731 | Py_ssize_t startinpos, endinpos, outpos; |
| 6732 | |
| 6733 | startinpos = in - startin; |
| 6734 | endinpos = startinpos + 1; |
| 6735 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6736 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6737 | errors, &errorHandler, |
| 6738 | encoding, reason, |
| 6739 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6740 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6741 | { |
| 6742 | goto error; |
| 6743 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6744 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6745 | } |
| 6746 | else { |
| 6747 | in += insize; |
| 6748 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6749 | out += outsize; |
| 6750 | } |
| 6751 | } |
| 6752 | |
| 6753 | /* write a NUL character at the end */ |
| 6754 | *out = 0; |
| 6755 | |
| 6756 | /* Extend unicode object */ |
| 6757 | outsize = out - startout; |
| 6758 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6759 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6760 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6761 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6762 | |
| 6763 | error: |
| 6764 | Py_XDECREF(encoding_obj); |
| 6765 | Py_XDECREF(errorHandler); |
| 6766 | Py_XDECREF(exc); |
| 6767 | return ret; |
| 6768 | } |
| 6769 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6770 | static PyObject * |
| 6771 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6772 | const char *s, Py_ssize_t size, |
| 6773 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6774 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6775 | PyObject *v = NULL; |
| 6776 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6777 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6778 | if (code_page < 0) { |
| 6779 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6780 | return NULL; |
| 6781 | } |
| 6782 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6783 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6784 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6785 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6786 | do |
| 6787 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6788 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6789 | if (size > INT_MAX) { |
| 6790 | chunk_size = INT_MAX; |
| 6791 | final = 0; |
| 6792 | done = 0; |
| 6793 | } |
| 6794 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6795 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6796 | { |
| 6797 | chunk_size = (int)size; |
| 6798 | final = (consumed == NULL); |
| 6799 | done = 1; |
| 6800 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6801 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6802 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6803 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6804 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6805 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6806 | if (chunk_size == 0 && done) { |
| 6807 | if (v != NULL) |
| 6808 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6809 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6810 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6811 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6812 | |
| 6813 | converted = decode_code_page_strict(code_page, &v, |
| 6814 | s, chunk_size); |
| 6815 | if (converted == -2) |
| 6816 | converted = decode_code_page_errors(code_page, &v, |
| 6817 | s, chunk_size, |
| 6818 | errors); |
| 6819 | assert(converted != 0); |
| 6820 | |
| 6821 | if (converted < 0) { |
| 6822 | Py_XDECREF(v); |
| 6823 | return NULL; |
| 6824 | } |
| 6825 | |
| 6826 | if (consumed) |
| 6827 | *consumed += converted; |
| 6828 | |
| 6829 | s += converted; |
| 6830 | size -= converted; |
| 6831 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6832 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6833 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6836 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6837 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6838 | const char *s, |
| 6839 | Py_ssize_t size, |
| 6840 | const char *errors, |
| 6841 | Py_ssize_t *consumed) |
| 6842 | { |
| 6843 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6844 | } |
| 6845 | |
| 6846 | PyObject * |
| 6847 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6848 | Py_ssize_t size, |
| 6849 | const char *errors, |
| 6850 | Py_ssize_t *consumed) |
| 6851 | { |
| 6852 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6853 | } |
| 6854 | |
| 6855 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6856 | PyUnicode_DecodeMBCS(const char *s, |
| 6857 | Py_ssize_t size, |
| 6858 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6859 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6860 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6861 | } |
| 6862 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6863 | static DWORD |
| 6864 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6865 | { |
| 6866 | if (code_page == CP_UTF8) { |
| 6867 | if (winver.dwMajorVersion >= 6) |
| 6868 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6869 | and later */ |
| 6870 | return WC_ERR_INVALID_CHARS; |
| 6871 | else |
| 6872 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 6873 | return 0; |
| 6874 | } |
| 6875 | else if (code_page == CP_UTF7) { |
| 6876 | /* CP_UTF7 only supports flags=0 */ |
| 6877 | return 0; |
| 6878 | } |
| 6879 | else { |
| 6880 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 6881 | return 0; |
| 6882 | else |
| 6883 | return WC_NO_BEST_FIT_CHARS; |
| 6884 | } |
| 6885 | } |
| 6886 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6887 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6888 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 6889 | * mode. |
| 6890 | * |
| 6891 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6892 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6893 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6894 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6895 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6896 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6897 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6898 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6899 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6900 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 6901 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6902 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6903 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6904 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6905 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 6906 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6907 | /* Create a substring so that we can get the UTF-16 representation |
| 6908 | of just the slice under consideration. */ |
| 6909 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6910 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6911 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6912 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6913 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6914 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6915 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6916 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6917 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6918 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 6919 | if (substring == NULL) |
| 6920 | return -1; |
| 6921 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 6922 | if (p == NULL) { |
| 6923 | Py_DECREF(substring); |
| 6924 | return -1; |
| 6925 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6926 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6927 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6928 | outsize = WideCharToMultiByte(code_page, flags, |
| 6929 | p, size, |
| 6930 | NULL, 0, |
| 6931 | NULL, pusedDefaultChar); |
| 6932 | if (outsize <= 0) |
| 6933 | goto error; |
| 6934 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6935 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 6936 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6937 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6938 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6939 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6940 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6941 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6942 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6943 | if (*outbytes == NULL) { |
| 6944 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6945 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6946 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6947 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6948 | } |
| 6949 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6950 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6951 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 6952 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 6953 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6954 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6955 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6956 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6957 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 6958 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6959 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6960 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6961 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6965 | outsize = WideCharToMultiByte(code_page, flags, |
| 6966 | p, size, |
| 6967 | out, outsize, |
| 6968 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6969 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6970 | if (outsize <= 0) |
| 6971 | goto error; |
| 6972 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6973 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6974 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6975 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6976 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6977 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6978 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6979 | return -2; |
| 6980 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6981 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6984 | /* |
| 6985 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 6986 | * error handler. |
| 6987 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6988 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6989 | * -1 on other error. |
| 6990 | */ |
| 6991 | static int |
| 6992 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 6993 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6994 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6995 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6996 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6997 | Py_ssize_t pos = unicode_offset; |
| 6998 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6999 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7000 | 2000 English version of the message. */ |
| 7001 | const char *reason = "invalid character"; |
| 7002 | /* 4=maximum length of a UTF-8 sequence */ |
| 7003 | char buffer[4]; |
| 7004 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7005 | Py_ssize_t outsize; |
| 7006 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7007 | PyObject *errorHandler = NULL; |
| 7008 | PyObject *exc = NULL; |
| 7009 | PyObject *encoding_obj = NULL; |
| 7010 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7011 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7012 | PyObject *rep; |
| 7013 | int ret = -1; |
| 7014 | |
| 7015 | assert(insize > 0); |
| 7016 | |
| 7017 | encoding = code_page_name(code_page, &encoding_obj); |
| 7018 | if (encoding == NULL) |
| 7019 | return -1; |
| 7020 | |
| 7021 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7022 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7023 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7024 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7025 | if (exc != NULL) { |
| 7026 | PyCodec_StrictErrors(exc); |
| 7027 | Py_DECREF(exc); |
| 7028 | } |
| 7029 | Py_XDECREF(encoding_obj); |
| 7030 | return -1; |
| 7031 | } |
| 7032 | |
| 7033 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7034 | pusedDefaultChar = &usedDefaultChar; |
| 7035 | else |
| 7036 | pusedDefaultChar = NULL; |
| 7037 | |
| 7038 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7039 | PyErr_NoMemory(); |
| 7040 | goto error; |
| 7041 | } |
| 7042 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7043 | |
| 7044 | if (*outbytes == NULL) { |
| 7045 | /* Create string object */ |
| 7046 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7047 | if (*outbytes == NULL) |
| 7048 | goto error; |
| 7049 | out = PyBytes_AS_STRING(*outbytes); |
| 7050 | } |
| 7051 | else { |
| 7052 | /* Extend string object */ |
| 7053 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7054 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7055 | PyErr_NoMemory(); |
| 7056 | goto error; |
| 7057 | } |
| 7058 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7059 | goto error; |
| 7060 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7061 | } |
| 7062 | |
| 7063 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7064 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7065 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7066 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7067 | wchar_t chars[2]; |
| 7068 | int charsize; |
| 7069 | if (ch < 0x10000) { |
| 7070 | chars[0] = (wchar_t)ch; |
| 7071 | charsize = 1; |
| 7072 | } |
| 7073 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7074 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7075 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7076 | charsize = 2; |
| 7077 | } |
| 7078 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7079 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7080 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7081 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7082 | NULL, pusedDefaultChar); |
| 7083 | if (outsize > 0) { |
| 7084 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7085 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7086 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7087 | memcpy(out, buffer, outsize); |
| 7088 | out += outsize; |
| 7089 | continue; |
| 7090 | } |
| 7091 | } |
| 7092 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7093 | PyErr_SetFromWindowsErr(0); |
| 7094 | goto error; |
| 7095 | } |
| 7096 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7097 | rep = unicode_encode_call_errorhandler( |
| 7098 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7099 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7100 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7101 | if (rep == NULL) |
| 7102 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7103 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7104 | |
| 7105 | if (PyBytes_Check(rep)) { |
| 7106 | outsize = PyBytes_GET_SIZE(rep); |
| 7107 | if (outsize != 1) { |
| 7108 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7109 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7110 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7111 | Py_DECREF(rep); |
| 7112 | goto error; |
| 7113 | } |
| 7114 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7115 | } |
| 7116 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7117 | out += outsize; |
| 7118 | } |
| 7119 | else { |
| 7120 | Py_ssize_t i; |
| 7121 | enum PyUnicode_Kind kind; |
| 7122 | void *data; |
| 7123 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7124 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7125 | Py_DECREF(rep); |
| 7126 | goto error; |
| 7127 | } |
| 7128 | |
| 7129 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7130 | if (outsize != 1) { |
| 7131 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7132 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7133 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7134 | Py_DECREF(rep); |
| 7135 | goto error; |
| 7136 | } |
| 7137 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7138 | } |
| 7139 | kind = PyUnicode_KIND(rep); |
| 7140 | data = PyUnicode_DATA(rep); |
| 7141 | for (i=0; i < outsize; i++) { |
| 7142 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7143 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7144 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7145 | encoding, unicode, |
| 7146 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7147 | "unable to encode error handler result to ASCII"); |
| 7148 | Py_DECREF(rep); |
| 7149 | goto error; |
| 7150 | } |
| 7151 | *out = (unsigned char)ch; |
| 7152 | out++; |
| 7153 | } |
| 7154 | } |
| 7155 | Py_DECREF(rep); |
| 7156 | } |
| 7157 | /* write a NUL byte */ |
| 7158 | *out = 0; |
| 7159 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7160 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7161 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7162 | goto error; |
| 7163 | ret = 0; |
| 7164 | |
| 7165 | error: |
| 7166 | Py_XDECREF(encoding_obj); |
| 7167 | Py_XDECREF(errorHandler); |
| 7168 | Py_XDECREF(exc); |
| 7169 | return ret; |
| 7170 | } |
| 7171 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | static PyObject * |
| 7173 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7174 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7175 | const char *errors) |
| 7176 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7177 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7178 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7179 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7180 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7181 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7182 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7183 | return NULL; |
| 7184 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7185 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7186 | if (code_page < 0) { |
| 7187 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7188 | return NULL; |
| 7189 | } |
| 7190 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7191 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7192 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7193 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7194 | offset = 0; |
| 7195 | do |
| 7196 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7197 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7198 | /* 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] | 7199 | chunks. */ |
| 7200 | if (len > INT_MAX/2) { |
| 7201 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7202 | done = 0; |
| 7203 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7204 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7205 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7206 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7207 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7208 | done = 1; |
| 7209 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7210 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7211 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7212 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7213 | errors); |
| 7214 | if (ret == -2) |
| 7215 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7216 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7217 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7218 | if (ret < 0) { |
| 7219 | Py_XDECREF(outbytes); |
| 7220 | return NULL; |
| 7221 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7222 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7223 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7224 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7225 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7226 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7227 | return outbytes; |
| 7228 | } |
| 7229 | |
| 7230 | PyObject * |
| 7231 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7232 | Py_ssize_t size, |
| 7233 | const char *errors) |
| 7234 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7235 | PyObject *unicode, *res; |
| 7236 | unicode = PyUnicode_FromUnicode(p, size); |
| 7237 | if (unicode == NULL) |
| 7238 | return NULL; |
| 7239 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7240 | Py_DECREF(unicode); |
| 7241 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7242 | } |
| 7243 | |
| 7244 | PyObject * |
| 7245 | PyUnicode_EncodeCodePage(int code_page, |
| 7246 | PyObject *unicode, |
| 7247 | const char *errors) |
| 7248 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7249 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7250 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7251 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7252 | PyObject * |
| 7253 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7254 | { |
| 7255 | if (!PyUnicode_Check(unicode)) { |
| 7256 | PyErr_BadArgument(); |
| 7257 | return NULL; |
| 7258 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7259 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7260 | } |
| 7261 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7262 | #undef NEED_RETRY |
| 7263 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7264 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7266 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7267 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7268 | static int |
| 7269 | charmap_decode_string(const char *s, |
| 7270 | Py_ssize_t size, |
| 7271 | PyObject *mapping, |
| 7272 | const char *errors, |
| 7273 | _PyUnicodeWriter *writer) |
| 7274 | { |
| 7275 | const char *starts = s; |
| 7276 | const char *e; |
| 7277 | Py_ssize_t startinpos, endinpos; |
| 7278 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7279 | Py_ssize_t maplen; |
| 7280 | enum PyUnicode_Kind mapkind; |
| 7281 | void *mapdata; |
| 7282 | Py_UCS4 x; |
| 7283 | unsigned char ch; |
| 7284 | |
| 7285 | if (PyUnicode_READY(mapping) == -1) |
| 7286 | return -1; |
| 7287 | |
| 7288 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7289 | mapdata = PyUnicode_DATA(mapping); |
| 7290 | mapkind = PyUnicode_KIND(mapping); |
| 7291 | |
| 7292 | e = s + size; |
| 7293 | |
| 7294 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7295 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7296 | * is disabled in encoding aliases, latin1 is preferred because |
| 7297 | * its implementation is faster. */ |
| 7298 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7299 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7300 | Py_UCS4 maxchar = writer->maxchar; |
| 7301 | |
| 7302 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7303 | while (s < e) { |
| 7304 | ch = *s; |
| 7305 | x = mapdata_ucs1[ch]; |
| 7306 | if (x > maxchar) { |
| 7307 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7308 | goto onError; |
| 7309 | maxchar = writer->maxchar; |
| 7310 | outdata = (Py_UCS1 *)writer->data; |
| 7311 | } |
| 7312 | outdata[writer->pos] = x; |
| 7313 | writer->pos++; |
| 7314 | ++s; |
| 7315 | } |
| 7316 | return 0; |
| 7317 | } |
| 7318 | |
| 7319 | while (s < e) { |
| 7320 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7321 | enum PyUnicode_Kind outkind = writer->kind; |
| 7322 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7323 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7324 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7325 | Py_UCS4 maxchar = writer->maxchar; |
| 7326 | while (s < e) { |
| 7327 | ch = *s; |
| 7328 | x = mapdata_ucs2[ch]; |
| 7329 | if (x > maxchar) |
| 7330 | goto Error; |
| 7331 | outdata[writer->pos] = x; |
| 7332 | writer->pos++; |
| 7333 | ++s; |
| 7334 | } |
| 7335 | break; |
| 7336 | } |
| 7337 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7338 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7339 | while (s < e) { |
| 7340 | ch = *s; |
| 7341 | x = mapdata_ucs2[ch]; |
| 7342 | if (x == 0xFFFE) |
| 7343 | goto Error; |
| 7344 | outdata[writer->pos] = x; |
| 7345 | writer->pos++; |
| 7346 | ++s; |
| 7347 | } |
| 7348 | break; |
| 7349 | } |
| 7350 | } |
| 7351 | ch = *s; |
| 7352 | |
| 7353 | if (ch < maplen) |
| 7354 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7355 | else |
| 7356 | x = 0xfffe; /* invalid value */ |
| 7357 | Error: |
| 7358 | if (x == 0xfffe) |
| 7359 | { |
| 7360 | /* undefined mapping */ |
| 7361 | startinpos = s-starts; |
| 7362 | endinpos = startinpos+1; |
| 7363 | if (unicode_decode_call_errorhandler_writer( |
| 7364 | errors, &errorHandler, |
| 7365 | "charmap", "character maps to <undefined>", |
| 7366 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7367 | writer)) { |
| 7368 | goto onError; |
| 7369 | } |
| 7370 | continue; |
| 7371 | } |
| 7372 | |
| 7373 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7374 | goto onError; |
| 7375 | ++s; |
| 7376 | } |
| 7377 | Py_XDECREF(errorHandler); |
| 7378 | Py_XDECREF(exc); |
| 7379 | return 0; |
| 7380 | |
| 7381 | onError: |
| 7382 | Py_XDECREF(errorHandler); |
| 7383 | Py_XDECREF(exc); |
| 7384 | return -1; |
| 7385 | } |
| 7386 | |
| 7387 | static int |
| 7388 | charmap_decode_mapping(const char *s, |
| 7389 | Py_ssize_t size, |
| 7390 | PyObject *mapping, |
| 7391 | const char *errors, |
| 7392 | _PyUnicodeWriter *writer) |
| 7393 | { |
| 7394 | const char *starts = s; |
| 7395 | const char *e; |
| 7396 | Py_ssize_t startinpos, endinpos; |
| 7397 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7398 | unsigned char ch; |
| 7399 | PyObject *key, *item; |
| 7400 | |
| 7401 | e = s + size; |
| 7402 | |
| 7403 | while (s < e) { |
| 7404 | ch = *s; |
| 7405 | |
| 7406 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7407 | key = PyLong_FromLong((long)ch); |
| 7408 | if (key == NULL) |
| 7409 | goto onError; |
| 7410 | |
| 7411 | item = PyObject_GetItem(mapping, key); |
| 7412 | Py_DECREF(key); |
| 7413 | if (item == NULL) { |
| 7414 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7415 | /* No mapping found means: mapping is undefined. */ |
| 7416 | PyErr_Clear(); |
| 7417 | goto Undefined; |
| 7418 | } else |
| 7419 | goto onError; |
| 7420 | } |
| 7421 | |
| 7422 | /* Apply mapping */ |
| 7423 | if (item == Py_None) |
| 7424 | goto Undefined; |
| 7425 | if (PyLong_Check(item)) { |
| 7426 | long value = PyLong_AS_LONG(item); |
| 7427 | if (value == 0xFFFE) |
| 7428 | goto Undefined; |
| 7429 | if (value < 0 || value > MAX_UNICODE) { |
| 7430 | PyErr_Format(PyExc_TypeError, |
| 7431 | "character mapping must be in range(0x%lx)", |
| 7432 | (unsigned long)MAX_UNICODE + 1); |
| 7433 | goto onError; |
| 7434 | } |
| 7435 | |
| 7436 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7437 | goto onError; |
| 7438 | } |
| 7439 | else if (PyUnicode_Check(item)) { |
| 7440 | if (PyUnicode_READY(item) == -1) |
| 7441 | goto onError; |
| 7442 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7443 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7444 | if (value == 0xFFFE) |
| 7445 | goto Undefined; |
| 7446 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7447 | goto onError; |
| 7448 | } |
| 7449 | else { |
| 7450 | writer->overallocate = 1; |
| 7451 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7452 | goto onError; |
| 7453 | } |
| 7454 | } |
| 7455 | else { |
| 7456 | /* wrong return value */ |
| 7457 | PyErr_SetString(PyExc_TypeError, |
| 7458 | "character mapping must return integer, None or str"); |
| 7459 | goto onError; |
| 7460 | } |
| 7461 | Py_CLEAR(item); |
| 7462 | ++s; |
| 7463 | continue; |
| 7464 | |
| 7465 | Undefined: |
| 7466 | /* undefined mapping */ |
| 7467 | Py_CLEAR(item); |
| 7468 | startinpos = s-starts; |
| 7469 | endinpos = startinpos+1; |
| 7470 | if (unicode_decode_call_errorhandler_writer( |
| 7471 | errors, &errorHandler, |
| 7472 | "charmap", "character maps to <undefined>", |
| 7473 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7474 | writer)) { |
| 7475 | goto onError; |
| 7476 | } |
| 7477 | } |
| 7478 | Py_XDECREF(errorHandler); |
| 7479 | Py_XDECREF(exc); |
| 7480 | return 0; |
| 7481 | |
| 7482 | onError: |
| 7483 | Py_XDECREF(item); |
| 7484 | Py_XDECREF(errorHandler); |
| 7485 | Py_XDECREF(exc); |
| 7486 | return -1; |
| 7487 | } |
| 7488 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7489 | PyObject * |
| 7490 | PyUnicode_DecodeCharmap(const char *s, |
| 7491 | Py_ssize_t size, |
| 7492 | PyObject *mapping, |
| 7493 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7494 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7495 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7497 | /* Default to Latin-1 */ |
| 7498 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7499 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7501 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7502 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7503 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7504 | writer.min_length = size; |
| 7505 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7507 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7508 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7509 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7510 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7511 | } |
| 7512 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7513 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7514 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7515 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7516 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7517 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7518 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7519 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7520 | return NULL; |
| 7521 | } |
| 7522 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7523 | /* Charmap encoding: the lookup table */ |
| 7524 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7525 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7526 | PyObject_HEAD |
| 7527 | unsigned char level1[32]; |
| 7528 | int count2, count3; |
| 7529 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7530 | }; |
| 7531 | |
| 7532 | static PyObject* |
| 7533 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7534 | { |
| 7535 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7536 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7537 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7538 | } |
| 7539 | |
| 7540 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7541 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7542 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7543 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7544 | }; |
| 7545 | |
| 7546 | static void |
| 7547 | encoding_map_dealloc(PyObject* o) |
| 7548 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7549 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7553 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7554 | "EncodingMap", /*tp_name*/ |
| 7555 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7556 | 0, /*tp_itemsize*/ |
| 7557 | /* methods */ |
| 7558 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7559 | 0, /*tp_print*/ |
| 7560 | 0, /*tp_getattr*/ |
| 7561 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7562 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7563 | 0, /*tp_repr*/ |
| 7564 | 0, /*tp_as_number*/ |
| 7565 | 0, /*tp_as_sequence*/ |
| 7566 | 0, /*tp_as_mapping*/ |
| 7567 | 0, /*tp_hash*/ |
| 7568 | 0, /*tp_call*/ |
| 7569 | 0, /*tp_str*/ |
| 7570 | 0, /*tp_getattro*/ |
| 7571 | 0, /*tp_setattro*/ |
| 7572 | 0, /*tp_as_buffer*/ |
| 7573 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7574 | 0, /*tp_doc*/ |
| 7575 | 0, /*tp_traverse*/ |
| 7576 | 0, /*tp_clear*/ |
| 7577 | 0, /*tp_richcompare*/ |
| 7578 | 0, /*tp_weaklistoffset*/ |
| 7579 | 0, /*tp_iter*/ |
| 7580 | 0, /*tp_iternext*/ |
| 7581 | encoding_map_methods, /*tp_methods*/ |
| 7582 | 0, /*tp_members*/ |
| 7583 | 0, /*tp_getset*/ |
| 7584 | 0, /*tp_base*/ |
| 7585 | 0, /*tp_dict*/ |
| 7586 | 0, /*tp_descr_get*/ |
| 7587 | 0, /*tp_descr_set*/ |
| 7588 | 0, /*tp_dictoffset*/ |
| 7589 | 0, /*tp_init*/ |
| 7590 | 0, /*tp_alloc*/ |
| 7591 | 0, /*tp_new*/ |
| 7592 | 0, /*tp_free*/ |
| 7593 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7594 | }; |
| 7595 | |
| 7596 | PyObject* |
| 7597 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7598 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7599 | PyObject *result; |
| 7600 | struct encoding_map *mresult; |
| 7601 | int i; |
| 7602 | int need_dict = 0; |
| 7603 | unsigned char level1[32]; |
| 7604 | unsigned char level2[512]; |
| 7605 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7606 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7607 | int kind; |
| 7608 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7609 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7610 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7611 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7612 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7613 | PyErr_BadArgument(); |
| 7614 | return NULL; |
| 7615 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7616 | kind = PyUnicode_KIND(string); |
| 7617 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7618 | length = PyUnicode_GET_LENGTH(string); |
| 7619 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7620 | memset(level1, 0xFF, sizeof level1); |
| 7621 | memset(level2, 0xFF, sizeof level2); |
| 7622 | |
| 7623 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7624 | or if there are non-BMP characters, we need to use |
| 7625 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7626 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7627 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7628 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7629 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7630 | ch = PyUnicode_READ(kind, data, i); |
| 7631 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7632 | need_dict = 1; |
| 7633 | break; |
| 7634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7635 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7636 | /* unmapped character */ |
| 7637 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7638 | l1 = ch >> 11; |
| 7639 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7640 | if (level1[l1] == 0xFF) |
| 7641 | level1[l1] = count2++; |
| 7642 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7643 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7644 | } |
| 7645 | |
| 7646 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7647 | need_dict = 1; |
| 7648 | |
| 7649 | if (need_dict) { |
| 7650 | PyObject *result = PyDict_New(); |
| 7651 | PyObject *key, *value; |
| 7652 | if (!result) |
| 7653 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7654 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7655 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7656 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7657 | if (!key || !value) |
| 7658 | goto failed1; |
| 7659 | if (PyDict_SetItem(result, key, value) == -1) |
| 7660 | goto failed1; |
| 7661 | Py_DECREF(key); |
| 7662 | Py_DECREF(value); |
| 7663 | } |
| 7664 | return result; |
| 7665 | failed1: |
| 7666 | Py_XDECREF(key); |
| 7667 | Py_XDECREF(value); |
| 7668 | Py_DECREF(result); |
| 7669 | return NULL; |
| 7670 | } |
| 7671 | |
| 7672 | /* Create a three-level trie */ |
| 7673 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7674 | 16*count2 + 128*count3 - 1); |
| 7675 | if (!result) |
| 7676 | return PyErr_NoMemory(); |
| 7677 | PyObject_Init(result, &EncodingMapType); |
| 7678 | mresult = (struct encoding_map*)result; |
| 7679 | mresult->count2 = count2; |
| 7680 | mresult->count3 = count3; |
| 7681 | mlevel1 = mresult->level1; |
| 7682 | mlevel2 = mresult->level23; |
| 7683 | mlevel3 = mresult->level23 + 16*count2; |
| 7684 | memcpy(mlevel1, level1, 32); |
| 7685 | memset(mlevel2, 0xFF, 16*count2); |
| 7686 | memset(mlevel3, 0, 128*count3); |
| 7687 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7688 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7689 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7690 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7691 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7692 | /* unmapped character */ |
| 7693 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7694 | o1 = ch>>11; |
| 7695 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7696 | i2 = 16*mlevel1[o1] + o2; |
| 7697 | if (mlevel2[i2] == 0xFF) |
| 7698 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7699 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7700 | i3 = 128*mlevel2[i2] + o3; |
| 7701 | mlevel3[i3] = i; |
| 7702 | } |
| 7703 | return result; |
| 7704 | } |
| 7705 | |
| 7706 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7707 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7708 | { |
| 7709 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7710 | int l1 = c>>11; |
| 7711 | int l2 = (c>>7) & 0xF; |
| 7712 | int l3 = c & 0x7F; |
| 7713 | int i; |
| 7714 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7715 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7716 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7717 | if (c == 0) |
| 7718 | return 0; |
| 7719 | /* level 1*/ |
| 7720 | i = map->level1[l1]; |
| 7721 | if (i == 0xFF) { |
| 7722 | return -1; |
| 7723 | } |
| 7724 | /* level 2*/ |
| 7725 | i = map->level23[16*i+l2]; |
| 7726 | if (i == 0xFF) { |
| 7727 | return -1; |
| 7728 | } |
| 7729 | /* level 3 */ |
| 7730 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7731 | if (i == 0) { |
| 7732 | return -1; |
| 7733 | } |
| 7734 | return i; |
| 7735 | } |
| 7736 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7737 | /* Lookup the character ch in the mapping. If the character |
| 7738 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7739 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7740 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7741 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7742 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7743 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7744 | PyObject *x; |
| 7745 | |
| 7746 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7747 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7748 | x = PyObject_GetItem(mapping, w); |
| 7749 | Py_DECREF(w); |
| 7750 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7751 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7752 | /* No mapping found means: mapping is undefined. */ |
| 7753 | PyErr_Clear(); |
| 7754 | x = Py_None; |
| 7755 | Py_INCREF(x); |
| 7756 | return x; |
| 7757 | } else |
| 7758 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7760 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7762 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7763 | long value = PyLong_AS_LONG(x); |
| 7764 | if (value < 0 || value > 255) { |
| 7765 | PyErr_SetString(PyExc_TypeError, |
| 7766 | "character mapping must be in range(256)"); |
| 7767 | Py_DECREF(x); |
| 7768 | return NULL; |
| 7769 | } |
| 7770 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7772 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7773 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7775 | /* wrong return value */ |
| 7776 | PyErr_Format(PyExc_TypeError, |
| 7777 | "character mapping must return integer, bytes or None, not %.400s", |
| 7778 | x->ob_type->tp_name); |
| 7779 | Py_DECREF(x); |
| 7780 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7781 | } |
| 7782 | } |
| 7783 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7784 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7785 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7786 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7787 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7788 | /* exponentially overallocate to minimize reallocations */ |
| 7789 | if (requiredsize < 2*outsize) |
| 7790 | requiredsize = 2*outsize; |
| 7791 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7792 | return -1; |
| 7793 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7794 | } |
| 7795 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7796 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7797 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7798 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7799 | /* 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] | 7800 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7801 | space is available. Return a new reference to the object that |
| 7802 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7803 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7804 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7805 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7806 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7807 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7808 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7809 | PyObject *rep; |
| 7810 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7811 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7812 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7813 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7814 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7815 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7816 | if (res == -1) |
| 7817 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7818 | if (outsize<requiredsize) |
| 7819 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7820 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7821 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7822 | outstart[(*outpos)++] = (char)res; |
| 7823 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7824 | } |
| 7825 | |
| 7826 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7827 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7828 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7829 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7830 | Py_DECREF(rep); |
| 7831 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7832 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7833 | if (PyLong_Check(rep)) { |
| 7834 | Py_ssize_t requiredsize = *outpos+1; |
| 7835 | if (outsize<requiredsize) |
| 7836 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7837 | Py_DECREF(rep); |
| 7838 | return enc_EXCEPTION; |
| 7839 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7840 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7842 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7843 | else { |
| 7844 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7845 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7846 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7847 | if (outsize<requiredsize) |
| 7848 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7849 | Py_DECREF(rep); |
| 7850 | return enc_EXCEPTION; |
| 7851 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7852 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7853 | memcpy(outstart + *outpos, repchars, repsize); |
| 7854 | *outpos += repsize; |
| 7855 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7856 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7857 | Py_DECREF(rep); |
| 7858 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7859 | } |
| 7860 | |
| 7861 | /* handle an error in PyUnicode_EncodeCharmap |
| 7862 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7863 | static int |
| 7864 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7865 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7866 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7867 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7868 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7869 | { |
| 7870 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7871 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7872 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7873 | enum PyUnicode_Kind kind; |
| 7874 | void *data; |
| 7875 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7876 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7877 | Py_ssize_t collstartpos = *inpos; |
| 7878 | Py_ssize_t collendpos = *inpos+1; |
| 7879 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7880 | char *encoding = "charmap"; |
| 7881 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7882 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7883 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7884 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7885 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7886 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7887 | return -1; |
| 7888 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7889 | /* find all unencodable characters */ |
| 7890 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7891 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7892 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7893 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7894 | val = encoding_map_lookup(ch, mapping); |
| 7895 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7896 | break; |
| 7897 | ++collendpos; |
| 7898 | continue; |
| 7899 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7900 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7901 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7902 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | if (rep==NULL) |
| 7904 | return -1; |
| 7905 | else if (rep!=Py_None) { |
| 7906 | Py_DECREF(rep); |
| 7907 | break; |
| 7908 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7909 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7911 | } |
| 7912 | /* cache callback name lookup |
| 7913 | * (if not done yet, i.e. it's the first error) */ |
| 7914 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7915 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7916 | *known_errorHandler = 1; |
| 7917 | else if (!strcmp(errors, "replace")) |
| 7918 | *known_errorHandler = 2; |
| 7919 | else if (!strcmp(errors, "ignore")) |
| 7920 | *known_errorHandler = 3; |
| 7921 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7922 | *known_errorHandler = 4; |
| 7923 | else |
| 7924 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7925 | } |
| 7926 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7927 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7928 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7929 | return -1; |
| 7930 | case 2: /* replace */ |
| 7931 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7932 | x = charmapencode_output('?', mapping, res, respos); |
| 7933 | if (x==enc_EXCEPTION) { |
| 7934 | return -1; |
| 7935 | } |
| 7936 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7937 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7938 | return -1; |
| 7939 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7940 | } |
| 7941 | /* fall through */ |
| 7942 | case 3: /* ignore */ |
| 7943 | *inpos = collendpos; |
| 7944 | break; |
| 7945 | case 4: /* xmlcharrefreplace */ |
| 7946 | /* generate replacement (temporarily (mis)uses p) */ |
| 7947 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7948 | char buffer[2+29+1+1]; |
| 7949 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7950 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7951 | for (cp = buffer; *cp; ++cp) { |
| 7952 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7953 | if (x==enc_EXCEPTION) |
| 7954 | return -1; |
| 7955 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7956 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7957 | return -1; |
| 7958 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7959 | } |
| 7960 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7961 | *inpos = collendpos; |
| 7962 | break; |
| 7963 | default: |
| 7964 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7965 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7966 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7967 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7968 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7969 | if (PyBytes_Check(repunicode)) { |
| 7970 | /* Directly copy bytes result to output. */ |
| 7971 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7972 | Py_ssize_t requiredsize; |
| 7973 | repsize = PyBytes_Size(repunicode); |
| 7974 | requiredsize = *respos + repsize; |
| 7975 | if (requiredsize > outsize) |
| 7976 | /* Make room for all additional bytes. */ |
| 7977 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7978 | Py_DECREF(repunicode); |
| 7979 | return -1; |
| 7980 | } |
| 7981 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7982 | PyBytes_AsString(repunicode), repsize); |
| 7983 | *respos += repsize; |
| 7984 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7985 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7986 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7987 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7988 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7989 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7990 | Py_DECREF(repunicode); |
| 7991 | return -1; |
| 7992 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 7993 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7994 | data = PyUnicode_DATA(repunicode); |
| 7995 | kind = PyUnicode_KIND(repunicode); |
| 7996 | for (index = 0; index < repsize; index++) { |
| 7997 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 7998 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7999 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8000 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8001 | return -1; |
| 8002 | } |
| 8003 | else if (x==enc_FAILED) { |
| 8004 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8005 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8006 | return -1; |
| 8007 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8008 | } |
| 8009 | *inpos = newpos; |
| 8010 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8011 | } |
| 8012 | return 0; |
| 8013 | } |
| 8014 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8015 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8016 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8017 | PyObject *mapping, |
| 8018 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8019 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8020 | /* output object */ |
| 8021 | PyObject *res = NULL; |
| 8022 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8023 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8024 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8025 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8026 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8027 | PyObject *errorHandler = NULL; |
| 8028 | PyObject *exc = NULL; |
| 8029 | /* the following variable is used for caching string comparisons |
| 8030 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8031 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8032 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8033 | void *data; |
| 8034 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8036 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8037 | return NULL; |
| 8038 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8039 | data = PyUnicode_DATA(unicode); |
| 8040 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8041 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8042 | /* Default to Latin-1 */ |
| 8043 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8044 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8045 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8046 | /* allocate enough for a simple encoding without |
| 8047 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8048 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8049 | if (res == NULL) |
| 8050 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8051 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8054 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8055 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8056 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8057 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8058 | if (x==enc_EXCEPTION) /* error */ |
| 8059 | goto onError; |
| 8060 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8061 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8062 | &exc, |
| 8063 | &known_errorHandler, &errorHandler, errors, |
| 8064 | &res, &respos)) { |
| 8065 | goto onError; |
| 8066 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8067 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8068 | else |
| 8069 | /* done with this character => adjust input position */ |
| 8070 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8071 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8072 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8073 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8074 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8075 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8076 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8077 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8078 | Py_XDECREF(exc); |
| 8079 | Py_XDECREF(errorHandler); |
| 8080 | return res; |
| 8081 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8082 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8083 | Py_XDECREF(res); |
| 8084 | Py_XDECREF(exc); |
| 8085 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8086 | return NULL; |
| 8087 | } |
| 8088 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8089 | /* Deprecated */ |
| 8090 | PyObject * |
| 8091 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8092 | Py_ssize_t size, |
| 8093 | PyObject *mapping, |
| 8094 | const char *errors) |
| 8095 | { |
| 8096 | PyObject *result; |
| 8097 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8098 | if (unicode == NULL) |
| 8099 | return NULL; |
| 8100 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8101 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8102 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8103 | } |
| 8104 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8105 | PyObject * |
| 8106 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8107 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8108 | { |
| 8109 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8110 | PyErr_BadArgument(); |
| 8111 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8112 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8113 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8114 | } |
| 8115 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8116 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8117 | static void |
| 8118 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8119 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8120 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8121 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8122 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8123 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8124 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8125 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | } |
| 8127 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8128 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8129 | goto onError; |
| 8130 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8131 | goto onError; |
| 8132 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8133 | goto onError; |
| 8134 | return; |
| 8135 | onError: |
| 8136 | Py_DECREF(*exceptionObject); |
| 8137 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8138 | } |
| 8139 | } |
| 8140 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8141 | /* error handling callback helper: |
| 8142 | build arguments, call the callback and check the arguments, |
| 8143 | put the result into newpos and return the replacement string, which |
| 8144 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8145 | static PyObject * |
| 8146 | unicode_translate_call_errorhandler(const char *errors, |
| 8147 | PyObject **errorHandler, |
| 8148 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8149 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8150 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8151 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8152 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8153 | 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] | 8154 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8155 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8156 | PyObject *restuple; |
| 8157 | PyObject *resunicode; |
| 8158 | |
| 8159 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8161 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8162 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8163 | } |
| 8164 | |
| 8165 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8166 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8167 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8169 | |
| 8170 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8171 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8172 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8173 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8174 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8175 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8176 | Py_DECREF(restuple); |
| 8177 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8178 | } |
| 8179 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | &resunicode, &i_newpos)) { |
| 8181 | Py_DECREF(restuple); |
| 8182 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8183 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8184 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8185 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8186 | else |
| 8187 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8188 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8189 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8190 | Py_DECREF(restuple); |
| 8191 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8192 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8193 | Py_INCREF(resunicode); |
| 8194 | Py_DECREF(restuple); |
| 8195 | return resunicode; |
| 8196 | } |
| 8197 | |
| 8198 | /* Lookup the character ch in the mapping and put the result in result, |
| 8199 | which must be decrefed by the caller. |
| 8200 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8201 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8202 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8203 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8204 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8205 | PyObject *x; |
| 8206 | |
| 8207 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8208 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8209 | x = PyObject_GetItem(mapping, w); |
| 8210 | Py_DECREF(w); |
| 8211 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8212 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8213 | /* No mapping found means: use 1:1 mapping. */ |
| 8214 | PyErr_Clear(); |
| 8215 | *result = NULL; |
| 8216 | return 0; |
| 8217 | } else |
| 8218 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8219 | } |
| 8220 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8221 | *result = x; |
| 8222 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8223 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8224 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8225 | long value = PyLong_AS_LONG(x); |
| 8226 | long max = PyUnicode_GetMax(); |
| 8227 | if (value < 0 || value > max) { |
| 8228 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8229 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | Py_DECREF(x); |
| 8231 | return -1; |
| 8232 | } |
| 8233 | *result = x; |
| 8234 | return 0; |
| 8235 | } |
| 8236 | else if (PyUnicode_Check(x)) { |
| 8237 | *result = x; |
| 8238 | return 0; |
| 8239 | } |
| 8240 | else { |
| 8241 | /* wrong return value */ |
| 8242 | PyErr_SetString(PyExc_TypeError, |
| 8243 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8244 | Py_DECREF(x); |
| 8245 | return -1; |
| 8246 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8247 | } |
| 8248 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8249 | if not reallocate and adjust various state variables. |
| 8250 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8251 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8252 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8253 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8254 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8255 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8256 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8257 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8258 | /* exponentially overallocate to minimize reallocations */ |
| 8259 | if (requiredsize < 2 * oldsize) |
| 8260 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8261 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8262 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8263 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8264 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8265 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8266 | } |
| 8267 | return 0; |
| 8268 | } |
| 8269 | /* lookup the character, put the result in the output string and adjust |
| 8270 | various state variables. Return a new reference to the object that |
| 8271 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8272 | undefined (in which case no character was written). |
| 8273 | The called must decref result. |
| 8274 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8275 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8276 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8277 | PyObject *mapping, Py_UCS4 **output, |
| 8278 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8279 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8280 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8281 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8282 | if (charmaptranslate_lookup(curinp, mapping, res)) |
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 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8285 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8286 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8287 | } |
| 8288 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8289 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8290 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8291 | /* 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] | 8292 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8293 | } |
| 8294 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8295 | Py_ssize_t repsize; |
| 8296 | if (PyUnicode_READY(*res) == -1) |
| 8297 | return -1; |
| 8298 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8299 | if (repsize==1) { |
| 8300 | /* 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] | 8301 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8302 | } |
| 8303 | else if (repsize!=0) { |
| 8304 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8305 | Py_ssize_t requiredsize = *opos + |
| 8306 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8307 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8308 | Py_ssize_t i; |
| 8309 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8310 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8311 | for(i = 0; i < repsize; i++) |
| 8312 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8313 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8314 | } |
| 8315 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8316 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8317 | return 0; |
| 8318 | } |
| 8319 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8320 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8321 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8322 | PyObject *mapping, |
| 8323 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8324 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8325 | /* input object */ |
| 8326 | char *idata; |
| 8327 | Py_ssize_t size, i; |
| 8328 | int kind; |
| 8329 | /* output buffer */ |
| 8330 | Py_UCS4 *output = NULL; |
| 8331 | Py_ssize_t osize; |
| 8332 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8333 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8334 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8335 | char *reason = "character maps to <undefined>"; |
| 8336 | PyObject *errorHandler = NULL; |
| 8337 | PyObject *exc = NULL; |
| 8338 | /* the following variable is used for caching string comparisons |
| 8339 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8340 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8341 | int known_errorHandler = -1; |
| 8342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8344 | PyErr_BadArgument(); |
| 8345 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8346 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8347 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8348 | if (PyUnicode_READY(input) == -1) |
| 8349 | return NULL; |
| 8350 | idata = (char*)PyUnicode_DATA(input); |
| 8351 | kind = PyUnicode_KIND(input); |
| 8352 | size = PyUnicode_GET_LENGTH(input); |
| 8353 | i = 0; |
| 8354 | |
| 8355 | if (size == 0) { |
| 8356 | Py_INCREF(input); |
| 8357 | return input; |
| 8358 | } |
| 8359 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8360 | /* allocate enough for a simple 1:1 translation without |
| 8361 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8362 | osize = size; |
| 8363 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8364 | opos = 0; |
| 8365 | if (output == NULL) { |
| 8366 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8367 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8368 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8369 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8370 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8371 | /* try to encode it */ |
| 8372 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8373 | if (charmaptranslate_output(input, i, mapping, |
| 8374 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8375 | Py_XDECREF(x); |
| 8376 | goto onError; |
| 8377 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8378 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8379 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8380 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8381 | else { /* untranslatable character */ |
| 8382 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8383 | Py_ssize_t repsize; |
| 8384 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8385 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8386 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8387 | Py_ssize_t collstart = i; |
| 8388 | Py_ssize_t collend = i+1; |
| 8389 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8390 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8391 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8392 | while (collend < size) { |
| 8393 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8394 | goto onError; |
| 8395 | Py_XDECREF(x); |
| 8396 | if (x!=Py_None) |
| 8397 | break; |
| 8398 | ++collend; |
| 8399 | } |
| 8400 | /* cache callback name lookup |
| 8401 | * (if not done yet, i.e. it's the first error) */ |
| 8402 | if (known_errorHandler==-1) { |
| 8403 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8404 | known_errorHandler = 1; |
| 8405 | else if (!strcmp(errors, "replace")) |
| 8406 | known_errorHandler = 2; |
| 8407 | else if (!strcmp(errors, "ignore")) |
| 8408 | known_errorHandler = 3; |
| 8409 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8410 | known_errorHandler = 4; |
| 8411 | else |
| 8412 | known_errorHandler = 0; |
| 8413 | } |
| 8414 | switch (known_errorHandler) { |
| 8415 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8416 | make_translate_exception(&exc, |
| 8417 | input, collstart, collend, reason); |
| 8418 | if (exc != NULL) |
| 8419 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8420 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8421 | case 2: /* replace */ |
| 8422 | /* 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] | 8423 | for (coll = collstart; coll<collend; coll++) |
| 8424 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8425 | /* fall through */ |
| 8426 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8427 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | break; |
| 8429 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8430 | /* generate replacement (temporarily (mis)uses i) */ |
| 8431 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8432 | char buffer[2+29+1+1]; |
| 8433 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8434 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8435 | if (charmaptranslate_makespace(&output, &osize, |
| 8436 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | goto onError; |
| 8438 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8439 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8440 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8441 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8442 | break; |
| 8443 | default: |
| 8444 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8445 | reason, input, &exc, |
| 8446 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8447 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8448 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8449 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8450 | Py_DECREF(repunicode); |
| 8451 | goto onError; |
| 8452 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8453 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8454 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8455 | if (charmaptranslate_makespace(&output, &osize, |
| 8456 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8457 | Py_DECREF(repunicode); |
| 8458 | goto onError; |
| 8459 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8461 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8462 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8464 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8465 | } |
| 8466 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8467 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8468 | if (!res) |
| 8469 | goto onError; |
| 8470 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8471 | Py_XDECREF(exc); |
| 8472 | Py_XDECREF(errorHandler); |
| 8473 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8474 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8475 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8476 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8477 | Py_XDECREF(exc); |
| 8478 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8479 | return NULL; |
| 8480 | } |
| 8481 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8482 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8483 | PyObject * |
| 8484 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8485 | Py_ssize_t size, |
| 8486 | PyObject *mapping, |
| 8487 | const char *errors) |
| 8488 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8489 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8490 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8491 | if (!unicode) |
| 8492 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8493 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8494 | Py_DECREF(unicode); |
| 8495 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8496 | } |
| 8497 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8498 | PyObject * |
| 8499 | PyUnicode_Translate(PyObject *str, |
| 8500 | PyObject *mapping, |
| 8501 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8502 | { |
| 8503 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8504 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8505 | str = PyUnicode_FromObject(str); |
| 8506 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8507 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8508 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8509 | Py_DECREF(str); |
| 8510 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8511 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8513 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8514 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8515 | { |
| 8516 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8517 | called as a callback from fixup() which does it already. */ |
| 8518 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8519 | const int kind = PyUnicode_KIND(self); |
| 8520 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8521 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8522 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8523 | Py_ssize_t i; |
| 8524 | |
| 8525 | for (i = 0; i < len; ++i) { |
| 8526 | ch = PyUnicode_READ(kind, data, i); |
| 8527 | fixed = 0; |
| 8528 | if (ch > 127) { |
| 8529 | if (Py_UNICODE_ISSPACE(ch)) |
| 8530 | fixed = ' '; |
| 8531 | else { |
| 8532 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8533 | if (decimal >= 0) |
| 8534 | fixed = '0' + decimal; |
| 8535 | } |
| 8536 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8537 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8538 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8539 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8540 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8541 | else |
| 8542 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8543 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8544 | } |
| 8545 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8546 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8547 | } |
| 8548 | |
| 8549 | PyObject * |
| 8550 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8551 | { |
| 8552 | if (!PyUnicode_Check(unicode)) { |
| 8553 | PyErr_BadInternalCall(); |
| 8554 | return NULL; |
| 8555 | } |
| 8556 | if (PyUnicode_READY(unicode) == -1) |
| 8557 | return NULL; |
| 8558 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8559 | /* If the string is already ASCII, just return the same string */ |
| 8560 | Py_INCREF(unicode); |
| 8561 | return unicode; |
| 8562 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8563 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8564 | } |
| 8565 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8566 | PyObject * |
| 8567 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8568 | Py_ssize_t length) |
| 8569 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8570 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8571 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8572 | Py_UCS4 maxchar; |
| 8573 | enum PyUnicode_Kind kind; |
| 8574 | void *data; |
| 8575 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8576 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8577 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8578 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8579 | if (ch > 127) { |
| 8580 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8581 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8582 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8583 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8584 | } |
| 8585 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8586 | |
| 8587 | /* Copy to a new string */ |
| 8588 | decimal = PyUnicode_New(length, maxchar); |
| 8589 | if (decimal == NULL) |
| 8590 | return decimal; |
| 8591 | kind = PyUnicode_KIND(decimal); |
| 8592 | data = PyUnicode_DATA(decimal); |
| 8593 | /* Iterate over code points */ |
| 8594 | for (i = 0; i < length; i++) { |
| 8595 | Py_UNICODE ch = s[i]; |
| 8596 | if (ch > 127) { |
| 8597 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8598 | if (decimal >= 0) |
| 8599 | ch = '0' + decimal; |
| 8600 | } |
| 8601 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8602 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8603 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8604 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8605 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8606 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8607 | int |
| 8608 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8609 | Py_ssize_t length, |
| 8610 | char *output, |
| 8611 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8612 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8613 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8614 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8615 | enum PyUnicode_Kind kind; |
| 8616 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8617 | |
| 8618 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8619 | PyErr_BadArgument(); |
| 8620 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8621 | } |
| 8622 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8623 | unicode = PyUnicode_FromUnicode(s, length); |
| 8624 | if (unicode == NULL) |
| 8625 | return -1; |
| 8626 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8627 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8628 | Py_DECREF(unicode); |
| 8629 | return -1; |
| 8630 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8631 | kind = PyUnicode_KIND(unicode); |
| 8632 | data = PyUnicode_DATA(unicode); |
| 8633 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8634 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8635 | PyObject *exc; |
| 8636 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8637 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8638 | Py_ssize_t startpos; |
| 8639 | |
| 8640 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8641 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8642 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8643 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8644 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8645 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8646 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8647 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8648 | if (decimal >= 0) { |
| 8649 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8650 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8651 | continue; |
| 8652 | } |
| 8653 | if (0 < ch && ch < 256) { |
| 8654 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8655 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8656 | continue; |
| 8657 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8658 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8659 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8660 | exc = NULL; |
| 8661 | raise_encode_exception(&exc, "decimal", unicode, |
| 8662 | startpos, startpos+1, |
| 8663 | "invalid decimal Unicode string"); |
| 8664 | Py_XDECREF(exc); |
| 8665 | Py_DECREF(unicode); |
| 8666 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8667 | } |
| 8668 | /* 0-terminate the output string */ |
| 8669 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8670 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8671 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8672 | } |
| 8673 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8674 | /* --- Helpers ------------------------------------------------------------ */ |
| 8675 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8676 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8677 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8678 | Py_ssize_t start, |
| 8679 | Py_ssize_t end) |
| 8680 | { |
| 8681 | int kind1, kind2, kind; |
| 8682 | void *buf1, *buf2; |
| 8683 | Py_ssize_t len1, len2, result; |
| 8684 | |
| 8685 | kind1 = PyUnicode_KIND(s1); |
| 8686 | kind2 = PyUnicode_KIND(s2); |
| 8687 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8688 | buf1 = PyUnicode_DATA(s1); |
| 8689 | buf2 = PyUnicode_DATA(s2); |
| 8690 | if (kind1 != kind) |
| 8691 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8692 | if (!buf1) |
| 8693 | return -2; |
| 8694 | if (kind2 != kind) |
| 8695 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8696 | if (!buf2) { |
| 8697 | if (kind1 != kind) PyMem_Free(buf1); |
| 8698 | return -2; |
| 8699 | } |
| 8700 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8701 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8702 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8703 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8704 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8705 | case PyUnicode_1BYTE_KIND: |
| 8706 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8707 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8708 | else |
| 8709 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8710 | break; |
| 8711 | case PyUnicode_2BYTE_KIND: |
| 8712 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8713 | break; |
| 8714 | case PyUnicode_4BYTE_KIND: |
| 8715 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8716 | break; |
| 8717 | default: |
| 8718 | assert(0); result = -2; |
| 8719 | } |
| 8720 | } |
| 8721 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8722 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8723 | case PyUnicode_1BYTE_KIND: |
| 8724 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8725 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8726 | else |
| 8727 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8728 | break; |
| 8729 | case PyUnicode_2BYTE_KIND: |
| 8730 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8731 | break; |
| 8732 | case PyUnicode_4BYTE_KIND: |
| 8733 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8734 | break; |
| 8735 | default: |
| 8736 | assert(0); result = -2; |
| 8737 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8738 | } |
| 8739 | |
| 8740 | if (kind1 != kind) |
| 8741 | PyMem_Free(buf1); |
| 8742 | if (kind2 != kind) |
| 8743 | PyMem_Free(buf2); |
| 8744 | |
| 8745 | return result; |
| 8746 | } |
| 8747 | |
| 8748 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8749 | _PyUnicode_InsertThousandsGrouping( |
| 8750 | PyObject *unicode, Py_ssize_t index, |
| 8751 | Py_ssize_t n_buffer, |
| 8752 | void *digits, Py_ssize_t n_digits, |
| 8753 | Py_ssize_t min_width, |
| 8754 | const char *grouping, PyObject *thousands_sep, |
| 8755 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8756 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8757 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8758 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8759 | Py_ssize_t thousands_sep_len; |
| 8760 | Py_ssize_t len; |
| 8761 | |
| 8762 | if (unicode != NULL) { |
| 8763 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8764 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8765 | } |
| 8766 | else { |
| 8767 | kind = PyUnicode_1BYTE_KIND; |
| 8768 | data = NULL; |
| 8769 | } |
| 8770 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8771 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8772 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8773 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8774 | if (thousands_sep_kind < kind) { |
| 8775 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8776 | if (!thousands_sep_data) |
| 8777 | return -1; |
| 8778 | } |
| 8779 | else { |
| 8780 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8781 | if (!data) |
| 8782 | return -1; |
| 8783 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8784 | } |
| 8785 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8786 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8787 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8788 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8789 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8790 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8791 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8792 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8793 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8794 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8795 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8796 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8797 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8798 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8799 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8800 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8801 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8802 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8803 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8804 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8805 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8806 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8807 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8808 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8809 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8810 | break; |
| 8811 | default: |
| 8812 | assert(0); |
| 8813 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8814 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8815 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8816 | if (thousands_sep_kind < kind) |
| 8817 | PyMem_Free(thousands_sep_data); |
| 8818 | else |
| 8819 | PyMem_Free(data); |
| 8820 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8821 | if (unicode == NULL) { |
| 8822 | *maxchar = 127; |
| 8823 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8824 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8825 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8826 | } |
| 8827 | } |
| 8828 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8829 | } |
| 8830 | |
| 8831 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8832 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8833 | #define ADJUST_INDICES(start, end, len) \ |
| 8834 | if (end > len) \ |
| 8835 | end = len; \ |
| 8836 | else if (end < 0) { \ |
| 8837 | end += len; \ |
| 8838 | if (end < 0) \ |
| 8839 | end = 0; \ |
| 8840 | } \ |
| 8841 | if (start < 0) { \ |
| 8842 | start += len; \ |
| 8843 | if (start < 0) \ |
| 8844 | start = 0; \ |
| 8845 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8846 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8847 | Py_ssize_t |
| 8848 | PyUnicode_Count(PyObject *str, |
| 8849 | PyObject *substr, |
| 8850 | Py_ssize_t start, |
| 8851 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8852 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8853 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8854 | PyObject* str_obj; |
| 8855 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8856 | int kind1, kind2, kind; |
| 8857 | void *buf1 = NULL, *buf2 = NULL; |
| 8858 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8859 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8860 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8861 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8862 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8863 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8864 | if (!sub_obj) { |
| 8865 | Py_DECREF(str_obj); |
| 8866 | return -1; |
| 8867 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8868 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8869 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8870 | Py_DECREF(str_obj); |
| 8871 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8872 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8873 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8874 | kind1 = PyUnicode_KIND(str_obj); |
| 8875 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8876 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8877 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8878 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8879 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8880 | if (kind2 > kind) { |
| 8881 | Py_DECREF(sub_obj); |
| 8882 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8883 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8884 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8885 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8886 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8887 | if (!buf2) |
| 8888 | goto onError; |
| 8889 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8890 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8891 | |
| 8892 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8893 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8894 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8895 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8896 | result = asciilib_count( |
| 8897 | ((Py_UCS1*)buf1) + start, end - start, |
| 8898 | buf2, len2, PY_SSIZE_T_MAX |
| 8899 | ); |
| 8900 | else |
| 8901 | result = ucs1lib_count( |
| 8902 | ((Py_UCS1*)buf1) + start, end - start, |
| 8903 | buf2, len2, PY_SSIZE_T_MAX |
| 8904 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8905 | break; |
| 8906 | case PyUnicode_2BYTE_KIND: |
| 8907 | result = ucs2lib_count( |
| 8908 | ((Py_UCS2*)buf1) + start, end - start, |
| 8909 | buf2, len2, PY_SSIZE_T_MAX |
| 8910 | ); |
| 8911 | break; |
| 8912 | case PyUnicode_4BYTE_KIND: |
| 8913 | result = ucs4lib_count( |
| 8914 | ((Py_UCS4*)buf1) + start, end - start, |
| 8915 | buf2, len2, PY_SSIZE_T_MAX |
| 8916 | ); |
| 8917 | break; |
| 8918 | default: |
| 8919 | assert(0); result = 0; |
| 8920 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8921 | |
| 8922 | Py_DECREF(sub_obj); |
| 8923 | Py_DECREF(str_obj); |
| 8924 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8925 | if (kind2 != kind) |
| 8926 | PyMem_Free(buf2); |
| 8927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8928 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8929 | onError: |
| 8930 | Py_DECREF(sub_obj); |
| 8931 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8932 | if (kind2 != kind && buf2) |
| 8933 | PyMem_Free(buf2); |
| 8934 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8935 | } |
| 8936 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8937 | Py_ssize_t |
| 8938 | PyUnicode_Find(PyObject *str, |
| 8939 | PyObject *sub, |
| 8940 | Py_ssize_t start, |
| 8941 | Py_ssize_t end, |
| 8942 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8943 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8944 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8945 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8946 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8947 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8948 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8949 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8950 | if (!sub) { |
| 8951 | Py_DECREF(str); |
| 8952 | return -2; |
| 8953 | } |
| 8954 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 8955 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8956 | Py_DECREF(str); |
| 8957 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8958 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8959 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8960 | result = any_find_slice(direction, |
| 8961 | str, sub, start, end |
| 8962 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8963 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8964 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8965 | Py_DECREF(sub); |
| 8966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | return result; |
| 8968 | } |
| 8969 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | Py_ssize_t |
| 8971 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8972 | Py_ssize_t start, Py_ssize_t end, |
| 8973 | int direction) |
| 8974 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8975 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8976 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8977 | if (PyUnicode_READY(str) == -1) |
| 8978 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8979 | if (start < 0 || end < 0) { |
| 8980 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8981 | return -2; |
| 8982 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8983 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8984 | end = PyUnicode_GET_LENGTH(str); |
| 8985 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8986 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 8987 | kind, end-start, ch, direction); |
| 8988 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8989 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8990 | else |
| 8991 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | } |
| 8993 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8994 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8995 | tailmatch(PyObject *self, |
| 8996 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8997 | Py_ssize_t start, |
| 8998 | Py_ssize_t end, |
| 8999 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9000 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | int kind_self; |
| 9002 | int kind_sub; |
| 9003 | void *data_self; |
| 9004 | void *data_sub; |
| 9005 | Py_ssize_t offset; |
| 9006 | Py_ssize_t i; |
| 9007 | Py_ssize_t end_sub; |
| 9008 | |
| 9009 | if (PyUnicode_READY(self) == -1 || |
| 9010 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9011 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9012 | |
| 9013 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9014 | return 1; |
| 9015 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9016 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9017 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9018 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9019 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9020 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9021 | kind_self = PyUnicode_KIND(self); |
| 9022 | data_self = PyUnicode_DATA(self); |
| 9023 | kind_sub = PyUnicode_KIND(substring); |
| 9024 | data_sub = PyUnicode_DATA(substring); |
| 9025 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9026 | |
| 9027 | if (direction > 0) |
| 9028 | offset = end; |
| 9029 | else |
| 9030 | offset = start; |
| 9031 | |
| 9032 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9033 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9034 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9035 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9036 | /* If both are of the same kind, memcmp is sufficient */ |
| 9037 | if (kind_self == kind_sub) { |
| 9038 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9039 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9040 | data_sub, |
| 9041 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9042 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9043 | } |
| 9044 | /* otherwise we have to compare each character by first accesing it */ |
| 9045 | else { |
| 9046 | /* We do not need to compare 0 and len(substring)-1 because |
| 9047 | the if statement above ensured already that they are equal |
| 9048 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9049 | for (i = 1; i < end_sub; ++i) { |
| 9050 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9051 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9052 | return 0; |
| 9053 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9054 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9055 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | } |
| 9057 | |
| 9058 | return 0; |
| 9059 | } |
| 9060 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9061 | Py_ssize_t |
| 9062 | PyUnicode_Tailmatch(PyObject *str, |
| 9063 | PyObject *substr, |
| 9064 | Py_ssize_t start, |
| 9065 | Py_ssize_t end, |
| 9066 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9067 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9068 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9070 | str = PyUnicode_FromObject(str); |
| 9071 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9072 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9073 | substr = PyUnicode_FromObject(substr); |
| 9074 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9075 | Py_DECREF(str); |
| 9076 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9077 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9078 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9079 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9080 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9081 | Py_DECREF(str); |
| 9082 | Py_DECREF(substr); |
| 9083 | return result; |
| 9084 | } |
| 9085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9086 | /* Apply fixfct filter to the Unicode object self and return a |
| 9087 | reference to the modified object */ |
| 9088 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9089 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9090 | fixup(PyObject *self, |
| 9091 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9092 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9093 | PyObject *u; |
| 9094 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9095 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9096 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9097 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9098 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9099 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9100 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9101 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9102 | /* fix functions return the new maximum character in a string, |
| 9103 | if the kind of the resulting unicode object does not change, |
| 9104 | everything is fine. Otherwise we need to change the string kind |
| 9105 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9106 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9107 | |
| 9108 | if (maxchar_new == 0) { |
| 9109 | /* no changes */; |
| 9110 | if (PyUnicode_CheckExact(self)) { |
| 9111 | Py_DECREF(u); |
| 9112 | Py_INCREF(self); |
| 9113 | return self; |
| 9114 | } |
| 9115 | else |
| 9116 | return u; |
| 9117 | } |
| 9118 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9119 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9120 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9121 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9122 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9123 | |
| 9124 | /* In case the maximum character changed, we need to |
| 9125 | convert the string to the new category. */ |
| 9126 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9127 | if (v == NULL) { |
| 9128 | Py_DECREF(u); |
| 9129 | return NULL; |
| 9130 | } |
| 9131 | if (maxchar_new > maxchar_old) { |
| 9132 | /* If the maxchar increased so that the kind changed, not all |
| 9133 | characters are representable anymore and we need to fix the |
| 9134 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9135 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9136 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9137 | maxchar_old = fixfct(v); |
| 9138 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9139 | } |
| 9140 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9141 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9142 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9143 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9144 | Py_DECREF(u); |
| 9145 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9146 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9147 | } |
| 9148 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9149 | static PyObject * |
| 9150 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9151 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9152 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9153 | char *resdata, *data = PyUnicode_DATA(self); |
| 9154 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9155 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9156 | res = PyUnicode_New(len, 127); |
| 9157 | if (res == NULL) |
| 9158 | return NULL; |
| 9159 | resdata = PyUnicode_DATA(res); |
| 9160 | if (lower) |
| 9161 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9162 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9163 | _Py_bytes_upper(resdata, data, len); |
| 9164 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9165 | } |
| 9166 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9167 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9168 | 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] | 9169 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9170 | Py_ssize_t j; |
| 9171 | int final_sigma; |
| 9172 | Py_UCS4 c; |
| 9173 | /* 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] | 9174 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9175 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9176 | |
| 9177 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9178 | */ |
| 9179 | for (j = i - 1; j >= 0; j--) { |
| 9180 | c = PyUnicode_READ(kind, data, j); |
| 9181 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9182 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9183 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9184 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9185 | if (final_sigma) { |
| 9186 | for (j = i + 1; j < length; j++) { |
| 9187 | c = PyUnicode_READ(kind, data, j); |
| 9188 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9189 | break; |
| 9190 | } |
| 9191 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9192 | } |
| 9193 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9194 | } |
| 9195 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9196 | static int |
| 9197 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9198 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9199 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9200 | /* Obscure special case. */ |
| 9201 | if (c == 0x3A3) { |
| 9202 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9203 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9204 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9205 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9206 | } |
| 9207 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9208 | static Py_ssize_t |
| 9209 | 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] | 9210 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9211 | Py_ssize_t i, k = 0; |
| 9212 | int n_res, j; |
| 9213 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9214 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9215 | c = PyUnicode_READ(kind, data, 0); |
| 9216 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9217 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9218 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9219 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9220 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9221 | for (i = 1; i < length; i++) { |
| 9222 | c = PyUnicode_READ(kind, data, i); |
| 9223 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9224 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9225 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9226 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9227 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9228 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9229 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9230 | } |
| 9231 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9232 | static Py_ssize_t |
| 9233 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9234 | Py_ssize_t i, k = 0; |
| 9235 | |
| 9236 | for (i = 0; i < length; i++) { |
| 9237 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9238 | int n_res, j; |
| 9239 | if (Py_UNICODE_ISUPPER(c)) { |
| 9240 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9241 | } |
| 9242 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9243 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9244 | } |
| 9245 | else { |
| 9246 | n_res = 1; |
| 9247 | mapped[0] = c; |
| 9248 | } |
| 9249 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9250 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9251 | res[k++] = mapped[j]; |
| 9252 | } |
| 9253 | } |
| 9254 | return k; |
| 9255 | } |
| 9256 | |
| 9257 | static Py_ssize_t |
| 9258 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9259 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9260 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9261 | Py_ssize_t i, k = 0; |
| 9262 | |
| 9263 | for (i = 0; i < length; i++) { |
| 9264 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9265 | int n_res, j; |
| 9266 | if (lower) |
| 9267 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9268 | else |
| 9269 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9270 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9271 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9272 | res[k++] = mapped[j]; |
| 9273 | } |
| 9274 | } |
| 9275 | return k; |
| 9276 | } |
| 9277 | |
| 9278 | static Py_ssize_t |
| 9279 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9280 | { |
| 9281 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9282 | } |
| 9283 | |
| 9284 | static Py_ssize_t |
| 9285 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9286 | { |
| 9287 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9288 | } |
| 9289 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9290 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9291 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9292 | { |
| 9293 | Py_ssize_t i, k = 0; |
| 9294 | |
| 9295 | for (i = 0; i < length; i++) { |
| 9296 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9297 | Py_UCS4 mapped[3]; |
| 9298 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9299 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9300 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9301 | res[k++] = mapped[j]; |
| 9302 | } |
| 9303 | } |
| 9304 | return k; |
| 9305 | } |
| 9306 | |
| 9307 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9308 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9309 | { |
| 9310 | Py_ssize_t i, k = 0; |
| 9311 | int previous_is_cased; |
| 9312 | |
| 9313 | previous_is_cased = 0; |
| 9314 | for (i = 0; i < length; i++) { |
| 9315 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9316 | Py_UCS4 mapped[3]; |
| 9317 | int n_res, j; |
| 9318 | |
| 9319 | if (previous_is_cased) |
| 9320 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9321 | else |
| 9322 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9323 | |
| 9324 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9325 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9326 | res[k++] = mapped[j]; |
| 9327 | } |
| 9328 | |
| 9329 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9330 | } |
| 9331 | return k; |
| 9332 | } |
| 9333 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9334 | static PyObject * |
| 9335 | case_operation(PyObject *self, |
| 9336 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9337 | { |
| 9338 | PyObject *res = NULL; |
| 9339 | Py_ssize_t length, newlength = 0; |
| 9340 | int kind, outkind; |
| 9341 | void *data, *outdata; |
| 9342 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9343 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9344 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9345 | |
| 9346 | kind = PyUnicode_KIND(self); |
| 9347 | data = PyUnicode_DATA(self); |
| 9348 | length = PyUnicode_GET_LENGTH(self); |
| 9349 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9350 | if (tmp == NULL) |
| 9351 | return PyErr_NoMemory(); |
| 9352 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9353 | res = PyUnicode_New(newlength, maxchar); |
| 9354 | if (res == NULL) |
| 9355 | goto leave; |
| 9356 | tmpend = tmp + newlength; |
| 9357 | outdata = PyUnicode_DATA(res); |
| 9358 | outkind = PyUnicode_KIND(res); |
| 9359 | switch (outkind) { |
| 9360 | case PyUnicode_1BYTE_KIND: |
| 9361 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9362 | break; |
| 9363 | case PyUnicode_2BYTE_KIND: |
| 9364 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9365 | break; |
| 9366 | case PyUnicode_4BYTE_KIND: |
| 9367 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9368 | break; |
| 9369 | default: |
| 9370 | assert(0); |
| 9371 | break; |
| 9372 | } |
| 9373 | leave: |
| 9374 | PyMem_FREE(tmp); |
| 9375 | return res; |
| 9376 | } |
| 9377 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9378 | PyObject * |
| 9379 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9380 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9381 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9382 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9383 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9384 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9385 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9386 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9387 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9388 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9389 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9390 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9391 | int use_memcpy; |
| 9392 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9393 | PyObject *last_obj; |
| 9394 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9395 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9396 | fseq = PySequence_Fast(seq, ""); |
| 9397 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9398 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9399 | } |
| 9400 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9401 | /* NOTE: the following code can't call back into Python code, |
| 9402 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9403 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9404 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9405 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9406 | /* If empty sequence, return u"". */ |
| 9407 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9408 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9409 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9410 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9411 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9412 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9413 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9414 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9415 | if (seqlen == 1) { |
| 9416 | if (PyUnicode_CheckExact(items[0])) { |
| 9417 | res = items[0]; |
| 9418 | Py_INCREF(res); |
| 9419 | Py_DECREF(fseq); |
| 9420 | return res; |
| 9421 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9422 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9423 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9424 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9425 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9426 | /* Set up sep and seplen */ |
| 9427 | if (separator == NULL) { |
| 9428 | /* fall back to a blank space separator */ |
| 9429 | sep = PyUnicode_FromOrdinal(' '); |
| 9430 | if (!sep) |
| 9431 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9432 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9433 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9434 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9435 | else { |
| 9436 | if (!PyUnicode_Check(separator)) { |
| 9437 | PyErr_Format(PyExc_TypeError, |
| 9438 | "separator: expected str instance," |
| 9439 | " %.80s found", |
| 9440 | Py_TYPE(separator)->tp_name); |
| 9441 | goto onError; |
| 9442 | } |
| 9443 | if (PyUnicode_READY(separator)) |
| 9444 | goto onError; |
| 9445 | sep = separator; |
| 9446 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9447 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9448 | /* inc refcount to keep this code path symmetric with the |
| 9449 | above case of a blank separator */ |
| 9450 | Py_INCREF(sep); |
| 9451 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9452 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9453 | } |
| 9454 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9455 | /* There are at least two things to join, or else we have a subclass |
| 9456 | * of str in the sequence. |
| 9457 | * Do a pre-pass to figure out the total amount of space we'll |
| 9458 | * need (sz), and see whether all argument are strings. |
| 9459 | */ |
| 9460 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9461 | #ifdef Py_DEBUG |
| 9462 | use_memcpy = 0; |
| 9463 | #else |
| 9464 | use_memcpy = 1; |
| 9465 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9466 | for (i = 0; i < seqlen; i++) { |
| 9467 | const Py_ssize_t old_sz = sz; |
| 9468 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9469 | if (!PyUnicode_Check(item)) { |
| 9470 | PyErr_Format(PyExc_TypeError, |
| 9471 | "sequence item %zd: expected str instance," |
| 9472 | " %.80s found", |
| 9473 | i, Py_TYPE(item)->tp_name); |
| 9474 | goto onError; |
| 9475 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9476 | if (PyUnicode_READY(item) == -1) |
| 9477 | goto onError; |
| 9478 | sz += PyUnicode_GET_LENGTH(item); |
| 9479 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9480 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9481 | if (i != 0) |
| 9482 | sz += seplen; |
| 9483 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9484 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9485 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9486 | goto onError; |
| 9487 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9488 | if (use_memcpy && last_obj != NULL) { |
| 9489 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9490 | use_memcpy = 0; |
| 9491 | } |
| 9492 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9493 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9494 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9496 | if (res == NULL) |
| 9497 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9498 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9499 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9500 | #ifdef Py_DEBUG |
| 9501 | use_memcpy = 0; |
| 9502 | #else |
| 9503 | if (use_memcpy) { |
| 9504 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9505 | kind = PyUnicode_KIND(res); |
| 9506 | if (seplen != 0) |
| 9507 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9508 | } |
| 9509 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9510 | if (use_memcpy) { |
| 9511 | for (i = 0; i < seqlen; ++i) { |
| 9512 | Py_ssize_t itemlen; |
| 9513 | item = items[i]; |
| 9514 | |
| 9515 | /* Copy item, and maybe the separator. */ |
| 9516 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9517 | Py_MEMCPY(res_data, |
| 9518 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9519 | kind * seplen); |
| 9520 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9521 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9522 | |
| 9523 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9524 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9525 | Py_MEMCPY(res_data, |
| 9526 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9527 | kind * itemlen); |
| 9528 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9529 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9530 | } |
| 9531 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9532 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9533 | } |
| 9534 | else { |
| 9535 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9536 | Py_ssize_t itemlen; |
| 9537 | item = items[i]; |
| 9538 | |
| 9539 | /* Copy item, and maybe the separator. */ |
| 9540 | if (i && seplen != 0) { |
| 9541 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9542 | res_offset += seplen; |
| 9543 | } |
| 9544 | |
| 9545 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9546 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9547 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9548 | res_offset += itemlen; |
| 9549 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9550 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9551 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9552 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9553 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9554 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9555 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9556 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9557 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9558 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9559 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9560 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9561 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9562 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9563 | return NULL; |
| 9564 | } |
| 9565 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9566 | #define FILL(kind, data, value, start, length) \ |
| 9567 | do { \ |
| 9568 | Py_ssize_t i_ = 0; \ |
| 9569 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9570 | switch ((kind)) { \ |
| 9571 | case PyUnicode_1BYTE_KIND: { \ |
| 9572 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9573 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9574 | break; \ |
| 9575 | } \ |
| 9576 | case PyUnicode_2BYTE_KIND: { \ |
| 9577 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9578 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9579 | break; \ |
| 9580 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9581 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9582 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9583 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9584 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9585 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9586 | } \ |
| 9587 | } \ |
| 9588 | } while (0) |
| 9589 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9590 | void |
| 9591 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9592 | Py_UCS4 fill_char) |
| 9593 | { |
| 9594 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9595 | const void *data = PyUnicode_DATA(unicode); |
| 9596 | assert(PyUnicode_IS_READY(unicode)); |
| 9597 | assert(unicode_modifiable(unicode)); |
| 9598 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9599 | assert(start >= 0); |
| 9600 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9601 | FILL(kind, data, fill_char, start, length); |
| 9602 | } |
| 9603 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9604 | Py_ssize_t |
| 9605 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9606 | Py_UCS4 fill_char) |
| 9607 | { |
| 9608 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9609 | |
| 9610 | if (!PyUnicode_Check(unicode)) { |
| 9611 | PyErr_BadInternalCall(); |
| 9612 | return -1; |
| 9613 | } |
| 9614 | if (PyUnicode_READY(unicode) == -1) |
| 9615 | return -1; |
| 9616 | if (unicode_check_modifiable(unicode)) |
| 9617 | return -1; |
| 9618 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9619 | if (start < 0) { |
| 9620 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9621 | return -1; |
| 9622 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9623 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9624 | PyErr_SetString(PyExc_ValueError, |
| 9625 | "fill character is bigger than " |
| 9626 | "the string maximum character"); |
| 9627 | return -1; |
| 9628 | } |
| 9629 | |
| 9630 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9631 | length = Py_MIN(maxlen, length); |
| 9632 | if (length <= 0) |
| 9633 | return 0; |
| 9634 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9635 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9636 | return length; |
| 9637 | } |
| 9638 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9639 | static PyObject * |
| 9640 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9641 | Py_ssize_t left, |
| 9642 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9643 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9644 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9645 | PyObject *u; |
| 9646 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9647 | int kind; |
| 9648 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9649 | |
| 9650 | if (left < 0) |
| 9651 | left = 0; |
| 9652 | if (right < 0) |
| 9653 | right = 0; |
| 9654 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9655 | if (left == 0 && right == 0) |
| 9656 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9657 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9658 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9659 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9660 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9661 | return NULL; |
| 9662 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9663 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9664 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9665 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9666 | if (!u) |
| 9667 | return NULL; |
| 9668 | |
| 9669 | kind = PyUnicode_KIND(u); |
| 9670 | data = PyUnicode_DATA(u); |
| 9671 | if (left) |
| 9672 | FILL(kind, data, fill, 0, left); |
| 9673 | if (right) |
| 9674 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9675 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9676 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9677 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9678 | } |
| 9679 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9680 | PyObject * |
| 9681 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9682 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9683 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9684 | |
| 9685 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9686 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9687 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9688 | if (PyUnicode_READY(string) == -1) { |
| 9689 | Py_DECREF(string); |
| 9690 | return NULL; |
| 9691 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9692 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9693 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9694 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9695 | if (PyUnicode_IS_ASCII(string)) |
| 9696 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9697 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9698 | PyUnicode_GET_LENGTH(string), keepends); |
| 9699 | else |
| 9700 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9701 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9702 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9703 | break; |
| 9704 | case PyUnicode_2BYTE_KIND: |
| 9705 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9706 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9707 | PyUnicode_GET_LENGTH(string), keepends); |
| 9708 | break; |
| 9709 | case PyUnicode_4BYTE_KIND: |
| 9710 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9711 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9712 | PyUnicode_GET_LENGTH(string), keepends); |
| 9713 | break; |
| 9714 | default: |
| 9715 | assert(0); |
| 9716 | list = 0; |
| 9717 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9718 | Py_DECREF(string); |
| 9719 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9720 | } |
| 9721 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9722 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9723 | split(PyObject *self, |
| 9724 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9725 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9726 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9727 | int kind1, kind2, kind; |
| 9728 | void *buf1, *buf2; |
| 9729 | Py_ssize_t len1, len2; |
| 9730 | PyObject* out; |
| 9731 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9732 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9733 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9735 | if (PyUnicode_READY(self) == -1) |
| 9736 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9737 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9738 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9739 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9740 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9741 | if (PyUnicode_IS_ASCII(self)) |
| 9742 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9743 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9744 | PyUnicode_GET_LENGTH(self), maxcount |
| 9745 | ); |
| 9746 | else |
| 9747 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9748 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9749 | PyUnicode_GET_LENGTH(self), maxcount |
| 9750 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9751 | case PyUnicode_2BYTE_KIND: |
| 9752 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9753 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9754 | PyUnicode_GET_LENGTH(self), maxcount |
| 9755 | ); |
| 9756 | case PyUnicode_4BYTE_KIND: |
| 9757 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9758 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9759 | PyUnicode_GET_LENGTH(self), maxcount |
| 9760 | ); |
| 9761 | default: |
| 9762 | assert(0); |
| 9763 | return NULL; |
| 9764 | } |
| 9765 | |
| 9766 | if (PyUnicode_READY(substring) == -1) |
| 9767 | return NULL; |
| 9768 | |
| 9769 | kind1 = PyUnicode_KIND(self); |
| 9770 | kind2 = PyUnicode_KIND(substring); |
| 9771 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9772 | buf1 = PyUnicode_DATA(self); |
| 9773 | buf2 = PyUnicode_DATA(substring); |
| 9774 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9775 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9776 | if (!buf1) |
| 9777 | return NULL; |
| 9778 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9779 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9780 | if (!buf2) { |
| 9781 | if (kind1 != kind) PyMem_Free(buf1); |
| 9782 | return NULL; |
| 9783 | } |
| 9784 | len1 = PyUnicode_GET_LENGTH(self); |
| 9785 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9786 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9787 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9788 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9789 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9790 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9791 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9792 | else |
| 9793 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9794 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9795 | break; |
| 9796 | case PyUnicode_2BYTE_KIND: |
| 9797 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9798 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9799 | break; |
| 9800 | case PyUnicode_4BYTE_KIND: |
| 9801 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9802 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9803 | break; |
| 9804 | default: |
| 9805 | out = NULL; |
| 9806 | } |
| 9807 | if (kind1 != kind) |
| 9808 | PyMem_Free(buf1); |
| 9809 | if (kind2 != kind) |
| 9810 | PyMem_Free(buf2); |
| 9811 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9814 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9815 | rsplit(PyObject *self, |
| 9816 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9817 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9818 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9819 | int kind1, kind2, kind; |
| 9820 | void *buf1, *buf2; |
| 9821 | Py_ssize_t len1, len2; |
| 9822 | PyObject* out; |
| 9823 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9824 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9825 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9826 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9827 | if (PyUnicode_READY(self) == -1) |
| 9828 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9829 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9830 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9831 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9832 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9833 | if (PyUnicode_IS_ASCII(self)) |
| 9834 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9835 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9836 | PyUnicode_GET_LENGTH(self), maxcount |
| 9837 | ); |
| 9838 | else |
| 9839 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9840 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9841 | PyUnicode_GET_LENGTH(self), maxcount |
| 9842 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9843 | case PyUnicode_2BYTE_KIND: |
| 9844 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9845 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9846 | PyUnicode_GET_LENGTH(self), maxcount |
| 9847 | ); |
| 9848 | case PyUnicode_4BYTE_KIND: |
| 9849 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9850 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9851 | PyUnicode_GET_LENGTH(self), maxcount |
| 9852 | ); |
| 9853 | default: |
| 9854 | assert(0); |
| 9855 | return NULL; |
| 9856 | } |
| 9857 | |
| 9858 | if (PyUnicode_READY(substring) == -1) |
| 9859 | return NULL; |
| 9860 | |
| 9861 | kind1 = PyUnicode_KIND(self); |
| 9862 | kind2 = PyUnicode_KIND(substring); |
| 9863 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9864 | buf1 = PyUnicode_DATA(self); |
| 9865 | buf2 = PyUnicode_DATA(substring); |
| 9866 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9867 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9868 | if (!buf1) |
| 9869 | return NULL; |
| 9870 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9871 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9872 | if (!buf2) { |
| 9873 | if (kind1 != kind) PyMem_Free(buf1); |
| 9874 | return NULL; |
| 9875 | } |
| 9876 | len1 = PyUnicode_GET_LENGTH(self); |
| 9877 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9878 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9879 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9880 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9881 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9882 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9883 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9884 | else |
| 9885 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9886 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9887 | break; |
| 9888 | case PyUnicode_2BYTE_KIND: |
| 9889 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9890 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9891 | break; |
| 9892 | case PyUnicode_4BYTE_KIND: |
| 9893 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9894 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9895 | break; |
| 9896 | default: |
| 9897 | out = NULL; |
| 9898 | } |
| 9899 | if (kind1 != kind) |
| 9900 | PyMem_Free(buf1); |
| 9901 | if (kind2 != kind) |
| 9902 | PyMem_Free(buf2); |
| 9903 | return out; |
| 9904 | } |
| 9905 | |
| 9906 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9907 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9908 | 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] | 9909 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9910 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9911 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9912 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9913 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9914 | else |
| 9915 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9916 | case PyUnicode_2BYTE_KIND: |
| 9917 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9918 | case PyUnicode_4BYTE_KIND: |
| 9919 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9920 | } |
| 9921 | assert(0); |
| 9922 | return -1; |
| 9923 | } |
| 9924 | |
| 9925 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9926 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9927 | 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] | 9928 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 9929 | switch (kind) { |
| 9930 | case PyUnicode_1BYTE_KIND: |
| 9931 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9932 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9933 | else |
| 9934 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9935 | case PyUnicode_2BYTE_KIND: |
| 9936 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9937 | case PyUnicode_4BYTE_KIND: |
| 9938 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9939 | } |
| 9940 | assert(0); |
| 9941 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9942 | } |
| 9943 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9944 | static void |
| 9945 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 9946 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 9947 | { |
| 9948 | int kind = PyUnicode_KIND(u); |
| 9949 | void *data = PyUnicode_DATA(u); |
| 9950 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 9951 | if (kind == PyUnicode_1BYTE_KIND) { |
| 9952 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 9953 | (Py_UCS1 *)data + len, |
| 9954 | u1, u2, maxcount); |
| 9955 | } |
| 9956 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 9957 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 9958 | (Py_UCS2 *)data + len, |
| 9959 | u1, u2, maxcount); |
| 9960 | } |
| 9961 | else { |
| 9962 | assert(kind == PyUnicode_4BYTE_KIND); |
| 9963 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 9964 | (Py_UCS4 *)data + len, |
| 9965 | u1, u2, maxcount); |
| 9966 | } |
| 9967 | } |
| 9968 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9969 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9970 | replace(PyObject *self, PyObject *str1, |
| 9971 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9972 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | PyObject *u; |
| 9974 | char *sbuf = PyUnicode_DATA(self); |
| 9975 | char *buf1 = PyUnicode_DATA(str1); |
| 9976 | char *buf2 = PyUnicode_DATA(str2); |
| 9977 | int srelease = 0, release1 = 0, release2 = 0; |
| 9978 | int skind = PyUnicode_KIND(self); |
| 9979 | int kind1 = PyUnicode_KIND(str1); |
| 9980 | int kind2 = PyUnicode_KIND(str2); |
| 9981 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9982 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9983 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9984 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9985 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9986 | |
| 9987 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9988 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9989 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9990 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9991 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9992 | if (str1 == str2) |
| 9993 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9994 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9995 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9996 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 9997 | if (maxchar < maxchar_str1) |
| 9998 | /* substring too wide to be present */ |
| 9999 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10000 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10001 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10002 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10003 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10004 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10005 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10006 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10007 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10008 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10009 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10010 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10011 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10012 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10013 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10014 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10015 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10016 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10017 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10018 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10019 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10020 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10021 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10022 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10023 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10024 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10025 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10026 | } |
| 10027 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10028 | int rkind = skind; |
| 10029 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10030 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10031 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10032 | if (kind1 < rkind) { |
| 10033 | /* widen substring */ |
| 10034 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10035 | if (!buf1) goto error; |
| 10036 | release1 = 1; |
| 10037 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10038 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10039 | if (i < 0) |
| 10040 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10041 | if (rkind > kind2) { |
| 10042 | /* widen replacement */ |
| 10043 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10044 | if (!buf2) goto error; |
| 10045 | release2 = 1; |
| 10046 | } |
| 10047 | else if (rkind < kind2) { |
| 10048 | /* widen self and buf1 */ |
| 10049 | rkind = kind2; |
| 10050 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10051 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10052 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10053 | if (!sbuf) goto error; |
| 10054 | srelease = 1; |
| 10055 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10056 | if (!buf1) goto error; |
| 10057 | release1 = 1; |
| 10058 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10059 | u = PyUnicode_New(slen, maxchar); |
| 10060 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10061 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10062 | assert(PyUnicode_KIND(u) == rkind); |
| 10063 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10064 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10065 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10066 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10067 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10068 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10069 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10070 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10071 | |
| 10072 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10073 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10074 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10075 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10076 | if (i == -1) |
| 10077 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10078 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10079 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10080 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10081 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10082 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10083 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10084 | } |
| 10085 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10086 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10087 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | int rkind = skind; |
| 10089 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10090 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10091 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10092 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10094 | if (!buf1) goto error; |
| 10095 | release1 = 1; |
| 10096 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10097 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10098 | if (n == 0) |
| 10099 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10100 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10101 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10103 | if (!buf2) goto error; |
| 10104 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10105 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10106 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10107 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10108 | rkind = kind2; |
| 10109 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10110 | if (!sbuf) goto error; |
| 10111 | srelease = 1; |
| 10112 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10113 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10114 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10115 | if (!buf1) goto error; |
| 10116 | release1 = 1; |
| 10117 | } |
| 10118 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10119 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10120 | 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] | 10121 | PyErr_SetString(PyExc_OverflowError, |
| 10122 | "replace string is too long"); |
| 10123 | goto error; |
| 10124 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10125 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10126 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10127 | _Py_INCREF_UNICODE_EMPTY(); |
| 10128 | if (!unicode_empty) |
| 10129 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10130 | u = unicode_empty; |
| 10131 | goto done; |
| 10132 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10133 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10134 | PyErr_SetString(PyExc_OverflowError, |
| 10135 | "replace string is too long"); |
| 10136 | goto error; |
| 10137 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10138 | u = PyUnicode_New(new_size, maxchar); |
| 10139 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10140 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10141 | assert(PyUnicode_KIND(u) == rkind); |
| 10142 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | ires = i = 0; |
| 10144 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10145 | while (n-- > 0) { |
| 10146 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10147 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10148 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10149 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10150 | if (j == -1) |
| 10151 | break; |
| 10152 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10153 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10154 | memcpy(res + rkind * ires, |
| 10155 | sbuf + rkind * i, |
| 10156 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10158 | } |
| 10159 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10160 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10161 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10162 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10163 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10164 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10165 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10166 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10167 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10168 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10169 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10170 | memcpy(res + rkind * ires, |
| 10171 | sbuf + rkind * i, |
| 10172 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10173 | } |
| 10174 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10175 | /* interleave */ |
| 10176 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10177 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10178 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10179 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10180 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10181 | if (--n <= 0) |
| 10182 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10183 | memcpy(res + rkind * ires, |
| 10184 | sbuf + rkind * i, |
| 10185 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10186 | ires++; |
| 10187 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10188 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10189 | memcpy(res + rkind * ires, |
| 10190 | sbuf + rkind * i, |
| 10191 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10192 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10193 | } |
| 10194 | |
| 10195 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10196 | unicode_adjust_maxchar(&u); |
| 10197 | if (u == NULL) |
| 10198 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10199 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10200 | |
| 10201 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10202 | if (srelease) |
| 10203 | PyMem_FREE(sbuf); |
| 10204 | if (release1) |
| 10205 | PyMem_FREE(buf1); |
| 10206 | if (release2) |
| 10207 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10208 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10209 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10210 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10211 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10212 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | if (srelease) |
| 10214 | PyMem_FREE(sbuf); |
| 10215 | if (release1) |
| 10216 | PyMem_FREE(buf1); |
| 10217 | if (release2) |
| 10218 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10219 | return unicode_result_unchanged(self); |
| 10220 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10221 | error: |
| 10222 | if (srelease && sbuf) |
| 10223 | PyMem_FREE(sbuf); |
| 10224 | if (release1 && buf1) |
| 10225 | PyMem_FREE(buf1); |
| 10226 | if (release2 && buf2) |
| 10227 | PyMem_FREE(buf2); |
| 10228 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10229 | } |
| 10230 | |
| 10231 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10232 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10233 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10234 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10235 | \n\ |
| 10236 | 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] | 10237 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10238 | |
| 10239 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10240 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10241 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10242 | if (PyUnicode_READY(self) == -1) |
| 10243 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10244 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10245 | } |
| 10246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10247 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10248 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10249 | \n\ |
| 10250 | 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] | 10251 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10252 | |
| 10253 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10254 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10255 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10256 | if (PyUnicode_READY(self) == -1) |
| 10257 | return NULL; |
| 10258 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10259 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10260 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10261 | } |
| 10262 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10263 | PyDoc_STRVAR(casefold__doc__, |
| 10264 | "S.casefold() -> str\n\ |
| 10265 | \n\ |
| 10266 | Return a version of S suitable for caseless comparisons."); |
| 10267 | |
| 10268 | static PyObject * |
| 10269 | unicode_casefold(PyObject *self) |
| 10270 | { |
| 10271 | if (PyUnicode_READY(self) == -1) |
| 10272 | return NULL; |
| 10273 | if (PyUnicode_IS_ASCII(self)) |
| 10274 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10275 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10276 | } |
| 10277 | |
| 10278 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10279 | /* Argument converter. Coerces to a single unicode character */ |
| 10280 | |
| 10281 | static int |
| 10282 | convert_uc(PyObject *obj, void *addr) |
| 10283 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10284 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10285 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10286 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10287 | uniobj = PyUnicode_FromObject(obj); |
| 10288 | if (uniobj == NULL) { |
| 10289 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10290 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10291 | return 0; |
| 10292 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10294 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10295 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10296 | Py_DECREF(uniobj); |
| 10297 | return 0; |
| 10298 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10299 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10300 | Py_DECREF(uniobj); |
| 10301 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10302 | } |
| 10303 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10304 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10305 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10306 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10307 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10308 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10309 | |
| 10310 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10311 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10312 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10313 | Py_ssize_t marg, left; |
| 10314 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10315 | Py_UCS4 fillchar = ' '; |
| 10316 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10317 | 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] | 10318 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10319 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10320 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10321 | return NULL; |
| 10322 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10323 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10324 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10325 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10326 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10327 | left = marg / 2 + (marg & width & 1); |
| 10328 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10329 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10330 | } |
| 10331 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10332 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10333 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10334 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10335 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10336 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10337 | #define COMPARE(TYPE1, TYPE2) \ |
| 10338 | do { \ |
| 10339 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10340 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10341 | TYPE1* end = p1 + len; \ |
| 10342 | Py_UCS4 c1, c2; \ |
| 10343 | for (; p1 != end; p1++, p2++) { \ |
| 10344 | c1 = *p1; \ |
| 10345 | c2 = *p2; \ |
| 10346 | if (c1 != c2) \ |
| 10347 | return (c1 < c2) ? -1 : 1; \ |
| 10348 | } \ |
| 10349 | } \ |
| 10350 | while (0) |
| 10351 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10352 | int kind1, kind2; |
| 10353 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10354 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10355 | |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10356 | /* a string is equal to itself */ |
| 10357 | if (str1 == str2) |
| 10358 | return 0; |
| 10359 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | kind1 = PyUnicode_KIND(str1); |
| 10361 | kind2 = PyUnicode_KIND(str2); |
| 10362 | data1 = PyUnicode_DATA(str1); |
| 10363 | data2 = PyUnicode_DATA(str2); |
| 10364 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10365 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10366 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10367 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10368 | switch(kind1) { |
| 10369 | case PyUnicode_1BYTE_KIND: |
| 10370 | { |
| 10371 | switch(kind2) { |
| 10372 | case PyUnicode_1BYTE_KIND: |
| 10373 | { |
| 10374 | int cmp = memcmp(data1, data2, len); |
| 10375 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10376 | if (cmp < 0) |
| 10377 | return -1; |
| 10378 | if (cmp > 0) |
| 10379 | return 1; |
| 10380 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10381 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10382 | case PyUnicode_2BYTE_KIND: |
| 10383 | COMPARE(Py_UCS1, Py_UCS2); |
| 10384 | break; |
| 10385 | case PyUnicode_4BYTE_KIND: |
| 10386 | COMPARE(Py_UCS1, Py_UCS4); |
| 10387 | break; |
| 10388 | default: |
| 10389 | assert(0); |
| 10390 | } |
| 10391 | break; |
| 10392 | } |
| 10393 | case PyUnicode_2BYTE_KIND: |
| 10394 | { |
| 10395 | switch(kind2) { |
| 10396 | case PyUnicode_1BYTE_KIND: |
| 10397 | COMPARE(Py_UCS2, Py_UCS1); |
| 10398 | break; |
| 10399 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10400 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10401 | COMPARE(Py_UCS2, Py_UCS2); |
| 10402 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10403 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10404 | case PyUnicode_4BYTE_KIND: |
| 10405 | COMPARE(Py_UCS2, Py_UCS4); |
| 10406 | break; |
| 10407 | default: |
| 10408 | assert(0); |
| 10409 | } |
| 10410 | break; |
| 10411 | } |
| 10412 | case PyUnicode_4BYTE_KIND: |
| 10413 | { |
| 10414 | switch(kind2) { |
| 10415 | case PyUnicode_1BYTE_KIND: |
| 10416 | COMPARE(Py_UCS4, Py_UCS1); |
| 10417 | break; |
| 10418 | case PyUnicode_2BYTE_KIND: |
| 10419 | COMPARE(Py_UCS4, Py_UCS2); |
| 10420 | break; |
| 10421 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10422 | { |
| 10423 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10424 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10425 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10426 | if (cmp < 0) |
| 10427 | return -1; |
| 10428 | if (cmp > 0) |
| 10429 | return 1; |
| 10430 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10431 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10432 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10433 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10434 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10435 | default: |
| 10436 | assert(0); |
| 10437 | } |
| 10438 | break; |
| 10439 | } |
| 10440 | default: |
| 10441 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10442 | } |
| 10443 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10444 | if (len1 == len2) |
| 10445 | return 0; |
| 10446 | if (len1 < len2) |
| 10447 | return -1; |
| 10448 | else |
| 10449 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10450 | |
| 10451 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10452 | } |
| 10453 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10454 | static int |
| 10455 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10456 | { |
| 10457 | int kind; |
| 10458 | void *data1, *data2; |
| 10459 | Py_ssize_t len; |
| 10460 | int cmp; |
| 10461 | |
| 10462 | /* a string is equal to itself */ |
| 10463 | if (str1 == str2) |
| 10464 | return 1; |
| 10465 | |
| 10466 | len = PyUnicode_GET_LENGTH(str1); |
| 10467 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10468 | return 0; |
| 10469 | kind = PyUnicode_KIND(str1); |
| 10470 | if (PyUnicode_KIND(str2) != kind) |
| 10471 | return 0; |
| 10472 | data1 = PyUnicode_DATA(str1); |
| 10473 | data2 = PyUnicode_DATA(str2); |
| 10474 | |
| 10475 | cmp = memcmp(data1, data2, len * kind); |
| 10476 | return (cmp == 0); |
| 10477 | } |
| 10478 | |
| 10479 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10480 | int |
| 10481 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10482 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10483 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10484 | if (PyUnicode_READY(left) == -1 || |
| 10485 | PyUnicode_READY(right) == -1) |
| 10486 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10487 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10488 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10489 | PyErr_Format(PyExc_TypeError, |
| 10490 | "Can't compare %.100s and %.100s", |
| 10491 | left->ob_type->tp_name, |
| 10492 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10493 | return -1; |
| 10494 | } |
| 10495 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10496 | int |
| 10497 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10498 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10499 | Py_ssize_t i; |
| 10500 | int kind; |
| 10501 | void *data; |
| 10502 | Py_UCS4 chr; |
| 10503 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10504 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10505 | if (PyUnicode_READY(uni) == -1) |
| 10506 | return -1; |
| 10507 | kind = PyUnicode_KIND(uni); |
| 10508 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10509 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10510 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10511 | if (chr != str[i]) |
| 10512 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10513 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10514 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10515 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10516 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10517 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10518 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10519 | return 0; |
| 10520 | } |
| 10521 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10522 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10523 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10524 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10525 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10526 | PyObject * |
| 10527 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10528 | { |
| 10529 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10530 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10531 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10532 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10533 | Py_RETURN_NOTIMPLEMENTED; |
| 10534 | |
| 10535 | if (PyUnicode_READY(left) == -1 || |
| 10536 | PyUnicode_READY(right) == -1) |
| 10537 | return NULL; |
| 10538 | |
| 10539 | if (op == Py_EQ || op == Py_NE) { |
| 10540 | result = unicode_compare_eq(left, right); |
| 10541 | if (op == Py_EQ) |
| 10542 | v = TEST_COND(result); |
| 10543 | else |
| 10544 | v = TEST_COND(!result); |
| 10545 | } |
| 10546 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10547 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10548 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10549 | /* Convert the return value to a Boolean */ |
| 10550 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10551 | case Py_LE: |
| 10552 | v = TEST_COND(result <= 0); |
| 10553 | break; |
| 10554 | case Py_GE: |
| 10555 | v = TEST_COND(result >= 0); |
| 10556 | break; |
| 10557 | case Py_LT: |
| 10558 | v = TEST_COND(result == -1); |
| 10559 | break; |
| 10560 | case Py_GT: |
| 10561 | v = TEST_COND(result == 1); |
| 10562 | break; |
| 10563 | default: |
| 10564 | PyErr_BadArgument(); |
| 10565 | return NULL; |
| 10566 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10567 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10568 | Py_INCREF(v); |
| 10569 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10570 | } |
| 10571 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10572 | int |
| 10573 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10574 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10575 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10576 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10577 | void *buf1, *buf2; |
| 10578 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10579 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10580 | |
| 10581 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10582 | sub = PyUnicode_FromObject(element); |
| 10583 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10584 | PyErr_Format(PyExc_TypeError, |
| 10585 | "'in <string>' requires string as left operand, not %s", |
| 10586 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10587 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10588 | } |
| 10589 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10590 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10591 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10592 | Py_DECREF(sub); |
| 10593 | return -1; |
| 10594 | } |
| 10595 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10596 | kind1 = PyUnicode_KIND(str); |
| 10597 | kind2 = PyUnicode_KIND(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10598 | buf1 = PyUnicode_DATA(str); |
| 10599 | buf2 = PyUnicode_DATA(sub); |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10600 | if (kind2 != kind1) { |
| 10601 | if (kind2 > kind1) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10602 | Py_DECREF(sub); |
| 10603 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10604 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10605 | } |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10606 | buf2 = _PyUnicode_AsKind(sub, kind1); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10607 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10608 | if (!buf2) { |
| 10609 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10610 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10611 | return -1; |
| 10612 | } |
| 10613 | len1 = PyUnicode_GET_LENGTH(str); |
| 10614 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10615 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10616 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10617 | case PyUnicode_1BYTE_KIND: |
| 10618 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10619 | break; |
| 10620 | case PyUnicode_2BYTE_KIND: |
| 10621 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10622 | break; |
| 10623 | case PyUnicode_4BYTE_KIND: |
| 10624 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10625 | break; |
| 10626 | default: |
| 10627 | result = -1; |
| 10628 | assert(0); |
| 10629 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10630 | |
| 10631 | Py_DECREF(str); |
| 10632 | Py_DECREF(sub); |
| 10633 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10634 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | PyMem_Free(buf2); |
| 10636 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10637 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10638 | } |
| 10639 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10640 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10641 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10642 | PyObject * |
| 10643 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10644 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10645 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10646 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10647 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10648 | |
| 10649 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10650 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10651 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10652 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10653 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10654 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10655 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10656 | |
| 10657 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10658 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10659 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10660 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10661 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10662 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10663 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10664 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10665 | } |
| 10666 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10667 | u_len = PyUnicode_GET_LENGTH(u); |
| 10668 | v_len = PyUnicode_GET_LENGTH(v); |
| 10669 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10670 | PyErr_SetString(PyExc_OverflowError, |
| 10671 | "strings are too large to concat"); |
| 10672 | goto onError; |
| 10673 | } |
| 10674 | new_len = u_len + v_len; |
| 10675 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10676 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10677 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10678 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10680 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10681 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10682 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10683 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10684 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10685 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10686 | Py_DECREF(u); |
| 10687 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10688 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10689 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10690 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10691 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10692 | Py_XDECREF(u); |
| 10693 | Py_XDECREF(v); |
| 10694 | return NULL; |
| 10695 | } |
| 10696 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10697 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10698 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10699 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10700 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10701 | Py_UCS4 maxchar, maxchar2; |
| 10702 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10703 | |
| 10704 | if (p_left == NULL) { |
| 10705 | if (!PyErr_Occurred()) |
| 10706 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10707 | return; |
| 10708 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10709 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 10710 | if (right == NULL || left == NULL |
| 10711 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10712 | if (!PyErr_Occurred()) |
| 10713 | PyErr_BadInternalCall(); |
| 10714 | goto error; |
| 10715 | } |
| 10716 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10717 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10718 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10719 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10720 | goto error; |
| 10721 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10722 | /* Shortcuts */ |
| 10723 | if (left == unicode_empty) { |
| 10724 | Py_DECREF(left); |
| 10725 | Py_INCREF(right); |
| 10726 | *p_left = right; |
| 10727 | return; |
| 10728 | } |
| 10729 | if (right == unicode_empty) |
| 10730 | return; |
| 10731 | |
| 10732 | left_len = PyUnicode_GET_LENGTH(left); |
| 10733 | right_len = PyUnicode_GET_LENGTH(right); |
| 10734 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10735 | PyErr_SetString(PyExc_OverflowError, |
| 10736 | "strings are too large to concat"); |
| 10737 | goto error; |
| 10738 | } |
| 10739 | new_len = left_len + right_len; |
| 10740 | |
| 10741 | if (unicode_modifiable(left) |
| 10742 | && PyUnicode_CheckExact(right) |
| 10743 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10744 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10745 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10746 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10747 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10748 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10749 | { |
| 10750 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame^] | 10751 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10752 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 10753 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame^] | 10754 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10755 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10756 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10757 | else { |
| 10758 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10759 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10760 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10761 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10762 | /* Concat the two Unicode strings */ |
| 10763 | res = PyUnicode_New(new_len, maxchar); |
| 10764 | if (res == NULL) |
| 10765 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10766 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10767 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10768 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame^] | 10769 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10770 | } |
| 10771 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10772 | return; |
| 10773 | |
| 10774 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10775 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10776 | } |
| 10777 | |
| 10778 | void |
| 10779 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10780 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10781 | PyUnicode_Append(pleft, right); |
| 10782 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10783 | } |
| 10784 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10785 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10786 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10788 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10789 | 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] | 10790 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10791 | |
| 10792 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10793 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10794 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10795 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10796 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10797 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10798 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10799 | int kind1, kind2, kind; |
| 10800 | void *buf1, *buf2; |
| 10801 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10802 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10803 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10804 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10805 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10807 | kind1 = PyUnicode_KIND(self); |
| 10808 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10809 | if (kind2 > kind1) |
| 10810 | return PyLong_FromLong(0); |
| 10811 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10812 | buf1 = PyUnicode_DATA(self); |
| 10813 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10814 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10815 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10816 | if (!buf2) { |
| 10817 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10818 | return NULL; |
| 10819 | } |
| 10820 | len1 = PyUnicode_GET_LENGTH(self); |
| 10821 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10822 | |
| 10823 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10824 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10825 | case PyUnicode_1BYTE_KIND: |
| 10826 | iresult = ucs1lib_count( |
| 10827 | ((Py_UCS1*)buf1) + start, end - start, |
| 10828 | buf2, len2, PY_SSIZE_T_MAX |
| 10829 | ); |
| 10830 | break; |
| 10831 | case PyUnicode_2BYTE_KIND: |
| 10832 | iresult = ucs2lib_count( |
| 10833 | ((Py_UCS2*)buf1) + start, end - start, |
| 10834 | buf2, len2, PY_SSIZE_T_MAX |
| 10835 | ); |
| 10836 | break; |
| 10837 | case PyUnicode_4BYTE_KIND: |
| 10838 | iresult = ucs4lib_count( |
| 10839 | ((Py_UCS4*)buf1) + start, end - start, |
| 10840 | buf2, len2, PY_SSIZE_T_MAX |
| 10841 | ); |
| 10842 | break; |
| 10843 | default: |
| 10844 | assert(0); iresult = 0; |
| 10845 | } |
| 10846 | |
| 10847 | result = PyLong_FromSsize_t(iresult); |
| 10848 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10849 | if (kind2 != kind) |
| 10850 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10851 | |
| 10852 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10854 | return result; |
| 10855 | } |
| 10856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10857 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10858 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10859 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10860 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10861 | 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] | 10862 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10863 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10864 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10865 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10866 | |
| 10867 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10868 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10869 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10870 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10871 | char *encoding = NULL; |
| 10872 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10873 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10874 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10875 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10876 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10877 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10878 | } |
| 10879 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10880 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10881 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10882 | \n\ |
| 10883 | 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] | 10884 | 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] | 10885 | |
| 10886 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10887 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10888 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10889 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10890 | Py_UCS4 ch; |
| 10891 | PyObject *u; |
| 10892 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10893 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10894 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10895 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10896 | |
| 10897 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10898 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10900 | if (PyUnicode_READY(self) == -1) |
| 10901 | return NULL; |
| 10902 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10903 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10904 | src_len = PyUnicode_GET_LENGTH(self); |
| 10905 | i = j = line_pos = 0; |
| 10906 | kind = PyUnicode_KIND(self); |
| 10907 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10908 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10909 | for (; i < src_len; i++) { |
| 10910 | ch = PyUnicode_READ(kind, src_data, i); |
| 10911 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10912 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10913 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10914 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10915 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10916 | goto overflow; |
| 10917 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10918 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10919 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10920 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10921 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10922 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10923 | goto overflow; |
| 10924 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10925 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10926 | if (ch == '\n' || ch == '\r') |
| 10927 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10928 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10929 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10930 | if (!found) |
| 10931 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10932 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10933 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10934 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10935 | if (!u) |
| 10936 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10937 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10938 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10939 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10940 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10941 | for (; i < src_len; i++) { |
| 10942 | ch = PyUnicode_READ(kind, src_data, i); |
| 10943 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10944 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10945 | incr = tabsize - (line_pos % tabsize); |
| 10946 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10947 | FILL(kind, dest_data, ' ', j, incr); |
| 10948 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10949 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10950 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10951 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10952 | line_pos++; |
| 10953 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10954 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10955 | if (ch == '\n' || ch == '\r') |
| 10956 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10957 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10958 | } |
| 10959 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10960 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10961 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10962 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10963 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10964 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10965 | } |
| 10966 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10967 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10968 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10969 | \n\ |
| 10970 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10971 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10972 | arguments start and end are interpreted as in slice notation.\n\ |
| 10973 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10974 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | |
| 10976 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10977 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10979 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10980 | Py_ssize_t start; |
| 10981 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10982 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10983 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10984 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10985 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10986 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10987 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10988 | if (PyUnicode_READY(self) == -1) |
| 10989 | return NULL; |
| 10990 | if (PyUnicode_READY(substring) == -1) |
| 10991 | return NULL; |
| 10992 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10993 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | |
| 10995 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10996 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10997 | if (result == -2) |
| 10998 | return NULL; |
| 10999 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11000 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11001 | } |
| 11002 | |
| 11003 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11004 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11005 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11006 | void *data; |
| 11007 | enum PyUnicode_Kind kind; |
| 11008 | Py_UCS4 ch; |
| 11009 | PyObject *res; |
| 11010 | |
| 11011 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11012 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11013 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11014 | } |
| 11015 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11016 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11017 | return NULL; |
| 11018 | } |
| 11019 | kind = PyUnicode_KIND(self); |
| 11020 | data = PyUnicode_DATA(self); |
| 11021 | ch = PyUnicode_READ(kind, data, index); |
| 11022 | if (ch < 256) |
| 11023 | return get_latin1_char(ch); |
| 11024 | |
| 11025 | res = PyUnicode_New(1, ch); |
| 11026 | if (res == NULL) |
| 11027 | return NULL; |
| 11028 | kind = PyUnicode_KIND(res); |
| 11029 | data = PyUnicode_DATA(res); |
| 11030 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11031 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11032 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11033 | } |
| 11034 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11035 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11036 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11037 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11038 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11039 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11040 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11041 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11042 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11043 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11044 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11045 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11046 | if (_PyUnicode_HASH(self) != -1) |
| 11047 | return _PyUnicode_HASH(self); |
| 11048 | if (PyUnicode_READY(self) == -1) |
| 11049 | return -1; |
| 11050 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11051 | /* |
| 11052 | We make the hash of the empty string be 0, rather than using |
| 11053 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11054 | */ |
| 11055 | if (len == 0) { |
| 11056 | _PyUnicode_HASH(self) = 0; |
| 11057 | return 0; |
| 11058 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11059 | |
| 11060 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11061 | #define HASH(P) \ |
| 11062 | x ^= (Py_uhash_t) *P << 7; \ |
| 11063 | while (--len >= 0) \ |
| 11064 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11065 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11066 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11067 | switch (PyUnicode_KIND(self)) { |
| 11068 | case PyUnicode_1BYTE_KIND: { |
| 11069 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11070 | HASH(c); |
| 11071 | break; |
| 11072 | } |
| 11073 | case PyUnicode_2BYTE_KIND: { |
| 11074 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11075 | HASH(s); |
| 11076 | break; |
| 11077 | } |
| 11078 | default: { |
| 11079 | Py_UCS4 *l; |
| 11080 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11081 | "Impossible switch case in unicode_hash"); |
| 11082 | l = PyUnicode_4BYTE_DATA(self); |
| 11083 | HASH(l); |
| 11084 | break; |
| 11085 | } |
| 11086 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11087 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11088 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11089 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11090 | if (x == -1) |
| 11091 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11092 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11093 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11094 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11095 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11097 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11098 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11099 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11100 | 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] | 11101 | |
| 11102 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11103 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11104 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11105 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11106 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11107 | Py_ssize_t start; |
| 11108 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11110 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11111 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11112 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11113 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11114 | if (PyUnicode_READY(self) == -1) |
| 11115 | return NULL; |
| 11116 | if (PyUnicode_READY(substring) == -1) |
| 11117 | return NULL; |
| 11118 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11119 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11120 | |
| 11121 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11122 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11123 | if (result == -2) |
| 11124 | return NULL; |
| 11125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11126 | if (result < 0) { |
| 11127 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11128 | return NULL; |
| 11129 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11130 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11131 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11132 | } |
| 11133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11134 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11135 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11136 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11137 | 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] | 11138 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11139 | |
| 11140 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11141 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | Py_ssize_t i, length; |
| 11144 | int kind; |
| 11145 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | int cased; |
| 11147 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11148 | if (PyUnicode_READY(self) == -1) |
| 11149 | return NULL; |
| 11150 | length = PyUnicode_GET_LENGTH(self); |
| 11151 | kind = PyUnicode_KIND(self); |
| 11152 | data = PyUnicode_DATA(self); |
| 11153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11154 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11155 | if (length == 1) |
| 11156 | return PyBool_FromLong( |
| 11157 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11158 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11159 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11160 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11161 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11163 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11164 | for (i = 0; i < length; i++) { |
| 11165 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11166 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11167 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11168 | return PyBool_FromLong(0); |
| 11169 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11170 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11171 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11172 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | } |
| 11174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11175 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11176 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11178 | 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] | 11179 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | |
| 11181 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11182 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11184 | Py_ssize_t i, length; |
| 11185 | int kind; |
| 11186 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11187 | int cased; |
| 11188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11189 | if (PyUnicode_READY(self) == -1) |
| 11190 | return NULL; |
| 11191 | length = PyUnicode_GET_LENGTH(self); |
| 11192 | kind = PyUnicode_KIND(self); |
| 11193 | data = PyUnicode_DATA(self); |
| 11194 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11196 | if (length == 1) |
| 11197 | return PyBool_FromLong( |
| 11198 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11200 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11201 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11202 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11204 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11205 | for (i = 0; i < length; i++) { |
| 11206 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11207 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11208 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11209 | return PyBool_FromLong(0); |
| 11210 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11211 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11213 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11214 | } |
| 11215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11216 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11217 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11219 | Return True if S is a titlecased string and there is at least one\n\ |
| 11220 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11221 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11222 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11223 | |
| 11224 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11225 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11226 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11227 | Py_ssize_t i, length; |
| 11228 | int kind; |
| 11229 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | int cased, previous_is_cased; |
| 11231 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11232 | if (PyUnicode_READY(self) == -1) |
| 11233 | return NULL; |
| 11234 | length = PyUnicode_GET_LENGTH(self); |
| 11235 | kind = PyUnicode_KIND(self); |
| 11236 | data = PyUnicode_DATA(self); |
| 11237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11239 | if (length == 1) { |
| 11240 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11241 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11242 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11243 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11244 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11245 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11246 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11247 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11248 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | cased = 0; |
| 11250 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11251 | for (i = 0; i < length; i++) { |
| 11252 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11253 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11254 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11255 | if (previous_is_cased) |
| 11256 | return PyBool_FromLong(0); |
| 11257 | previous_is_cased = 1; |
| 11258 | cased = 1; |
| 11259 | } |
| 11260 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11261 | if (!previous_is_cased) |
| 11262 | return PyBool_FromLong(0); |
| 11263 | previous_is_cased = 1; |
| 11264 | cased = 1; |
| 11265 | } |
| 11266 | else |
| 11267 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11268 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11269 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11270 | } |
| 11271 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11272 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11273 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11274 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11275 | Return True if all characters in S are whitespace\n\ |
| 11276 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11277 | |
| 11278 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11279 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11280 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11281 | Py_ssize_t i, length; |
| 11282 | int kind; |
| 11283 | void *data; |
| 11284 | |
| 11285 | if (PyUnicode_READY(self) == -1) |
| 11286 | return NULL; |
| 11287 | length = PyUnicode_GET_LENGTH(self); |
| 11288 | kind = PyUnicode_KIND(self); |
| 11289 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11291 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11292 | if (length == 1) |
| 11293 | return PyBool_FromLong( |
| 11294 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11295 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11296 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11297 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11298 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11299 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11300 | for (i = 0; i < length; i++) { |
| 11301 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11302 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11303 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11304 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11305 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | } |
| 11307 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11308 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11309 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11310 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11311 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11312 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11313 | |
| 11314 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11315 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11316 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11317 | Py_ssize_t i, length; |
| 11318 | int kind; |
| 11319 | void *data; |
| 11320 | |
| 11321 | if (PyUnicode_READY(self) == -1) |
| 11322 | return NULL; |
| 11323 | length = PyUnicode_GET_LENGTH(self); |
| 11324 | kind = PyUnicode_KIND(self); |
| 11325 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11326 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11327 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11328 | if (length == 1) |
| 11329 | return PyBool_FromLong( |
| 11330 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11331 | |
| 11332 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11333 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11334 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11336 | for (i = 0; i < length; i++) { |
| 11337 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11338 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11339 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11340 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11341 | } |
| 11342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11343 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11344 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11345 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11346 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11347 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11348 | |
| 11349 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11350 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11351 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11352 | int kind; |
| 11353 | void *data; |
| 11354 | Py_ssize_t len, i; |
| 11355 | |
| 11356 | if (PyUnicode_READY(self) == -1) |
| 11357 | return NULL; |
| 11358 | |
| 11359 | kind = PyUnicode_KIND(self); |
| 11360 | data = PyUnicode_DATA(self); |
| 11361 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11362 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11363 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11364 | if (len == 1) { |
| 11365 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11366 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11367 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11368 | |
| 11369 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11370 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11371 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11372 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11373 | for (i = 0; i < len; i++) { |
| 11374 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11375 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11376 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11377 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11378 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11379 | } |
| 11380 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11381 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11382 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11383 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11384 | 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] | 11385 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | |
| 11387 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11388 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11389 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11390 | Py_ssize_t i, length; |
| 11391 | int kind; |
| 11392 | void *data; |
| 11393 | |
| 11394 | if (PyUnicode_READY(self) == -1) |
| 11395 | return NULL; |
| 11396 | length = PyUnicode_GET_LENGTH(self); |
| 11397 | kind = PyUnicode_KIND(self); |
| 11398 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11399 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11400 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | if (length == 1) |
| 11402 | return PyBool_FromLong( |
| 11403 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11404 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11405 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11406 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11407 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11408 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11409 | for (i = 0; i < length; i++) { |
| 11410 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11411 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11412 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11413 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11414 | } |
| 11415 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11416 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11417 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11419 | Return True if all characters in S are digits\n\ |
| 11420 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | |
| 11422 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11423 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11424 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11425 | Py_ssize_t i, length; |
| 11426 | int kind; |
| 11427 | void *data; |
| 11428 | |
| 11429 | if (PyUnicode_READY(self) == -1) |
| 11430 | return NULL; |
| 11431 | length = PyUnicode_GET_LENGTH(self); |
| 11432 | kind = PyUnicode_KIND(self); |
| 11433 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | if (length == 1) { |
| 11437 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11438 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11439 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11440 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11441 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11442 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11443 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11444 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11445 | for (i = 0; i < length; i++) { |
| 11446 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11447 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11448 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11449 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11450 | } |
| 11451 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11452 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11453 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11455 | 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] | 11456 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | |
| 11458 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11459 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11461 | Py_ssize_t i, length; |
| 11462 | int kind; |
| 11463 | void *data; |
| 11464 | |
| 11465 | if (PyUnicode_READY(self) == -1) |
| 11466 | return NULL; |
| 11467 | length = PyUnicode_GET_LENGTH(self); |
| 11468 | kind = PyUnicode_KIND(self); |
| 11469 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11470 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11472 | if (length == 1) |
| 11473 | return PyBool_FromLong( |
| 11474 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11476 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11477 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11478 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11479 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11480 | for (i = 0; i < length; i++) { |
| 11481 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11482 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11484 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 | } |
| 11486 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11487 | int |
| 11488 | PyUnicode_IsIdentifier(PyObject *self) |
| 11489 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11490 | int kind; |
| 11491 | void *data; |
| 11492 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11493 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11494 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11495 | if (PyUnicode_READY(self) == -1) { |
| 11496 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11497 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11498 | } |
| 11499 | |
| 11500 | /* Special case for empty strings */ |
| 11501 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11502 | return 0; |
| 11503 | kind = PyUnicode_KIND(self); |
| 11504 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11505 | |
| 11506 | /* PEP 3131 says that the first character must be in |
| 11507 | XID_Start and subsequent characters in XID_Continue, |
| 11508 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11509 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11510 | letters, digits, underscore). However, given the current |
| 11511 | definition of XID_Start and XID_Continue, it is sufficient |
| 11512 | to check just for these, except that _ must be allowed |
| 11513 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11514 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11515 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11516 | return 0; |
| 11517 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11518 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11519 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11520 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11521 | return 1; |
| 11522 | } |
| 11523 | |
| 11524 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11525 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11526 | \n\ |
| 11527 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11528 | to the language definition.\n\ |
| 11529 | \n\ |
| 11530 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11531 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11532 | |
| 11533 | static PyObject* |
| 11534 | unicode_isidentifier(PyObject *self) |
| 11535 | { |
| 11536 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11537 | } |
| 11538 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11539 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11540 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11541 | \n\ |
| 11542 | Return True if all characters in S are considered\n\ |
| 11543 | printable in repr() or S is empty, False otherwise."); |
| 11544 | |
| 11545 | static PyObject* |
| 11546 | unicode_isprintable(PyObject *self) |
| 11547 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11548 | Py_ssize_t i, length; |
| 11549 | int kind; |
| 11550 | void *data; |
| 11551 | |
| 11552 | if (PyUnicode_READY(self) == -1) |
| 11553 | return NULL; |
| 11554 | length = PyUnicode_GET_LENGTH(self); |
| 11555 | kind = PyUnicode_KIND(self); |
| 11556 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11557 | |
| 11558 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11559 | if (length == 1) |
| 11560 | return PyBool_FromLong( |
| 11561 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11562 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11563 | for (i = 0; i < length; i++) { |
| 11564 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11565 | Py_RETURN_FALSE; |
| 11566 | } |
| 11567 | } |
| 11568 | Py_RETURN_TRUE; |
| 11569 | } |
| 11570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11571 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11572 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11573 | \n\ |
| 11574 | 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] | 11575 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11576 | |
| 11577 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11578 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11579 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11580 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11581 | } |
| 11582 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11583 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11584 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11586 | if (PyUnicode_READY(self) == -1) |
| 11587 | return -1; |
| 11588 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11589 | } |
| 11590 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11591 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11592 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11593 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11594 | 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] | 11595 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11596 | |
| 11597 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11598 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11599 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11600 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11601 | Py_UCS4 fillchar = ' '; |
| 11602 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11603 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11604 | return NULL; |
| 11605 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11606 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11607 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11608 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11609 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11610 | return unicode_result_unchanged(self); |
| 11611 | |
| 11612 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11613 | } |
| 11614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11615 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11616 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11617 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11618 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11619 | |
| 11620 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11621 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11622 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11623 | if (PyUnicode_READY(self) == -1) |
| 11624 | return NULL; |
| 11625 | if (PyUnicode_IS_ASCII(self)) |
| 11626 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11627 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11628 | } |
| 11629 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11630 | #define LEFTSTRIP 0 |
| 11631 | #define RIGHTSTRIP 1 |
| 11632 | #define BOTHSTRIP 2 |
| 11633 | |
| 11634 | /* Arrays indexed by above */ |
| 11635 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11636 | |
| 11637 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11638 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11639 | /* externally visible for str.strip(unicode) */ |
| 11640 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11641 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11642 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11643 | void *data; |
| 11644 | int kind; |
| 11645 | Py_ssize_t i, j, len; |
| 11646 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11647 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11648 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11649 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11650 | return NULL; |
| 11651 | |
| 11652 | kind = PyUnicode_KIND(self); |
| 11653 | data = PyUnicode_DATA(self); |
| 11654 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11655 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11656 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11657 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11658 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11659 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11660 | i = 0; |
| 11661 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11662 | while (i < len) { |
| 11663 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11664 | if (!BLOOM(sepmask, ch)) |
| 11665 | break; |
| 11666 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11667 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11668 | i++; |
| 11669 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11670 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11671 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11672 | j = len; |
| 11673 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11674 | j--; |
| 11675 | while (j >= i) { |
| 11676 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11677 | if (!BLOOM(sepmask, ch)) |
| 11678 | break; |
| 11679 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11680 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11681 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11682 | } |
| 11683 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11684 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11685 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11686 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11687 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11688 | } |
| 11689 | |
| 11690 | PyObject* |
| 11691 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11692 | { |
| 11693 | unsigned char *data; |
| 11694 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11695 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11697 | if (PyUnicode_READY(self) == -1) |
| 11698 | return NULL; |
| 11699 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11700 | length = PyUnicode_GET_LENGTH(self); |
| 11701 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11702 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11703 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11704 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11705 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11706 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11707 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11708 | return NULL; |
| 11709 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11710 | if (start >= length || end < start) |
| 11711 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11712 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11713 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11714 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11715 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11716 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11717 | } |
| 11718 | else { |
| 11719 | kind = PyUnicode_KIND(self); |
| 11720 | data = PyUnicode_1BYTE_DATA(self); |
| 11721 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11722 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11723 | length); |
| 11724 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11725 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11726 | |
| 11727 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11728 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11730 | Py_ssize_t len, i, j; |
| 11731 | |
| 11732 | if (PyUnicode_READY(self) == -1) |
| 11733 | return NULL; |
| 11734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11735 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11736 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11737 | if (PyUnicode_IS_ASCII(self)) { |
| 11738 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 11739 | |
| 11740 | i = 0; |
| 11741 | if (striptype != RIGHTSTRIP) { |
| 11742 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 11743 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11744 | if (!_Py_ascii_whitespace[ch]) |
| 11745 | break; |
| 11746 | i++; |
| 11747 | } |
| 11748 | } |
| 11749 | |
| 11750 | j = len; |
| 11751 | if (striptype != LEFTSTRIP) { |
| 11752 | j--; |
| 11753 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 11754 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11755 | if (!_Py_ascii_whitespace[ch]) |
| 11756 | break; |
| 11757 | j--; |
| 11758 | } |
| 11759 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11760 | } |
| 11761 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11762 | else { |
| 11763 | int kind = PyUnicode_KIND(self); |
| 11764 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11765 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11766 | i = 0; |
| 11767 | if (striptype != RIGHTSTRIP) { |
| 11768 | while (i < len) { |
| 11769 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11770 | if (!Py_UNICODE_ISSPACE(ch)) |
| 11771 | break; |
| 11772 | i++; |
| 11773 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 11774 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11775 | |
| 11776 | j = len; |
| 11777 | if (striptype != LEFTSTRIP) { |
| 11778 | j--; |
| 11779 | while (j >= i) { |
| 11780 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11781 | if (!Py_UNICODE_ISSPACE(ch)) |
| 11782 | break; |
| 11783 | j--; |
| 11784 | } |
| 11785 | j++; |
| 11786 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11787 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11788 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11789 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11790 | } |
| 11791 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11792 | |
| 11793 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11794 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11795 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11796 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11797 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11798 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11799 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11800 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11801 | if (sep != NULL && sep != Py_None) { |
| 11802 | if (PyUnicode_Check(sep)) |
| 11803 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11804 | else { |
| 11805 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11806 | "%s arg must be None or str", |
| 11807 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11808 | return NULL; |
| 11809 | } |
| 11810 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11811 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11812 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11813 | } |
| 11814 | |
| 11815 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11816 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11817 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11818 | \n\ |
| 11819 | Return a copy of the string S with leading and trailing\n\ |
| 11820 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11821 | 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] | 11822 | |
| 11823 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11824 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11825 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11826 | if (PyTuple_GET_SIZE(args) == 0) |
| 11827 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11828 | else |
| 11829 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11830 | } |
| 11831 | |
| 11832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11833 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11834 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11835 | \n\ |
| 11836 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11837 | 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] | 11838 | |
| 11839 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11840 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11841 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11842 | if (PyTuple_GET_SIZE(args) == 0) |
| 11843 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11844 | else |
| 11845 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11846 | } |
| 11847 | |
| 11848 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11849 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11850 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11851 | \n\ |
| 11852 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11853 | 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] | 11854 | |
| 11855 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11856 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11857 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11858 | if (PyTuple_GET_SIZE(args) == 0) |
| 11859 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11860 | else |
| 11861 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11862 | } |
| 11863 | |
| 11864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11865 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11866 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11867 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11868 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11869 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 11871 | if (len < 1) |
| 11872 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11873 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11874 | /* no repeat, return original string */ |
| 11875 | if (len == 1) |
| 11876 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11877 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11878 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11879 | return NULL; |
| 11880 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11881 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11882 | PyErr_SetString(PyExc_OverflowError, |
| 11883 | "repeated string is too long"); |
| 11884 | return NULL; |
| 11885 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11886 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11887 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11888 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11889 | if (!u) |
| 11890 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11891 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11892 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11893 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11894 | const int kind = PyUnicode_KIND(str); |
| 11895 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11896 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11897 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11898 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11899 | } |
| 11900 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11901 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11902 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11903 | ucs2[n] = fill_char; |
| 11904 | } else { |
| 11905 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11906 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11907 | for (n = 0; n < len; ++n) |
| 11908 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11909 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11910 | } |
| 11911 | else { |
| 11912 | /* number of characters copied this far */ |
| 11913 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11914 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11915 | char *to = (char *) PyUnicode_DATA(u); |
| 11916 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11917 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11918 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11919 | n = (done <= nchars-done) ? done : nchars-done; |
| 11920 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11921 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11922 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11923 | } |
| 11924 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11925 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11926 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11927 | } |
| 11928 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11929 | PyObject * |
| 11930 | PyUnicode_Replace(PyObject *obj, |
| 11931 | PyObject *subobj, |
| 11932 | PyObject *replobj, |
| 11933 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | { |
| 11935 | PyObject *self; |
| 11936 | PyObject *str1; |
| 11937 | PyObject *str2; |
| 11938 | PyObject *result; |
| 11939 | |
| 11940 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11941 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11942 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11944 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11945 | Py_DECREF(self); |
| 11946 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11947 | } |
| 11948 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11949 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11950 | Py_DECREF(self); |
| 11951 | Py_DECREF(str1); |
| 11952 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11954 | if (PyUnicode_READY(self) == -1 || |
| 11955 | PyUnicode_READY(str1) == -1 || |
| 11956 | PyUnicode_READY(str2) == -1) |
| 11957 | result = NULL; |
| 11958 | else |
| 11959 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11960 | Py_DECREF(self); |
| 11961 | Py_DECREF(str1); |
| 11962 | Py_DECREF(str2); |
| 11963 | return result; |
| 11964 | } |
| 11965 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11966 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11967 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11968 | \n\ |
| 11969 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11970 | old replaced by new. If the optional argument count is\n\ |
| 11971 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11972 | |
| 11973 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11974 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11975 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11976 | PyObject *str1; |
| 11977 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11978 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11979 | PyObject *result; |
| 11980 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11981 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11982 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11983 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11984 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11985 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11986 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11987 | return NULL; |
| 11988 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11989 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11990 | Py_DECREF(str1); |
| 11991 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11992 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11993 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11994 | result = NULL; |
| 11995 | else |
| 11996 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11997 | |
| 11998 | Py_DECREF(str1); |
| 11999 | Py_DECREF(str2); |
| 12000 | return result; |
| 12001 | } |
| 12002 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12003 | static PyObject * |
| 12004 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12005 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12006 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12007 | Py_ssize_t isize; |
| 12008 | Py_ssize_t osize, squote, dquote, i, o; |
| 12009 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12010 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12011 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12012 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12013 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12014 | return NULL; |
| 12015 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12016 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12017 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12018 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12019 | /* Compute length of output, quote characters, and |
| 12020 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12021 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12022 | max = 127; |
| 12023 | squote = dquote = 0; |
| 12024 | ikind = PyUnicode_KIND(unicode); |
| 12025 | for (i = 0; i < isize; i++) { |
| 12026 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 12027 | switch (ch) { |
| 12028 | case '\'': squote++; osize++; break; |
| 12029 | case '"': dquote++; osize++; break; |
| 12030 | case '\\': case '\t': case '\r': case '\n': |
| 12031 | osize += 2; break; |
| 12032 | default: |
| 12033 | /* Fast-path ASCII */ |
| 12034 | if (ch < ' ' || ch == 0x7f) |
| 12035 | osize += 4; /* \xHH */ |
| 12036 | else if (ch < 0x7f) |
| 12037 | osize++; |
| 12038 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12039 | osize++; |
| 12040 | max = ch > max ? ch : max; |
| 12041 | } |
| 12042 | else if (ch < 0x100) |
| 12043 | osize += 4; /* \xHH */ |
| 12044 | else if (ch < 0x10000) |
| 12045 | osize += 6; /* \uHHHH */ |
| 12046 | else |
| 12047 | osize += 10; /* \uHHHHHHHH */ |
| 12048 | } |
| 12049 | } |
| 12050 | |
| 12051 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12052 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12053 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12054 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12055 | if (dquote) |
| 12056 | /* Both squote and dquote present. Use squote, |
| 12057 | and escape them */ |
| 12058 | osize += squote; |
| 12059 | else |
| 12060 | quote = '"'; |
| 12061 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12062 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12063 | |
| 12064 | repr = PyUnicode_New(osize, max); |
| 12065 | if (repr == NULL) |
| 12066 | return NULL; |
| 12067 | okind = PyUnicode_KIND(repr); |
| 12068 | odata = PyUnicode_DATA(repr); |
| 12069 | |
| 12070 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12071 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12072 | if (unchanged) { |
| 12073 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12074 | unicode, 0, |
| 12075 | isize); |
| 12076 | } |
| 12077 | else { |
| 12078 | for (i = 0, o = 1; i < isize; i++) { |
| 12079 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12080 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12081 | /* Escape quotes and backslashes */ |
| 12082 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12083 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12084 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12085 | continue; |
| 12086 | } |
| 12087 | |
| 12088 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12089 | if (ch == '\t') { |
| 12090 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12091 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12092 | } |
| 12093 | else if (ch == '\n') { |
| 12094 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12095 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12096 | } |
| 12097 | else if (ch == '\r') { |
| 12098 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12099 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12100 | } |
| 12101 | |
| 12102 | /* Map non-printable US ASCII to '\xhh' */ |
| 12103 | else if (ch < ' ' || ch == 0x7F) { |
| 12104 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12105 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12106 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12107 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12108 | } |
| 12109 | |
| 12110 | /* Copy ASCII characters as-is */ |
| 12111 | else if (ch < 0x7F) { |
| 12112 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12113 | } |
| 12114 | |
| 12115 | /* Non-ASCII characters */ |
| 12116 | else { |
| 12117 | /* Map Unicode whitespace and control characters |
| 12118 | (categories Z* and C* except ASCII space) |
| 12119 | */ |
| 12120 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12121 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12122 | /* Map 8-bit characters to '\xhh' */ |
| 12123 | if (ch <= 0xff) { |
| 12124 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12125 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12126 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12127 | } |
| 12128 | /* Map 16-bit characters to '\uxxxx' */ |
| 12129 | else if (ch <= 0xffff) { |
| 12130 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12131 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12132 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12133 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12134 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12135 | } |
| 12136 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12137 | else { |
| 12138 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12139 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12140 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12141 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12142 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12143 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12144 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12145 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12146 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12147 | } |
| 12148 | } |
| 12149 | /* Copy characters as-is */ |
| 12150 | else { |
| 12151 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12152 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12153 | } |
| 12154 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12155 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12156 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12157 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12158 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12159 | } |
| 12160 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12161 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12162 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12163 | \n\ |
| 12164 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12165 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12166 | arguments start and end are interpreted as in slice notation.\n\ |
| 12167 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12168 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12169 | |
| 12170 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12171 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12172 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12173 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12174 | Py_ssize_t start; |
| 12175 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12176 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12177 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12178 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12179 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12180 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12181 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | if (PyUnicode_READY(self) == -1) |
| 12183 | return NULL; |
| 12184 | if (PyUnicode_READY(substring) == -1) |
| 12185 | return NULL; |
| 12186 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12187 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12188 | |
| 12189 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12190 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12191 | if (result == -2) |
| 12192 | return NULL; |
| 12193 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12194 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | } |
| 12196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12197 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12198 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12199 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12200 | 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] | 12201 | |
| 12202 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12203 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12204 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12205 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12206 | Py_ssize_t start; |
| 12207 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12208 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12209 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12210 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12211 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12212 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12213 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12214 | if (PyUnicode_READY(self) == -1) |
| 12215 | return NULL; |
| 12216 | if (PyUnicode_READY(substring) == -1) |
| 12217 | return NULL; |
| 12218 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12219 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | |
| 12221 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12222 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12223 | if (result == -2) |
| 12224 | return NULL; |
| 12225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12226 | if (result < 0) { |
| 12227 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12228 | return NULL; |
| 12229 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12230 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12231 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | } |
| 12233 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12234 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12235 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12236 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12237 | 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] | 12238 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12239 | |
| 12240 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12241 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12242 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12243 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12244 | Py_UCS4 fillchar = ' '; |
| 12245 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12246 | 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] | 12247 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12248 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12249 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12250 | return NULL; |
| 12251 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12252 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12253 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12254 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12255 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12256 | } |
| 12257 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12258 | PyObject * |
| 12259 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12260 | { |
| 12261 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12262 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12263 | s = PyUnicode_FromObject(s); |
| 12264 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12265 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12266 | if (sep != NULL) { |
| 12267 | sep = PyUnicode_FromObject(sep); |
| 12268 | if (sep == NULL) { |
| 12269 | Py_DECREF(s); |
| 12270 | return NULL; |
| 12271 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 | } |
| 12273 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12274 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12275 | |
| 12276 | Py_DECREF(s); |
| 12277 | Py_XDECREF(sep); |
| 12278 | return result; |
| 12279 | } |
| 12280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12281 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12282 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | \n\ |
| 12284 | Return a list of the words in S, using sep as the\n\ |
| 12285 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12286 | 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] | 12287 | whitespace string is a separator and empty strings are\n\ |
| 12288 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12289 | |
| 12290 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12291 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12292 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12293 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12294 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12295 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12296 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12297 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12298 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12299 | return NULL; |
| 12300 | |
| 12301 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12302 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12303 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12304 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12305 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12306 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12307 | } |
| 12308 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12309 | PyObject * |
| 12310 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12311 | { |
| 12312 | PyObject* str_obj; |
| 12313 | PyObject* sep_obj; |
| 12314 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12315 | int kind1, kind2, kind; |
| 12316 | void *buf1 = NULL, *buf2 = NULL; |
| 12317 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12318 | |
| 12319 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12320 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12321 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12322 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12323 | if (!sep_obj) { |
| 12324 | Py_DECREF(str_obj); |
| 12325 | return NULL; |
| 12326 | } |
| 12327 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12328 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12329 | Py_DECREF(str_obj); |
| 12330 | return NULL; |
| 12331 | } |
| 12332 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12333 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12335 | kind = Py_MAX(kind1, kind2); |
| 12336 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12337 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12338 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12339 | if (!buf1) |
| 12340 | goto onError; |
| 12341 | buf2 = PyUnicode_DATA(sep_obj); |
| 12342 | if (kind2 != kind) |
| 12343 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12344 | if (!buf2) |
| 12345 | goto onError; |
| 12346 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12347 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12348 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12349 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12350 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12351 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12352 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12353 | else |
| 12354 | 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] | 12355 | break; |
| 12356 | case PyUnicode_2BYTE_KIND: |
| 12357 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12358 | break; |
| 12359 | case PyUnicode_4BYTE_KIND: |
| 12360 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12361 | break; |
| 12362 | default: |
| 12363 | assert(0); |
| 12364 | out = 0; |
| 12365 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12366 | |
| 12367 | Py_DECREF(sep_obj); |
| 12368 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12369 | if (kind1 != kind) |
| 12370 | PyMem_Free(buf1); |
| 12371 | if (kind2 != kind) |
| 12372 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12373 | |
| 12374 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12375 | onError: |
| 12376 | Py_DECREF(sep_obj); |
| 12377 | Py_DECREF(str_obj); |
| 12378 | if (kind1 != kind && buf1) |
| 12379 | PyMem_Free(buf1); |
| 12380 | if (kind2 != kind && buf2) |
| 12381 | PyMem_Free(buf2); |
| 12382 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12383 | } |
| 12384 | |
| 12385 | |
| 12386 | PyObject * |
| 12387 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12388 | { |
| 12389 | PyObject* str_obj; |
| 12390 | PyObject* sep_obj; |
| 12391 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12392 | int kind1, kind2, kind; |
| 12393 | void *buf1 = NULL, *buf2 = NULL; |
| 12394 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12395 | |
| 12396 | str_obj = PyUnicode_FromObject(str_in); |
| 12397 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12398 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12399 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12400 | if (!sep_obj) { |
| 12401 | Py_DECREF(str_obj); |
| 12402 | return NULL; |
| 12403 | } |
| 12404 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12405 | kind1 = PyUnicode_KIND(str_in); |
| 12406 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12407 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12408 | buf1 = PyUnicode_DATA(str_in); |
| 12409 | if (kind1 != kind) |
| 12410 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12411 | if (!buf1) |
| 12412 | goto onError; |
| 12413 | buf2 = PyUnicode_DATA(sep_obj); |
| 12414 | if (kind2 != kind) |
| 12415 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12416 | if (!buf2) |
| 12417 | goto onError; |
| 12418 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12419 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12420 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12421 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12422 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12423 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12424 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12425 | else |
| 12426 | 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] | 12427 | break; |
| 12428 | case PyUnicode_2BYTE_KIND: |
| 12429 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12430 | break; |
| 12431 | case PyUnicode_4BYTE_KIND: |
| 12432 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12433 | break; |
| 12434 | default: |
| 12435 | assert(0); |
| 12436 | out = 0; |
| 12437 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12438 | |
| 12439 | Py_DECREF(sep_obj); |
| 12440 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12441 | if (kind1 != kind) |
| 12442 | PyMem_Free(buf1); |
| 12443 | if (kind2 != kind) |
| 12444 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12445 | |
| 12446 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12447 | onError: |
| 12448 | Py_DECREF(sep_obj); |
| 12449 | Py_DECREF(str_obj); |
| 12450 | if (kind1 != kind && buf1) |
| 12451 | PyMem_Free(buf1); |
| 12452 | if (kind2 != kind && buf2) |
| 12453 | PyMem_Free(buf2); |
| 12454 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12455 | } |
| 12456 | |
| 12457 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12458 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12459 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12460 | 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] | 12461 | 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] | 12462 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12463 | |
| 12464 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12465 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12466 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12467 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12468 | } |
| 12469 | |
| 12470 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12471 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12472 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12473 | 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] | 12474 | 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] | 12475 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12476 | |
| 12477 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12478 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12479 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12480 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12481 | } |
| 12482 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12483 | PyObject * |
| 12484 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12485 | { |
| 12486 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12487 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12488 | s = PyUnicode_FromObject(s); |
| 12489 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12490 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12491 | if (sep != NULL) { |
| 12492 | sep = PyUnicode_FromObject(sep); |
| 12493 | if (sep == NULL) { |
| 12494 | Py_DECREF(s); |
| 12495 | return NULL; |
| 12496 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12497 | } |
| 12498 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12499 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12500 | |
| 12501 | Py_DECREF(s); |
| 12502 | Py_XDECREF(sep); |
| 12503 | return result; |
| 12504 | } |
| 12505 | |
| 12506 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12507 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12508 | \n\ |
| 12509 | Return a list of the words in S, using sep as the\n\ |
| 12510 | delimiter string, starting at the end of the string and\n\ |
| 12511 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12512 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12513 | is a separator."); |
| 12514 | |
| 12515 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12516 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12517 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12518 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12519 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12520 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12521 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12522 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12523 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12524 | return NULL; |
| 12525 | |
| 12526 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12527 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12528 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12529 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12530 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12531 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12532 | } |
| 12533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12534 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12535 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12536 | \n\ |
| 12537 | 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] | 12538 | 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] | 12539 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12540 | |
| 12541 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12542 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12544 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12545 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12546 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12547 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12548 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12549 | return NULL; |
| 12550 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12551 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12552 | } |
| 12553 | |
| 12554 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12555 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12556 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12557 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12558 | } |
| 12559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12560 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12561 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12562 | \n\ |
| 12563 | 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] | 12564 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12565 | |
| 12566 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12567 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12568 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12569 | if (PyUnicode_READY(self) == -1) |
| 12570 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12571 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12572 | } |
| 12573 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12574 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12575 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12576 | \n\ |
| 12577 | Return a translation table usable for str.translate().\n\ |
| 12578 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12579 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12580 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12581 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12582 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12583 | character at the same position in y. If there is a third argument, it\n\ |
| 12584 | must be a string, whose characters will be mapped to None in the result."); |
| 12585 | |
| 12586 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12587 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12588 | { |
| 12589 | PyObject *x, *y = NULL, *z = NULL; |
| 12590 | PyObject *new = NULL, *key, *value; |
| 12591 | Py_ssize_t i = 0; |
| 12592 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12593 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12594 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12595 | return NULL; |
| 12596 | new = PyDict_New(); |
| 12597 | if (!new) |
| 12598 | return NULL; |
| 12599 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12600 | int x_kind, y_kind, z_kind; |
| 12601 | void *x_data, *y_data, *z_data; |
| 12602 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12603 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12604 | if (!PyUnicode_Check(x)) { |
| 12605 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12606 | "be a string if there is a second argument"); |
| 12607 | goto err; |
| 12608 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12609 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12610 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12611 | "arguments must have equal length"); |
| 12612 | goto err; |
| 12613 | } |
| 12614 | /* 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] | 12615 | x_kind = PyUnicode_KIND(x); |
| 12616 | y_kind = PyUnicode_KIND(y); |
| 12617 | x_data = PyUnicode_DATA(x); |
| 12618 | y_data = PyUnicode_DATA(y); |
| 12619 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12620 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12621 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12622 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12623 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12624 | if (!value) { |
| 12625 | Py_DECREF(key); |
| 12626 | goto err; |
| 12627 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12628 | res = PyDict_SetItem(new, key, value); |
| 12629 | Py_DECREF(key); |
| 12630 | Py_DECREF(value); |
| 12631 | if (res < 0) |
| 12632 | goto err; |
| 12633 | } |
| 12634 | /* create entries for deleting chars in z */ |
| 12635 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12636 | z_kind = PyUnicode_KIND(z); |
| 12637 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12638 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12639 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12640 | if (!key) |
| 12641 | goto err; |
| 12642 | res = PyDict_SetItem(new, key, Py_None); |
| 12643 | Py_DECREF(key); |
| 12644 | if (res < 0) |
| 12645 | goto err; |
| 12646 | } |
| 12647 | } |
| 12648 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12649 | int kind; |
| 12650 | void *data; |
| 12651 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12652 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12653 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12654 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12655 | "to maketrans it must be a dict"); |
| 12656 | goto err; |
| 12657 | } |
| 12658 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12659 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12660 | if (PyUnicode_Check(key)) { |
| 12661 | /* convert string keys to integer keys */ |
| 12662 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12663 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12664 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12665 | "table must be of length 1"); |
| 12666 | goto err; |
| 12667 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12668 | kind = PyUnicode_KIND(key); |
| 12669 | data = PyUnicode_DATA(key); |
| 12670 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12671 | if (!newkey) |
| 12672 | goto err; |
| 12673 | res = PyDict_SetItem(new, newkey, value); |
| 12674 | Py_DECREF(newkey); |
| 12675 | if (res < 0) |
| 12676 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12677 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12678 | /* just keep integer keys */ |
| 12679 | if (PyDict_SetItem(new, key, value) < 0) |
| 12680 | goto err; |
| 12681 | } else { |
| 12682 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12683 | "be strings or integers"); |
| 12684 | goto err; |
| 12685 | } |
| 12686 | } |
| 12687 | } |
| 12688 | return new; |
| 12689 | err: |
| 12690 | Py_DECREF(new); |
| 12691 | return NULL; |
| 12692 | } |
| 12693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12694 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12695 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12696 | \n\ |
| 12697 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12698 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12699 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12700 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12701 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12702 | |
| 12703 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12704 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12705 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12706 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12707 | } |
| 12708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12709 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12710 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12711 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12712 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12713 | |
| 12714 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12715 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12716 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12717 | if (PyUnicode_READY(self) == -1) |
| 12718 | return NULL; |
| 12719 | if (PyUnicode_IS_ASCII(self)) |
| 12720 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12721 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12722 | } |
| 12723 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12724 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12725 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12726 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12727 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12728 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12729 | |
| 12730 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12731 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12732 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12733 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12734 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12735 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12736 | int kind; |
| 12737 | void *data; |
| 12738 | Py_UCS4 chr; |
| 12739 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12740 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12741 | return NULL; |
| 12742 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12743 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12744 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12745 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12746 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12747 | return unicode_result_unchanged(self); |
| 12748 | |
| 12749 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12750 | |
| 12751 | u = pad(self, fill, 0, '0'); |
| 12752 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12753 | if (u == NULL) |
| 12754 | return NULL; |
| 12755 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12756 | kind = PyUnicode_KIND(u); |
| 12757 | data = PyUnicode_DATA(u); |
| 12758 | chr = PyUnicode_READ(kind, data, fill); |
| 12759 | |
| 12760 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12761 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12762 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12763 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12764 | } |
| 12765 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12766 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12767 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12768 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12769 | |
| 12770 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12771 | static PyObject * |
| 12772 | unicode__decimal2ascii(PyObject *self) |
| 12773 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12774 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12775 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12776 | #endif |
| 12777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12778 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12779 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12780 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12781 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12782 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12783 | With optional end, stop comparing S at that position.\n\ |
| 12784 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12785 | |
| 12786 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12787 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12788 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12789 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12790 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12791 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12792 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12793 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12794 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12795 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12796 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12797 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12798 | if (PyTuple_Check(subobj)) { |
| 12799 | Py_ssize_t i; |
| 12800 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12801 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12802 | if (substring == NULL) |
| 12803 | return NULL; |
| 12804 | result = tailmatch(self, substring, start, end, -1); |
| 12805 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12806 | if (result == -1) |
| 12807 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12808 | if (result) { |
| 12809 | Py_RETURN_TRUE; |
| 12810 | } |
| 12811 | } |
| 12812 | /* nothing matched */ |
| 12813 | Py_RETURN_FALSE; |
| 12814 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12815 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12816 | if (substring == NULL) { |
| 12817 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12818 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12819 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12820 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12821 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12822 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12823 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12824 | if (result == -1) |
| 12825 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12826 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12827 | } |
| 12828 | |
| 12829 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12830 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12831 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12832 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12833 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12834 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12835 | With optional end, stop comparing S at that position.\n\ |
| 12836 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12837 | |
| 12838 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12839 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12840 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12841 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12842 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12843 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12844 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12845 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12846 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12848 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12849 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12850 | if (PyTuple_Check(subobj)) { |
| 12851 | Py_ssize_t i; |
| 12852 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12853 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12854 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12855 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12856 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12857 | result = tailmatch(self, substring, start, end, +1); |
| 12858 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12859 | if (result == -1) |
| 12860 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12861 | if (result) { |
| 12862 | Py_RETURN_TRUE; |
| 12863 | } |
| 12864 | } |
| 12865 | Py_RETURN_FALSE; |
| 12866 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12867 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12868 | if (substring == NULL) { |
| 12869 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12870 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12871 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12872 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12873 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12874 | result = tailmatch(self, substring, start, end, +1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12875 | if (result == -1) |
| 12876 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12877 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12878 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12879 | } |
| 12880 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12881 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12882 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12883 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12884 | if (!writer->readonly) |
| 12885 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 12886 | else { |
| 12887 | /* Copy-on-write mode: set buffer size to 0 so |
| 12888 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 12889 | * next write. */ |
| 12890 | writer->size = 0; |
| 12891 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12892 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12893 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12894 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12895 | } |
| 12896 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12897 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12898 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12899 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12900 | memset(writer, 0, sizeof(*writer)); |
| 12901 | #ifdef Py_DEBUG |
| 12902 | writer->kind = 5; /* invalid kind */ |
| 12903 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12904 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12905 | } |
| 12906 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12907 | int |
| 12908 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12909 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12910 | { |
| 12911 | Py_ssize_t newlen; |
| 12912 | PyObject *newbuffer; |
| 12913 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12914 | assert(length > 0); |
| 12915 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12916 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12917 | PyErr_NoMemory(); |
| 12918 | return -1; |
| 12919 | } |
| 12920 | newlen = writer->pos + length; |
| 12921 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12922 | maxchar = MAX_MAXCHAR(maxchar, writer->min_char); |
| 12923 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12924 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12925 | assert(!writer->readonly); |
| 12926 | if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12927 | /* overallocate 25% to limit the number of resize */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12928 | newlen += newlen / 4; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12929 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12930 | if (newlen < writer->min_length) |
| 12931 | newlen = writer->min_length; |
| 12932 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12933 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12934 | if (writer->buffer == NULL) |
| 12935 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12936 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12937 | else if (newlen > writer->size) { |
| 12938 | if (writer->overallocate && newlen <= (PY_SSIZE_T_MAX - newlen / 4)) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12939 | /* overallocate 25% to limit the number of resize */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12940 | newlen += newlen / 4; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12941 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12942 | if (newlen < writer->min_length) |
| 12943 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12944 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12945 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12946 | /* resize + widen */ |
| 12947 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12948 | if (newbuffer == NULL) |
| 12949 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12950 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12951 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12952 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12953 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12954 | } |
| 12955 | else { |
| 12956 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12957 | if (newbuffer == NULL) |
| 12958 | return -1; |
| 12959 | } |
| 12960 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12961 | } |
| 12962 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12963 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12964 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12965 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12966 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12967 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12968 | writer->buffer, 0, writer->pos); |
| 12969 | Py_DECREF(writer->buffer); |
| 12970 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12971 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 12972 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12973 | return 0; |
| 12974 | } |
| 12975 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 12976 | Py_LOCAL_INLINE(int) |
| 12977 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 12978 | { |
| 12979 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 12980 | return -1; |
| 12981 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 12982 | writer->pos++; |
| 12983 | return 0; |
| 12984 | } |
| 12985 | |
| 12986 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 12987 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 12988 | { |
| 12989 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 12990 | } |
| 12991 | |
| 12992 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12993 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12994 | { |
| 12995 | Py_UCS4 maxchar; |
| 12996 | Py_ssize_t len; |
| 12997 | |
| 12998 | if (PyUnicode_READY(str) == -1) |
| 12999 | return -1; |
| 13000 | len = PyUnicode_GET_LENGTH(str); |
| 13001 | if (len == 0) |
| 13002 | return 0; |
| 13003 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13004 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13005 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13006 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13007 | Py_INCREF(str); |
| 13008 | writer->buffer = str; |
| 13009 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13010 | writer->pos += len; |
| 13011 | return 0; |
| 13012 | } |
| 13013 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13014 | return -1; |
| 13015 | } |
| 13016 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13017 | str, 0, len); |
| 13018 | writer->pos += len; |
| 13019 | return 0; |
| 13020 | } |
| 13021 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13022 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13023 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13024 | Py_ssize_t start, Py_ssize_t end) |
| 13025 | { |
| 13026 | Py_UCS4 maxchar; |
| 13027 | Py_ssize_t len; |
| 13028 | |
| 13029 | if (PyUnicode_READY(str) == -1) |
| 13030 | return -1; |
| 13031 | |
| 13032 | assert(0 <= start); |
| 13033 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13034 | assert(start <= end); |
| 13035 | |
| 13036 | if (end == 0) |
| 13037 | return 0; |
| 13038 | |
| 13039 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13040 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13041 | |
| 13042 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13043 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13044 | else |
| 13045 | maxchar = writer->maxchar; |
| 13046 | len = end - start; |
| 13047 | |
| 13048 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13049 | return -1; |
| 13050 | |
| 13051 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13052 | str, start, len); |
| 13053 | writer->pos += len; |
| 13054 | return 0; |
| 13055 | } |
| 13056 | |
| 13057 | int |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13058 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) |
| 13059 | { |
| 13060 | Py_UCS4 maxchar; |
| 13061 | |
| 13062 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13063 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13064 | return -1; |
| 13065 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13066 | writer->pos += len; |
| 13067 | return 0; |
| 13068 | } |
| 13069 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13070 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13071 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13072 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13073 | if (writer->pos == 0) { |
| 13074 | Py_XDECREF(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13075 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13076 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13077 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13078 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 13079 | return writer->buffer; |
| 13080 | } |
| 13081 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13082 | PyObject *newbuffer; |
| 13083 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13084 | if (newbuffer == NULL) { |
| 13085 | Py_DECREF(writer->buffer); |
| 13086 | return NULL; |
| 13087 | } |
| 13088 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13089 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 13090 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 2cb16aa | 2013-03-06 19:28:37 +0100 | [diff] [blame] | 13091 | return unicode_result_ready(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13092 | } |
| 13093 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13094 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13095 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13096 | { |
| 13097 | Py_CLEAR(writer->buffer); |
| 13098 | } |
| 13099 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13100 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13101 | |
| 13102 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13103 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13104 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13105 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13106 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13107 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13108 | PyDoc_STRVAR(format_map__doc__, |
| 13109 | "S.format_map(mapping) -> str\n\ |
| 13110 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13111 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13112 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13113 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13114 | static PyObject * |
| 13115 | unicode__format__(PyObject* self, PyObject* args) |
| 13116 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13117 | PyObject *format_spec; |
| 13118 | _PyUnicodeWriter writer; |
| 13119 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13120 | |
| 13121 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13122 | return NULL; |
| 13123 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13124 | if (PyUnicode_READY(self) == -1) |
| 13125 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13126 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13127 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13128 | self, format_spec, 0, |
| 13129 | PyUnicode_GET_LENGTH(format_spec)); |
| 13130 | if (ret == -1) { |
| 13131 | _PyUnicodeWriter_Dealloc(&writer); |
| 13132 | return NULL; |
| 13133 | } |
| 13134 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13135 | } |
| 13136 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13137 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13138 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13139 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13140 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13141 | |
| 13142 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13143 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13144 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13145 | Py_ssize_t size; |
| 13146 | |
| 13147 | /* If it's a compact object, account for base structure + |
| 13148 | character data. */ |
| 13149 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13150 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13151 | else if (PyUnicode_IS_COMPACT(v)) |
| 13152 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13153 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13154 | else { |
| 13155 | /* If it is a two-block object, account for base object, and |
| 13156 | for character block if present. */ |
| 13157 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13158 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13159 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13160 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13161 | } |
| 13162 | /* 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] | 13163 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13164 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13165 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13166 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13167 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13168 | |
| 13169 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13170 | } |
| 13171 | |
| 13172 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13173 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13174 | |
| 13175 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13176 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13177 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13178 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13179 | if (!copy) |
| 13180 | return NULL; |
| 13181 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13182 | } |
| 13183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13184 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13185 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13186 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13187 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13188 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13189 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13190 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13191 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13192 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13193 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13194 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13195 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13196 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13197 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13198 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13199 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13200 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13201 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13202 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13203 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13204 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13205 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13206 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13207 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13208 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13209 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13210 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13211 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13212 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13213 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13214 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13215 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13216 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13217 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13218 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13219 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13220 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13221 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13222 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13223 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13224 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13225 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13226 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13227 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13228 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13229 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13230 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13231 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13232 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13233 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13234 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13235 | #endif |
| 13236 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13237 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13238 | {NULL, NULL} |
| 13239 | }; |
| 13240 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13241 | static PyObject * |
| 13242 | unicode_mod(PyObject *v, PyObject *w) |
| 13243 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13244 | if (!PyUnicode_Check(v)) |
| 13245 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13246 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13247 | } |
| 13248 | |
| 13249 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13250 | 0, /*nb_add*/ |
| 13251 | 0, /*nb_subtract*/ |
| 13252 | 0, /*nb_multiply*/ |
| 13253 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13254 | }; |
| 13255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13256 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13257 | (lenfunc) unicode_length, /* sq_length */ |
| 13258 | PyUnicode_Concat, /* sq_concat */ |
| 13259 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13260 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13261 | 0, /* sq_slice */ |
| 13262 | 0, /* sq_ass_item */ |
| 13263 | 0, /* sq_ass_slice */ |
| 13264 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13265 | }; |
| 13266 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13267 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13268 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13269 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13270 | if (PyUnicode_READY(self) == -1) |
| 13271 | return NULL; |
| 13272 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13273 | if (PyIndex_Check(item)) { |
| 13274 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13275 | if (i == -1 && PyErr_Occurred()) |
| 13276 | return NULL; |
| 13277 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13278 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13279 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13280 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13281 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13282 | PyObject *result; |
| 13283 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13284 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13285 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13286 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13287 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13288 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13289 | return NULL; |
| 13290 | } |
| 13291 | |
| 13292 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13293 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13294 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13295 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13296 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13297 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13298 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13299 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13300 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13301 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13302 | src_kind = PyUnicode_KIND(self); |
| 13303 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13304 | if (!PyUnicode_IS_ASCII(self)) { |
| 13305 | kind_limit = kind_maxchar_limit(src_kind); |
| 13306 | max_char = 0; |
| 13307 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13308 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13309 | if (ch > max_char) { |
| 13310 | max_char = ch; |
| 13311 | if (max_char >= kind_limit) |
| 13312 | break; |
| 13313 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13314 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13315 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13316 | else |
| 13317 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13318 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13319 | if (result == NULL) |
| 13320 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13321 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13322 | dest_data = PyUnicode_DATA(result); |
| 13323 | |
| 13324 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13325 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13326 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13327 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13328 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13329 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13330 | } else { |
| 13331 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13332 | return NULL; |
| 13333 | } |
| 13334 | } |
| 13335 | |
| 13336 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13337 | (lenfunc)unicode_length, /* mp_length */ |
| 13338 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13339 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13340 | }; |
| 13341 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13343 | /* Helpers for PyUnicode_Format() */ |
| 13344 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13345 | struct unicode_formatter_t { |
| 13346 | PyObject *args; |
| 13347 | int args_owned; |
| 13348 | Py_ssize_t arglen, argidx; |
| 13349 | PyObject *dict; |
| 13350 | |
| 13351 | enum PyUnicode_Kind fmtkind; |
| 13352 | Py_ssize_t fmtcnt, fmtpos; |
| 13353 | void *fmtdata; |
| 13354 | PyObject *fmtstr; |
| 13355 | |
| 13356 | _PyUnicodeWriter writer; |
| 13357 | }; |
| 13358 | |
| 13359 | struct unicode_format_arg_t { |
| 13360 | Py_UCS4 ch; |
| 13361 | int flags; |
| 13362 | Py_ssize_t width; |
| 13363 | int prec; |
| 13364 | int sign; |
| 13365 | }; |
| 13366 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13367 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13368 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13369 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13370 | Py_ssize_t argidx = ctx->argidx; |
| 13371 | |
| 13372 | if (argidx < ctx->arglen) { |
| 13373 | ctx->argidx++; |
| 13374 | if (ctx->arglen < 0) |
| 13375 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13376 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13377 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13378 | } |
| 13379 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13380 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13381 | return NULL; |
| 13382 | } |
| 13383 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13384 | /* 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] | 13385 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13386 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13387 | otherwise. |
| 13388 | |
| 13389 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13390 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13391 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13392 | PyObject **p_output, |
| 13393 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13394 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13395 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13396 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13397 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13398 | int prec; |
| 13399 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13401 | x = PyFloat_AsDouble(v); |
| 13402 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13403 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13404 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13405 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13406 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13407 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13408 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13409 | if (arg->flags & F_ALT) |
| 13410 | dtoa_flags = Py_DTSF_ALT; |
| 13411 | else |
| 13412 | dtoa_flags = 0; |
| 13413 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13414 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13415 | return -1; |
| 13416 | len = strlen(p); |
| 13417 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13418 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13419 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13420 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13421 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13422 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13423 | writer->pos += len; |
| 13424 | } |
| 13425 | else |
| 13426 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13427 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13428 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13429 | } |
| 13430 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13431 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13432 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13433 | * Python's regular ints. |
| 13434 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13435 | * The output string is of the form |
| 13436 | * "-"? ("0x" | "0X")? digit+ |
| 13437 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13438 | * set in flags. The case of hex digits will be correct, |
| 13439 | * There will be at least prec digits, zero-filled on the left if |
| 13440 | * necessary to get that many. |
| 13441 | * val object to be converted |
| 13442 | * flags bitmask of format flags; only F_ALT is looked at |
| 13443 | * prec minimum number of digits; 0-fill on left if needed |
| 13444 | * type a character in [duoxX]; u acts the same as d |
| 13445 | * |
| 13446 | * CAUTION: o, x and X conversions on regular ints can never |
| 13447 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13448 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13449 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13450 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13451 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13452 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13453 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13454 | Py_ssize_t i; |
| 13455 | int sign; /* 1 if '-', else 0 */ |
| 13456 | int len; /* number of characters */ |
| 13457 | Py_ssize_t llen; |
| 13458 | int numdigits; /* len == numnondigits + numdigits */ |
| 13459 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13460 | int prec = arg->prec; |
| 13461 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13462 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13463 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13464 | if (prec > INT_MAX-3) { |
| 13465 | PyErr_SetString(PyExc_OverflowError, |
| 13466 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13467 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13468 | } |
| 13469 | |
| 13470 | assert(PyLong_Check(val)); |
| 13471 | |
| 13472 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13473 | default: |
| 13474 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13475 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13476 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13477 | case 'u': |
| 13478 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13479 | if (PyBool_Check(val)) |
| 13480 | result = PyNumber_ToBase(val, 10); |
| 13481 | else |
| 13482 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13483 | break; |
| 13484 | case 'o': |
| 13485 | numnondigits = 2; |
| 13486 | result = PyNumber_ToBase(val, 8); |
| 13487 | break; |
| 13488 | case 'x': |
| 13489 | case 'X': |
| 13490 | numnondigits = 2; |
| 13491 | result = PyNumber_ToBase(val, 16); |
| 13492 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13493 | } |
| 13494 | if (!result) |
| 13495 | return NULL; |
| 13496 | |
| 13497 | assert(unicode_modifiable(result)); |
| 13498 | assert(PyUnicode_IS_READY(result)); |
| 13499 | assert(PyUnicode_IS_ASCII(result)); |
| 13500 | |
| 13501 | /* To modify the string in-place, there can only be one reference. */ |
| 13502 | if (Py_REFCNT(result) != 1) { |
| 13503 | PyErr_BadInternalCall(); |
| 13504 | return NULL; |
| 13505 | } |
| 13506 | buf = PyUnicode_DATA(result); |
| 13507 | llen = PyUnicode_GET_LENGTH(result); |
| 13508 | if (llen > INT_MAX) { |
| 13509 | PyErr_SetString(PyExc_ValueError, |
| 13510 | "string too large in _PyBytes_FormatLong"); |
| 13511 | return NULL; |
| 13512 | } |
| 13513 | len = (int)llen; |
| 13514 | sign = buf[0] == '-'; |
| 13515 | numnondigits += sign; |
| 13516 | numdigits = len - numnondigits; |
| 13517 | assert(numdigits > 0); |
| 13518 | |
| 13519 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13520 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13521 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13522 | assert(buf[sign] == '0'); |
| 13523 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13524 | buf[sign+1] == 'o'); |
| 13525 | numnondigits -= 2; |
| 13526 | buf += 2; |
| 13527 | len -= 2; |
| 13528 | if (sign) |
| 13529 | buf[0] = '-'; |
| 13530 | assert(len == numnondigits + numdigits); |
| 13531 | assert(numdigits > 0); |
| 13532 | } |
| 13533 | |
| 13534 | /* Fill with leading zeroes to meet minimum width. */ |
| 13535 | if (prec > numdigits) { |
| 13536 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13537 | numnondigits + prec); |
| 13538 | char *b1; |
| 13539 | if (!r1) { |
| 13540 | Py_DECREF(result); |
| 13541 | return NULL; |
| 13542 | } |
| 13543 | b1 = PyBytes_AS_STRING(r1); |
| 13544 | for (i = 0; i < numnondigits; ++i) |
| 13545 | *b1++ = *buf++; |
| 13546 | for (i = 0; i < prec - numdigits; i++) |
| 13547 | *b1++ = '0'; |
| 13548 | for (i = 0; i < numdigits; i++) |
| 13549 | *b1++ = *buf++; |
| 13550 | *b1 = '\0'; |
| 13551 | Py_DECREF(result); |
| 13552 | result = r1; |
| 13553 | buf = PyBytes_AS_STRING(result); |
| 13554 | len = numnondigits + prec; |
| 13555 | } |
| 13556 | |
| 13557 | /* Fix up case for hex conversions. */ |
| 13558 | if (type == 'X') { |
| 13559 | /* Need to convert all lower case letters to upper case. |
| 13560 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13561 | for (i = 0; i < len; i++) |
| 13562 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13563 | buf[i] -= 'a'-'A'; |
| 13564 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13565 | if (!PyUnicode_Check(result) |
| 13566 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13567 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13568 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13569 | Py_DECREF(result); |
| 13570 | result = unicode; |
| 13571 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13572 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13573 | if (PyUnicode_Resize(&result, len) < 0) |
| 13574 | Py_CLEAR(result); |
| 13575 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13576 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13577 | } |
| 13578 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13579 | /* Format an integer. |
| 13580 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13581 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13582 | * -1 and raise an exception on error */ |
| 13583 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13584 | mainformatlong(PyObject *v, |
| 13585 | struct unicode_format_arg_t *arg, |
| 13586 | PyObject **p_output, |
| 13587 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13588 | { |
| 13589 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13590 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13591 | |
| 13592 | if (!PyNumber_Check(v)) |
| 13593 | goto wrongtype; |
| 13594 | |
| 13595 | if (!PyLong_Check(v)) { |
| 13596 | iobj = PyNumber_Long(v); |
| 13597 | if (iobj == NULL) { |
| 13598 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13599 | goto wrongtype; |
| 13600 | return -1; |
| 13601 | } |
| 13602 | assert(PyLong_Check(iobj)); |
| 13603 | } |
| 13604 | else { |
| 13605 | iobj = v; |
| 13606 | Py_INCREF(iobj); |
| 13607 | } |
| 13608 | |
| 13609 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13610 | && arg->width == -1 && arg->prec == -1 |
| 13611 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 13612 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13613 | { |
| 13614 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13615 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13616 | int base; |
| 13617 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13618 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13619 | { |
| 13620 | default: |
| 13621 | assert(0 && "'type' not in [diuoxX]"); |
| 13622 | case 'd': |
| 13623 | case 'i': |
| 13624 | case 'u': |
| 13625 | base = 10; |
| 13626 | break; |
| 13627 | case 'o': |
| 13628 | base = 8; |
| 13629 | break; |
| 13630 | case 'x': |
| 13631 | case 'X': |
| 13632 | base = 16; |
| 13633 | break; |
| 13634 | } |
| 13635 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13636 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 13637 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13638 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13639 | } |
| 13640 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13641 | return 1; |
| 13642 | } |
| 13643 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13644 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13645 | Py_DECREF(iobj); |
| 13646 | if (res == NULL) |
| 13647 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13648 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13649 | return 0; |
| 13650 | |
| 13651 | wrongtype: |
| 13652 | PyErr_Format(PyExc_TypeError, |
| 13653 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13654 | "not %.200s", |
| 13655 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13656 | return -1; |
| 13657 | } |
| 13658 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13659 | static Py_UCS4 |
| 13660 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13661 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13662 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13663 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13664 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13665 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13666 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13667 | goto onError; |
| 13668 | } |
| 13669 | else { |
| 13670 | /* Integer input truncated to a character */ |
| 13671 | long x; |
| 13672 | x = PyLong_AsLong(v); |
| 13673 | if (x == -1 && PyErr_Occurred()) |
| 13674 | goto onError; |
| 13675 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13676 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13677 | PyErr_SetString(PyExc_OverflowError, |
| 13678 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13679 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13680 | } |
| 13681 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13682 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13683 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13684 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13685 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13686 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13687 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13688 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13689 | } |
| 13690 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13691 | /* Parse options of an argument: flags, width, precision. |
| 13692 | Handle also "%(name)" syntax. |
| 13693 | |
| 13694 | Return 0 if the argument has been formatted into arg->str. |
| 13695 | Return 1 if the argument has been written into ctx->writer, |
| 13696 | Raise an exception and return -1 on error. */ |
| 13697 | static int |
| 13698 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 13699 | struct unicode_format_arg_t *arg) |
| 13700 | { |
| 13701 | #define FORMAT_READ(ctx) \ |
| 13702 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 13703 | |
| 13704 | PyObject *v; |
| 13705 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13706 | if (arg->ch == '(') { |
| 13707 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 13708 | Py_ssize_t keystart; |
| 13709 | Py_ssize_t keylen; |
| 13710 | PyObject *key; |
| 13711 | int pcount = 1; |
| 13712 | |
| 13713 | if (ctx->dict == NULL) { |
| 13714 | PyErr_SetString(PyExc_TypeError, |
| 13715 | "format requires a mapping"); |
| 13716 | return -1; |
| 13717 | } |
| 13718 | ++ctx->fmtpos; |
| 13719 | --ctx->fmtcnt; |
| 13720 | keystart = ctx->fmtpos; |
| 13721 | /* Skip over balanced parentheses */ |
| 13722 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 13723 | arg->ch = FORMAT_READ(ctx); |
| 13724 | if (arg->ch == ')') |
| 13725 | --pcount; |
| 13726 | else if (arg->ch == '(') |
| 13727 | ++pcount; |
| 13728 | ctx->fmtpos++; |
| 13729 | } |
| 13730 | keylen = ctx->fmtpos - keystart - 1; |
| 13731 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 13732 | PyErr_SetString(PyExc_ValueError, |
| 13733 | "incomplete format key"); |
| 13734 | return -1; |
| 13735 | } |
| 13736 | key = PyUnicode_Substring(ctx->fmtstr, |
| 13737 | keystart, keystart + keylen); |
| 13738 | if (key == NULL) |
| 13739 | return -1; |
| 13740 | if (ctx->args_owned) { |
| 13741 | Py_DECREF(ctx->args); |
| 13742 | ctx->args_owned = 0; |
| 13743 | } |
| 13744 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 13745 | Py_DECREF(key); |
| 13746 | if (ctx->args == NULL) |
| 13747 | return -1; |
| 13748 | ctx->args_owned = 1; |
| 13749 | ctx->arglen = -1; |
| 13750 | ctx->argidx = -2; |
| 13751 | } |
| 13752 | |
| 13753 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13754 | while (--ctx->fmtcnt >= 0) { |
| 13755 | arg->ch = FORMAT_READ(ctx); |
| 13756 | ctx->fmtpos++; |
| 13757 | switch (arg->ch) { |
| 13758 | case '-': arg->flags |= F_LJUST; continue; |
| 13759 | case '+': arg->flags |= F_SIGN; continue; |
| 13760 | case ' ': arg->flags |= F_BLANK; continue; |
| 13761 | case '#': arg->flags |= F_ALT; continue; |
| 13762 | case '0': arg->flags |= F_ZERO; continue; |
| 13763 | } |
| 13764 | break; |
| 13765 | } |
| 13766 | |
| 13767 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13768 | if (arg->ch == '*') { |
| 13769 | v = unicode_format_getnextarg(ctx); |
| 13770 | if (v == NULL) |
| 13771 | return -1; |
| 13772 | if (!PyLong_Check(v)) { |
| 13773 | PyErr_SetString(PyExc_TypeError, |
| 13774 | "* wants int"); |
| 13775 | return -1; |
| 13776 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13777 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13778 | if (arg->width == -1 && PyErr_Occurred()) |
| 13779 | return -1; |
| 13780 | if (arg->width < 0) { |
| 13781 | arg->flags |= F_LJUST; |
| 13782 | arg->width = -arg->width; |
| 13783 | } |
| 13784 | if (--ctx->fmtcnt >= 0) { |
| 13785 | arg->ch = FORMAT_READ(ctx); |
| 13786 | ctx->fmtpos++; |
| 13787 | } |
| 13788 | } |
| 13789 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13790 | arg->width = arg->ch - '0'; |
| 13791 | while (--ctx->fmtcnt >= 0) { |
| 13792 | arg->ch = FORMAT_READ(ctx); |
| 13793 | ctx->fmtpos++; |
| 13794 | if (arg->ch < '0' || arg->ch > '9') |
| 13795 | break; |
| 13796 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 13797 | mixing signed and unsigned comparison. Since arg->ch is between |
| 13798 | '0' and '9', casting to int is safe. */ |
| 13799 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 13800 | PyErr_SetString(PyExc_ValueError, |
| 13801 | "width too big"); |
| 13802 | return -1; |
| 13803 | } |
| 13804 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 13805 | } |
| 13806 | } |
| 13807 | |
| 13808 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13809 | if (arg->ch == '.') { |
| 13810 | arg->prec = 0; |
| 13811 | if (--ctx->fmtcnt >= 0) { |
| 13812 | arg->ch = FORMAT_READ(ctx); |
| 13813 | ctx->fmtpos++; |
| 13814 | } |
| 13815 | if (arg->ch == '*') { |
| 13816 | v = unicode_format_getnextarg(ctx); |
| 13817 | if (v == NULL) |
| 13818 | return -1; |
| 13819 | if (!PyLong_Check(v)) { |
| 13820 | PyErr_SetString(PyExc_TypeError, |
| 13821 | "* wants int"); |
| 13822 | return -1; |
| 13823 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13824 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13825 | if (arg->prec == -1 && PyErr_Occurred()) |
| 13826 | return -1; |
| 13827 | if (arg->prec < 0) |
| 13828 | arg->prec = 0; |
| 13829 | if (--ctx->fmtcnt >= 0) { |
| 13830 | arg->ch = FORMAT_READ(ctx); |
| 13831 | ctx->fmtpos++; |
| 13832 | } |
| 13833 | } |
| 13834 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13835 | arg->prec = arg->ch - '0'; |
| 13836 | while (--ctx->fmtcnt >= 0) { |
| 13837 | arg->ch = FORMAT_READ(ctx); |
| 13838 | ctx->fmtpos++; |
| 13839 | if (arg->ch < '0' || arg->ch > '9') |
| 13840 | break; |
| 13841 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 13842 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 13843 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13844 | return -1; |
| 13845 | } |
| 13846 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 13847 | } |
| 13848 | } |
| 13849 | } |
| 13850 | |
| 13851 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 13852 | if (ctx->fmtcnt >= 0) { |
| 13853 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 13854 | if (--ctx->fmtcnt >= 0) { |
| 13855 | arg->ch = FORMAT_READ(ctx); |
| 13856 | ctx->fmtpos++; |
| 13857 | } |
| 13858 | } |
| 13859 | } |
| 13860 | if (ctx->fmtcnt < 0) { |
| 13861 | PyErr_SetString(PyExc_ValueError, |
| 13862 | "incomplete format"); |
| 13863 | return -1; |
| 13864 | } |
| 13865 | return 0; |
| 13866 | |
| 13867 | #undef FORMAT_READ |
| 13868 | } |
| 13869 | |
| 13870 | /* Format one argument. Supported conversion specifiers: |
| 13871 | |
| 13872 | - "s", "r", "a": any type |
| 13873 | - "i", "d", "u", "o", "x", "X": int |
| 13874 | - "e", "E", "f", "F", "g", "G": float |
| 13875 | - "c": int or str (1 character) |
| 13876 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13877 | When possible, the output is written directly into the Unicode writer |
| 13878 | (ctx->writer). A string is created when padding is required. |
| 13879 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13880 | Return 0 if the argument has been formatted into *p_str, |
| 13881 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13882 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13883 | static int |
| 13884 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 13885 | struct unicode_format_arg_t *arg, |
| 13886 | PyObject **p_str) |
| 13887 | { |
| 13888 | PyObject *v; |
| 13889 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13890 | |
| 13891 | if (ctx->fmtcnt == 0) |
| 13892 | ctx->writer.overallocate = 0; |
| 13893 | |
| 13894 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13895 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13896 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13897 | return 1; |
| 13898 | } |
| 13899 | |
| 13900 | v = unicode_format_getnextarg(ctx); |
| 13901 | if (v == NULL) |
| 13902 | return -1; |
| 13903 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13904 | |
| 13905 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13906 | case 's': |
| 13907 | case 'r': |
| 13908 | case 'a': |
| 13909 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 13910 | /* Fast path */ |
| 13911 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 13912 | return -1; |
| 13913 | return 1; |
| 13914 | } |
| 13915 | |
| 13916 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 13917 | *p_str = v; |
| 13918 | Py_INCREF(*p_str); |
| 13919 | } |
| 13920 | else { |
| 13921 | if (arg->ch == 's') |
| 13922 | *p_str = PyObject_Str(v); |
| 13923 | else if (arg->ch == 'r') |
| 13924 | *p_str = PyObject_Repr(v); |
| 13925 | else |
| 13926 | *p_str = PyObject_ASCII(v); |
| 13927 | } |
| 13928 | break; |
| 13929 | |
| 13930 | case 'i': |
| 13931 | case 'd': |
| 13932 | case 'u': |
| 13933 | case 'o': |
| 13934 | case 'x': |
| 13935 | case 'X': |
| 13936 | { |
| 13937 | int ret = mainformatlong(v, arg, p_str, writer); |
| 13938 | if (ret != 0) |
| 13939 | return ret; |
| 13940 | arg->sign = 1; |
| 13941 | break; |
| 13942 | } |
| 13943 | |
| 13944 | case 'e': |
| 13945 | case 'E': |
| 13946 | case 'f': |
| 13947 | case 'F': |
| 13948 | case 'g': |
| 13949 | case 'G': |
| 13950 | if (arg->width == -1 && arg->prec == -1 |
| 13951 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13952 | { |
| 13953 | /* Fast path */ |
| 13954 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 13955 | return -1; |
| 13956 | return 1; |
| 13957 | } |
| 13958 | |
| 13959 | arg->sign = 1; |
| 13960 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 13961 | return -1; |
| 13962 | break; |
| 13963 | |
| 13964 | case 'c': |
| 13965 | { |
| 13966 | Py_UCS4 ch = formatchar(v); |
| 13967 | if (ch == (Py_UCS4) -1) |
| 13968 | return -1; |
| 13969 | if (arg->width == -1 && arg->prec == -1) { |
| 13970 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13971 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13972 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13973 | return 1; |
| 13974 | } |
| 13975 | *p_str = PyUnicode_FromOrdinal(ch); |
| 13976 | break; |
| 13977 | } |
| 13978 | |
| 13979 | default: |
| 13980 | PyErr_Format(PyExc_ValueError, |
| 13981 | "unsupported format character '%c' (0x%x) " |
| 13982 | "at index %zd", |
| 13983 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 13984 | (int)arg->ch, |
| 13985 | ctx->fmtpos - 1); |
| 13986 | return -1; |
| 13987 | } |
| 13988 | if (*p_str == NULL) |
| 13989 | return -1; |
| 13990 | assert (PyUnicode_Check(*p_str)); |
| 13991 | return 0; |
| 13992 | } |
| 13993 | |
| 13994 | static int |
| 13995 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 13996 | struct unicode_format_arg_t *arg, |
| 13997 | PyObject *str) |
| 13998 | { |
| 13999 | Py_ssize_t len; |
| 14000 | enum PyUnicode_Kind kind; |
| 14001 | void *pbuf; |
| 14002 | Py_ssize_t pindex; |
| 14003 | Py_UCS4 signchar; |
| 14004 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14005 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14006 | Py_ssize_t sublen; |
| 14007 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14008 | Py_UCS4 fill; |
| 14009 | |
| 14010 | fill = ' '; |
| 14011 | if (arg->sign && arg->flags & F_ZERO) |
| 14012 | fill = '0'; |
| 14013 | |
| 14014 | if (PyUnicode_READY(str) == -1) |
| 14015 | return -1; |
| 14016 | |
| 14017 | len = PyUnicode_GET_LENGTH(str); |
| 14018 | if ((arg->width == -1 || arg->width <= len) |
| 14019 | && (arg->prec == -1 || arg->prec >= len) |
| 14020 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14021 | { |
| 14022 | /* Fast path */ |
| 14023 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14024 | return -1; |
| 14025 | return 0; |
| 14026 | } |
| 14027 | |
| 14028 | /* Truncate the string for "s", "r" and "a" formats |
| 14029 | if the precision is set */ |
| 14030 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14031 | if (arg->prec >= 0 && len > arg->prec) |
| 14032 | len = arg->prec; |
| 14033 | } |
| 14034 | |
| 14035 | /* Adjust sign and width */ |
| 14036 | kind = PyUnicode_KIND(str); |
| 14037 | pbuf = PyUnicode_DATA(str); |
| 14038 | pindex = 0; |
| 14039 | signchar = '\0'; |
| 14040 | if (arg->sign) { |
| 14041 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14042 | if (ch == '-' || ch == '+') { |
| 14043 | signchar = ch; |
| 14044 | len--; |
| 14045 | pindex++; |
| 14046 | } |
| 14047 | else if (arg->flags & F_SIGN) |
| 14048 | signchar = '+'; |
| 14049 | else if (arg->flags & F_BLANK) |
| 14050 | signchar = ' '; |
| 14051 | else |
| 14052 | arg->sign = 0; |
| 14053 | } |
| 14054 | if (arg->width < len) |
| 14055 | arg->width = len; |
| 14056 | |
| 14057 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14058 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14059 | if (!(arg->flags & F_LJUST)) { |
| 14060 | if (arg->sign) { |
| 14061 | if ((arg->width-1) > len) |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14062 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14063 | } |
| 14064 | else { |
| 14065 | if (arg->width > len) |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14066 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14067 | } |
| 14068 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14069 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14070 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
| 14071 | maxchar = MAX_MAXCHAR(maxchar, strmaxchar); |
| 14072 | } |
| 14073 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14074 | buflen = arg->width; |
| 14075 | if (arg->sign && len == arg->width) |
| 14076 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14077 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14078 | return -1; |
| 14079 | |
| 14080 | /* Write the sign if needed */ |
| 14081 | if (arg->sign) { |
| 14082 | if (fill != ' ') { |
| 14083 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14084 | writer->pos += 1; |
| 14085 | } |
| 14086 | if (arg->width > len) |
| 14087 | arg->width--; |
| 14088 | } |
| 14089 | |
| 14090 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14091 | if the alternate form is used. |
| 14092 | For example, write "0x" for the "%#x" format. */ |
| 14093 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14094 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14095 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14096 | if (fill != ' ') { |
| 14097 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14098 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14099 | writer->pos += 2; |
| 14100 | pindex += 2; |
| 14101 | } |
| 14102 | arg->width -= 2; |
| 14103 | if (arg->width < 0) |
| 14104 | arg->width = 0; |
| 14105 | len -= 2; |
| 14106 | } |
| 14107 | |
| 14108 | /* Pad left with the fill character if needed */ |
| 14109 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14110 | sublen = arg->width - len; |
| 14111 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14112 | writer->pos += sublen; |
| 14113 | arg->width = len; |
| 14114 | } |
| 14115 | |
| 14116 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14117 | the alternate form is used */ |
| 14118 | if (fill == ' ') { |
| 14119 | if (arg->sign) { |
| 14120 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14121 | writer->pos += 1; |
| 14122 | } |
| 14123 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14124 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14125 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14126 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14127 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14128 | writer->pos += 2; |
| 14129 | pindex += 2; |
| 14130 | } |
| 14131 | } |
| 14132 | |
| 14133 | /* Write characters */ |
| 14134 | if (len) { |
| 14135 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14136 | str, pindex, len); |
| 14137 | writer->pos += len; |
| 14138 | } |
| 14139 | |
| 14140 | /* Pad right with the fill character if needed */ |
| 14141 | if (arg->width > len) { |
| 14142 | sublen = arg->width - len; |
| 14143 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14144 | writer->pos += sublen; |
| 14145 | } |
| 14146 | return 0; |
| 14147 | } |
| 14148 | |
| 14149 | /* Helper of PyUnicode_Format(): format one arg. |
| 14150 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14151 | static int |
| 14152 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14153 | { |
| 14154 | struct unicode_format_arg_t arg; |
| 14155 | PyObject *str; |
| 14156 | int ret; |
| 14157 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14158 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14159 | arg.flags = 0; |
| 14160 | arg.width = -1; |
| 14161 | arg.prec = -1; |
| 14162 | arg.sign = 0; |
| 14163 | str = NULL; |
| 14164 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14165 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14166 | if (ret == -1) |
| 14167 | return -1; |
| 14168 | |
| 14169 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14170 | if (ret == -1) |
| 14171 | return -1; |
| 14172 | |
| 14173 | if (ret != 1) { |
| 14174 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14175 | Py_DECREF(str); |
| 14176 | if (ret == -1) |
| 14177 | return -1; |
| 14178 | } |
| 14179 | |
| 14180 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14181 | PyErr_SetString(PyExc_TypeError, |
| 14182 | "not all arguments converted during string formatting"); |
| 14183 | return -1; |
| 14184 | } |
| 14185 | return 0; |
| 14186 | } |
| 14187 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14188 | PyObject * |
| 14189 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14190 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14191 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14192 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14193 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14194 | PyErr_BadInternalCall(); |
| 14195 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14196 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14197 | |
| 14198 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14199 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14200 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14201 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14202 | Py_DECREF(ctx.fmtstr); |
| 14203 | return NULL; |
| 14204 | } |
| 14205 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14206 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14207 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14208 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14209 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14210 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14211 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14212 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14214 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14215 | ctx.arglen = PyTuple_Size(args); |
| 14216 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14217 | } |
| 14218 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14219 | ctx.arglen = -1; |
| 14220 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14221 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14222 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14223 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14224 | ctx.dict = args; |
| 14225 | else |
| 14226 | ctx.dict = NULL; |
| 14227 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14228 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14229 | while (--ctx.fmtcnt >= 0) { |
| 14230 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14231 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14232 | |
| 14233 | nonfmtpos = ctx.fmtpos++; |
| 14234 | while (ctx.fmtcnt >= 0 && |
| 14235 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14236 | ctx.fmtpos++; |
| 14237 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14238 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14239 | if (ctx.fmtcnt < 0) { |
| 14240 | ctx.fmtpos--; |
| 14241 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14242 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14243 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14244 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14245 | nonfmtpos, ctx.fmtpos) < 0) |
| 14246 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14247 | } |
| 14248 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14249 | ctx.fmtpos++; |
| 14250 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14251 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14252 | } |
| 14253 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14254 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14255 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14256 | PyErr_SetString(PyExc_TypeError, |
| 14257 | "not all arguments converted during string formatting"); |
| 14258 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14259 | } |
| 14260 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14261 | if (ctx.args_owned) { |
| 14262 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14263 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14264 | Py_DECREF(ctx.fmtstr); |
| 14265 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14266 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14267 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14268 | Py_DECREF(ctx.fmtstr); |
| 14269 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14270 | if (ctx.args_owned) { |
| 14271 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14272 | } |
| 14273 | return NULL; |
| 14274 | } |
| 14275 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14276 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14277 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14278 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14279 | static PyObject * |
| 14280 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14281 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14282 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14283 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14284 | char *encoding = NULL; |
| 14285 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14286 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14287 | if (type != &PyUnicode_Type) |
| 14288 | return unicode_subtype_new(type, args, kwds); |
| 14289 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14290 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14291 | return NULL; |
| 14292 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14293 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14294 | if (encoding == NULL && errors == NULL) |
| 14295 | return PyObject_Str(x); |
| 14296 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14297 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14298 | } |
| 14299 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14300 | static PyObject * |
| 14301 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14302 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14303 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14304 | Py_ssize_t length, char_size; |
| 14305 | int share_wstr, share_utf8; |
| 14306 | unsigned int kind; |
| 14307 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14308 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14309 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14310 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14311 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14312 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14313 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14314 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14315 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14316 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14317 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14318 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14319 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14320 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14321 | if (self == NULL) { |
| 14322 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14323 | return NULL; |
| 14324 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14325 | kind = PyUnicode_KIND(unicode); |
| 14326 | length = PyUnicode_GET_LENGTH(unicode); |
| 14327 | |
| 14328 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14329 | #ifdef Py_DEBUG |
| 14330 | _PyUnicode_HASH(self) = -1; |
| 14331 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14332 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14333 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14334 | _PyUnicode_STATE(self).interned = 0; |
| 14335 | _PyUnicode_STATE(self).kind = kind; |
| 14336 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14337 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14338 | _PyUnicode_STATE(self).ready = 1; |
| 14339 | _PyUnicode_WSTR(self) = NULL; |
| 14340 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14341 | _PyUnicode_UTF8(self) = NULL; |
| 14342 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14343 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14344 | |
| 14345 | share_utf8 = 0; |
| 14346 | share_wstr = 0; |
| 14347 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14348 | char_size = 1; |
| 14349 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14350 | share_utf8 = 1; |
| 14351 | } |
| 14352 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14353 | char_size = 2; |
| 14354 | if (sizeof(wchar_t) == 2) |
| 14355 | share_wstr = 1; |
| 14356 | } |
| 14357 | else { |
| 14358 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14359 | char_size = 4; |
| 14360 | if (sizeof(wchar_t) == 4) |
| 14361 | share_wstr = 1; |
| 14362 | } |
| 14363 | |
| 14364 | /* Ensure we won't overflow the length. */ |
| 14365 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14366 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14367 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14368 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14369 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14370 | if (data == NULL) { |
| 14371 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14372 | goto onError; |
| 14373 | } |
| 14374 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14375 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14376 | if (share_utf8) { |
| 14377 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14378 | _PyUnicode_UTF8(self) = data; |
| 14379 | } |
| 14380 | if (share_wstr) { |
| 14381 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14382 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14383 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14384 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14385 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14386 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14387 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14388 | #ifdef Py_DEBUG |
| 14389 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14390 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14391 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14392 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14393 | |
| 14394 | onError: |
| 14395 | Py_DECREF(unicode); |
| 14396 | Py_DECREF(self); |
| 14397 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14398 | } |
| 14399 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14400 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14401 | "str(object='') -> str\n\ |
| 14402 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14403 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14404 | Create a new string object from the given object. If encoding or\n\ |
| 14405 | errors is specified, then the object must expose a data buffer\n\ |
| 14406 | that will be decoded using the given encoding and error handler.\n\ |
| 14407 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14408 | or repr(object).\n\ |
| 14409 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14410 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14411 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14412 | static PyObject *unicode_iter(PyObject *seq); |
| 14413 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14414 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14415 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14416 | "str", /* tp_name */ |
| 14417 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14418 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14419 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14420 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14421 | 0, /* tp_print */ |
| 14422 | 0, /* tp_getattr */ |
| 14423 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14424 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14425 | unicode_repr, /* tp_repr */ |
| 14426 | &unicode_as_number, /* tp_as_number */ |
| 14427 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14428 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14429 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14430 | 0, /* tp_call*/ |
| 14431 | (reprfunc) unicode_str, /* tp_str */ |
| 14432 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14433 | 0, /* tp_setattro */ |
| 14434 | 0, /* tp_as_buffer */ |
| 14435 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14436 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14437 | unicode_doc, /* tp_doc */ |
| 14438 | 0, /* tp_traverse */ |
| 14439 | 0, /* tp_clear */ |
| 14440 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14441 | 0, /* tp_weaklistoffset */ |
| 14442 | unicode_iter, /* tp_iter */ |
| 14443 | 0, /* tp_iternext */ |
| 14444 | unicode_methods, /* tp_methods */ |
| 14445 | 0, /* tp_members */ |
| 14446 | 0, /* tp_getset */ |
| 14447 | &PyBaseObject_Type, /* tp_base */ |
| 14448 | 0, /* tp_dict */ |
| 14449 | 0, /* tp_descr_get */ |
| 14450 | 0, /* tp_descr_set */ |
| 14451 | 0, /* tp_dictoffset */ |
| 14452 | 0, /* tp_init */ |
| 14453 | 0, /* tp_alloc */ |
| 14454 | unicode_new, /* tp_new */ |
| 14455 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14456 | }; |
| 14457 | |
| 14458 | /* Initialize the Unicode implementation */ |
| 14459 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14460 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14461 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14462 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14463 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14464 | 0x000A, /* LINE FEED */ |
| 14465 | 0x000D, /* CARRIAGE RETURN */ |
| 14466 | 0x001C, /* FILE SEPARATOR */ |
| 14467 | 0x001D, /* GROUP SEPARATOR */ |
| 14468 | 0x001E, /* RECORD SEPARATOR */ |
| 14469 | 0x0085, /* NEXT LINE */ |
| 14470 | 0x2028, /* LINE SEPARATOR */ |
| 14471 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14472 | }; |
| 14473 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14474 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14475 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14476 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14477 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14478 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14479 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14480 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14481 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14482 | |
| 14483 | /* initialize the linebreak bloom filter */ |
| 14484 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14485 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14486 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14487 | |
| 14488 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14489 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14490 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14491 | Py_FatalError("Can't initialize field name iterator type"); |
| 14492 | |
| 14493 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14494 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14495 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14496 | #ifdef HAVE_MBCS |
| 14497 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14498 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14499 | PyErr_SetFromWindowsErr(0); |
| 14500 | return -1; |
| 14501 | } |
| 14502 | #endif |
| 14503 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14504 | } |
| 14505 | |
| 14506 | /* Finalize the Unicode implementation */ |
| 14507 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14508 | int |
| 14509 | PyUnicode_ClearFreeList(void) |
| 14510 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14511 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14512 | } |
| 14513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14514 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14515 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14516 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14517 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14518 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14519 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14520 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14521 | for (i = 0; i < 256; i++) |
| 14522 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14523 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14524 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14525 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14526 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14527 | void |
| 14528 | PyUnicode_InternInPlace(PyObject **p) |
| 14529 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14530 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14531 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14532 | #ifdef Py_DEBUG |
| 14533 | assert(s != NULL); |
| 14534 | assert(_PyUnicode_CHECK(s)); |
| 14535 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14536 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14537 | return; |
| 14538 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14539 | /* If it's a subclass, we don't really know what putting |
| 14540 | it in the interned dict might do. */ |
| 14541 | if (!PyUnicode_CheckExact(s)) |
| 14542 | return; |
| 14543 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14544 | return; |
| 14545 | if (interned == NULL) { |
| 14546 | interned = PyDict_New(); |
| 14547 | if (interned == NULL) { |
| 14548 | PyErr_Clear(); /* Don't leave an exception */ |
| 14549 | return; |
| 14550 | } |
| 14551 | } |
| 14552 | /* It might be that the GetItem call fails even |
| 14553 | though the key is present in the dictionary, |
| 14554 | namely when this happens during a stack overflow. */ |
| 14555 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14556 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14557 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14558 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 14559 | if (t) { |
| 14560 | Py_INCREF(t); |
| 14561 | Py_DECREF(*p); |
| 14562 | *p = t; |
| 14563 | return; |
| 14564 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14565 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14566 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14567 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14568 | PyErr_Clear(); |
| 14569 | PyThreadState_GET()->recursion_critical = 0; |
| 14570 | return; |
| 14571 | } |
| 14572 | PyThreadState_GET()->recursion_critical = 0; |
| 14573 | /* The two references in interned are not counted by refcnt. |
| 14574 | The deallocator will take care of this */ |
| 14575 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14576 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14577 | } |
| 14578 | |
| 14579 | void |
| 14580 | PyUnicode_InternImmortal(PyObject **p) |
| 14581 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14582 | PyUnicode_InternInPlace(p); |
| 14583 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14584 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14585 | Py_INCREF(*p); |
| 14586 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14587 | } |
| 14588 | |
| 14589 | PyObject * |
| 14590 | PyUnicode_InternFromString(const char *cp) |
| 14591 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14592 | PyObject *s = PyUnicode_FromString(cp); |
| 14593 | if (s == NULL) |
| 14594 | return NULL; |
| 14595 | PyUnicode_InternInPlace(&s); |
| 14596 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14597 | } |
| 14598 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14599 | void |
| 14600 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14601 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14602 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14603 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14604 | Py_ssize_t i, n; |
| 14605 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14606 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14607 | if (interned == NULL || !PyDict_Check(interned)) |
| 14608 | return; |
| 14609 | keys = PyDict_Keys(interned); |
| 14610 | if (keys == NULL || !PyList_Check(keys)) { |
| 14611 | PyErr_Clear(); |
| 14612 | return; |
| 14613 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14614 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14615 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14616 | detector, interned unicode strings are not forcibly deallocated; |
| 14617 | rather, we give them their stolen references back, and then clear |
| 14618 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14619 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14620 | n = PyList_GET_SIZE(keys); |
| 14621 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14622 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14623 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14624 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14625 | if (PyUnicode_READY(s) == -1) { |
| 14626 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14627 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14628 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14629 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14630 | case SSTATE_NOT_INTERNED: |
| 14631 | /* XXX Shouldn't happen */ |
| 14632 | break; |
| 14633 | case SSTATE_INTERNED_IMMORTAL: |
| 14634 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14635 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14636 | break; |
| 14637 | case SSTATE_INTERNED_MORTAL: |
| 14638 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14639 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14640 | break; |
| 14641 | default: |
| 14642 | Py_FatalError("Inconsistent interned string state."); |
| 14643 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14644 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14645 | } |
| 14646 | fprintf(stderr, "total size of all interned strings: " |
| 14647 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14648 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14649 | Py_DECREF(keys); |
| 14650 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14651 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14652 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14653 | |
| 14654 | |
| 14655 | /********************* Unicode Iterator **************************/ |
| 14656 | |
| 14657 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14658 | PyObject_HEAD |
| 14659 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14660 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14661 | } unicodeiterobject; |
| 14662 | |
| 14663 | static void |
| 14664 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14665 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14666 | _PyObject_GC_UNTRACK(it); |
| 14667 | Py_XDECREF(it->it_seq); |
| 14668 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14669 | } |
| 14670 | |
| 14671 | static int |
| 14672 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14673 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14674 | Py_VISIT(it->it_seq); |
| 14675 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14676 | } |
| 14677 | |
| 14678 | static PyObject * |
| 14679 | unicodeiter_next(unicodeiterobject *it) |
| 14680 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14681 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14682 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14683 | assert(it != NULL); |
| 14684 | seq = it->it_seq; |
| 14685 | if (seq == NULL) |
| 14686 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14687 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14688 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14689 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14690 | int kind = PyUnicode_KIND(seq); |
| 14691 | void *data = PyUnicode_DATA(seq); |
| 14692 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14693 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14694 | if (item != NULL) |
| 14695 | ++it->it_index; |
| 14696 | return item; |
| 14697 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14698 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14699 | Py_DECREF(seq); |
| 14700 | it->it_seq = NULL; |
| 14701 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14702 | } |
| 14703 | |
| 14704 | static PyObject * |
| 14705 | unicodeiter_len(unicodeiterobject *it) |
| 14706 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14707 | Py_ssize_t len = 0; |
| 14708 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14709 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14710 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14711 | } |
| 14712 | |
| 14713 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14714 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14715 | static PyObject * |
| 14716 | unicodeiter_reduce(unicodeiterobject *it) |
| 14717 | { |
| 14718 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14719 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14720 | it->it_seq, it->it_index); |
| 14721 | } else { |
| 14722 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14723 | if (u == NULL) |
| 14724 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14725 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14726 | } |
| 14727 | } |
| 14728 | |
| 14729 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14730 | |
| 14731 | static PyObject * |
| 14732 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14733 | { |
| 14734 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14735 | if (index == -1 && PyErr_Occurred()) |
| 14736 | return NULL; |
| 14737 | if (index < 0) |
| 14738 | index = 0; |
| 14739 | it->it_index = index; |
| 14740 | Py_RETURN_NONE; |
| 14741 | } |
| 14742 | |
| 14743 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14744 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14745 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14746 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14747 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14748 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14749 | reduce_doc}, |
| 14750 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14751 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14752 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14753 | }; |
| 14754 | |
| 14755 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14756 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14757 | "str_iterator", /* tp_name */ |
| 14758 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14759 | 0, /* tp_itemsize */ |
| 14760 | /* methods */ |
| 14761 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14762 | 0, /* tp_print */ |
| 14763 | 0, /* tp_getattr */ |
| 14764 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14765 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14766 | 0, /* tp_repr */ |
| 14767 | 0, /* tp_as_number */ |
| 14768 | 0, /* tp_as_sequence */ |
| 14769 | 0, /* tp_as_mapping */ |
| 14770 | 0, /* tp_hash */ |
| 14771 | 0, /* tp_call */ |
| 14772 | 0, /* tp_str */ |
| 14773 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14774 | 0, /* tp_setattro */ |
| 14775 | 0, /* tp_as_buffer */ |
| 14776 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14777 | 0, /* tp_doc */ |
| 14778 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14779 | 0, /* tp_clear */ |
| 14780 | 0, /* tp_richcompare */ |
| 14781 | 0, /* tp_weaklistoffset */ |
| 14782 | PyObject_SelfIter, /* tp_iter */ |
| 14783 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14784 | unicodeiter_methods, /* tp_methods */ |
| 14785 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14786 | }; |
| 14787 | |
| 14788 | static PyObject * |
| 14789 | unicode_iter(PyObject *seq) |
| 14790 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14791 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14792 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14793 | if (!PyUnicode_Check(seq)) { |
| 14794 | PyErr_BadInternalCall(); |
| 14795 | return NULL; |
| 14796 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14797 | if (PyUnicode_READY(seq) == -1) |
| 14798 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14799 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14800 | if (it == NULL) |
| 14801 | return NULL; |
| 14802 | it->it_index = 0; |
| 14803 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14804 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14805 | _PyObject_GC_TRACK(it); |
| 14806 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14807 | } |
| 14808 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14809 | |
| 14810 | size_t |
| 14811 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14812 | { |
| 14813 | int res = 0; |
| 14814 | while(*u++) |
| 14815 | res++; |
| 14816 | return res; |
| 14817 | } |
| 14818 | |
| 14819 | Py_UNICODE* |
| 14820 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14821 | { |
| 14822 | Py_UNICODE *u = s1; |
| 14823 | while ((*u++ = *s2++)); |
| 14824 | return s1; |
| 14825 | } |
| 14826 | |
| 14827 | Py_UNICODE* |
| 14828 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14829 | { |
| 14830 | Py_UNICODE *u = s1; |
| 14831 | while ((*u++ = *s2++)) |
| 14832 | if (n-- == 0) |
| 14833 | break; |
| 14834 | return s1; |
| 14835 | } |
| 14836 | |
| 14837 | Py_UNICODE* |
| 14838 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14839 | { |
| 14840 | Py_UNICODE *u1 = s1; |
| 14841 | u1 += Py_UNICODE_strlen(u1); |
| 14842 | Py_UNICODE_strcpy(u1, s2); |
| 14843 | return s1; |
| 14844 | } |
| 14845 | |
| 14846 | int |
| 14847 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14848 | { |
| 14849 | while (*s1 && *s2 && *s1 == *s2) |
| 14850 | s1++, s2++; |
| 14851 | if (*s1 && *s2) |
| 14852 | return (*s1 < *s2) ? -1 : +1; |
| 14853 | if (*s1) |
| 14854 | return 1; |
| 14855 | if (*s2) |
| 14856 | return -1; |
| 14857 | return 0; |
| 14858 | } |
| 14859 | |
| 14860 | int |
| 14861 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14862 | { |
| 14863 | register Py_UNICODE u1, u2; |
| 14864 | for (; n != 0; n--) { |
| 14865 | u1 = *s1; |
| 14866 | u2 = *s2; |
| 14867 | if (u1 != u2) |
| 14868 | return (u1 < u2) ? -1 : +1; |
| 14869 | if (u1 == '\0') |
| 14870 | return 0; |
| 14871 | s1++; |
| 14872 | s2++; |
| 14873 | } |
| 14874 | return 0; |
| 14875 | } |
| 14876 | |
| 14877 | Py_UNICODE* |
| 14878 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14879 | { |
| 14880 | const Py_UNICODE *p; |
| 14881 | for (p = s; *p; p++) |
| 14882 | if (*p == c) |
| 14883 | return (Py_UNICODE*)p; |
| 14884 | return NULL; |
| 14885 | } |
| 14886 | |
| 14887 | Py_UNICODE* |
| 14888 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14889 | { |
| 14890 | const Py_UNICODE *p; |
| 14891 | p = s + Py_UNICODE_strlen(s); |
| 14892 | while (p != s) { |
| 14893 | p--; |
| 14894 | if (*p == c) |
| 14895 | return (Py_UNICODE*)p; |
| 14896 | } |
| 14897 | return NULL; |
| 14898 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14899 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14900 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14901 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14902 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14903 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14904 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14905 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14906 | if (!PyUnicode_Check(unicode)) { |
| 14907 | PyErr_BadArgument(); |
| 14908 | return NULL; |
| 14909 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14910 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14911 | if (u == NULL) |
| 14912 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14913 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14914 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14915 | PyErr_NoMemory(); |
| 14916 | return NULL; |
| 14917 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14918 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14919 | size *= sizeof(Py_UNICODE); |
| 14920 | copy = PyMem_Malloc(size); |
| 14921 | if (copy == NULL) { |
| 14922 | PyErr_NoMemory(); |
| 14923 | return NULL; |
| 14924 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14925 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14926 | return copy; |
| 14927 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14928 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14929 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14930 | to the string.Formatter class implemented in Python. */ |
| 14931 | |
| 14932 | static PyMethodDef _string_methods[] = { |
| 14933 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14934 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14935 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14936 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14937 | {NULL, NULL} |
| 14938 | }; |
| 14939 | |
| 14940 | static struct PyModuleDef _string_module = { |
| 14941 | PyModuleDef_HEAD_INIT, |
| 14942 | "_string", |
| 14943 | PyDoc_STR("string helper module"), |
| 14944 | 0, |
| 14945 | _string_methods, |
| 14946 | NULL, |
| 14947 | NULL, |
| 14948 | NULL, |
| 14949 | NULL |
| 14950 | }; |
| 14951 | |
| 14952 | PyMODINIT_FUNC |
| 14953 | PyInit__string(void) |
| 14954 | { |
| 14955 | return PyModule_Create(&_string_module); |
| 14956 | } |
| 14957 | |
| 14958 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14959 | #ifdef __cplusplus |
| 14960 | } |
| 14961 | #endif |