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 | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2668 | _PyUnicodeWriter_Init(&writer, strlen(format) + 100); |
| 2669 | |
| 2670 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2671 | Copy it to be able to pass a reference to a subfunction. */ |
| 2672 | Py_VA_COPY(vargs2, vargs); |
| 2673 | |
| 2674 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2675 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2676 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2677 | if (f == NULL) |
| 2678 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2679 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2680 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2681 | const char *p; |
| 2682 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2683 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2684 | p = f; |
| 2685 | do |
| 2686 | { |
| 2687 | if ((unsigned char)*p > 127) { |
| 2688 | PyErr_Format(PyExc_ValueError, |
| 2689 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2690 | "string, got a non-ASCII byte: 0x%02x", |
| 2691 | (unsigned char)*p); |
| 2692 | return NULL; |
| 2693 | } |
| 2694 | p++; |
| 2695 | } |
| 2696 | while (*p != '\0' && *p != '%'); |
| 2697 | len = p - f; |
| 2698 | |
| 2699 | if (*p == '\0') |
| 2700 | writer.overallocate = 0; |
| 2701 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
| 2702 | goto fail; |
| 2703 | unicode_write_cstr(writer.buffer, writer.pos, f, len); |
| 2704 | writer.pos += len; |
| 2705 | |
| 2706 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2707 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2708 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2709 | return _PyUnicodeWriter_Finish(&writer); |
| 2710 | |
| 2711 | fail: |
| 2712 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2713 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2716 | PyObject * |
| 2717 | PyUnicode_FromFormat(const char *format, ...) |
| 2718 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2719 | PyObject* ret; |
| 2720 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2721 | |
| 2722 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2723 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2724 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2725 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2726 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2727 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2728 | va_end(vargs); |
| 2729 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2732 | #ifdef HAVE_WCHAR_H |
| 2733 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2734 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2735 | convert a Unicode object to a wide character string. |
| 2736 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2737 | - 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] | 2738 | character) required to convert the unicode object. Ignore size argument. |
| 2739 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2740 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2741 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2742 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2743 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2744 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2745 | wchar_t *w, |
| 2746 | Py_ssize_t size) |
| 2747 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2748 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2749 | const wchar_t *wstr; |
| 2750 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2751 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2752 | if (wstr == NULL) |
| 2753 | return -1; |
| 2754 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2755 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2756 | if (size > res) |
| 2757 | size = res + 1; |
| 2758 | else |
| 2759 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2760 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2761 | return res; |
| 2762 | } |
| 2763 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2764 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2768 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2769 | wchar_t *w, |
| 2770 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2771 | { |
| 2772 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2773 | PyErr_BadInternalCall(); |
| 2774 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2775 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2776 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2779 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2780 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2781 | Py_ssize_t *size) |
| 2782 | { |
| 2783 | wchar_t* buffer; |
| 2784 | Py_ssize_t buflen; |
| 2785 | |
| 2786 | if (unicode == NULL) { |
| 2787 | PyErr_BadInternalCall(); |
| 2788 | return NULL; |
| 2789 | } |
| 2790 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2791 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2792 | if (buflen == -1) |
| 2793 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2794 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2795 | PyErr_NoMemory(); |
| 2796 | return NULL; |
| 2797 | } |
| 2798 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2799 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2800 | if (buffer == NULL) { |
| 2801 | PyErr_NoMemory(); |
| 2802 | return NULL; |
| 2803 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2804 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2805 | if (buflen == -1) { |
| 2806 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2807 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2808 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2809 | if (size != NULL) |
| 2810 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2811 | return buffer; |
| 2812 | } |
| 2813 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2814 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2816 | PyObject * |
| 2817 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2818 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2819 | PyObject *v; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2820 | void *data; |
| 2821 | int kind; |
| 2822 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2823 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2824 | PyErr_SetString(PyExc_ValueError, |
| 2825 | "chr() arg not in range(0x110000)"); |
| 2826 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2827 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2828 | |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2829 | if ((Py_UCS4)ordinal < 256) |
| 2830 | return get_latin1_char((unsigned char)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2831 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2832 | v = PyUnicode_New(1, ordinal); |
| 2833 | if (v == NULL) |
| 2834 | return NULL; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2835 | kind = PyUnicode_KIND(v); |
| 2836 | data = PyUnicode_DATA(v); |
| 2837 | PyUnicode_WRITE(kind, data, 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2838 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2839 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2842 | PyObject * |
| 2843 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2844 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2845 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2846 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2847 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2848 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2849 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2850 | Py_INCREF(obj); |
| 2851 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2852 | } |
| 2853 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2854 | /* For a Unicode subtype that's not a Unicode object, |
| 2855 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2856 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2857 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2858 | PyErr_Format(PyExc_TypeError, |
| 2859 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2860 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2861 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2864 | PyObject * |
| 2865 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2866 | const char *encoding, |
| 2867 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2868 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2869 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2870 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2872 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2873 | PyErr_BadInternalCall(); |
| 2874 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2875 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2876 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2877 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2878 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2879 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2880 | _Py_RETURN_UNICODE_EMPTY(); |
| 2881 | v = PyUnicode_Decode( |
| 2882 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2883 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2884 | return v; |
| 2885 | } |
| 2886 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2887 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2888 | PyErr_SetString(PyExc_TypeError, |
| 2889 | "decoding str is not supported"); |
| 2890 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2891 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2893 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2894 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2895 | PyErr_Format(PyExc_TypeError, |
| 2896 | "coercing to str: need bytes, bytearray " |
| 2897 | "or buffer-like object, %.80s found", |
| 2898 | Py_TYPE(obj)->tp_name); |
| 2899 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2900 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2901 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2902 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2903 | PyBuffer_Release(&buffer); |
| 2904 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2905 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2906 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2907 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2908 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2909 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2912 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2913 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2914 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2915 | int |
| 2916 | _Py_normalize_encoding(const char *encoding, |
| 2917 | char *lower, |
| 2918 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2920 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2921 | char *l; |
| 2922 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2923 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2924 | if (encoding == NULL) { |
| 2925 | strcpy(lower, "utf-8"); |
| 2926 | return 1; |
| 2927 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2928 | e = encoding; |
| 2929 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2930 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2931 | while (*e) { |
| 2932 | if (l == l_end) |
| 2933 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2934 | if (Py_ISUPPER(*e)) { |
| 2935 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2936 | } |
| 2937 | else if (*e == '_') { |
| 2938 | *l++ = '-'; |
| 2939 | e++; |
| 2940 | } |
| 2941 | else { |
| 2942 | *l++ = *e++; |
| 2943 | } |
| 2944 | } |
| 2945 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2946 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2949 | PyObject * |
| 2950 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2951 | Py_ssize_t size, |
| 2952 | const char *encoding, |
| 2953 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2954 | { |
| 2955 | PyObject *buffer = NULL, *unicode; |
| 2956 | Py_buffer info; |
| 2957 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2958 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2959 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2960 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2961 | if ((strcmp(lower, "utf-8") == 0) || |
| 2962 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2963 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2964 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2965 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2966 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2967 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2968 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2969 | else if (strcmp(lower, "mbcs") == 0) |
| 2970 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2971 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2972 | else if (strcmp(lower, "ascii") == 0) |
| 2973 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2974 | else if (strcmp(lower, "utf-16") == 0) |
| 2975 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2976 | else if (strcmp(lower, "utf-32") == 0) |
| 2977 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2978 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2979 | |
| 2980 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2981 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2982 | 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] | 2983 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2984 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2985 | if (buffer == NULL) |
| 2986 | goto onError; |
| 2987 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2988 | if (unicode == NULL) |
| 2989 | goto onError; |
| 2990 | if (!PyUnicode_Check(unicode)) { |
| 2991 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2992 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2993 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2994 | Py_DECREF(unicode); |
| 2995 | goto onError; |
| 2996 | } |
| 2997 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2998 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2999 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3000 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3001 | Py_XDECREF(buffer); |
| 3002 | return NULL; |
| 3003 | } |
| 3004 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3005 | PyObject * |
| 3006 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3007 | const char *encoding, |
| 3008 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3009 | { |
| 3010 | PyObject *v; |
| 3011 | |
| 3012 | if (!PyUnicode_Check(unicode)) { |
| 3013 | PyErr_BadArgument(); |
| 3014 | goto onError; |
| 3015 | } |
| 3016 | |
| 3017 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3018 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3019 | |
| 3020 | /* Decode via the codec registry */ |
| 3021 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3022 | if (v == NULL) |
| 3023 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3024 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3025 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3026 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3027 | return NULL; |
| 3028 | } |
| 3029 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3030 | PyObject * |
| 3031 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3032 | const char *encoding, |
| 3033 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3034 | { |
| 3035 | PyObject *v; |
| 3036 | |
| 3037 | if (!PyUnicode_Check(unicode)) { |
| 3038 | PyErr_BadArgument(); |
| 3039 | goto onError; |
| 3040 | } |
| 3041 | |
| 3042 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3043 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3044 | |
| 3045 | /* Decode via the codec registry */ |
| 3046 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3047 | if (v == NULL) |
| 3048 | goto onError; |
| 3049 | if (!PyUnicode_Check(v)) { |
| 3050 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3051 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3052 | Py_TYPE(v)->tp_name); |
| 3053 | Py_DECREF(v); |
| 3054 | goto onError; |
| 3055 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3056 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3057 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3058 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3059 | return NULL; |
| 3060 | } |
| 3061 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3062 | PyObject * |
| 3063 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3064 | Py_ssize_t size, |
| 3065 | const char *encoding, |
| 3066 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3067 | { |
| 3068 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3070 | unicode = PyUnicode_FromUnicode(s, size); |
| 3071 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3072 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3073 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3074 | Py_DECREF(unicode); |
| 3075 | return v; |
| 3076 | } |
| 3077 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3078 | PyObject * |
| 3079 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3080 | const char *encoding, |
| 3081 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3082 | { |
| 3083 | PyObject *v; |
| 3084 | |
| 3085 | if (!PyUnicode_Check(unicode)) { |
| 3086 | PyErr_BadArgument(); |
| 3087 | goto onError; |
| 3088 | } |
| 3089 | |
| 3090 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3091 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3092 | |
| 3093 | /* Encode via the codec registry */ |
| 3094 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3095 | if (v == NULL) |
| 3096 | goto onError; |
| 3097 | return v; |
| 3098 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3099 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3100 | return NULL; |
| 3101 | } |
| 3102 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3103 | static size_t |
| 3104 | wcstombs_errorpos(const wchar_t *wstr) |
| 3105 | { |
| 3106 | size_t len; |
| 3107 | #if SIZEOF_WCHAR_T == 2 |
| 3108 | wchar_t buf[3]; |
| 3109 | #else |
| 3110 | wchar_t buf[2]; |
| 3111 | #endif |
| 3112 | char outbuf[MB_LEN_MAX]; |
| 3113 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3114 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3115 | #if SIZEOF_WCHAR_T == 2 |
| 3116 | buf[2] = 0; |
| 3117 | #else |
| 3118 | buf[1] = 0; |
| 3119 | #endif |
| 3120 | start = wstr; |
| 3121 | while (*wstr != L'\0') |
| 3122 | { |
| 3123 | previous = wstr; |
| 3124 | #if SIZEOF_WCHAR_T == 2 |
| 3125 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3126 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3127 | { |
| 3128 | buf[0] = wstr[0]; |
| 3129 | buf[1] = wstr[1]; |
| 3130 | wstr += 2; |
| 3131 | } |
| 3132 | else { |
| 3133 | buf[0] = *wstr; |
| 3134 | buf[1] = 0; |
| 3135 | wstr++; |
| 3136 | } |
| 3137 | #else |
| 3138 | buf[0] = *wstr; |
| 3139 | wstr++; |
| 3140 | #endif |
| 3141 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3142 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3143 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3147 | return 0; |
| 3148 | } |
| 3149 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3150 | static int |
| 3151 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3152 | { |
| 3153 | if (errors == NULL) { |
| 3154 | *surrogateescape = 0; |
| 3155 | return 0; |
| 3156 | } |
| 3157 | |
| 3158 | if (strcmp(errors, "strict") == 0) { |
| 3159 | *surrogateescape = 0; |
| 3160 | return 0; |
| 3161 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3162 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3163 | *surrogateescape = 1; |
| 3164 | return 0; |
| 3165 | } |
| 3166 | PyErr_Format(PyExc_ValueError, |
| 3167 | "only 'strict' and 'surrogateescape' error handlers " |
| 3168 | "are supported, not '%s'", |
| 3169 | errors); |
| 3170 | return -1; |
| 3171 | } |
| 3172 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3173 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3174 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3175 | { |
| 3176 | Py_ssize_t wlen, wlen2; |
| 3177 | wchar_t *wstr; |
| 3178 | PyObject *bytes = NULL; |
| 3179 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3180 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3181 | PyObject *exc; |
| 3182 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3183 | int surrogateescape; |
| 3184 | |
| 3185 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3186 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3187 | |
| 3188 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3189 | if (wstr == NULL) |
| 3190 | return NULL; |
| 3191 | |
| 3192 | wlen2 = wcslen(wstr); |
| 3193 | if (wlen2 != wlen) { |
| 3194 | PyMem_Free(wstr); |
| 3195 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3196 | return NULL; |
| 3197 | } |
| 3198 | |
| 3199 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3200 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3201 | char *str; |
| 3202 | |
| 3203 | str = _Py_wchar2char(wstr, &error_pos); |
| 3204 | if (str == NULL) { |
| 3205 | if (error_pos == (size_t)-1) { |
| 3206 | PyErr_NoMemory(); |
| 3207 | PyMem_Free(wstr); |
| 3208 | return NULL; |
| 3209 | } |
| 3210 | else { |
| 3211 | goto encode_error; |
| 3212 | } |
| 3213 | } |
| 3214 | PyMem_Free(wstr); |
| 3215 | |
| 3216 | bytes = PyBytes_FromString(str); |
| 3217 | PyMem_Free(str); |
| 3218 | } |
| 3219 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3220 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3221 | size_t len, len2; |
| 3222 | |
| 3223 | len = wcstombs(NULL, wstr, 0); |
| 3224 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3225 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3226 | goto encode_error; |
| 3227 | } |
| 3228 | |
| 3229 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3230 | if (bytes == NULL) { |
| 3231 | PyMem_Free(wstr); |
| 3232 | return NULL; |
| 3233 | } |
| 3234 | |
| 3235 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3236 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3237 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3238 | goto encode_error; |
| 3239 | } |
| 3240 | PyMem_Free(wstr); |
| 3241 | } |
| 3242 | return bytes; |
| 3243 | |
| 3244 | encode_error: |
| 3245 | errmsg = strerror(errno); |
| 3246 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3247 | |
| 3248 | if (error_pos == (size_t)-1) |
| 3249 | error_pos = wcstombs_errorpos(wstr); |
| 3250 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3251 | PyMem_Free(wstr); |
| 3252 | Py_XDECREF(bytes); |
| 3253 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3254 | if (errmsg != NULL) { |
| 3255 | size_t errlen; |
| 3256 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3257 | if (wstr != NULL) { |
| 3258 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3259 | PyMem_Free(wstr); |
| 3260 | } else |
| 3261 | errmsg = NULL; |
| 3262 | } |
| 3263 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3264 | reason = PyUnicode_FromString( |
| 3265 | "wcstombs() encountered an unencodable " |
| 3266 | "wide character"); |
| 3267 | if (reason == NULL) |
| 3268 | return NULL; |
| 3269 | |
| 3270 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3271 | "locale", unicode, |
| 3272 | (Py_ssize_t)error_pos, |
| 3273 | (Py_ssize_t)(error_pos+1), |
| 3274 | reason); |
| 3275 | Py_DECREF(reason); |
| 3276 | if (exc != NULL) { |
| 3277 | PyCodec_StrictErrors(exc); |
| 3278 | Py_XDECREF(exc); |
| 3279 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3280 | return NULL; |
| 3281 | } |
| 3282 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3283 | PyObject * |
| 3284 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3285 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3286 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3287 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3288 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3289 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3290 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3291 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3292 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3293 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3294 | the Python codec requires to encode at least its own filename. Use the C |
| 3295 | version of the locale codec until the codec registry is initialized and |
| 3296 | the Python codec is loaded. |
| 3297 | |
| 3298 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3299 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3300 | subinterpreters. */ |
| 3301 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3302 | return PyUnicode_AsEncodedString(unicode, |
| 3303 | Py_FileSystemDefaultEncoding, |
| 3304 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3305 | } |
| 3306 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3307 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3308 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3309 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3312 | PyObject * |
| 3313 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3314 | const char *encoding, |
| 3315 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3316 | { |
| 3317 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3318 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3319 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3320 | if (!PyUnicode_Check(unicode)) { |
| 3321 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3322 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3323 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3324 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3325 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3326 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3327 | if ((strcmp(lower, "utf-8") == 0) || |
| 3328 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3329 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3330 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3331 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3332 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3333 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3334 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3335 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3336 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3337 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3338 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3339 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3340 | else if (strcmp(lower, "mbcs") == 0) |
| 3341 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3342 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3343 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3344 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3345 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3346 | |
| 3347 | /* Encode via the codec registry */ |
| 3348 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3349 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3350 | return NULL; |
| 3351 | |
| 3352 | /* The normal path */ |
| 3353 | if (PyBytes_Check(v)) |
| 3354 | return v; |
| 3355 | |
| 3356 | /* 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] | 3357 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3358 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3359 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3360 | |
| 3361 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3362 | "encoder %s returned bytearray instead of bytes", |
| 3363 | encoding); |
| 3364 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3365 | Py_DECREF(v); |
| 3366 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3367 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3368 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3369 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3370 | Py_DECREF(v); |
| 3371 | return b; |
| 3372 | } |
| 3373 | |
| 3374 | PyErr_Format(PyExc_TypeError, |
| 3375 | "encoder did not return a bytes object (type=%.400s)", |
| 3376 | Py_TYPE(v)->tp_name); |
| 3377 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3378 | return NULL; |
| 3379 | } |
| 3380 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3381 | PyObject * |
| 3382 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3383 | const char *encoding, |
| 3384 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3385 | { |
| 3386 | PyObject *v; |
| 3387 | |
| 3388 | if (!PyUnicode_Check(unicode)) { |
| 3389 | PyErr_BadArgument(); |
| 3390 | goto onError; |
| 3391 | } |
| 3392 | |
| 3393 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3394 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3395 | |
| 3396 | /* Encode via the codec registry */ |
| 3397 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3398 | if (v == NULL) |
| 3399 | goto onError; |
| 3400 | if (!PyUnicode_Check(v)) { |
| 3401 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3402 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3403 | Py_TYPE(v)->tp_name); |
| 3404 | Py_DECREF(v); |
| 3405 | goto onError; |
| 3406 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3407 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3408 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3409 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3410 | return NULL; |
| 3411 | } |
| 3412 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3413 | static size_t |
| 3414 | mbstowcs_errorpos(const char *str, size_t len) |
| 3415 | { |
| 3416 | #ifdef HAVE_MBRTOWC |
| 3417 | const char *start = str; |
| 3418 | mbstate_t mbs; |
| 3419 | size_t converted; |
| 3420 | wchar_t ch; |
| 3421 | |
| 3422 | memset(&mbs, 0, sizeof mbs); |
| 3423 | while (len) |
| 3424 | { |
| 3425 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3426 | if (converted == 0) |
| 3427 | /* Reached end of string */ |
| 3428 | break; |
| 3429 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3430 | /* Conversion error or incomplete character */ |
| 3431 | return str - start; |
| 3432 | } |
| 3433 | else { |
| 3434 | str += converted; |
| 3435 | len -= converted; |
| 3436 | } |
| 3437 | } |
| 3438 | /* failed to find the undecodable byte sequence */ |
| 3439 | return 0; |
| 3440 | #endif |
| 3441 | return 0; |
| 3442 | } |
| 3443 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3444 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3445 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3446 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3447 | { |
| 3448 | wchar_t smallbuf[256]; |
| 3449 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3450 | wchar_t *wstr; |
| 3451 | size_t wlen, wlen2; |
| 3452 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3453 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3454 | size_t error_pos; |
| 3455 | char *errmsg; |
| 3456 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3457 | |
| 3458 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3459 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3460 | |
| 3461 | if (str[len] != '\0' || len != strlen(str)) { |
| 3462 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3463 | return NULL; |
| 3464 | } |
| 3465 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3466 | if (surrogateescape) { |
| 3467 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3468 | wstr = _Py_char2wchar(str, &wlen); |
| 3469 | if (wstr == NULL) { |
| 3470 | if (wlen == (size_t)-1) |
| 3471 | PyErr_NoMemory(); |
| 3472 | else |
| 3473 | PyErr_SetFromErrno(PyExc_OSError); |
| 3474 | return NULL; |
| 3475 | } |
| 3476 | |
| 3477 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3478 | PyMem_Free(wstr); |
| 3479 | } |
| 3480 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3481 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3482 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3483 | wlen = mbstowcs(NULL, str, 0); |
| 3484 | #else |
| 3485 | wlen = len; |
| 3486 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3487 | if (wlen == (size_t)-1) |
| 3488 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3489 | if (wlen+1 <= smallbuf_len) { |
| 3490 | wstr = smallbuf; |
| 3491 | } |
| 3492 | else { |
| 3493 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3494 | return PyErr_NoMemory(); |
| 3495 | |
| 3496 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3497 | if (!wstr) |
| 3498 | return PyErr_NoMemory(); |
| 3499 | } |
| 3500 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3501 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3502 | if (wlen2 == (size_t)-1) { |
| 3503 | if (wstr != smallbuf) |
| 3504 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3505 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3506 | } |
| 3507 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3508 | assert(wlen2 == wlen); |
| 3509 | #endif |
| 3510 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3511 | if (wstr != smallbuf) |
| 3512 | PyMem_Free(wstr); |
| 3513 | } |
| 3514 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3515 | |
| 3516 | decode_error: |
| 3517 | errmsg = strerror(errno); |
| 3518 | assert(errmsg != NULL); |
| 3519 | |
| 3520 | error_pos = mbstowcs_errorpos(str, len); |
| 3521 | if (errmsg != NULL) { |
| 3522 | size_t errlen; |
| 3523 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3524 | if (wstr != NULL) { |
| 3525 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3526 | PyMem_Free(wstr); |
| 3527 | } else |
| 3528 | errmsg = NULL; |
| 3529 | } |
| 3530 | if (errmsg == NULL) |
| 3531 | reason = PyUnicode_FromString( |
| 3532 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3533 | if (reason == NULL) |
| 3534 | return NULL; |
| 3535 | |
| 3536 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3537 | "locale", str, len, |
| 3538 | (Py_ssize_t)error_pos, |
| 3539 | (Py_ssize_t)(error_pos+1), |
| 3540 | reason); |
| 3541 | Py_DECREF(reason); |
| 3542 | if (exc != NULL) { |
| 3543 | PyCodec_StrictErrors(exc); |
| 3544 | Py_XDECREF(exc); |
| 3545 | } |
| 3546 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3550 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3551 | { |
| 3552 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3553 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | |
| 3557 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3558 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3559 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3560 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3561 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3562 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3563 | PyObject* |
| 3564 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3565 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3566 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3567 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3568 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3569 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3570 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3571 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3572 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3573 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3574 | the Python codec requires to encode at least its own filename. Use the C |
| 3575 | version of the locale codec until the codec registry is initialized and |
| 3576 | the Python codec is loaded. |
| 3577 | |
| 3578 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3579 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3580 | subinterpreters. */ |
| 3581 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3582 | return PyUnicode_Decode(s, size, |
| 3583 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3584 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3585 | } |
| 3586 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3587 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3588 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3589 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3592 | |
| 3593 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3594 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3595 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3596 | Py_ssize_t pos; |
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 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3599 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3600 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3601 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3602 | if (pos == -1) |
| 3603 | return 0; |
| 3604 | else |
| 3605 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3606 | } |
| 3607 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3608 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3609 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3610 | { |
| 3611 | PyObject *output = NULL; |
| 3612 | Py_ssize_t size; |
| 3613 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3614 | if (arg == NULL) { |
| 3615 | Py_DECREF(*(PyObject**)addr); |
| 3616 | return 1; |
| 3617 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3618 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3619 | output = arg; |
| 3620 | Py_INCREF(output); |
| 3621 | } |
| 3622 | else { |
| 3623 | arg = PyUnicode_FromObject(arg); |
| 3624 | if (!arg) |
| 3625 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3626 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3627 | Py_DECREF(arg); |
| 3628 | if (!output) |
| 3629 | return 0; |
| 3630 | if (!PyBytes_Check(output)) { |
| 3631 | Py_DECREF(output); |
| 3632 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3633 | return 0; |
| 3634 | } |
| 3635 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3636 | size = PyBytes_GET_SIZE(output); |
| 3637 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3638 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3639 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3640 | Py_DECREF(output); |
| 3641 | return 0; |
| 3642 | } |
| 3643 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3644 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3648 | int |
| 3649 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3650 | { |
| 3651 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3652 | if (arg == NULL) { |
| 3653 | Py_DECREF(*(PyObject**)addr); |
| 3654 | return 1; |
| 3655 | } |
| 3656 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3657 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3658 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3659 | output = arg; |
| 3660 | Py_INCREF(output); |
| 3661 | } |
| 3662 | else { |
| 3663 | arg = PyBytes_FromObject(arg); |
| 3664 | if (!arg) |
| 3665 | return 0; |
| 3666 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3667 | PyBytes_GET_SIZE(arg)); |
| 3668 | Py_DECREF(arg); |
| 3669 | if (!output) |
| 3670 | return 0; |
| 3671 | if (!PyUnicode_Check(output)) { |
| 3672 | Py_DECREF(output); |
| 3673 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3674 | return 0; |
| 3675 | } |
| 3676 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3677 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3678 | Py_DECREF(output); |
| 3679 | return 0; |
| 3680 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3681 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3682 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3683 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3684 | Py_DECREF(output); |
| 3685 | return 0; |
| 3686 | } |
| 3687 | *(PyObject**)addr = output; |
| 3688 | return Py_CLEANUP_SUPPORTED; |
| 3689 | } |
| 3690 | |
| 3691 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3692 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3693 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3694 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3695 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3696 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3697 | if (!PyUnicode_Check(unicode)) { |
| 3698 | PyErr_BadArgument(); |
| 3699 | return NULL; |
| 3700 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3701 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3702 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3703 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3704 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3705 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3706 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3707 | if (bytes == NULL) |
| 3708 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3709 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3710 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3711 | Py_DECREF(bytes); |
| 3712 | return NULL; |
| 3713 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3714 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3715 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3716 | PyBytes_AS_STRING(bytes), |
| 3717 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3718 | Py_DECREF(bytes); |
| 3719 | } |
| 3720 | |
| 3721 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3722 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3723 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3727 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3728 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3729 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3730 | } |
| 3731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3732 | Py_UNICODE * |
| 3733 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3734 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3735 | const unsigned char *one_byte; |
| 3736 | #if SIZEOF_WCHAR_T == 4 |
| 3737 | const Py_UCS2 *two_bytes; |
| 3738 | #else |
| 3739 | const Py_UCS4 *four_bytes; |
| 3740 | const Py_UCS4 *ucs4_end; |
| 3741 | Py_ssize_t num_surrogates; |
| 3742 | #endif |
| 3743 | wchar_t *w; |
| 3744 | wchar_t *wchar_end; |
| 3745 | |
| 3746 | if (!PyUnicode_Check(unicode)) { |
| 3747 | PyErr_BadArgument(); |
| 3748 | return NULL; |
| 3749 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3750 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3751 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3752 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3753 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3754 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3755 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3756 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3757 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3758 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3759 | num_surrogates = 0; |
| 3760 | |
| 3761 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3762 | if (*four_bytes > 0xFFFF) |
| 3763 | ++num_surrogates; |
| 3764 | } |
| 3765 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3766 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3767 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3768 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3769 | PyErr_NoMemory(); |
| 3770 | return NULL; |
| 3771 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3772 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3773 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3774 | w = _PyUnicode_WSTR(unicode); |
| 3775 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3776 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3777 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3778 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3779 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3780 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3781 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3782 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3783 | } |
| 3784 | else |
| 3785 | *w = *four_bytes; |
| 3786 | |
| 3787 | if (w > wchar_end) { |
| 3788 | assert(0 && "Miscalculated string end"); |
| 3789 | } |
| 3790 | } |
| 3791 | *w = 0; |
| 3792 | #else |
| 3793 | /* sizeof(wchar_t) == 4 */ |
| 3794 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3795 | "should share memory already."); |
| 3796 | return NULL; |
| 3797 | #endif |
| 3798 | } |
| 3799 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3800 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3801 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3802 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3803 | PyErr_NoMemory(); |
| 3804 | return NULL; |
| 3805 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3806 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3807 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3808 | w = _PyUnicode_WSTR(unicode); |
| 3809 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3810 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3811 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3812 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3813 | for (; w < wchar_end; ++one_byte, ++w) |
| 3814 | *w = *one_byte; |
| 3815 | /* null-terminate the wstr */ |
| 3816 | *w = 0; |
| 3817 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3818 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3819 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3820 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3821 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3822 | *w = *two_bytes; |
| 3823 | /* null-terminate the wstr */ |
| 3824 | *w = 0; |
| 3825 | #else |
| 3826 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3827 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3828 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3829 | Py_FatalError("Impossible unicode object state, wstr " |
| 3830 | "and str should share memory already."); |
| 3831 | return NULL; |
| 3832 | #endif |
| 3833 | } |
| 3834 | else { |
| 3835 | assert(0 && "This should never happen."); |
| 3836 | } |
| 3837 | } |
| 3838 | } |
| 3839 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3840 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3841 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3844 | Py_UNICODE * |
| 3845 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3846 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3847 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3850 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3851 | Py_ssize_t |
| 3852 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3853 | { |
| 3854 | if (!PyUnicode_Check(unicode)) { |
| 3855 | PyErr_BadArgument(); |
| 3856 | goto onError; |
| 3857 | } |
| 3858 | return PyUnicode_GET_SIZE(unicode); |
| 3859 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3860 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3861 | return -1; |
| 3862 | } |
| 3863 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3864 | Py_ssize_t |
| 3865 | PyUnicode_GetLength(PyObject *unicode) |
| 3866 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3867 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3868 | PyErr_BadArgument(); |
| 3869 | return -1; |
| 3870 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3871 | if (PyUnicode_READY(unicode) == -1) |
| 3872 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3873 | return PyUnicode_GET_LENGTH(unicode); |
| 3874 | } |
| 3875 | |
| 3876 | Py_UCS4 |
| 3877 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3878 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3879 | void *data; |
| 3880 | int kind; |
| 3881 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3882 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3883 | PyErr_BadArgument(); |
| 3884 | return (Py_UCS4)-1; |
| 3885 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3886 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3887 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3888 | return (Py_UCS4)-1; |
| 3889 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3890 | data = PyUnicode_DATA(unicode); |
| 3891 | kind = PyUnicode_KIND(unicode); |
| 3892 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | int |
| 3896 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3897 | { |
| 3898 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3899 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3900 | return -1; |
| 3901 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3902 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3903 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3904 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3905 | return -1; |
| 3906 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3907 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3908 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3909 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3910 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3911 | return -1; |
| 3912 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3913 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3914 | index, ch); |
| 3915 | return 0; |
| 3916 | } |
| 3917 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3918 | const char * |
| 3919 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3920 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3921 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3924 | /* create or adjust a UnicodeDecodeError */ |
| 3925 | static void |
| 3926 | make_decode_exception(PyObject **exceptionObject, |
| 3927 | const char *encoding, |
| 3928 | const char *input, Py_ssize_t length, |
| 3929 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3930 | const char *reason) |
| 3931 | { |
| 3932 | if (*exceptionObject == NULL) { |
| 3933 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3934 | encoding, input, length, startpos, endpos, reason); |
| 3935 | } |
| 3936 | else { |
| 3937 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3938 | goto onError; |
| 3939 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3940 | goto onError; |
| 3941 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3942 | goto onError; |
| 3943 | } |
| 3944 | return; |
| 3945 | |
| 3946 | onError: |
| 3947 | Py_DECREF(*exceptionObject); |
| 3948 | *exceptionObject = NULL; |
| 3949 | } |
| 3950 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3951 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3952 | /* error handling callback helper: |
| 3953 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3954 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3955 | and adjust various state variables. |
| 3956 | return 0 on success, -1 on error |
| 3957 | */ |
| 3958 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3959 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3960 | unicode_decode_call_errorhandler_wchar( |
| 3961 | const char *errors, PyObject **errorHandler, |
| 3962 | const char *encoding, const char *reason, |
| 3963 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3964 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3965 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3966 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3967 | 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] | 3968 | |
| 3969 | PyObject *restuple = NULL; |
| 3970 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3971 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3972 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3973 | Py_ssize_t requiredsize; |
| 3974 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3975 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3976 | wchar_t *repwstr; |
| 3977 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3978 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3979 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 3980 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3981 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3982 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3983 | *errorHandler = PyCodec_LookupError(errors); |
| 3984 | if (*errorHandler == NULL) |
| 3985 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3988 | make_decode_exception(exceptionObject, |
| 3989 | encoding, |
| 3990 | *input, *inend - *input, |
| 3991 | *startinpos, *endinpos, |
| 3992 | reason); |
| 3993 | if (*exceptionObject == NULL) |
| 3994 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3995 | |
| 3996 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3997 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3998 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3999 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4000 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4001 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4002 | } |
| 4003 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4004 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4005 | |
| 4006 | /* Copy back the bytes variables, which might have been modified by the |
| 4007 | callback */ |
| 4008 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4009 | if (!inputobj) |
| 4010 | goto onError; |
| 4011 | if (!PyBytes_Check(inputobj)) { |
| 4012 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4013 | } |
| 4014 | *input = PyBytes_AS_STRING(inputobj); |
| 4015 | insize = PyBytes_GET_SIZE(inputobj); |
| 4016 | *inend = *input + insize; |
| 4017 | /* we can DECREF safely, as the exception has another reference, |
| 4018 | so the object won't go away. */ |
| 4019 | Py_DECREF(inputobj); |
| 4020 | |
| 4021 | if (newpos<0) |
| 4022 | newpos = insize+newpos; |
| 4023 | if (newpos<0 || newpos>insize) { |
| 4024 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4025 | goto onError; |
| 4026 | } |
| 4027 | |
| 4028 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4029 | if (repwstr == NULL) |
| 4030 | goto onError; |
| 4031 | /* need more space? (at least enough for what we |
| 4032 | have+the replacement+the rest of the string (starting |
| 4033 | at the new input position), so we won't have to check space |
| 4034 | when there are no errors in the rest of the string) */ |
| 4035 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4036 | if (requiredsize > outsize) { |
| 4037 | if (requiredsize < 2*outsize) |
| 4038 | requiredsize = 2*outsize; |
| 4039 | if (unicode_resize(output, requiredsize) < 0) |
| 4040 | goto onError; |
| 4041 | } |
| 4042 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4043 | *outpos += repwlen; |
| 4044 | |
| 4045 | *endinpos = newpos; |
| 4046 | *inptr = *input + newpos; |
| 4047 | |
| 4048 | /* we made it! */ |
| 4049 | Py_XDECREF(restuple); |
| 4050 | return 0; |
| 4051 | |
| 4052 | onError: |
| 4053 | Py_XDECREF(restuple); |
| 4054 | return -1; |
| 4055 | } |
| 4056 | #endif /* HAVE_MBCS */ |
| 4057 | |
| 4058 | static int |
| 4059 | unicode_decode_call_errorhandler_writer( |
| 4060 | const char *errors, PyObject **errorHandler, |
| 4061 | const char *encoding, const char *reason, |
| 4062 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4063 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4064 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4065 | { |
| 4066 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4067 | |
| 4068 | PyObject *restuple = NULL; |
| 4069 | PyObject *repunicode = NULL; |
| 4070 | Py_ssize_t insize; |
| 4071 | Py_ssize_t newpos; |
| 4072 | PyObject *inputobj = NULL; |
| 4073 | |
| 4074 | if (*errorHandler == NULL) { |
| 4075 | *errorHandler = PyCodec_LookupError(errors); |
| 4076 | if (*errorHandler == NULL) |
| 4077 | goto onError; |
| 4078 | } |
| 4079 | |
| 4080 | make_decode_exception(exceptionObject, |
| 4081 | encoding, |
| 4082 | *input, *inend - *input, |
| 4083 | *startinpos, *endinpos, |
| 4084 | reason); |
| 4085 | if (*exceptionObject == NULL) |
| 4086 | goto onError; |
| 4087 | |
| 4088 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4089 | if (restuple == NULL) |
| 4090 | goto onError; |
| 4091 | if (!PyTuple_Check(restuple)) { |
| 4092 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4093 | goto onError; |
| 4094 | } |
| 4095 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4096 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4097 | |
| 4098 | /* Copy back the bytes variables, which might have been modified by the |
| 4099 | callback */ |
| 4100 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4101 | if (!inputobj) |
| 4102 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4103 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4104 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4105 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4106 | *input = PyBytes_AS_STRING(inputobj); |
| 4107 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4108 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4109 | /* we can DECREF safely, as the exception has another reference, |
| 4110 | so the object won't go away. */ |
| 4111 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4112 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4113 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4114 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4115 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4116 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4117 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4118 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4119 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4120 | writer->overallocate = 1; |
| 4121 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
| 4122 | return |
| 4123 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4124 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4125 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4126 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4127 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4128 | Py_XDECREF(restuple); |
| 4129 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4130 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4131 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4132 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4133 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4136 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4137 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4138 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4139 | |
| 4140 | /* Three simple macros defining base-64. */ |
| 4141 | |
| 4142 | /* Is c a base-64 character? */ |
| 4143 | |
| 4144 | #define IS_BASE64(c) \ |
| 4145 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4146 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4147 | ((c) >= '0' && (c) <= '9') || \ |
| 4148 | (c) == '+' || (c) == '/') |
| 4149 | |
| 4150 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4151 | |
| 4152 | #define FROM_BASE64(c) \ |
| 4153 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4154 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4155 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4156 | (c) == '+' ? 62 : 63) |
| 4157 | |
| 4158 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4159 | |
| 4160 | #define TO_BASE64(n) \ |
| 4161 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4162 | |
| 4163 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4164 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4165 | * byte not decoding to itself is the + which begins a base64 |
| 4166 | * string. */ |
| 4167 | |
| 4168 | #define DECODE_DIRECT(c) \ |
| 4169 | ((c) <= 127 && (c) != '+') |
| 4170 | |
| 4171 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4172 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4173 | * the above). See RFC2152. This array identifies these different |
| 4174 | * sets: |
| 4175 | * 0 : "Set D" |
| 4176 | * alphanumeric and '(),-./:? |
| 4177 | * 1 : "Set O" |
| 4178 | * !"#$%&*;<=>@[]^_`{|} |
| 4179 | * 2 : "whitespace" |
| 4180 | * ht nl cr sp |
| 4181 | * 3 : special (must be base64 encoded) |
| 4182 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4183 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4184 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4185 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4186 | char utf7_category[128] = { |
| 4187 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4188 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4189 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4190 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4191 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4192 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4193 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4194 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4195 | /* @ A B C D E F G H I J K L M N O */ |
| 4196 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4197 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4198 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4199 | /* ` a b c d e f g h i j k l m n o */ |
| 4200 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4201 | /* p q r s t u v w x y z { | } ~ del */ |
| 4202 | 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] | 4203 | }; |
| 4204 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4205 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4206 | * answer depends on whether we are encoding set O as itself, and also |
| 4207 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4208 | * clear that the answers to these questions vary between |
| 4209 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4210 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4211 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4212 | ((c) < 128 && (c) > 0 && \ |
| 4213 | ((utf7_category[(c)] == 0) || \ |
| 4214 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4215 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4216 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4217 | PyObject * |
| 4218 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4219 | Py_ssize_t size, |
| 4220 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4221 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4222 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4223 | } |
| 4224 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4225 | /* The decoder. The only state we preserve is our read position, |
| 4226 | * i.e. how many characters we have consumed. So if we end in the |
| 4227 | * middle of a shift sequence we have to back off the read position |
| 4228 | * and the output to the beginning of the sequence, otherwise we lose |
| 4229 | * all the shift state (seen bits, number of bits seen, high |
| 4230 | * surrogate). */ |
| 4231 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4232 | PyObject * |
| 4233 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4234 | Py_ssize_t size, |
| 4235 | const char *errors, |
| 4236 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4237 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4238 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4239 | Py_ssize_t startinpos; |
| 4240 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4241 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4242 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4243 | const char *errmsg = ""; |
| 4244 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4245 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4246 | unsigned int base64bits = 0; |
| 4247 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4248 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4249 | PyObject *errorHandler = NULL; |
| 4250 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4251 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4252 | if (size == 0) { |
| 4253 | if (consumed) |
| 4254 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4255 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4256 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4257 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4258 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4259 | _PyUnicodeWriter_Init(&writer, 0); |
| 4260 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4261 | goto onError; |
| 4262 | |
| 4263 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4264 | e = s + size; |
| 4265 | |
| 4266 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4267 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4268 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4269 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4270 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4271 | if (inShift) { /* in a base-64 section */ |
| 4272 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4273 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4274 | base64bits += 6; |
| 4275 | s++; |
| 4276 | if (base64bits >= 16) { |
| 4277 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4278 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4279 | base64bits -= 16; |
| 4280 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4281 | if (surrogate) { |
| 4282 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4283 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4284 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4285 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4286 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4287 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4288 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4289 | } |
| 4290 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4291 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4292 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4293 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4296 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4297 | /* first surrogate */ |
| 4298 | surrogate = outCh; |
| 4299 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4300 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4301 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4302 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4303 | } |
| 4304 | } |
| 4305 | } |
| 4306 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4307 | inShift = 0; |
| 4308 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4309 | if (surrogate) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4310 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4311 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4312 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4313 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4314 | if (base64bits > 0) { /* left-over bits */ |
| 4315 | if (base64bits >= 6) { |
| 4316 | /* We've seen at least one base-64 character */ |
| 4317 | errmsg = "partial character in shift sequence"; |
| 4318 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4319 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4320 | else { |
| 4321 | /* Some bits remain; they should be zero */ |
| 4322 | if (base64buffer != 0) { |
| 4323 | errmsg = "non-zero padding bits in shift sequence"; |
| 4324 | goto utf7Error; |
| 4325 | } |
| 4326 | } |
| 4327 | } |
| 4328 | if (ch != '-') { |
| 4329 | /* '-' is absorbed; other terminating |
| 4330 | characters are preserved */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4331 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4332 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4333 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4334 | } |
| 4335 | } |
| 4336 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4337 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4338 | s++; /* consume '+' */ |
| 4339 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4340 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4341 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4342 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4343 | } |
| 4344 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4345 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4346 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4347 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4348 | } |
| 4349 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4350 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4351 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4352 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4353 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4354 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4355 | else { |
| 4356 | startinpos = s-starts; |
| 4357 | s++; |
| 4358 | errmsg = "unexpected special character"; |
| 4359 | goto utf7Error; |
| 4360 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4361 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4362 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4364 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4365 | errors, &errorHandler, |
| 4366 | "utf7", errmsg, |
| 4367 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4368 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4369 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4372 | /* end of string */ |
| 4373 | |
| 4374 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4375 | /* if we're in an inconsistent state, that's an error */ |
| 4376 | if (surrogate || |
| 4377 | (base64bits >= 6) || |
| 4378 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4379 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4380 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4381 | errors, &errorHandler, |
| 4382 | "utf7", "unterminated shift sequence", |
| 4383 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4384 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4385 | goto onError; |
| 4386 | if (s < e) |
| 4387 | goto restart; |
| 4388 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4389 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4390 | |
| 4391 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4392 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4393 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4394 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4395 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4396 | } |
| 4397 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4398 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4399 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4400 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4401 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4402 | Py_XDECREF(errorHandler); |
| 4403 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4404 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4406 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4407 | Py_XDECREF(errorHandler); |
| 4408 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4409 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4410 | return NULL; |
| 4411 | } |
| 4412 | |
| 4413 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4414 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4415 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4416 | int base64SetO, |
| 4417 | int base64WhiteSpace, |
| 4418 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4419 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4420 | int kind; |
| 4421 | void *data; |
| 4422 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4423 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4424 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4425 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4426 | unsigned int base64bits = 0; |
| 4427 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4428 | char * out; |
| 4429 | char * start; |
| 4430 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4431 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4432 | return NULL; |
| 4433 | kind = PyUnicode_KIND(str); |
| 4434 | data = PyUnicode_DATA(str); |
| 4435 | len = PyUnicode_GET_LENGTH(str); |
| 4436 | |
| 4437 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4438 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4439 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4440 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4441 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4442 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4443 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4444 | if (v == NULL) |
| 4445 | return NULL; |
| 4446 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4447 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4448 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4449 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4450 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4451 | if (inShift) { |
| 4452 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4453 | /* shifting out */ |
| 4454 | if (base64bits) { /* output remaining bits */ |
| 4455 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4456 | base64buffer = 0; |
| 4457 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4458 | } |
| 4459 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4460 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4461 | so no '-' is required, except if the character is itself a '-' */ |
| 4462 | if (IS_BASE64(ch) || ch == '-') { |
| 4463 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4464 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4465 | *out++ = (char) ch; |
| 4466 | } |
| 4467 | else { |
| 4468 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4469 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4470 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4471 | else { /* not in a shift sequence */ |
| 4472 | if (ch == '+') { |
| 4473 | *out++ = '+'; |
| 4474 | *out++ = '-'; |
| 4475 | } |
| 4476 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4477 | *out++ = (char) ch; |
| 4478 | } |
| 4479 | else { |
| 4480 | *out++ = '+'; |
| 4481 | inShift = 1; |
| 4482 | goto encode_char; |
| 4483 | } |
| 4484 | } |
| 4485 | continue; |
| 4486 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4487 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4488 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4489 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4490 | /* code first surrogate */ |
| 4491 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4492 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4493 | while (base64bits >= 6) { |
| 4494 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4495 | base64bits -= 6; |
| 4496 | } |
| 4497 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4498 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4499 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4500 | base64bits += 16; |
| 4501 | base64buffer = (base64buffer << 16) | ch; |
| 4502 | while (base64bits >= 6) { |
| 4503 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4504 | base64bits -= 6; |
| 4505 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4506 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4507 | if (base64bits) |
| 4508 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4509 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4510 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4511 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4512 | return NULL; |
| 4513 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4514 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4515 | PyObject * |
| 4516 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4517 | Py_ssize_t size, |
| 4518 | int base64SetO, |
| 4519 | int base64WhiteSpace, |
| 4520 | const char *errors) |
| 4521 | { |
| 4522 | PyObject *result; |
| 4523 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4524 | if (tmp == NULL) |
| 4525 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4526 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4527 | base64WhiteSpace, errors); |
| 4528 | Py_DECREF(tmp); |
| 4529 | return result; |
| 4530 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4531 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4532 | #undef IS_BASE64 |
| 4533 | #undef FROM_BASE64 |
| 4534 | #undef TO_BASE64 |
| 4535 | #undef DECODE_DIRECT |
| 4536 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4537 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4538 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4539 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4540 | PyObject * |
| 4541 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4542 | Py_ssize_t size, |
| 4543 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4544 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4545 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4546 | } |
| 4547 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4548 | #include "stringlib/asciilib.h" |
| 4549 | #include "stringlib/codecs.h" |
| 4550 | #include "stringlib/undef.h" |
| 4551 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4552 | #include "stringlib/ucs1lib.h" |
| 4553 | #include "stringlib/codecs.h" |
| 4554 | #include "stringlib/undef.h" |
| 4555 | |
| 4556 | #include "stringlib/ucs2lib.h" |
| 4557 | #include "stringlib/codecs.h" |
| 4558 | #include "stringlib/undef.h" |
| 4559 | |
| 4560 | #include "stringlib/ucs4lib.h" |
| 4561 | #include "stringlib/codecs.h" |
| 4562 | #include "stringlib/undef.h" |
| 4563 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4564 | /* Mask to quickly check whether a C 'long' contains a |
| 4565 | non-ASCII, UTF8-encoded char. */ |
| 4566 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4567 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4568 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4569 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4570 | #else |
| 4571 | # error C 'long' size should be either 4 or 8! |
| 4572 | #endif |
| 4573 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4574 | static Py_ssize_t |
| 4575 | 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] | 4576 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4577 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4578 | 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] | 4579 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4580 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4581 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4582 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4583 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4584 | an explanation. */ |
| 4585 | /* Help register allocation */ |
| 4586 | register const char *_p = p; |
| 4587 | register Py_UCS1 * q = dest; |
| 4588 | while (_p < aligned_end) { |
| 4589 | unsigned long value = *(const unsigned long *) _p; |
| 4590 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4591 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4592 | *((unsigned long *)q) = value; |
| 4593 | _p += SIZEOF_LONG; |
| 4594 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4595 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4596 | p = _p; |
| 4597 | while (p < end) { |
| 4598 | if ((unsigned char)*p & 0x80) |
| 4599 | break; |
| 4600 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4601 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4602 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4603 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4604 | #endif |
| 4605 | while (p < end) { |
| 4606 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4607 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4608 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4609 | /* Help register allocation */ |
| 4610 | register const char *_p = p; |
| 4611 | while (_p < aligned_end) { |
| 4612 | unsigned long value = *(unsigned long *) _p; |
| 4613 | if (value & ASCII_CHAR_MASK) |
| 4614 | break; |
| 4615 | _p += SIZEOF_LONG; |
| 4616 | } |
| 4617 | p = _p; |
| 4618 | if (_p == end) |
| 4619 | break; |
| 4620 | } |
| 4621 | if ((unsigned char)*p & 0x80) |
| 4622 | break; |
| 4623 | ++p; |
| 4624 | } |
| 4625 | memcpy(dest, start, p - start); |
| 4626 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4627 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4628 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4629 | PyObject * |
| 4630 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4631 | Py_ssize_t size, |
| 4632 | const char *errors, |
| 4633 | Py_ssize_t *consumed) |
| 4634 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4635 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4636 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4637 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4638 | |
| 4639 | Py_ssize_t startinpos; |
| 4640 | Py_ssize_t endinpos; |
| 4641 | const char *errmsg = ""; |
| 4642 | PyObject *errorHandler = NULL; |
| 4643 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4644 | |
| 4645 | if (size == 0) { |
| 4646 | if (consumed) |
| 4647 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4648 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4649 | } |
| 4650 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4651 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4652 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4653 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4654 | *consumed = 1; |
| 4655 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4656 | } |
| 4657 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4658 | _PyUnicodeWriter_Init(&writer, 0); |
| 4659 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4660 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4661 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4662 | writer.pos = ascii_decode(s, end, writer.data); |
| 4663 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4664 | while (s < end) { |
| 4665 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4666 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4667 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4668 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4669 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4670 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4671 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4672 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4673 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4674 | } else { |
| 4675 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4676 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | switch (ch) { |
| 4680 | case 0: |
| 4681 | if (s == end || consumed) |
| 4682 | goto End; |
| 4683 | errmsg = "unexpected end of data"; |
| 4684 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4685 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4686 | break; |
| 4687 | case 1: |
| 4688 | errmsg = "invalid start byte"; |
| 4689 | startinpos = s - starts; |
| 4690 | endinpos = startinpos + 1; |
| 4691 | break; |
| 4692 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4693 | case 3: |
| 4694 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4695 | errmsg = "invalid continuation byte"; |
| 4696 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4697 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4698 | break; |
| 4699 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4700 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4701 | goto onError; |
| 4702 | continue; |
| 4703 | } |
| 4704 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4705 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4706 | errors, &errorHandler, |
| 4707 | "utf-8", errmsg, |
| 4708 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4709 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4710 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4711 | } |
| 4712 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4713 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4714 | if (consumed) |
| 4715 | *consumed = s - starts; |
| 4716 | |
| 4717 | Py_XDECREF(errorHandler); |
| 4718 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4719 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4720 | |
| 4721 | onError: |
| 4722 | Py_XDECREF(errorHandler); |
| 4723 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4724 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4725 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4728 | #ifdef __APPLE__ |
| 4729 | |
| 4730 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4731 | used to decode the command line arguments on Mac OS X. |
| 4732 | |
| 4733 | Return a pointer to a newly allocated wide character string (use |
| 4734 | PyMem_Free() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4735 | |
| 4736 | wchar_t* |
| 4737 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4738 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4739 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4740 | wchar_t *unicode; |
| 4741 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4742 | |
| 4743 | /* Note: size will always be longer than the resulting Unicode |
| 4744 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4745 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4746 | return NULL; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4747 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4748 | if (!unicode) |
| 4749 | return NULL; |
| 4750 | |
| 4751 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4752 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4753 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4754 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4755 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4756 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4757 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4758 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4759 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4760 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4761 | if (ch > 0xFF) { |
| 4762 | #if SIZEOF_WCHAR_T == 4 |
| 4763 | assert(0); |
| 4764 | #else |
| 4765 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4766 | /* compute and append the two surrogates: */ |
| 4767 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4768 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4769 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4770 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4771 | else { |
| 4772 | if (!ch && s == e) |
| 4773 | break; |
| 4774 | /* surrogateescape */ |
| 4775 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4776 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4777 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4778 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4779 | return unicode; |
| 4780 | } |
| 4781 | |
| 4782 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4783 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4784 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4785 | |
| 4786 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4787 | and allocate exactly as much space needed at the end. Else allocate the |
| 4788 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4789 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4790 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4791 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4792 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4793 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4794 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4795 | void *data; |
| 4796 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4797 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4798 | if (!PyUnicode_Check(unicode)) { |
| 4799 | PyErr_BadArgument(); |
| 4800 | return NULL; |
| 4801 | } |
| 4802 | |
| 4803 | if (PyUnicode_READY(unicode) == -1) |
| 4804 | return NULL; |
| 4805 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4806 | if (PyUnicode_UTF8(unicode)) |
| 4807 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4808 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4809 | |
| 4810 | kind = PyUnicode_KIND(unicode); |
| 4811 | data = PyUnicode_DATA(unicode); |
| 4812 | size = PyUnicode_GET_LENGTH(unicode); |
| 4813 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4814 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4815 | default: |
| 4816 | assert(0); |
| 4817 | case PyUnicode_1BYTE_KIND: |
| 4818 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4819 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4820 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4821 | case PyUnicode_2BYTE_KIND: |
| 4822 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4823 | case PyUnicode_4BYTE_KIND: |
| 4824 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4825 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4828 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4829 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4830 | Py_ssize_t size, |
| 4831 | const char *errors) |
| 4832 | { |
| 4833 | PyObject *v, *unicode; |
| 4834 | |
| 4835 | unicode = PyUnicode_FromUnicode(s, size); |
| 4836 | if (unicode == NULL) |
| 4837 | return NULL; |
| 4838 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4839 | Py_DECREF(unicode); |
| 4840 | return v; |
| 4841 | } |
| 4842 | |
| 4843 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4844 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4845 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4846 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4849 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4850 | |
| 4851 | PyObject * |
| 4852 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4853 | Py_ssize_t size, |
| 4854 | const char *errors, |
| 4855 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4856 | { |
| 4857 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4858 | } |
| 4859 | |
| 4860 | PyObject * |
| 4861 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4862 | Py_ssize_t size, |
| 4863 | const char *errors, |
| 4864 | int *byteorder, |
| 4865 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4866 | { |
| 4867 | const char *starts = s; |
| 4868 | Py_ssize_t startinpos; |
| 4869 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4870 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4871 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4872 | int le, bo = 0; /* assume native ordering by default */ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4873 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4874 | PyObject *errorHandler = NULL; |
| 4875 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4876 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4877 | q = (unsigned char *)s; |
| 4878 | e = q + size; |
| 4879 | |
| 4880 | if (byteorder) |
| 4881 | bo = *byteorder; |
| 4882 | |
| 4883 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4884 | byte order setting accordingly. In native mode, the leading BOM |
| 4885 | mark is skipped, in all other modes, it is copied to the output |
| 4886 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4887 | if (bo == 0 && size >= 4) { |
| 4888 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4889 | if (bom == 0x0000FEFF) { |
| 4890 | bo = -1; |
| 4891 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4892 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4893 | else if (bom == 0xFFFE0000) { |
| 4894 | bo = 1; |
| 4895 | q += 4; |
| 4896 | } |
| 4897 | if (byteorder) |
| 4898 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4901 | if (q == e) { |
| 4902 | if (consumed) |
| 4903 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4904 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4907 | #ifdef WORDS_BIGENDIAN |
| 4908 | le = bo < 0; |
| 4909 | #else |
| 4910 | le = bo <= 0; |
| 4911 | #endif |
| 4912 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4913 | _PyUnicodeWriter_Init(&writer, 0); |
| 4914 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 3) / 4, 127) == -1) |
| 4915 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4916 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4917 | while (1) { |
| 4918 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4919 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4920 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4921 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4922 | enum PyUnicode_Kind kind = writer.kind; |
| 4923 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4924 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4925 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4926 | if (le) { |
| 4927 | do { |
| 4928 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4929 | if (ch > maxch) |
| 4930 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4931 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4932 | q += 4; |
| 4933 | } while (q <= last); |
| 4934 | } |
| 4935 | else { |
| 4936 | do { |
| 4937 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 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 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4944 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | if (ch <= maxch) { |
| 4948 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4949 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4950 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4951 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4952 | startinpos = ((const char *)q) - starts; |
| 4953 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4954 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4955 | else { |
| 4956 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4957 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4958 | goto onError; |
| 4959 | q += 4; |
| 4960 | continue; |
| 4961 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4962 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4963 | startinpos = ((const char *)q) - starts; |
| 4964 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4965 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4966 | |
| 4967 | /* The remaining input chars are ignored if the callback |
| 4968 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4969 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4970 | errors, &errorHandler, |
| 4971 | "utf32", errmsg, |
| 4972 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4973 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4974 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4977 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4978 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4979 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4980 | Py_XDECREF(errorHandler); |
| 4981 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4982 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4983 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4984 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4985 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4986 | Py_XDECREF(errorHandler); |
| 4987 | Py_XDECREF(exc); |
| 4988 | return NULL; |
| 4989 | } |
| 4990 | |
| 4991 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4992 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 4993 | const char *errors, |
| 4994 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4995 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4996 | int kind; |
| 4997 | void *data; |
| 4998 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4999 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5000 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5001 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5002 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5003 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5004 | int iorder[] = {0, 1, 2, 3}; |
| 5005 | #else |
| 5006 | int iorder[] = {3, 2, 1, 0}; |
| 5007 | #endif |
| 5008 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5009 | #define STORECHAR(CH) \ |
| 5010 | do { \ |
| 5011 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5012 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5013 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5014 | p[iorder[0]] = (CH) & 0xff; \ |
| 5015 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5016 | } while(0) |
| 5017 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5018 | if (!PyUnicode_Check(str)) { |
| 5019 | PyErr_BadArgument(); |
| 5020 | return NULL; |
| 5021 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5022 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5023 | return NULL; |
| 5024 | kind = PyUnicode_KIND(str); |
| 5025 | data = PyUnicode_DATA(str); |
| 5026 | len = PyUnicode_GET_LENGTH(str); |
| 5027 | |
| 5028 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5029 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5030 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5031 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5032 | if (v == NULL) |
| 5033 | return NULL; |
| 5034 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5035 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5036 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5037 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5038 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5039 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5040 | |
| 5041 | if (byteorder == -1) { |
| 5042 | /* force LE */ |
| 5043 | iorder[0] = 0; |
| 5044 | iorder[1] = 1; |
| 5045 | iorder[2] = 2; |
| 5046 | iorder[3] = 3; |
| 5047 | } |
| 5048 | else if (byteorder == 1) { |
| 5049 | /* force BE */ |
| 5050 | iorder[0] = 3; |
| 5051 | iorder[1] = 2; |
| 5052 | iorder[2] = 1; |
| 5053 | iorder[3] = 0; |
| 5054 | } |
| 5055 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5056 | for (i = 0; i < len; i++) |
| 5057 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5058 | |
| 5059 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5060 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5061 | #undef STORECHAR |
| 5062 | } |
| 5063 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5064 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5065 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5066 | Py_ssize_t size, |
| 5067 | const char *errors, |
| 5068 | int byteorder) |
| 5069 | { |
| 5070 | PyObject *result; |
| 5071 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5072 | if (tmp == NULL) |
| 5073 | return NULL; |
| 5074 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5075 | Py_DECREF(tmp); |
| 5076 | return result; |
| 5077 | } |
| 5078 | |
| 5079 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5080 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5081 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5082 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5085 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5086 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5087 | PyObject * |
| 5088 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5089 | Py_ssize_t size, |
| 5090 | const char *errors, |
| 5091 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5092 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5093 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5094 | } |
| 5095 | |
| 5096 | PyObject * |
| 5097 | PyUnicode_DecodeUTF16Stateful(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, |
| 5101 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5102 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5103 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5104 | Py_ssize_t startinpos; |
| 5105 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5106 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5107 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5108 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5109 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5110 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5111 | PyObject *errorHandler = NULL; |
| 5112 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5113 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5114 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5115 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5116 | |
| 5117 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5118 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5120 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5121 | byte order setting accordingly. In native mode, the leading BOM |
| 5122 | mark is skipped, in all other modes, it is copied to the output |
| 5123 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5124 | if (bo == 0 && size >= 2) { |
| 5125 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5126 | if (bom == 0xFEFF) { |
| 5127 | q += 2; |
| 5128 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5129 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5130 | else if (bom == 0xFFFE) { |
| 5131 | q += 2; |
| 5132 | bo = 1; |
| 5133 | } |
| 5134 | if (byteorder) |
| 5135 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5136 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5138 | if (q == e) { |
| 5139 | if (consumed) |
| 5140 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5141 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5142 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5143 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5144 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5145 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5146 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5147 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5148 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5149 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5150 | /* Note: size will always be longer than the resulting Unicode |
| 5151 | character count */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5152 | _PyUnicodeWriter_Init(&writer, 0); |
| 5153 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 1) / 2, 127) == -1) |
| 5154 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5155 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5156 | while (1) { |
| 5157 | Py_UCS4 ch = 0; |
| 5158 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5159 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5160 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5161 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5162 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5163 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5164 | native_ordering); |
| 5165 | else |
| 5166 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5167 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5168 | native_ordering); |
| 5169 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5170 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5171 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5172 | native_ordering); |
| 5173 | } else { |
| 5174 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5175 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5176 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5177 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5178 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5179 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5180 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5181 | switch (ch) |
| 5182 | { |
| 5183 | case 0: |
| 5184 | /* remaining byte at the end? (size should be even) */ |
| 5185 | if (q == e || consumed) |
| 5186 | goto End; |
| 5187 | errmsg = "truncated data"; |
| 5188 | startinpos = ((const char *)q) - starts; |
| 5189 | endinpos = ((const char *)e) - starts; |
| 5190 | break; |
| 5191 | /* The remaining input chars are ignored if the callback |
| 5192 | chooses to skip the input */ |
| 5193 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5194 | q -= 2; |
| 5195 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5196 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5197 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5198 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5199 | endinpos = ((const char *)e) - starts; |
| 5200 | break; |
| 5201 | case 2: |
| 5202 | errmsg = "illegal encoding"; |
| 5203 | startinpos = ((const char *)q) - 2 - starts; |
| 5204 | endinpos = startinpos + 2; |
| 5205 | break; |
| 5206 | case 3: |
| 5207 | errmsg = "illegal UTF-16 surrogate"; |
| 5208 | startinpos = ((const char *)q) - 4 - starts; |
| 5209 | endinpos = startinpos + 2; |
| 5210 | break; |
| 5211 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5212 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5213 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5214 | continue; |
| 5215 | } |
| 5216 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5217 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5218 | errors, |
| 5219 | &errorHandler, |
| 5220 | "utf16", errmsg, |
| 5221 | &starts, |
| 5222 | (const char **)&e, |
| 5223 | &startinpos, |
| 5224 | &endinpos, |
| 5225 | &exc, |
| 5226 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5227 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5228 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5231 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5232 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5233 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5234 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5235 | Py_XDECREF(errorHandler); |
| 5236 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5237 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5238 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5239 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5240 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5241 | Py_XDECREF(errorHandler); |
| 5242 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5243 | return NULL; |
| 5244 | } |
| 5245 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5246 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5247 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5248 | const char *errors, |
| 5249 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5250 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5251 | enum PyUnicode_Kind kind; |
| 5252 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5253 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5254 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5255 | unsigned short *out; |
| 5256 | Py_ssize_t bytesize; |
| 5257 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5258 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5259 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5260 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5261 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5262 | #endif |
| 5263 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5264 | if (!PyUnicode_Check(str)) { |
| 5265 | PyErr_BadArgument(); |
| 5266 | return NULL; |
| 5267 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5268 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5269 | return NULL; |
| 5270 | kind = PyUnicode_KIND(str); |
| 5271 | data = PyUnicode_DATA(str); |
| 5272 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5273 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5274 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5275 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5276 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5277 | const Py_UCS4 *end = in + len; |
| 5278 | while (in < end) |
| 5279 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5280 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5281 | } |
| 5282 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5283 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5284 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5285 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5286 | if (v == NULL) |
| 5287 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5289 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5290 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5291 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5293 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5294 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5295 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5296 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5297 | switch (kind) { |
| 5298 | case PyUnicode_1BYTE_KIND: { |
| 5299 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5300 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5301 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5302 | case PyUnicode_2BYTE_KIND: { |
| 5303 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5304 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5305 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5306 | case PyUnicode_4BYTE_KIND: { |
| 5307 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5308 | break; |
| 5309 | } |
| 5310 | default: |
| 5311 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5312 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5313 | |
| 5314 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5315 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5318 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5319 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5320 | Py_ssize_t size, |
| 5321 | const char *errors, |
| 5322 | int byteorder) |
| 5323 | { |
| 5324 | PyObject *result; |
| 5325 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5326 | if (tmp == NULL) |
| 5327 | return NULL; |
| 5328 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5329 | Py_DECREF(tmp); |
| 5330 | return result; |
| 5331 | } |
| 5332 | |
| 5333 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5334 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5335 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5336 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5340 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5341 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5342 | if all the escapes in the string make it still a valid ASCII string. |
| 5343 | Returns -1 if any escapes were found which cause the string to |
| 5344 | pop out of ASCII range. Otherwise returns the length of the |
| 5345 | required buffer to hold the string. |
| 5346 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5347 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5348 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5349 | { |
| 5350 | const unsigned char *p = (const unsigned char *)s; |
| 5351 | const unsigned char *end = p + size; |
| 5352 | Py_ssize_t length = 0; |
| 5353 | |
| 5354 | if (size < 0) |
| 5355 | return -1; |
| 5356 | |
| 5357 | for (; p < end; ++p) { |
| 5358 | if (*p > 127) { |
| 5359 | /* Non-ASCII */ |
| 5360 | return -1; |
| 5361 | } |
| 5362 | else if (*p != '\\') { |
| 5363 | /* Normal character */ |
| 5364 | ++length; |
| 5365 | } |
| 5366 | else { |
| 5367 | /* Backslash-escape, check next char */ |
| 5368 | ++p; |
| 5369 | /* Escape sequence reaches till end of string or |
| 5370 | non-ASCII follow-up. */ |
| 5371 | if (p >= end || *p > 127) |
| 5372 | return -1; |
| 5373 | switch (*p) { |
| 5374 | case '\n': |
| 5375 | /* backslash + \n result in zero characters */ |
| 5376 | break; |
| 5377 | case '\\': case '\'': case '\"': |
| 5378 | case 'b': case 'f': case 't': |
| 5379 | case 'n': case 'r': case 'v': case 'a': |
| 5380 | ++length; |
| 5381 | break; |
| 5382 | case '0': case '1': case '2': case '3': |
| 5383 | case '4': case '5': case '6': case '7': |
| 5384 | case 'x': case 'u': case 'U': case 'N': |
| 5385 | /* these do not guarantee ASCII characters */ |
| 5386 | return -1; |
| 5387 | default: |
| 5388 | /* count the backslash + the other character */ |
| 5389 | length += 2; |
| 5390 | } |
| 5391 | } |
| 5392 | } |
| 5393 | return length; |
| 5394 | } |
| 5395 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5396 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5397 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5398 | PyObject * |
| 5399 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5400 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5401 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5403 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5404 | Py_ssize_t startinpos; |
| 5405 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5406 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5407 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5408 | char* message; |
| 5409 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5410 | PyObject *errorHandler = NULL; |
| 5411 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5412 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5413 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5414 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5415 | if (len == 0) |
| 5416 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5417 | |
| 5418 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5419 | either the string is pure ASCII with named escapes like \n, etc. |
| 5420 | and we determined it's exact size (common case) |
| 5421 | or it contains \x, \u, ... escape sequences. then we create a |
| 5422 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5423 | _PyUnicodeWriter_Init(&writer, 0); |
| 5424 | if (len > 0) { |
| 5425 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5426 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5427 | assert(writer.kind == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5428 | } |
| 5429 | else { |
| 5430 | /* Escaped strings will always be longer than the resulting |
| 5431 | Unicode string, so we start with size here and then reduce the |
| 5432 | length after conversion to the true value. |
| 5433 | (but if the error callback returns a long replacement string |
| 5434 | we'll have to allocate more space) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5435 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5436 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5437 | } |
| 5438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5440 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5442 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | while (s < end) { |
| 5444 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5445 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5446 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | |
| 5448 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5449 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5450 | x = (unsigned char)*s; |
| 5451 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5452 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5453 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | continue; |
| 5455 | } |
| 5456 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5457 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 | /* \ - Escapes */ |
| 5459 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5460 | c = *s++; |
| 5461 | if (s > end) |
| 5462 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5463 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5464 | /* The only case in which i == ascii_length is a backslash |
| 5465 | followed by a newline. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5466 | assert(writer.pos < writer.size || (writer.pos == writer.size && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5467 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5468 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5470 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5471 | #define WRITECHAR(ch) \ |
| 5472 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5473 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5474 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5475 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5476 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5477 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5478 | case '\\': WRITECHAR('\\'); break; |
| 5479 | case '\'': WRITECHAR('\''); break; |
| 5480 | case '\"': WRITECHAR('\"'); break; |
| 5481 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5482 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5483 | case 'f': WRITECHAR('\014'); break; |
| 5484 | case 't': WRITECHAR('\t'); break; |
| 5485 | case 'n': WRITECHAR('\n'); break; |
| 5486 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5487 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5488 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5489 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5490 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5492 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | case '0': case '1': case '2': case '3': |
| 5494 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5495 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5496 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5497 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5498 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5499 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5500 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5501 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | break; |
| 5503 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5504 | /* hex escapes */ |
| 5505 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5507 | digits = 2; |
| 5508 | message = "truncated \\xXX escape"; |
| 5509 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5510 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5511 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5512 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5513 | digits = 4; |
| 5514 | message = "truncated \\uXXXX escape"; |
| 5515 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5516 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5517 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5518 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5519 | digits = 8; |
| 5520 | message = "truncated \\UXXXXXXXX escape"; |
| 5521 | hexescape: |
| 5522 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5523 | if (end - s < digits) { |
| 5524 | /* count only hex digits */ |
| 5525 | for (; s < end; ++s) { |
| 5526 | c = (unsigned char)*s; |
| 5527 | if (!Py_ISXDIGIT(c)) |
| 5528 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5529 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5530 | goto error; |
| 5531 | } |
| 5532 | for (; digits--; ++s) { |
| 5533 | c = (unsigned char)*s; |
| 5534 | if (!Py_ISXDIGIT(c)) |
| 5535 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5536 | chr = (chr<<4) & ~0xF; |
| 5537 | if (c >= '0' && c <= '9') |
| 5538 | chr += c - '0'; |
| 5539 | else if (c >= 'a' && c <= 'f') |
| 5540 | chr += 10 + c - 'a'; |
| 5541 | else |
| 5542 | chr += 10 + c - 'A'; |
| 5543 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5544 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5545 | /* _decoding_error will have already written into the |
| 5546 | target buffer. */ |
| 5547 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5548 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5549 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5550 | message = "illegal Unicode character"; |
| 5551 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5552 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5553 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5554 | break; |
| 5555 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5556 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5557 | case 'N': |
| 5558 | message = "malformed \\N character escape"; |
| 5559 | if (ucnhash_CAPI == NULL) { |
| 5560 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5561 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5562 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5563 | if (ucnhash_CAPI == NULL) |
| 5564 | goto ucnhashError; |
| 5565 | } |
| 5566 | if (*s == '{') { |
| 5567 | const char *start = s+1; |
| 5568 | /* look for the closing brace */ |
| 5569 | while (*s != '}' && s < end) |
| 5570 | s++; |
| 5571 | if (s > start && s < end && *s == '}') { |
| 5572 | /* found a name. look it up in the unicode database */ |
| 5573 | message = "unknown Unicode character name"; |
| 5574 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5575 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5576 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5577 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5578 | goto store; |
| 5579 | } |
| 5580 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5581 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5582 | |
| 5583 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5584 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5585 | message = "\\ at end of string"; |
| 5586 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5587 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5588 | } |
| 5589 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5590 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5591 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5592 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5593 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5595 | continue; |
| 5596 | |
| 5597 | error: |
| 5598 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5599 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5600 | errors, &errorHandler, |
| 5601 | "unicodeescape", message, |
| 5602 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5603 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5604 | goto onError; |
| 5605 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5607 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5608 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5609 | Py_XDECREF(errorHandler); |
| 5610 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5611 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5612 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5613 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5614 | PyErr_SetString( |
| 5615 | PyExc_UnicodeError, |
| 5616 | "\\N escapes not supported (can't load unicodedata module)" |
| 5617 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5618 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5619 | Py_XDECREF(errorHandler); |
| 5620 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5621 | return NULL; |
| 5622 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5623 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5624 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5625 | Py_XDECREF(errorHandler); |
| 5626 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | return NULL; |
| 5628 | } |
| 5629 | |
| 5630 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5631 | |
| 5632 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5633 | appropriate. |
| 5634 | |
| 5635 | */ |
| 5636 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5637 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5638 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5640 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5641 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5643 | int kind; |
| 5644 | void *data; |
| 5645 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5646 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5647 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5648 | escape. |
| 5649 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5650 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5651 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5652 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5653 | */ |
| 5654 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5655 | if (!PyUnicode_Check(unicode)) { |
| 5656 | PyErr_BadArgument(); |
| 5657 | return NULL; |
| 5658 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5659 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5660 | return NULL; |
| 5661 | len = PyUnicode_GET_LENGTH(unicode); |
| 5662 | kind = PyUnicode_KIND(unicode); |
| 5663 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5664 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5665 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5666 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5667 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5668 | } |
| 5669 | |
| 5670 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5671 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5672 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5673 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5674 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5675 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5676 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5677 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5678 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5679 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5680 | if (repr == NULL) |
| 5681 | return NULL; |
| 5682 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5683 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5685 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5686 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5687 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5688 | /* Escape backslashes */ |
| 5689 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | *p++ = '\\'; |
| 5691 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5692 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5693 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5694 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5695 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5696 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5697 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5698 | *p++ = '\\'; |
| 5699 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5700 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5701 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5702 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5703 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5704 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5705 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5706 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5707 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5708 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5709 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5711 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5712 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | *p++ = '\\'; |
| 5714 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5715 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5716 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5717 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5718 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5720 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5721 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5722 | else if (ch == '\t') { |
| 5723 | *p++ = '\\'; |
| 5724 | *p++ = 't'; |
| 5725 | } |
| 5726 | else if (ch == '\n') { |
| 5727 | *p++ = '\\'; |
| 5728 | *p++ = 'n'; |
| 5729 | } |
| 5730 | else if (ch == '\r') { |
| 5731 | *p++ = '\\'; |
| 5732 | *p++ = 'r'; |
| 5733 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5734 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5735 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5736 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5738 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5739 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5740 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5741 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5742 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5743 | /* Copy everything else as-is */ |
| 5744 | else |
| 5745 | *p++ = (char) ch; |
| 5746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5747 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5748 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5749 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5750 | return NULL; |
| 5751 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5754 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5755 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5756 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5758 | PyObject *result; |
| 5759 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5760 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5762 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5763 | Py_DECREF(tmp); |
| 5764 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5768 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5769 | PyObject * |
| 5770 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5771 | Py_ssize_t size, |
| 5772 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5774 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5775 | Py_ssize_t startinpos; |
| 5776 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5777 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | const char *end; |
| 5779 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5780 | PyObject *errorHandler = NULL; |
| 5781 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5782 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5783 | if (size == 0) |
| 5784 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5785 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | /* Escaped strings will always be longer than the resulting |
| 5787 | 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] | 5788 | length after conversion to the true value. (But decoding error |
| 5789 | handler might have to resize the string) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5790 | _PyUnicodeWriter_Init(&writer, 1); |
| 5791 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5792 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5794 | end = s + size; |
| 5795 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5796 | unsigned char c; |
| 5797 | Py_UCS4 x; |
| 5798 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5799 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5800 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5801 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5802 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5803 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5804 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5805 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5806 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5807 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5808 | startinpos = s-starts; |
| 5809 | |
| 5810 | /* \u-escapes are only interpreted iff the number of leading |
| 5811 | backslashes if odd */ |
| 5812 | bs = s; |
| 5813 | for (;s < end;) { |
| 5814 | if (*s != '\\') |
| 5815 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5816 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5817 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5818 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5819 | } |
| 5820 | if (((s - bs) & 1) == 0 || |
| 5821 | s >= end || |
| 5822 | (*s != 'u' && *s != 'U')) { |
| 5823 | continue; |
| 5824 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5825 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5826 | count = *s=='u' ? 4 : 8; |
| 5827 | s++; |
| 5828 | |
| 5829 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5831 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5832 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5833 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5834 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5835 | errors, &errorHandler, |
| 5836 | "rawunicodeescape", "truncated \\uXXXX", |
| 5837 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5838 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5839 | goto onError; |
| 5840 | goto nextByte; |
| 5841 | } |
| 5842 | x = (x<<4) & ~0xF; |
| 5843 | if (c >= '0' && c <= '9') |
| 5844 | x += c - '0'; |
| 5845 | else if (c >= 'a' && c <= 'f') |
| 5846 | x += 10 + c - 'a'; |
| 5847 | else |
| 5848 | x += 10 + c - 'A'; |
| 5849 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5850 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5851 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5852 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5853 | } |
| 5854 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5855 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5856 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5857 | errors, &errorHandler, |
| 5858 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5859 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5860 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5861 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5862 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5863 | nextByte: |
| 5864 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5866 | Py_XDECREF(errorHandler); |
| 5867 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5868 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5869 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5870 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5871 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5872 | Py_XDECREF(errorHandler); |
| 5873 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5874 | return NULL; |
| 5875 | } |
| 5876 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5877 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5878 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5879 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5881 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5882 | char *p; |
| 5883 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5884 | Py_ssize_t expandsize, pos; |
| 5885 | int kind; |
| 5886 | void *data; |
| 5887 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5889 | if (!PyUnicode_Check(unicode)) { |
| 5890 | PyErr_BadArgument(); |
| 5891 | return NULL; |
| 5892 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5893 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5894 | return NULL; |
| 5895 | kind = PyUnicode_KIND(unicode); |
| 5896 | data = PyUnicode_DATA(unicode); |
| 5897 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5898 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 5899 | bytes, and 1 byte characters 4. */ |
| 5900 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5901 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5902 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5903 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5904 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5905 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5906 | if (repr == NULL) |
| 5907 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5908 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5909 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5911 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5912 | for (pos = 0; pos < len; pos++) { |
| 5913 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5914 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5915 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5916 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5917 | *p++ = '\\'; |
| 5918 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5919 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 5920 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 5921 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 5922 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 5923 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5924 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5925 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5926 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5927 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5928 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5929 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | *p++ = '\\'; |
| 5931 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5932 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5933 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5934 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5935 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5936 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5937 | /* Copy everything else as-is */ |
| 5938 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | *p++ = (char) ch; |
| 5940 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5941 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5942 | assert(p > q); |
| 5943 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5944 | return NULL; |
| 5945 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5948 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5949 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 5950 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5951 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5952 | PyObject *result; |
| 5953 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5954 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5955 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5956 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 5957 | Py_DECREF(tmp); |
| 5958 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | } |
| 5960 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5961 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5962 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5963 | PyObject * |
| 5964 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5965 | Py_ssize_t size, |
| 5966 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5967 | { |
| 5968 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5969 | Py_ssize_t startinpos; |
| 5970 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5971 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5972 | const char *end; |
| 5973 | const char *reason; |
| 5974 | PyObject *errorHandler = NULL; |
| 5975 | PyObject *exc = NULL; |
| 5976 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5977 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 5978 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5979 | 1)) |
| 5980 | return NULL; |
| 5981 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5982 | if (size == 0) |
| 5983 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5984 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5985 | /* XXX overflow detection missing */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5986 | _PyUnicodeWriter_Init(&writer, 0); |
| 5987 | if (_PyUnicodeWriter_Prepare(&writer, (size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5988 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5989 | end = s + size; |
| 5990 | |
| 5991 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 5992 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 5993 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 5994 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 5995 | endinpos = end-starts; |
| 5996 | reason = "truncated input"; |
| 5997 | goto error; |
| 5998 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 5999 | /* We copy the raw representation one byte at a time because the |
| 6000 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6001 | ((char *) &uch)[0] = s[0]; |
| 6002 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6003 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6004 | ((char *) &uch)[2] = s[2]; |
| 6005 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6006 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6007 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6008 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6009 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6010 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6011 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6012 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6013 | reason = "illegal code point (> 0x10FFFF)"; |
| 6014 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6015 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6016 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6017 | s += Py_UNICODE_SIZE; |
| 6018 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6019 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6020 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6021 | Py_UNICODE uch2; |
| 6022 | ((char *) &uch2)[0] = s[0]; |
| 6023 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6024 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6025 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6026 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6027 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6028 | } |
| 6029 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6030 | #endif |
| 6031 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6032 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6033 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6034 | continue; |
| 6035 | |
| 6036 | error: |
| 6037 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6038 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6039 | errors, &errorHandler, |
| 6040 | "unicode_internal", reason, |
| 6041 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6042 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6043 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6046 | Py_XDECREF(errorHandler); |
| 6047 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6048 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6049 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6050 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6051 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6052 | Py_XDECREF(errorHandler); |
| 6053 | Py_XDECREF(exc); |
| 6054 | return NULL; |
| 6055 | } |
| 6056 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6058 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6059 | PyObject * |
| 6060 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6061 | Py_ssize_t size, |
| 6062 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6065 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | } |
| 6067 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6068 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6069 | static void |
| 6070 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6071 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6072 | PyObject *unicode, |
| 6073 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6074 | const char *reason) |
| 6075 | { |
| 6076 | if (*exceptionObject == NULL) { |
| 6077 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6078 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6079 | encoding, unicode, startpos, endpos, reason); |
| 6080 | } |
| 6081 | else { |
| 6082 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6083 | goto onError; |
| 6084 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6085 | goto onError; |
| 6086 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6087 | goto onError; |
| 6088 | return; |
| 6089 | onError: |
| 6090 | Py_DECREF(*exceptionObject); |
| 6091 | *exceptionObject = NULL; |
| 6092 | } |
| 6093 | } |
| 6094 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6095 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6096 | static void |
| 6097 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6098 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6099 | PyObject *unicode, |
| 6100 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6101 | const char *reason) |
| 6102 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6103 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6104 | encoding, unicode, startpos, endpos, reason); |
| 6105 | if (*exceptionObject != NULL) |
| 6106 | PyCodec_StrictErrors(*exceptionObject); |
| 6107 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6108 | |
| 6109 | /* error handling callback helper: |
| 6110 | build arguments, call the callback and check the arguments, |
| 6111 | put the result into newpos and return the replacement string, which |
| 6112 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6113 | static PyObject * |
| 6114 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6115 | PyObject **errorHandler, |
| 6116 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6117 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6118 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6119 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6120 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6121 | 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] | 6122 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6123 | PyObject *restuple; |
| 6124 | PyObject *resunicode; |
| 6125 | |
| 6126 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6127 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6128 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6129 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6130 | } |
| 6131 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6132 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6133 | return NULL; |
| 6134 | len = PyUnicode_GET_LENGTH(unicode); |
| 6135 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6136 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6137 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6138 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6139 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6140 | |
| 6141 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6142 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6143 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6144 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6145 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6146 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6147 | Py_DECREF(restuple); |
| 6148 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6149 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6150 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6151 | &resunicode, newpos)) { |
| 6152 | Py_DECREF(restuple); |
| 6153 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6154 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6155 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6156 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6157 | Py_DECREF(restuple); |
| 6158 | return NULL; |
| 6159 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6160 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6161 | *newpos = len + *newpos; |
| 6162 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6163 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6164 | Py_DECREF(restuple); |
| 6165 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6166 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6167 | Py_INCREF(resunicode); |
| 6168 | Py_DECREF(restuple); |
| 6169 | return resunicode; |
| 6170 | } |
| 6171 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6172 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6173 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6174 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6175 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6176 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6177 | /* input state */ |
| 6178 | Py_ssize_t pos=0, size; |
| 6179 | int kind; |
| 6180 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6181 | /* output object */ |
| 6182 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6183 | /* pointer into the output */ |
| 6184 | char *str; |
| 6185 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6186 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6187 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6188 | 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] | 6189 | PyObject *errorHandler = NULL; |
| 6190 | PyObject *exc = NULL; |
| 6191 | /* the following variable is used for caching string comparisons |
| 6192 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6193 | int known_errorHandler = -1; |
| 6194 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6195 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6196 | return NULL; |
| 6197 | size = PyUnicode_GET_LENGTH(unicode); |
| 6198 | kind = PyUnicode_KIND(unicode); |
| 6199 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6200 | /* allocate enough for a simple encoding without |
| 6201 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6202 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6203 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6204 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6205 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6206 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6207 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6208 | ressize = size; |
| 6209 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6210 | while (pos < size) { |
| 6211 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6212 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6213 | /* can we encode this? */ |
| 6214 | if (c<limit) { |
| 6215 | /* no overflow check, because we know that the space is enough */ |
| 6216 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6217 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6218 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6219 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6220 | Py_ssize_t requiredsize; |
| 6221 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6222 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6223 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6224 | Py_ssize_t collstart = pos; |
| 6225 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6226 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6227 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | ++collend; |
| 6229 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6230 | if (known_errorHandler==-1) { |
| 6231 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6232 | known_errorHandler = 1; |
| 6233 | else if (!strcmp(errors, "replace")) |
| 6234 | known_errorHandler = 2; |
| 6235 | else if (!strcmp(errors, "ignore")) |
| 6236 | known_errorHandler = 3; |
| 6237 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6238 | known_errorHandler = 4; |
| 6239 | else |
| 6240 | known_errorHandler = 0; |
| 6241 | } |
| 6242 | switch (known_errorHandler) { |
| 6243 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6244 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6245 | goto onError; |
| 6246 | case 2: /* replace */ |
| 6247 | while (collstart++<collend) |
| 6248 | *str++ = '?'; /* fall through */ |
| 6249 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6250 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6251 | break; |
| 6252 | case 4: /* xmlcharrefreplace */ |
| 6253 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6254 | /* determine replacement size */ |
| 6255 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6256 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6257 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6258 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6259 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6260 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6261 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6263 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6264 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6265 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6266 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6267 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6268 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6269 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6270 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6271 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6272 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6273 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6274 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6275 | if (requiredsize > ressize) { |
| 6276 | if (requiredsize<2*ressize) |
| 6277 | requiredsize = 2*ressize; |
| 6278 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6279 | goto onError; |
| 6280 | str = PyBytes_AS_STRING(res) + respos; |
| 6281 | ressize = requiredsize; |
| 6282 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6283 | /* generate replacement */ |
| 6284 | for (i = collstart; i < collend; ++i) { |
| 6285 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6286 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6287 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6288 | break; |
| 6289 | default: |
| 6290 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6291 | encoding, reason, unicode, &exc, |
| 6292 | collstart, collend, &newpos); |
| 6293 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6294 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6295 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6296 | if (PyBytes_Check(repunicode)) { |
| 6297 | /* Directly copy bytes result to output. */ |
| 6298 | repsize = PyBytes_Size(repunicode); |
| 6299 | if (repsize > 1) { |
| 6300 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6301 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6302 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6303 | Py_DECREF(repunicode); |
| 6304 | goto onError; |
| 6305 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6306 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6307 | ressize += repsize-1; |
| 6308 | } |
| 6309 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6310 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6311 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6312 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6313 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6314 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6315 | /* need more space? (at least enough for what we |
| 6316 | have+the replacement+the rest of the string, so |
| 6317 | we won't have to check space for encodable characters) */ |
| 6318 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6319 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6320 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6321 | if (requiredsize > ressize) { |
| 6322 | if (requiredsize<2*ressize) |
| 6323 | requiredsize = 2*ressize; |
| 6324 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6325 | Py_DECREF(repunicode); |
| 6326 | goto onError; |
| 6327 | } |
| 6328 | str = PyBytes_AS_STRING(res) + respos; |
| 6329 | ressize = requiredsize; |
| 6330 | } |
| 6331 | /* check if there is anything unencodable in the replacement |
| 6332 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6333 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6334 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6335 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6336 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6337 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6338 | Py_DECREF(repunicode); |
| 6339 | goto onError; |
| 6340 | } |
| 6341 | *str = (char)c; |
| 6342 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6343 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6344 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6345 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6346 | } |
| 6347 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6348 | /* Resize if we allocated to much */ |
| 6349 | size = str - PyBytes_AS_STRING(res); |
| 6350 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6351 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6352 | if (_PyBytes_Resize(&res, size) < 0) |
| 6353 | goto onError; |
| 6354 | } |
| 6355 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6356 | Py_XDECREF(errorHandler); |
| 6357 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6358 | return res; |
| 6359 | |
| 6360 | onError: |
| 6361 | Py_XDECREF(res); |
| 6362 | Py_XDECREF(errorHandler); |
| 6363 | Py_XDECREF(exc); |
| 6364 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6365 | } |
| 6366 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6367 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6368 | PyObject * |
| 6369 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6370 | Py_ssize_t size, |
| 6371 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6372 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6373 | PyObject *result; |
| 6374 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6375 | if (unicode == NULL) |
| 6376 | return NULL; |
| 6377 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6378 | Py_DECREF(unicode); |
| 6379 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6380 | } |
| 6381 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6382 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6383 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 | { |
| 6385 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6386 | PyErr_BadArgument(); |
| 6387 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6388 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6389 | if (PyUnicode_READY(unicode) == -1) |
| 6390 | return NULL; |
| 6391 | /* Fast path: if it is a one-byte string, construct |
| 6392 | bytes object directly. */ |
| 6393 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6394 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6395 | PyUnicode_GET_LENGTH(unicode)); |
| 6396 | /* Non-Latin-1 characters present. Defer to above function to |
| 6397 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6398 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6399 | } |
| 6400 | |
| 6401 | PyObject* |
| 6402 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6403 | { |
| 6404 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
| 6407 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6408 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6409 | PyObject * |
| 6410 | PyUnicode_DecodeASCII(const char *s, |
| 6411 | Py_ssize_t size, |
| 6412 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6414 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6415 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6416 | int kind; |
| 6417 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6418 | Py_ssize_t startinpos; |
| 6419 | Py_ssize_t endinpos; |
| 6420 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6421 | const char *e; |
| 6422 | PyObject *errorHandler = NULL; |
| 6423 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6426 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6427 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6429 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6430 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6431 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6432 | _PyUnicodeWriter_Init(&writer, 0); |
| 6433 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6435 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6436 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6437 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6438 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6439 | writer.pos = outpos; |
| 6440 | if (writer.pos == size) |
| 6441 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6442 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6443 | s += writer.pos; |
| 6444 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6445 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6446 | register unsigned char c = (unsigned char)*s; |
| 6447 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6448 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6449 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6450 | ++s; |
| 6451 | } |
| 6452 | else { |
| 6453 | startinpos = s-starts; |
| 6454 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6455 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6456 | errors, &errorHandler, |
| 6457 | "ascii", "ordinal not in range(128)", |
| 6458 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6459 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6460 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6461 | kind = writer.kind; |
| 6462 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6463 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6464 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6465 | Py_XDECREF(errorHandler); |
| 6466 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6467 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6468 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6469 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6470 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6471 | Py_XDECREF(errorHandler); |
| 6472 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | return NULL; |
| 6474 | } |
| 6475 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6476 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6477 | PyObject * |
| 6478 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6479 | Py_ssize_t size, |
| 6480 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6481 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6482 | PyObject *result; |
| 6483 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6484 | if (unicode == NULL) |
| 6485 | return NULL; |
| 6486 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6487 | Py_DECREF(unicode); |
| 6488 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6491 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6492 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6493 | { |
| 6494 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6495 | PyErr_BadArgument(); |
| 6496 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6497 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6498 | if (PyUnicode_READY(unicode) == -1) |
| 6499 | return NULL; |
| 6500 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6501 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame^] | 6502 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6503 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6504 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6505 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6506 | } |
| 6507 | |
| 6508 | PyObject * |
| 6509 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6510 | { |
| 6511 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6512 | } |
| 6513 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6514 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6515 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6516 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6517 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6518 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6519 | #define NEED_RETRY |
| 6520 | #endif |
| 6521 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6522 | #ifndef WC_ERR_INVALID_CHARS |
| 6523 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6524 | #endif |
| 6525 | |
| 6526 | static char* |
| 6527 | code_page_name(UINT code_page, PyObject **obj) |
| 6528 | { |
| 6529 | *obj = NULL; |
| 6530 | if (code_page == CP_ACP) |
| 6531 | return "mbcs"; |
| 6532 | if (code_page == CP_UTF7) |
| 6533 | return "CP_UTF7"; |
| 6534 | if (code_page == CP_UTF8) |
| 6535 | return "CP_UTF8"; |
| 6536 | |
| 6537 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6538 | if (*obj == NULL) |
| 6539 | return NULL; |
| 6540 | return PyBytes_AS_STRING(*obj); |
| 6541 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6542 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6543 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6544 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6545 | { |
| 6546 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6547 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6548 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6549 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6550 | return 0; |
| 6551 | |
| 6552 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6553 | if (prev == curr) |
| 6554 | return 1; |
| 6555 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6556 | as it assumes an incomplete character consists of a single |
| 6557 | byte. */ |
| 6558 | if (curr - prev == 2) |
| 6559 | return 1; |
| 6560 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6561 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6562 | return 0; |
| 6563 | } |
| 6564 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6565 | static DWORD |
| 6566 | decode_code_page_flags(UINT code_page) |
| 6567 | { |
| 6568 | if (code_page == CP_UTF7) { |
| 6569 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6570 | return 0; |
| 6571 | } |
| 6572 | else |
| 6573 | return MB_ERR_INVALID_CHARS; |
| 6574 | } |
| 6575 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6576 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6577 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6578 | * mode. |
| 6579 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6580 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6581 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6582 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6583 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6584 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6585 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6586 | const char *in, |
| 6587 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6588 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6589 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6590 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6591 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6592 | |
| 6593 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6594 | assert(insize > 0); |
| 6595 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6596 | if (outsize <= 0) |
| 6597 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6598 | |
| 6599 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6600 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6601 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6602 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6603 | if (*v == NULL) |
| 6604 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6605 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6606 | } |
| 6607 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6608 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6609 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6610 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6611 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6612 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6613 | } |
| 6614 | |
| 6615 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6616 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6617 | if (outsize <= 0) |
| 6618 | goto error; |
| 6619 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6620 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6621 | error: |
| 6622 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6623 | return -2; |
| 6624 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6625 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6628 | /* |
| 6629 | * Decode a byte string from a code page into unicode object with an error |
| 6630 | * handler. |
| 6631 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6632 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6633 | * UnicodeDecodeError exception and returns -1 on error. |
| 6634 | */ |
| 6635 | static int |
| 6636 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6637 | PyObject **v, |
| 6638 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6639 | const char *errors) |
| 6640 | { |
| 6641 | const char *startin = in; |
| 6642 | const char *endin = in + size; |
| 6643 | const DWORD flags = decode_code_page_flags(code_page); |
| 6644 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6645 | 2000 English version of the message. */ |
| 6646 | const char *reason = "No mapping for the Unicode character exists " |
| 6647 | "in the target code page."; |
| 6648 | /* each step cannot decode more than 1 character, but a character can be |
| 6649 | represented as a surrogate pair */ |
| 6650 | wchar_t buffer[2], *startout, *out; |
| 6651 | int insize, outsize; |
| 6652 | PyObject *errorHandler = NULL; |
| 6653 | PyObject *exc = NULL; |
| 6654 | PyObject *encoding_obj = NULL; |
| 6655 | char *encoding; |
| 6656 | DWORD err; |
| 6657 | int ret = -1; |
| 6658 | |
| 6659 | assert(size > 0); |
| 6660 | |
| 6661 | encoding = code_page_name(code_page, &encoding_obj); |
| 6662 | if (encoding == NULL) |
| 6663 | return -1; |
| 6664 | |
| 6665 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6666 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6667 | UnicodeDecodeError. */ |
| 6668 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6669 | if (exc != NULL) { |
| 6670 | PyCodec_StrictErrors(exc); |
| 6671 | Py_CLEAR(exc); |
| 6672 | } |
| 6673 | goto error; |
| 6674 | } |
| 6675 | |
| 6676 | if (*v == NULL) { |
| 6677 | /* Create unicode object */ |
| 6678 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6679 | PyErr_NoMemory(); |
| 6680 | goto error; |
| 6681 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6682 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6683 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6684 | if (*v == NULL) |
| 6685 | goto error; |
| 6686 | startout = PyUnicode_AS_UNICODE(*v); |
| 6687 | } |
| 6688 | else { |
| 6689 | /* Extend unicode object */ |
| 6690 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6691 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6692 | PyErr_NoMemory(); |
| 6693 | goto error; |
| 6694 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6695 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6696 | goto error; |
| 6697 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6698 | } |
| 6699 | |
| 6700 | /* Decode the byte string character per character */ |
| 6701 | out = startout; |
| 6702 | while (in < endin) |
| 6703 | { |
| 6704 | /* Decode a character */ |
| 6705 | insize = 1; |
| 6706 | do |
| 6707 | { |
| 6708 | outsize = MultiByteToWideChar(code_page, flags, |
| 6709 | in, insize, |
| 6710 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6711 | if (outsize > 0) |
| 6712 | break; |
| 6713 | err = GetLastError(); |
| 6714 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6715 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6716 | { |
| 6717 | PyErr_SetFromWindowsErr(0); |
| 6718 | goto error; |
| 6719 | } |
| 6720 | insize++; |
| 6721 | } |
| 6722 | /* 4=maximum length of a UTF-8 sequence */ |
| 6723 | while (insize <= 4 && (in + insize) <= endin); |
| 6724 | |
| 6725 | if (outsize <= 0) { |
| 6726 | Py_ssize_t startinpos, endinpos, outpos; |
| 6727 | |
| 6728 | startinpos = in - startin; |
| 6729 | endinpos = startinpos + 1; |
| 6730 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6731 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6732 | errors, &errorHandler, |
| 6733 | encoding, reason, |
| 6734 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6735 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6736 | { |
| 6737 | goto error; |
| 6738 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6739 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6740 | } |
| 6741 | else { |
| 6742 | in += insize; |
| 6743 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6744 | out += outsize; |
| 6745 | } |
| 6746 | } |
| 6747 | |
| 6748 | /* write a NUL character at the end */ |
| 6749 | *out = 0; |
| 6750 | |
| 6751 | /* Extend unicode object */ |
| 6752 | outsize = out - startout; |
| 6753 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6754 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6755 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6756 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6757 | |
| 6758 | error: |
| 6759 | Py_XDECREF(encoding_obj); |
| 6760 | Py_XDECREF(errorHandler); |
| 6761 | Py_XDECREF(exc); |
| 6762 | return ret; |
| 6763 | } |
| 6764 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6765 | static PyObject * |
| 6766 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6767 | const char *s, Py_ssize_t size, |
| 6768 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6769 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6770 | PyObject *v = NULL; |
| 6771 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6772 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6773 | if (code_page < 0) { |
| 6774 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6775 | return NULL; |
| 6776 | } |
| 6777 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6778 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6779 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6780 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6781 | do |
| 6782 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6783 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6784 | if (size > INT_MAX) { |
| 6785 | chunk_size = INT_MAX; |
| 6786 | final = 0; |
| 6787 | done = 0; |
| 6788 | } |
| 6789 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6790 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6791 | { |
| 6792 | chunk_size = (int)size; |
| 6793 | final = (consumed == NULL); |
| 6794 | done = 1; |
| 6795 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6796 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6797 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6798 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6799 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6800 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6801 | if (chunk_size == 0 && done) { |
| 6802 | if (v != NULL) |
| 6803 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6804 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6805 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6806 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6807 | |
| 6808 | converted = decode_code_page_strict(code_page, &v, |
| 6809 | s, chunk_size); |
| 6810 | if (converted == -2) |
| 6811 | converted = decode_code_page_errors(code_page, &v, |
| 6812 | s, chunk_size, |
| 6813 | errors); |
| 6814 | assert(converted != 0); |
| 6815 | |
| 6816 | if (converted < 0) { |
| 6817 | Py_XDECREF(v); |
| 6818 | return NULL; |
| 6819 | } |
| 6820 | |
| 6821 | if (consumed) |
| 6822 | *consumed += converted; |
| 6823 | |
| 6824 | s += converted; |
| 6825 | size -= converted; |
| 6826 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6827 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6828 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6829 | } |
| 6830 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6831 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6832 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6833 | const char *s, |
| 6834 | Py_ssize_t size, |
| 6835 | const char *errors, |
| 6836 | Py_ssize_t *consumed) |
| 6837 | { |
| 6838 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6839 | } |
| 6840 | |
| 6841 | PyObject * |
| 6842 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6843 | Py_ssize_t size, |
| 6844 | const char *errors, |
| 6845 | Py_ssize_t *consumed) |
| 6846 | { |
| 6847 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6848 | } |
| 6849 | |
| 6850 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6851 | PyUnicode_DecodeMBCS(const char *s, |
| 6852 | Py_ssize_t size, |
| 6853 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6854 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6855 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6856 | } |
| 6857 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6858 | static DWORD |
| 6859 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6860 | { |
| 6861 | if (code_page == CP_UTF8) { |
| 6862 | if (winver.dwMajorVersion >= 6) |
| 6863 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6864 | and later */ |
| 6865 | return WC_ERR_INVALID_CHARS; |
| 6866 | else |
| 6867 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 6868 | return 0; |
| 6869 | } |
| 6870 | else if (code_page == CP_UTF7) { |
| 6871 | /* CP_UTF7 only supports flags=0 */ |
| 6872 | return 0; |
| 6873 | } |
| 6874 | else { |
| 6875 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 6876 | return 0; |
| 6877 | else |
| 6878 | return WC_NO_BEST_FIT_CHARS; |
| 6879 | } |
| 6880 | } |
| 6881 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6882 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6883 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 6884 | * mode. |
| 6885 | * |
| 6886 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6887 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6888 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6889 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6890 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6891 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6892 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6893 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6894 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6895 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 6896 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6897 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6898 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6899 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6900 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 6901 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6902 | /* Create a substring so that we can get the UTF-16 representation |
| 6903 | of just the slice under consideration. */ |
| 6904 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6905 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6906 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6907 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6908 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6909 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6910 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6911 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6912 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6913 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 6914 | if (substring == NULL) |
| 6915 | return -1; |
| 6916 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 6917 | if (p == NULL) { |
| 6918 | Py_DECREF(substring); |
| 6919 | return -1; |
| 6920 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6921 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6922 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6923 | outsize = WideCharToMultiByte(code_page, flags, |
| 6924 | p, size, |
| 6925 | NULL, 0, |
| 6926 | NULL, pusedDefaultChar); |
| 6927 | if (outsize <= 0) |
| 6928 | goto error; |
| 6929 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6930 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 6931 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6932 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6933 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6934 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6935 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6936 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6937 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6938 | if (*outbytes == NULL) { |
| 6939 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6940 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6941 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6942 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6943 | } |
| 6944 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6945 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6946 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 6947 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 6948 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6949 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6950 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6951 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6952 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 6953 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6954 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6955 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6956 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
| 6959 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6960 | outsize = WideCharToMultiByte(code_page, flags, |
| 6961 | p, size, |
| 6962 | out, outsize, |
| 6963 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6964 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6965 | if (outsize <= 0) |
| 6966 | goto error; |
| 6967 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6968 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6969 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6970 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6971 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6972 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6973 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6974 | return -2; |
| 6975 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6976 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6977 | } |
| 6978 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6979 | /* |
| 6980 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 6981 | * error handler. |
| 6982 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6983 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6984 | * -1 on other error. |
| 6985 | */ |
| 6986 | static int |
| 6987 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 6988 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6989 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6990 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6991 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6992 | Py_ssize_t pos = unicode_offset; |
| 6993 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6994 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6995 | 2000 English version of the message. */ |
| 6996 | const char *reason = "invalid character"; |
| 6997 | /* 4=maximum length of a UTF-8 sequence */ |
| 6998 | char buffer[4]; |
| 6999 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7000 | Py_ssize_t outsize; |
| 7001 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7002 | PyObject *errorHandler = NULL; |
| 7003 | PyObject *exc = NULL; |
| 7004 | PyObject *encoding_obj = NULL; |
| 7005 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7006 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7007 | PyObject *rep; |
| 7008 | int ret = -1; |
| 7009 | |
| 7010 | assert(insize > 0); |
| 7011 | |
| 7012 | encoding = code_page_name(code_page, &encoding_obj); |
| 7013 | if (encoding == NULL) |
| 7014 | return -1; |
| 7015 | |
| 7016 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7017 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7018 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7019 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7020 | if (exc != NULL) { |
| 7021 | PyCodec_StrictErrors(exc); |
| 7022 | Py_DECREF(exc); |
| 7023 | } |
| 7024 | Py_XDECREF(encoding_obj); |
| 7025 | return -1; |
| 7026 | } |
| 7027 | |
| 7028 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7029 | pusedDefaultChar = &usedDefaultChar; |
| 7030 | else |
| 7031 | pusedDefaultChar = NULL; |
| 7032 | |
| 7033 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7034 | PyErr_NoMemory(); |
| 7035 | goto error; |
| 7036 | } |
| 7037 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7038 | |
| 7039 | if (*outbytes == NULL) { |
| 7040 | /* Create string object */ |
| 7041 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7042 | if (*outbytes == NULL) |
| 7043 | goto error; |
| 7044 | out = PyBytes_AS_STRING(*outbytes); |
| 7045 | } |
| 7046 | else { |
| 7047 | /* Extend string object */ |
| 7048 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7049 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7050 | PyErr_NoMemory(); |
| 7051 | goto error; |
| 7052 | } |
| 7053 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7054 | goto error; |
| 7055 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7056 | } |
| 7057 | |
| 7058 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7059 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7060 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7061 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7062 | wchar_t chars[2]; |
| 7063 | int charsize; |
| 7064 | if (ch < 0x10000) { |
| 7065 | chars[0] = (wchar_t)ch; |
| 7066 | charsize = 1; |
| 7067 | } |
| 7068 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7069 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7070 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7071 | charsize = 2; |
| 7072 | } |
| 7073 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7074 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7075 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7076 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7077 | NULL, pusedDefaultChar); |
| 7078 | if (outsize > 0) { |
| 7079 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7080 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7081 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7082 | memcpy(out, buffer, outsize); |
| 7083 | out += outsize; |
| 7084 | continue; |
| 7085 | } |
| 7086 | } |
| 7087 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7088 | PyErr_SetFromWindowsErr(0); |
| 7089 | goto error; |
| 7090 | } |
| 7091 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7092 | rep = unicode_encode_call_errorhandler( |
| 7093 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7094 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7095 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7096 | if (rep == NULL) |
| 7097 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7098 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7099 | |
| 7100 | if (PyBytes_Check(rep)) { |
| 7101 | outsize = PyBytes_GET_SIZE(rep); |
| 7102 | if (outsize != 1) { |
| 7103 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7104 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7105 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7106 | Py_DECREF(rep); |
| 7107 | goto error; |
| 7108 | } |
| 7109 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7110 | } |
| 7111 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7112 | out += outsize; |
| 7113 | } |
| 7114 | else { |
| 7115 | Py_ssize_t i; |
| 7116 | enum PyUnicode_Kind kind; |
| 7117 | void *data; |
| 7118 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7119 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7120 | Py_DECREF(rep); |
| 7121 | goto error; |
| 7122 | } |
| 7123 | |
| 7124 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7125 | if (outsize != 1) { |
| 7126 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7127 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7128 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7129 | Py_DECREF(rep); |
| 7130 | goto error; |
| 7131 | } |
| 7132 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7133 | } |
| 7134 | kind = PyUnicode_KIND(rep); |
| 7135 | data = PyUnicode_DATA(rep); |
| 7136 | for (i=0; i < outsize; i++) { |
| 7137 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7138 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7139 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7140 | encoding, unicode, |
| 7141 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7142 | "unable to encode error handler result to ASCII"); |
| 7143 | Py_DECREF(rep); |
| 7144 | goto error; |
| 7145 | } |
| 7146 | *out = (unsigned char)ch; |
| 7147 | out++; |
| 7148 | } |
| 7149 | } |
| 7150 | Py_DECREF(rep); |
| 7151 | } |
| 7152 | /* write a NUL byte */ |
| 7153 | *out = 0; |
| 7154 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7155 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7156 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7157 | goto error; |
| 7158 | ret = 0; |
| 7159 | |
| 7160 | error: |
| 7161 | Py_XDECREF(encoding_obj); |
| 7162 | Py_XDECREF(errorHandler); |
| 7163 | Py_XDECREF(exc); |
| 7164 | return ret; |
| 7165 | } |
| 7166 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7167 | static PyObject * |
| 7168 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7169 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7170 | const char *errors) |
| 7171 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7172 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7173 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7174 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7175 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7176 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7177 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7178 | return NULL; |
| 7179 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7180 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7181 | if (code_page < 0) { |
| 7182 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7183 | return NULL; |
| 7184 | } |
| 7185 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7186 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7187 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7188 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7189 | offset = 0; |
| 7190 | do |
| 7191 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7192 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7193 | /* 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] | 7194 | chunks. */ |
| 7195 | if (len > INT_MAX/2) { |
| 7196 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7197 | done = 0; |
| 7198 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7199 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7200 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7201 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7202 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7203 | done = 1; |
| 7204 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7205 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7206 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7207 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7208 | errors); |
| 7209 | if (ret == -2) |
| 7210 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7211 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7212 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7213 | if (ret < 0) { |
| 7214 | Py_XDECREF(outbytes); |
| 7215 | return NULL; |
| 7216 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7217 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7218 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7219 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7220 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7221 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7222 | return outbytes; |
| 7223 | } |
| 7224 | |
| 7225 | PyObject * |
| 7226 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7227 | Py_ssize_t size, |
| 7228 | const char *errors) |
| 7229 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7230 | PyObject *unicode, *res; |
| 7231 | unicode = PyUnicode_FromUnicode(p, size); |
| 7232 | if (unicode == NULL) |
| 7233 | return NULL; |
| 7234 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7235 | Py_DECREF(unicode); |
| 7236 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7237 | } |
| 7238 | |
| 7239 | PyObject * |
| 7240 | PyUnicode_EncodeCodePage(int code_page, |
| 7241 | PyObject *unicode, |
| 7242 | const char *errors) |
| 7243 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7244 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7245 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7246 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7247 | PyObject * |
| 7248 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7249 | { |
| 7250 | if (!PyUnicode_Check(unicode)) { |
| 7251 | PyErr_BadArgument(); |
| 7252 | return NULL; |
| 7253 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7254 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7255 | } |
| 7256 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7257 | #undef NEED_RETRY |
| 7258 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7259 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7260 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7262 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7263 | PyObject * |
| 7264 | PyUnicode_DecodeCharmap(const char *s, |
| 7265 | Py_ssize_t size, |
| 7266 | PyObject *mapping, |
| 7267 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7268 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7269 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7270 | Py_ssize_t startinpos; |
| 7271 | Py_ssize_t endinpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7272 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7273 | _PyUnicodeWriter writer; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7274 | PyObject *errorHandler = NULL; |
| 7275 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7277 | /* Default to Latin-1 */ |
| 7278 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7279 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7280 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7281 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7282 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7283 | _PyUnicodeWriter_Init(&writer, 0); |
| 7284 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7285 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7286 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7287 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7288 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7289 | Py_ssize_t maplen; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7290 | enum PyUnicode_Kind mapkind; |
| 7291 | void *mapdata; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7292 | Py_UCS4 x; |
Victor Stinner | 03c3e35 | 2013-04-09 21:53:09 +0200 | [diff] [blame] | 7293 | unsigned char ch; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7294 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7295 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7296 | return NULL; |
| 7297 | |
| 7298 | maplen = PyUnicode_GET_LENGTH(mapping); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7299 | mapdata = PyUnicode_DATA(mapping); |
| 7300 | mapkind = PyUnicode_KIND(mapping); |
Victor Stinner | 03c3e35 | 2013-04-09 21:53:09 +0200 | [diff] [blame] | 7301 | |
| 7302 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7303 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7304 | * is disabled in encoding aliases, latin1 is preferred because |
| 7305 | * its implementation is faster. */ |
| 7306 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7307 | Py_UCS1 *outdata = (Py_UCS1 *)writer.data; |
| 7308 | Py_UCS4 maxchar = writer.maxchar; |
| 7309 | |
| 7310 | assert (writer.kind == PyUnicode_1BYTE_KIND); |
| 7311 | while (s < e) { |
| 7312 | ch = *s; |
| 7313 | x = mapdata_ucs1[ch]; |
| 7314 | if (x > maxchar) { |
| 7315 | if (_PyUnicodeWriter_PrepareInternal(&writer, 1, 0xff) == -1) |
| 7316 | goto onError; |
| 7317 | maxchar = writer.maxchar; |
| 7318 | outdata = (Py_UCS1 *)writer.data; |
| 7319 | } |
| 7320 | outdata[writer.pos] = x; |
| 7321 | writer.pos++; |
| 7322 | ++s; |
| 7323 | } |
| 7324 | } |
| 7325 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7326 | while (s < e) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7327 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7328 | enum PyUnicode_Kind outkind = writer.kind; |
Victor Stinner | 63d5c1a | 2013-04-09 22:13:33 +0200 | [diff] [blame] | 7329 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7330 | if (outkind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 63d5c1a | 2013-04-09 22:13:33 +0200 | [diff] [blame] | 7331 | Py_UCS1 *outdata = (Py_UCS1 *)writer.data; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7332 | Py_UCS4 maxchar = writer.maxchar; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7333 | while (s < e) { |
Victor Stinner | 63d5c1a | 2013-04-09 22:13:33 +0200 | [diff] [blame] | 7334 | ch = *s; |
| 7335 | x = mapdata_ucs2[ch]; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7336 | if (x > maxchar) |
| 7337 | goto Error; |
Victor Stinner | 63d5c1a | 2013-04-09 22:13:33 +0200 | [diff] [blame] | 7338 | outdata[writer.pos] = x; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7339 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7340 | ++s; |
| 7341 | } |
| 7342 | break; |
| 7343 | } |
| 7344 | else if (outkind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | 63d5c1a | 2013-04-09 22:13:33 +0200 | [diff] [blame] | 7345 | Py_UCS2 *outdata = (Py_UCS2 *)writer.data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7346 | while (s < e) { |
Victor Stinner | 63d5c1a | 2013-04-09 22:13:33 +0200 | [diff] [blame] | 7347 | ch = *s; |
| 7348 | x = mapdata_ucs2[ch]; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7349 | if (x == 0xFFFE) |
| 7350 | goto Error; |
Victor Stinner | 63d5c1a | 2013-04-09 22:13:33 +0200 | [diff] [blame] | 7351 | outdata[writer.pos] = x; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7352 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7353 | ++s; |
| 7354 | } |
| 7355 | break; |
| 7356 | } |
| 7357 | } |
| 7358 | ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7359 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7360 | if (ch < maplen) |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7361 | x = PyUnicode_READ(mapkind, mapdata, ch); |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7362 | else |
| 7363 | x = 0xfffe; /* invalid value */ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7364 | Error: |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7365 | if (x == 0xfffe) |
| 7366 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7367 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7368 | startinpos = s-starts; |
| 7369 | endinpos = startinpos+1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7370 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7371 | errors, &errorHandler, |
| 7372 | "charmap", "character maps to <undefined>", |
| 7373 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7374 | &writer)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7375 | goto onError; |
| 7376 | } |
| 7377 | continue; |
| 7378 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7379 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 7380 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7381 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7382 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7383 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7384 | } |
| 7385 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7386 | while (s < e) { |
| 7387 | unsigned char ch = *s; |
| 7388 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7390 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7391 | w = PyLong_FromLong((long)ch); |
| 7392 | if (w == NULL) |
| 7393 | goto onError; |
| 7394 | x = PyObject_GetItem(mapping, w); |
| 7395 | Py_DECREF(w); |
| 7396 | if (x == NULL) { |
| 7397 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7398 | /* No mapping found means: mapping is undefined. */ |
| 7399 | PyErr_Clear(); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7400 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7401 | } else |
| 7402 | goto onError; |
| 7403 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7404 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7405 | /* Apply mapping */ |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7406 | if (x == Py_None) |
| 7407 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7408 | if (PyLong_Check(x)) { |
| 7409 | long value = PyLong_AS_LONG(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7410 | if (value == 0xFFFE) |
| 7411 | goto Undefined; |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 7412 | if (value < 0 || value > MAX_UNICODE) { |
| 7413 | PyErr_Format(PyExc_TypeError, |
| 7414 | "character mapping must be in range(0x%lx)", |
| 7415 | (unsigned long)MAX_UNICODE + 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7416 | Py_DECREF(x); |
| 7417 | goto onError; |
| 7418 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7419 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 7420 | if (_PyUnicodeWriter_WriteCharInline(&writer, value) < 0) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7421 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7422 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7423 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7424 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7425 | else if (PyUnicode_Check(x)) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7426 | if (PyUnicode_READY(x) == -1) { |
| 7427 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7428 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7429 | } |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7430 | if (PyUnicode_GET_LENGTH(x) == 1) { |
Serhiy Storchaka | 45d16d9 | 2013-01-15 15:01:20 +0200 | [diff] [blame] | 7431 | Py_UCS4 value = PyUnicode_READ_CHAR(x, 0); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7432 | if (value == 0xFFFE) |
| 7433 | goto Undefined; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 7434 | if (_PyUnicodeWriter_WriteCharInline(&writer, value) < 0) { |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7435 | Py_DECREF(x); |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7436 | goto onError; |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7437 | } |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7438 | } |
| 7439 | else { |
| 7440 | writer.overallocate = 1; |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7441 | if (_PyUnicodeWriter_WriteStr(&writer, x) == -1) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7442 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7443 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7444 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7445 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7446 | } |
| 7447 | else { |
| 7448 | /* wrong return value */ |
| 7449 | PyErr_SetString(PyExc_TypeError, |
| 7450 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7451 | Py_DECREF(x); |
| 7452 | goto onError; |
| 7453 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7454 | Py_DECREF(x); |
| 7455 | ++s; |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7456 | continue; |
| 7457 | Undefined: |
| 7458 | /* undefined mapping */ |
| 7459 | Py_XDECREF(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7460 | startinpos = s-starts; |
| 7461 | endinpos = startinpos+1; |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7462 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7463 | errors, &errorHandler, |
| 7464 | "charmap", "character maps to <undefined>", |
| 7465 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7466 | &writer)) { |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7467 | goto onError; |
| 7468 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7469 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7470 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7471 | Py_XDECREF(errorHandler); |
| 7472 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7473 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7474 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7475 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7476 | Py_XDECREF(errorHandler); |
| 7477 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7478 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | return NULL; |
| 7480 | } |
| 7481 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7482 | /* Charmap encoding: the lookup table */ |
| 7483 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7484 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7485 | PyObject_HEAD |
| 7486 | unsigned char level1[32]; |
| 7487 | int count2, count3; |
| 7488 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7489 | }; |
| 7490 | |
| 7491 | static PyObject* |
| 7492 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7493 | { |
| 7494 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7495 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7496 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7497 | } |
| 7498 | |
| 7499 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7500 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7501 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7502 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7503 | }; |
| 7504 | |
| 7505 | static void |
| 7506 | encoding_map_dealloc(PyObject* o) |
| 7507 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7508 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7509 | } |
| 7510 | |
| 7511 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7512 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7513 | "EncodingMap", /*tp_name*/ |
| 7514 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7515 | 0, /*tp_itemsize*/ |
| 7516 | /* methods */ |
| 7517 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7518 | 0, /*tp_print*/ |
| 7519 | 0, /*tp_getattr*/ |
| 7520 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7521 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7522 | 0, /*tp_repr*/ |
| 7523 | 0, /*tp_as_number*/ |
| 7524 | 0, /*tp_as_sequence*/ |
| 7525 | 0, /*tp_as_mapping*/ |
| 7526 | 0, /*tp_hash*/ |
| 7527 | 0, /*tp_call*/ |
| 7528 | 0, /*tp_str*/ |
| 7529 | 0, /*tp_getattro*/ |
| 7530 | 0, /*tp_setattro*/ |
| 7531 | 0, /*tp_as_buffer*/ |
| 7532 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7533 | 0, /*tp_doc*/ |
| 7534 | 0, /*tp_traverse*/ |
| 7535 | 0, /*tp_clear*/ |
| 7536 | 0, /*tp_richcompare*/ |
| 7537 | 0, /*tp_weaklistoffset*/ |
| 7538 | 0, /*tp_iter*/ |
| 7539 | 0, /*tp_iternext*/ |
| 7540 | encoding_map_methods, /*tp_methods*/ |
| 7541 | 0, /*tp_members*/ |
| 7542 | 0, /*tp_getset*/ |
| 7543 | 0, /*tp_base*/ |
| 7544 | 0, /*tp_dict*/ |
| 7545 | 0, /*tp_descr_get*/ |
| 7546 | 0, /*tp_descr_set*/ |
| 7547 | 0, /*tp_dictoffset*/ |
| 7548 | 0, /*tp_init*/ |
| 7549 | 0, /*tp_alloc*/ |
| 7550 | 0, /*tp_new*/ |
| 7551 | 0, /*tp_free*/ |
| 7552 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7553 | }; |
| 7554 | |
| 7555 | PyObject* |
| 7556 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7557 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7558 | PyObject *result; |
| 7559 | struct encoding_map *mresult; |
| 7560 | int i; |
| 7561 | int need_dict = 0; |
| 7562 | unsigned char level1[32]; |
| 7563 | unsigned char level2[512]; |
| 7564 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7565 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7566 | int kind; |
| 7567 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7568 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7569 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7570 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7571 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7572 | PyErr_BadArgument(); |
| 7573 | return NULL; |
| 7574 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7575 | kind = PyUnicode_KIND(string); |
| 7576 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7577 | length = PyUnicode_GET_LENGTH(string); |
| 7578 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7579 | memset(level1, 0xFF, sizeof level1); |
| 7580 | memset(level2, 0xFF, sizeof level2); |
| 7581 | |
| 7582 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7583 | or if there are non-BMP characters, we need to use |
| 7584 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7585 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7586 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7587 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7588 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7589 | ch = PyUnicode_READ(kind, data, i); |
| 7590 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7591 | need_dict = 1; |
| 7592 | break; |
| 7593 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7594 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7595 | /* unmapped character */ |
| 7596 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7597 | l1 = ch >> 11; |
| 7598 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7599 | if (level1[l1] == 0xFF) |
| 7600 | level1[l1] = count2++; |
| 7601 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7602 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7603 | } |
| 7604 | |
| 7605 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7606 | need_dict = 1; |
| 7607 | |
| 7608 | if (need_dict) { |
| 7609 | PyObject *result = PyDict_New(); |
| 7610 | PyObject *key, *value; |
| 7611 | if (!result) |
| 7612 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7613 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7614 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7615 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7616 | if (!key || !value) |
| 7617 | goto failed1; |
| 7618 | if (PyDict_SetItem(result, key, value) == -1) |
| 7619 | goto failed1; |
| 7620 | Py_DECREF(key); |
| 7621 | Py_DECREF(value); |
| 7622 | } |
| 7623 | return result; |
| 7624 | failed1: |
| 7625 | Py_XDECREF(key); |
| 7626 | Py_XDECREF(value); |
| 7627 | Py_DECREF(result); |
| 7628 | return NULL; |
| 7629 | } |
| 7630 | |
| 7631 | /* Create a three-level trie */ |
| 7632 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7633 | 16*count2 + 128*count3 - 1); |
| 7634 | if (!result) |
| 7635 | return PyErr_NoMemory(); |
| 7636 | PyObject_Init(result, &EncodingMapType); |
| 7637 | mresult = (struct encoding_map*)result; |
| 7638 | mresult->count2 = count2; |
| 7639 | mresult->count3 = count3; |
| 7640 | mlevel1 = mresult->level1; |
| 7641 | mlevel2 = mresult->level23; |
| 7642 | mlevel3 = mresult->level23 + 16*count2; |
| 7643 | memcpy(mlevel1, level1, 32); |
| 7644 | memset(mlevel2, 0xFF, 16*count2); |
| 7645 | memset(mlevel3, 0, 128*count3); |
| 7646 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7647 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7648 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7649 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7650 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7651 | /* unmapped character */ |
| 7652 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7653 | o1 = ch>>11; |
| 7654 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7655 | i2 = 16*mlevel1[o1] + o2; |
| 7656 | if (mlevel2[i2] == 0xFF) |
| 7657 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7658 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7659 | i3 = 128*mlevel2[i2] + o3; |
| 7660 | mlevel3[i3] = i; |
| 7661 | } |
| 7662 | return result; |
| 7663 | } |
| 7664 | |
| 7665 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7666 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7667 | { |
| 7668 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7669 | int l1 = c>>11; |
| 7670 | int l2 = (c>>7) & 0xF; |
| 7671 | int l3 = c & 0x7F; |
| 7672 | int i; |
| 7673 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7674 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7675 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7676 | if (c == 0) |
| 7677 | return 0; |
| 7678 | /* level 1*/ |
| 7679 | i = map->level1[l1]; |
| 7680 | if (i == 0xFF) { |
| 7681 | return -1; |
| 7682 | } |
| 7683 | /* level 2*/ |
| 7684 | i = map->level23[16*i+l2]; |
| 7685 | if (i == 0xFF) { |
| 7686 | return -1; |
| 7687 | } |
| 7688 | /* level 3 */ |
| 7689 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7690 | if (i == 0) { |
| 7691 | return -1; |
| 7692 | } |
| 7693 | return i; |
| 7694 | } |
| 7695 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7696 | /* Lookup the character ch in the mapping. If the character |
| 7697 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7698 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7699 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7700 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7701 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7702 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7703 | PyObject *x; |
| 7704 | |
| 7705 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7706 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7707 | x = PyObject_GetItem(mapping, w); |
| 7708 | Py_DECREF(w); |
| 7709 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7710 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7711 | /* No mapping found means: mapping is undefined. */ |
| 7712 | PyErr_Clear(); |
| 7713 | x = Py_None; |
| 7714 | Py_INCREF(x); |
| 7715 | return x; |
| 7716 | } else |
| 7717 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7718 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7719 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7720 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7721 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7722 | long value = PyLong_AS_LONG(x); |
| 7723 | if (value < 0 || value > 255) { |
| 7724 | PyErr_SetString(PyExc_TypeError, |
| 7725 | "character mapping must be in range(256)"); |
| 7726 | Py_DECREF(x); |
| 7727 | return NULL; |
| 7728 | } |
| 7729 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7730 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7731 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7732 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7733 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7734 | /* wrong return value */ |
| 7735 | PyErr_Format(PyExc_TypeError, |
| 7736 | "character mapping must return integer, bytes or None, not %.400s", |
| 7737 | x->ob_type->tp_name); |
| 7738 | Py_DECREF(x); |
| 7739 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7740 | } |
| 7741 | } |
| 7742 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7743 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7744 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7745 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7746 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7747 | /* exponentially overallocate to minimize reallocations */ |
| 7748 | if (requiredsize < 2*outsize) |
| 7749 | requiredsize = 2*outsize; |
| 7750 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7751 | return -1; |
| 7752 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7753 | } |
| 7754 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7755 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7756 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7757 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7758 | /* 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] | 7759 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7760 | space is available. Return a new reference to the object that |
| 7761 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7762 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7763 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7764 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7765 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7766 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7767 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7768 | PyObject *rep; |
| 7769 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7770 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7771 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7772 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7773 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7774 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7775 | if (res == -1) |
| 7776 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7777 | if (outsize<requiredsize) |
| 7778 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7779 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7780 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7781 | outstart[(*outpos)++] = (char)res; |
| 7782 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7786 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7787 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7788 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7789 | Py_DECREF(rep); |
| 7790 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7791 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7792 | if (PyLong_Check(rep)) { |
| 7793 | Py_ssize_t requiredsize = *outpos+1; |
| 7794 | if (outsize<requiredsize) |
| 7795 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7796 | Py_DECREF(rep); |
| 7797 | return enc_EXCEPTION; |
| 7798 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7799 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7800 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7801 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7802 | else { |
| 7803 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7804 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7805 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7806 | if (outsize<requiredsize) |
| 7807 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7808 | Py_DECREF(rep); |
| 7809 | return enc_EXCEPTION; |
| 7810 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7811 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7812 | memcpy(outstart + *outpos, repchars, repsize); |
| 7813 | *outpos += repsize; |
| 7814 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7815 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7816 | Py_DECREF(rep); |
| 7817 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7818 | } |
| 7819 | |
| 7820 | /* handle an error in PyUnicode_EncodeCharmap |
| 7821 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7822 | static int |
| 7823 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7824 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7825 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7826 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7827 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7828 | { |
| 7829 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7830 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7831 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7832 | enum PyUnicode_Kind kind; |
| 7833 | void *data; |
| 7834 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7835 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7836 | Py_ssize_t collstartpos = *inpos; |
| 7837 | Py_ssize_t collendpos = *inpos+1; |
| 7838 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7839 | char *encoding = "charmap"; |
| 7840 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7841 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7842 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7843 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7844 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7845 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7846 | return -1; |
| 7847 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7848 | /* find all unencodable characters */ |
| 7849 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7850 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7851 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7852 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7853 | val = encoding_map_lookup(ch, mapping); |
| 7854 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7855 | break; |
| 7856 | ++collendpos; |
| 7857 | continue; |
| 7858 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7859 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7860 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7861 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7862 | if (rep==NULL) |
| 7863 | return -1; |
| 7864 | else if (rep!=Py_None) { |
| 7865 | Py_DECREF(rep); |
| 7866 | break; |
| 7867 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7868 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7869 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7870 | } |
| 7871 | /* cache callback name lookup |
| 7872 | * (if not done yet, i.e. it's the first error) */ |
| 7873 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7874 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7875 | *known_errorHandler = 1; |
| 7876 | else if (!strcmp(errors, "replace")) |
| 7877 | *known_errorHandler = 2; |
| 7878 | else if (!strcmp(errors, "ignore")) |
| 7879 | *known_errorHandler = 3; |
| 7880 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7881 | *known_errorHandler = 4; |
| 7882 | else |
| 7883 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7884 | } |
| 7885 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7886 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7887 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7888 | return -1; |
| 7889 | case 2: /* replace */ |
| 7890 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7891 | x = charmapencode_output('?', mapping, res, respos); |
| 7892 | if (x==enc_EXCEPTION) { |
| 7893 | return -1; |
| 7894 | } |
| 7895 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7896 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7897 | return -1; |
| 7898 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7899 | } |
| 7900 | /* fall through */ |
| 7901 | case 3: /* ignore */ |
| 7902 | *inpos = collendpos; |
| 7903 | break; |
| 7904 | case 4: /* xmlcharrefreplace */ |
| 7905 | /* generate replacement (temporarily (mis)uses p) */ |
| 7906 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7907 | char buffer[2+29+1+1]; |
| 7908 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7909 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | for (cp = buffer; *cp; ++cp) { |
| 7911 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7912 | if (x==enc_EXCEPTION) |
| 7913 | return -1; |
| 7914 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7915 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7916 | return -1; |
| 7917 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7918 | } |
| 7919 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7920 | *inpos = collendpos; |
| 7921 | break; |
| 7922 | default: |
| 7923 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7924 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7925 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7926 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7927 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7928 | if (PyBytes_Check(repunicode)) { |
| 7929 | /* Directly copy bytes result to output. */ |
| 7930 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7931 | Py_ssize_t requiredsize; |
| 7932 | repsize = PyBytes_Size(repunicode); |
| 7933 | requiredsize = *respos + repsize; |
| 7934 | if (requiredsize > outsize) |
| 7935 | /* Make room for all additional bytes. */ |
| 7936 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7937 | Py_DECREF(repunicode); |
| 7938 | return -1; |
| 7939 | } |
| 7940 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7941 | PyBytes_AsString(repunicode), repsize); |
| 7942 | *respos += repsize; |
| 7943 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7944 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7945 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7946 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7947 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7948 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7949 | Py_DECREF(repunicode); |
| 7950 | return -1; |
| 7951 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 7952 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7953 | data = PyUnicode_DATA(repunicode); |
| 7954 | kind = PyUnicode_KIND(repunicode); |
| 7955 | for (index = 0; index < repsize; index++) { |
| 7956 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 7957 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7958 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7959 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7960 | return -1; |
| 7961 | } |
| 7962 | else if (x==enc_FAILED) { |
| 7963 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7964 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7965 | return -1; |
| 7966 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7967 | } |
| 7968 | *inpos = newpos; |
| 7969 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7970 | } |
| 7971 | return 0; |
| 7972 | } |
| 7973 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7974 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7975 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 7976 | PyObject *mapping, |
| 7977 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7978 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7979 | /* output object */ |
| 7980 | PyObject *res = NULL; |
| 7981 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7982 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7983 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7984 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7985 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7986 | PyObject *errorHandler = NULL; |
| 7987 | PyObject *exc = NULL; |
| 7988 | /* the following variable is used for caching string comparisons |
| 7989 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7990 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7991 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 7992 | void *data; |
| 7993 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7994 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7995 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7996 | return NULL; |
| 7997 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 7998 | data = PyUnicode_DATA(unicode); |
| 7999 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8001 | /* Default to Latin-1 */ |
| 8002 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8003 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8004 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8005 | /* allocate enough for a simple encoding without |
| 8006 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8007 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8008 | if (res == NULL) |
| 8009 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8010 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8011 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8012 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8013 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8014 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8015 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8016 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8017 | if (x==enc_EXCEPTION) /* error */ |
| 8018 | goto onError; |
| 8019 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8020 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8021 | &exc, |
| 8022 | &known_errorHandler, &errorHandler, errors, |
| 8023 | &res, &respos)) { |
| 8024 | goto onError; |
| 8025 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8026 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8027 | else |
| 8028 | /* done with this character => adjust input position */ |
| 8029 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8031 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8032 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8033 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8034 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8035 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8036 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8037 | Py_XDECREF(exc); |
| 8038 | Py_XDECREF(errorHandler); |
| 8039 | return res; |
| 8040 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8041 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8042 | Py_XDECREF(res); |
| 8043 | Py_XDECREF(exc); |
| 8044 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8045 | return NULL; |
| 8046 | } |
| 8047 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8048 | /* Deprecated */ |
| 8049 | PyObject * |
| 8050 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8051 | Py_ssize_t size, |
| 8052 | PyObject *mapping, |
| 8053 | const char *errors) |
| 8054 | { |
| 8055 | PyObject *result; |
| 8056 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8057 | if (unicode == NULL) |
| 8058 | return NULL; |
| 8059 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8060 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8061 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8062 | } |
| 8063 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8064 | PyObject * |
| 8065 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8066 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8067 | { |
| 8068 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8069 | PyErr_BadArgument(); |
| 8070 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8071 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8072 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8073 | } |
| 8074 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8075 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8076 | static void |
| 8077 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8078 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8079 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8080 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8081 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8082 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8083 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8084 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8085 | } |
| 8086 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8088 | goto onError; |
| 8089 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8090 | goto onError; |
| 8091 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8092 | goto onError; |
| 8093 | return; |
| 8094 | onError: |
| 8095 | Py_DECREF(*exceptionObject); |
| 8096 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8097 | } |
| 8098 | } |
| 8099 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8100 | /* error handling callback helper: |
| 8101 | build arguments, call the callback and check the arguments, |
| 8102 | put the result into newpos and return the replacement string, which |
| 8103 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8104 | static PyObject * |
| 8105 | unicode_translate_call_errorhandler(const char *errors, |
| 8106 | PyObject **errorHandler, |
| 8107 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8108 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8109 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8110 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8111 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8112 | 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] | 8113 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8114 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8115 | PyObject *restuple; |
| 8116 | PyObject *resunicode; |
| 8117 | |
| 8118 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8119 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8120 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8121 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8122 | } |
| 8123 | |
| 8124 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8125 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8126 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8128 | |
| 8129 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8130 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8131 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8133 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8134 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8135 | Py_DECREF(restuple); |
| 8136 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8137 | } |
| 8138 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8139 | &resunicode, &i_newpos)) { |
| 8140 | Py_DECREF(restuple); |
| 8141 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8142 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8143 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8144 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8145 | else |
| 8146 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8147 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8149 | Py_DECREF(restuple); |
| 8150 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8151 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8152 | Py_INCREF(resunicode); |
| 8153 | Py_DECREF(restuple); |
| 8154 | return resunicode; |
| 8155 | } |
| 8156 | |
| 8157 | /* Lookup the character ch in the mapping and put the result in result, |
| 8158 | which must be decrefed by the caller. |
| 8159 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8160 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8161 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8162 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8163 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8164 | PyObject *x; |
| 8165 | |
| 8166 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8167 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8168 | x = PyObject_GetItem(mapping, w); |
| 8169 | Py_DECREF(w); |
| 8170 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8171 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8172 | /* No mapping found means: use 1:1 mapping. */ |
| 8173 | PyErr_Clear(); |
| 8174 | *result = NULL; |
| 8175 | return 0; |
| 8176 | } else |
| 8177 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8178 | } |
| 8179 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | *result = x; |
| 8181 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8182 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8183 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8184 | long value = PyLong_AS_LONG(x); |
| 8185 | long max = PyUnicode_GetMax(); |
| 8186 | if (value < 0 || value > max) { |
| 8187 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8188 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8189 | Py_DECREF(x); |
| 8190 | return -1; |
| 8191 | } |
| 8192 | *result = x; |
| 8193 | return 0; |
| 8194 | } |
| 8195 | else if (PyUnicode_Check(x)) { |
| 8196 | *result = x; |
| 8197 | return 0; |
| 8198 | } |
| 8199 | else { |
| 8200 | /* wrong return value */ |
| 8201 | PyErr_SetString(PyExc_TypeError, |
| 8202 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8203 | Py_DECREF(x); |
| 8204 | return -1; |
| 8205 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8206 | } |
| 8207 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8208 | if not reallocate and adjust various state variables. |
| 8209 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8210 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8211 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8212 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8213 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8214 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8215 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8216 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8217 | /* exponentially overallocate to minimize reallocations */ |
| 8218 | if (requiredsize < 2 * oldsize) |
| 8219 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8220 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8221 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8222 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8223 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8224 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8225 | } |
| 8226 | return 0; |
| 8227 | } |
| 8228 | /* lookup the character, put the result in the output string and adjust |
| 8229 | various state variables. Return a new reference to the object that |
| 8230 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8231 | undefined (in which case no character was written). |
| 8232 | The called must decref result. |
| 8233 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8234 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8235 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8236 | PyObject *mapping, Py_UCS4 **output, |
| 8237 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8238 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8239 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8240 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8241 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8242 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8243 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8244 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8245 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8246 | } |
| 8247 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8249 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8250 | /* 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] | 8251 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8252 | } |
| 8253 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8254 | Py_ssize_t repsize; |
| 8255 | if (PyUnicode_READY(*res) == -1) |
| 8256 | return -1; |
| 8257 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8258 | if (repsize==1) { |
| 8259 | /* 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] | 8260 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8261 | } |
| 8262 | else if (repsize!=0) { |
| 8263 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8264 | Py_ssize_t requiredsize = *opos + |
| 8265 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8266 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8267 | Py_ssize_t i; |
| 8268 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8269 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8270 | for(i = 0; i < repsize; i++) |
| 8271 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8272 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8273 | } |
| 8274 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8275 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8276 | return 0; |
| 8277 | } |
| 8278 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8279 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8280 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8281 | PyObject *mapping, |
| 8282 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8283 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8284 | /* input object */ |
| 8285 | char *idata; |
| 8286 | Py_ssize_t size, i; |
| 8287 | int kind; |
| 8288 | /* output buffer */ |
| 8289 | Py_UCS4 *output = NULL; |
| 8290 | Py_ssize_t osize; |
| 8291 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8292 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8293 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8294 | char *reason = "character maps to <undefined>"; |
| 8295 | PyObject *errorHandler = NULL; |
| 8296 | PyObject *exc = NULL; |
| 8297 | /* the following variable is used for caching string comparisons |
| 8298 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8299 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8300 | int known_errorHandler = -1; |
| 8301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8302 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8303 | PyErr_BadArgument(); |
| 8304 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8305 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8306 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8307 | if (PyUnicode_READY(input) == -1) |
| 8308 | return NULL; |
| 8309 | idata = (char*)PyUnicode_DATA(input); |
| 8310 | kind = PyUnicode_KIND(input); |
| 8311 | size = PyUnicode_GET_LENGTH(input); |
| 8312 | i = 0; |
| 8313 | |
| 8314 | if (size == 0) { |
| 8315 | Py_INCREF(input); |
| 8316 | return input; |
| 8317 | } |
| 8318 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8319 | /* allocate enough for a simple 1:1 translation without |
| 8320 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8321 | osize = size; |
| 8322 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8323 | opos = 0; |
| 8324 | if (output == NULL) { |
| 8325 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8327 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8328 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8329 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8330 | /* try to encode it */ |
| 8331 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8332 | if (charmaptranslate_output(input, i, mapping, |
| 8333 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8334 | Py_XDECREF(x); |
| 8335 | goto onError; |
| 8336 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8337 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8338 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8339 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8340 | else { /* untranslatable character */ |
| 8341 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8342 | Py_ssize_t repsize; |
| 8343 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8344 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8345 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8346 | Py_ssize_t collstart = i; |
| 8347 | Py_ssize_t collend = i+1; |
| 8348 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8349 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8350 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8351 | while (collend < size) { |
| 8352 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8353 | goto onError; |
| 8354 | Py_XDECREF(x); |
| 8355 | if (x!=Py_None) |
| 8356 | break; |
| 8357 | ++collend; |
| 8358 | } |
| 8359 | /* cache callback name lookup |
| 8360 | * (if not done yet, i.e. it's the first error) */ |
| 8361 | if (known_errorHandler==-1) { |
| 8362 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8363 | known_errorHandler = 1; |
| 8364 | else if (!strcmp(errors, "replace")) |
| 8365 | known_errorHandler = 2; |
| 8366 | else if (!strcmp(errors, "ignore")) |
| 8367 | known_errorHandler = 3; |
| 8368 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8369 | known_errorHandler = 4; |
| 8370 | else |
| 8371 | known_errorHandler = 0; |
| 8372 | } |
| 8373 | switch (known_errorHandler) { |
| 8374 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8375 | make_translate_exception(&exc, |
| 8376 | input, collstart, collend, reason); |
| 8377 | if (exc != NULL) |
| 8378 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8379 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | case 2: /* replace */ |
| 8381 | /* 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] | 8382 | for (coll = collstart; coll<collend; coll++) |
| 8383 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8384 | /* fall through */ |
| 8385 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8386 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8387 | break; |
| 8388 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8389 | /* generate replacement (temporarily (mis)uses i) */ |
| 8390 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8391 | char buffer[2+29+1+1]; |
| 8392 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8393 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8394 | if (charmaptranslate_makespace(&output, &osize, |
| 8395 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8396 | goto onError; |
| 8397 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8398 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8399 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8400 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8401 | break; |
| 8402 | default: |
| 8403 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8404 | reason, input, &exc, |
| 8405 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8406 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8407 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8408 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8409 | Py_DECREF(repunicode); |
| 8410 | goto onError; |
| 8411 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8412 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8413 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8414 | if (charmaptranslate_makespace(&output, &osize, |
| 8415 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8416 | Py_DECREF(repunicode); |
| 8417 | goto onError; |
| 8418 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8419 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8420 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8421 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8422 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8423 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8424 | } |
| 8425 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8426 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8427 | if (!res) |
| 8428 | goto onError; |
| 8429 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8430 | Py_XDECREF(exc); |
| 8431 | Py_XDECREF(errorHandler); |
| 8432 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8433 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8434 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8435 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8436 | Py_XDECREF(exc); |
| 8437 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8438 | return NULL; |
| 8439 | } |
| 8440 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8441 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8442 | PyObject * |
| 8443 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8444 | Py_ssize_t size, |
| 8445 | PyObject *mapping, |
| 8446 | const char *errors) |
| 8447 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8448 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8449 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8450 | if (!unicode) |
| 8451 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8452 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8453 | Py_DECREF(unicode); |
| 8454 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8455 | } |
| 8456 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8457 | PyObject * |
| 8458 | PyUnicode_Translate(PyObject *str, |
| 8459 | PyObject *mapping, |
| 8460 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8461 | { |
| 8462 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | str = PyUnicode_FromObject(str); |
| 8465 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8466 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8467 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8468 | Py_DECREF(str); |
| 8469 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8470 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8471 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8472 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8473 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8474 | { |
| 8475 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8476 | called as a callback from fixup() which does it already. */ |
| 8477 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8478 | const int kind = PyUnicode_KIND(self); |
| 8479 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8480 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8481 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8482 | Py_ssize_t i; |
| 8483 | |
| 8484 | for (i = 0; i < len; ++i) { |
| 8485 | ch = PyUnicode_READ(kind, data, i); |
| 8486 | fixed = 0; |
| 8487 | if (ch > 127) { |
| 8488 | if (Py_UNICODE_ISSPACE(ch)) |
| 8489 | fixed = ' '; |
| 8490 | else { |
| 8491 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8492 | if (decimal >= 0) |
| 8493 | fixed = '0' + decimal; |
| 8494 | } |
| 8495 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8496 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8497 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8498 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8499 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8500 | else |
| 8501 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8502 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8503 | } |
| 8504 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8505 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8506 | } |
| 8507 | |
| 8508 | PyObject * |
| 8509 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8510 | { |
| 8511 | if (!PyUnicode_Check(unicode)) { |
| 8512 | PyErr_BadInternalCall(); |
| 8513 | return NULL; |
| 8514 | } |
| 8515 | if (PyUnicode_READY(unicode) == -1) |
| 8516 | return NULL; |
| 8517 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8518 | /* If the string is already ASCII, just return the same string */ |
| 8519 | Py_INCREF(unicode); |
| 8520 | return unicode; |
| 8521 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8522 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8523 | } |
| 8524 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8525 | PyObject * |
| 8526 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8527 | Py_ssize_t length) |
| 8528 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8529 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8530 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8531 | Py_UCS4 maxchar; |
| 8532 | enum PyUnicode_Kind kind; |
| 8533 | void *data; |
| 8534 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8535 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8536 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8537 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8538 | if (ch > 127) { |
| 8539 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8540 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8541 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8542 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8543 | } |
| 8544 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8545 | |
| 8546 | /* Copy to a new string */ |
| 8547 | decimal = PyUnicode_New(length, maxchar); |
| 8548 | if (decimal == NULL) |
| 8549 | return decimal; |
| 8550 | kind = PyUnicode_KIND(decimal); |
| 8551 | data = PyUnicode_DATA(decimal); |
| 8552 | /* Iterate over code points */ |
| 8553 | for (i = 0; i < length; i++) { |
| 8554 | Py_UNICODE ch = s[i]; |
| 8555 | if (ch > 127) { |
| 8556 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8557 | if (decimal >= 0) |
| 8558 | ch = '0' + decimal; |
| 8559 | } |
| 8560 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8561 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8562 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8563 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8564 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8565 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8566 | int |
| 8567 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8568 | Py_ssize_t length, |
| 8569 | char *output, |
| 8570 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8571 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8572 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8573 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8574 | enum PyUnicode_Kind kind; |
| 8575 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8576 | |
| 8577 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8578 | PyErr_BadArgument(); |
| 8579 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8580 | } |
| 8581 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8582 | unicode = PyUnicode_FromUnicode(s, length); |
| 8583 | if (unicode == NULL) |
| 8584 | return -1; |
| 8585 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8586 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8587 | Py_DECREF(unicode); |
| 8588 | return -1; |
| 8589 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8590 | kind = PyUnicode_KIND(unicode); |
| 8591 | data = PyUnicode_DATA(unicode); |
| 8592 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8593 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8594 | PyObject *exc; |
| 8595 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8596 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8597 | Py_ssize_t startpos; |
| 8598 | |
| 8599 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8600 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8601 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8602 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8603 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8604 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8605 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8606 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8607 | if (decimal >= 0) { |
| 8608 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8609 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | continue; |
| 8611 | } |
| 8612 | if (0 < ch && ch < 256) { |
| 8613 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8614 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8615 | continue; |
| 8616 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8617 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8618 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8619 | exc = NULL; |
| 8620 | raise_encode_exception(&exc, "decimal", unicode, |
| 8621 | startpos, startpos+1, |
| 8622 | "invalid decimal Unicode string"); |
| 8623 | Py_XDECREF(exc); |
| 8624 | Py_DECREF(unicode); |
| 8625 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8626 | } |
| 8627 | /* 0-terminate the output string */ |
| 8628 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8629 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8630 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8631 | } |
| 8632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | /* --- Helpers ------------------------------------------------------------ */ |
| 8634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8635 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8636 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8637 | Py_ssize_t start, |
| 8638 | Py_ssize_t end) |
| 8639 | { |
| 8640 | int kind1, kind2, kind; |
| 8641 | void *buf1, *buf2; |
| 8642 | Py_ssize_t len1, len2, result; |
| 8643 | |
| 8644 | kind1 = PyUnicode_KIND(s1); |
| 8645 | kind2 = PyUnicode_KIND(s2); |
| 8646 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8647 | buf1 = PyUnicode_DATA(s1); |
| 8648 | buf2 = PyUnicode_DATA(s2); |
| 8649 | if (kind1 != kind) |
| 8650 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8651 | if (!buf1) |
| 8652 | return -2; |
| 8653 | if (kind2 != kind) |
| 8654 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8655 | if (!buf2) { |
| 8656 | if (kind1 != kind) PyMem_Free(buf1); |
| 8657 | return -2; |
| 8658 | } |
| 8659 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8660 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8661 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8662 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8663 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8664 | case PyUnicode_1BYTE_KIND: |
| 8665 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8666 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8667 | else |
| 8668 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8669 | break; |
| 8670 | case PyUnicode_2BYTE_KIND: |
| 8671 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8672 | break; |
| 8673 | case PyUnicode_4BYTE_KIND: |
| 8674 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8675 | break; |
| 8676 | default: |
| 8677 | assert(0); result = -2; |
| 8678 | } |
| 8679 | } |
| 8680 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8681 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8682 | case PyUnicode_1BYTE_KIND: |
| 8683 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8684 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8685 | else |
| 8686 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8687 | break; |
| 8688 | case PyUnicode_2BYTE_KIND: |
| 8689 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8690 | break; |
| 8691 | case PyUnicode_4BYTE_KIND: |
| 8692 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8693 | break; |
| 8694 | default: |
| 8695 | assert(0); result = -2; |
| 8696 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8697 | } |
| 8698 | |
| 8699 | if (kind1 != kind) |
| 8700 | PyMem_Free(buf1); |
| 8701 | if (kind2 != kind) |
| 8702 | PyMem_Free(buf2); |
| 8703 | |
| 8704 | return result; |
| 8705 | } |
| 8706 | |
| 8707 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8708 | _PyUnicode_InsertThousandsGrouping( |
| 8709 | PyObject *unicode, Py_ssize_t index, |
| 8710 | Py_ssize_t n_buffer, |
| 8711 | void *digits, Py_ssize_t n_digits, |
| 8712 | Py_ssize_t min_width, |
| 8713 | const char *grouping, PyObject *thousands_sep, |
| 8714 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8715 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8716 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8717 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8718 | Py_ssize_t thousands_sep_len; |
| 8719 | Py_ssize_t len; |
| 8720 | |
| 8721 | if (unicode != NULL) { |
| 8722 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8723 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8724 | } |
| 8725 | else { |
| 8726 | kind = PyUnicode_1BYTE_KIND; |
| 8727 | data = NULL; |
| 8728 | } |
| 8729 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8730 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8731 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8732 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8733 | if (thousands_sep_kind < kind) { |
| 8734 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8735 | if (!thousands_sep_data) |
| 8736 | return -1; |
| 8737 | } |
| 8738 | else { |
| 8739 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8740 | if (!data) |
| 8741 | return -1; |
| 8742 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8743 | } |
| 8744 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8745 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8747 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8748 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8749 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8750 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8751 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8752 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8753 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8754 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8755 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8756 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8757 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8758 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8759 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8760 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8761 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8762 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8763 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8764 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8765 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8766 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8767 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8768 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8769 | break; |
| 8770 | default: |
| 8771 | assert(0); |
| 8772 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8773 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8774 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8775 | if (thousands_sep_kind < kind) |
| 8776 | PyMem_Free(thousands_sep_data); |
| 8777 | else |
| 8778 | PyMem_Free(data); |
| 8779 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8780 | if (unicode == NULL) { |
| 8781 | *maxchar = 127; |
| 8782 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8783 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8784 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8785 | } |
| 8786 | } |
| 8787 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8788 | } |
| 8789 | |
| 8790 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8791 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8792 | #define ADJUST_INDICES(start, end, len) \ |
| 8793 | if (end > len) \ |
| 8794 | end = len; \ |
| 8795 | else if (end < 0) { \ |
| 8796 | end += len; \ |
| 8797 | if (end < 0) \ |
| 8798 | end = 0; \ |
| 8799 | } \ |
| 8800 | if (start < 0) { \ |
| 8801 | start += len; \ |
| 8802 | if (start < 0) \ |
| 8803 | start = 0; \ |
| 8804 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8805 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8806 | Py_ssize_t |
| 8807 | PyUnicode_Count(PyObject *str, |
| 8808 | PyObject *substr, |
| 8809 | Py_ssize_t start, |
| 8810 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8811 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8812 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8813 | PyObject* str_obj; |
| 8814 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8815 | int kind1, kind2, kind; |
| 8816 | void *buf1 = NULL, *buf2 = NULL; |
| 8817 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8818 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8819 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8820 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8821 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8822 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8823 | if (!sub_obj) { |
| 8824 | Py_DECREF(str_obj); |
| 8825 | return -1; |
| 8826 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8827 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8828 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8829 | Py_DECREF(str_obj); |
| 8830 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8831 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8832 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 | kind1 = PyUnicode_KIND(str_obj); |
| 8834 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8835 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8836 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8837 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8838 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8839 | if (kind2 > kind) { |
| 8840 | Py_DECREF(sub_obj); |
| 8841 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8842 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8843 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8844 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8845 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8846 | if (!buf2) |
| 8847 | goto onError; |
| 8848 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8849 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8850 | |
| 8851 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8852 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8853 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8854 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8855 | result = asciilib_count( |
| 8856 | ((Py_UCS1*)buf1) + start, end - start, |
| 8857 | buf2, len2, PY_SSIZE_T_MAX |
| 8858 | ); |
| 8859 | else |
| 8860 | result = ucs1lib_count( |
| 8861 | ((Py_UCS1*)buf1) + start, end - start, |
| 8862 | buf2, len2, PY_SSIZE_T_MAX |
| 8863 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8864 | break; |
| 8865 | case PyUnicode_2BYTE_KIND: |
| 8866 | result = ucs2lib_count( |
| 8867 | ((Py_UCS2*)buf1) + start, end - start, |
| 8868 | buf2, len2, PY_SSIZE_T_MAX |
| 8869 | ); |
| 8870 | break; |
| 8871 | case PyUnicode_4BYTE_KIND: |
| 8872 | result = ucs4lib_count( |
| 8873 | ((Py_UCS4*)buf1) + start, end - start, |
| 8874 | buf2, len2, PY_SSIZE_T_MAX |
| 8875 | ); |
| 8876 | break; |
| 8877 | default: |
| 8878 | assert(0); result = 0; |
| 8879 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8880 | |
| 8881 | Py_DECREF(sub_obj); |
| 8882 | Py_DECREF(str_obj); |
| 8883 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8884 | if (kind2 != kind) |
| 8885 | PyMem_Free(buf2); |
| 8886 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8887 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8888 | onError: |
| 8889 | Py_DECREF(sub_obj); |
| 8890 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8891 | if (kind2 != kind && buf2) |
| 8892 | PyMem_Free(buf2); |
| 8893 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8894 | } |
| 8895 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8896 | Py_ssize_t |
| 8897 | PyUnicode_Find(PyObject *str, |
| 8898 | PyObject *sub, |
| 8899 | Py_ssize_t start, |
| 8900 | Py_ssize_t end, |
| 8901 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8902 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8903 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8904 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8905 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8906 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8907 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8908 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8909 | if (!sub) { |
| 8910 | Py_DECREF(str); |
| 8911 | return -2; |
| 8912 | } |
| 8913 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 8914 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8915 | Py_DECREF(str); |
| 8916 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8917 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8918 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8919 | result = any_find_slice(direction, |
| 8920 | str, sub, start, end |
| 8921 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8922 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8923 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8924 | Py_DECREF(sub); |
| 8925 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8926 | return result; |
| 8927 | } |
| 8928 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8929 | Py_ssize_t |
| 8930 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8931 | Py_ssize_t start, Py_ssize_t end, |
| 8932 | int direction) |
| 8933 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8934 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8935 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8936 | if (PyUnicode_READY(str) == -1) |
| 8937 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8938 | if (start < 0 || end < 0) { |
| 8939 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8940 | return -2; |
| 8941 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8942 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8943 | end = PyUnicode_GET_LENGTH(str); |
| 8944 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8945 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 8946 | kind, end-start, ch, direction); |
| 8947 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8948 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8949 | else |
| 8950 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 | } |
| 8952 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8953 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8954 | tailmatch(PyObject *self, |
| 8955 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8956 | Py_ssize_t start, |
| 8957 | Py_ssize_t end, |
| 8958 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8959 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 | int kind_self; |
| 8961 | int kind_sub; |
| 8962 | void *data_self; |
| 8963 | void *data_sub; |
| 8964 | Py_ssize_t offset; |
| 8965 | Py_ssize_t i; |
| 8966 | Py_ssize_t end_sub; |
| 8967 | |
| 8968 | if (PyUnicode_READY(self) == -1 || |
| 8969 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 8970 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8971 | |
| 8972 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8973 | return 1; |
| 8974 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8975 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8976 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8977 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8978 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8979 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8980 | kind_self = PyUnicode_KIND(self); |
| 8981 | data_self = PyUnicode_DATA(self); |
| 8982 | kind_sub = PyUnicode_KIND(substring); |
| 8983 | data_sub = PyUnicode_DATA(substring); |
| 8984 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8985 | |
| 8986 | if (direction > 0) |
| 8987 | offset = end; |
| 8988 | else |
| 8989 | offset = start; |
| 8990 | |
| 8991 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8992 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8993 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8994 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8995 | /* If both are of the same kind, memcmp is sufficient */ |
| 8996 | if (kind_self == kind_sub) { |
| 8997 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8998 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8999 | data_sub, |
| 9000 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9001 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9002 | } |
| 9003 | /* otherwise we have to compare each character by first accesing it */ |
| 9004 | else { |
| 9005 | /* We do not need to compare 0 and len(substring)-1 because |
| 9006 | the if statement above ensured already that they are equal |
| 9007 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9008 | for (i = 1; i < end_sub; ++i) { |
| 9009 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9010 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9011 | return 0; |
| 9012 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9013 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9014 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9015 | } |
| 9016 | |
| 9017 | return 0; |
| 9018 | } |
| 9019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9020 | Py_ssize_t |
| 9021 | PyUnicode_Tailmatch(PyObject *str, |
| 9022 | PyObject *substr, |
| 9023 | Py_ssize_t start, |
| 9024 | Py_ssize_t end, |
| 9025 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9026 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9027 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9029 | str = PyUnicode_FromObject(str); |
| 9030 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9031 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9032 | substr = PyUnicode_FromObject(substr); |
| 9033 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9034 | Py_DECREF(str); |
| 9035 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9036 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9037 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9038 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9039 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9040 | Py_DECREF(str); |
| 9041 | Py_DECREF(substr); |
| 9042 | return result; |
| 9043 | } |
| 9044 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9045 | /* Apply fixfct filter to the Unicode object self and return a |
| 9046 | reference to the modified object */ |
| 9047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9048 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9049 | fixup(PyObject *self, |
| 9050 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9051 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9052 | PyObject *u; |
| 9053 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9054 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9055 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9056 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9057 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9058 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9059 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9060 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9061 | /* fix functions return the new maximum character in a string, |
| 9062 | if the kind of the resulting unicode object does not change, |
| 9063 | everything is fine. Otherwise we need to change the string kind |
| 9064 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9065 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9066 | |
| 9067 | if (maxchar_new == 0) { |
| 9068 | /* no changes */; |
| 9069 | if (PyUnicode_CheckExact(self)) { |
| 9070 | Py_DECREF(u); |
| 9071 | Py_INCREF(self); |
| 9072 | return self; |
| 9073 | } |
| 9074 | else |
| 9075 | return u; |
| 9076 | } |
| 9077 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9078 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9079 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9080 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9081 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9082 | |
| 9083 | /* In case the maximum character changed, we need to |
| 9084 | convert the string to the new category. */ |
| 9085 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9086 | if (v == NULL) { |
| 9087 | Py_DECREF(u); |
| 9088 | return NULL; |
| 9089 | } |
| 9090 | if (maxchar_new > maxchar_old) { |
| 9091 | /* If the maxchar increased so that the kind changed, not all |
| 9092 | characters are representable anymore and we need to fix the |
| 9093 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9094 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9095 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9096 | maxchar_old = fixfct(v); |
| 9097 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9098 | } |
| 9099 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9100 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9101 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9102 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9103 | Py_DECREF(u); |
| 9104 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9105 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9106 | } |
| 9107 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9108 | static PyObject * |
| 9109 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9111 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9112 | char *resdata, *data = PyUnicode_DATA(self); |
| 9113 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9114 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9115 | res = PyUnicode_New(len, 127); |
| 9116 | if (res == NULL) |
| 9117 | return NULL; |
| 9118 | resdata = PyUnicode_DATA(res); |
| 9119 | if (lower) |
| 9120 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9121 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9122 | _Py_bytes_upper(resdata, data, len); |
| 9123 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9124 | } |
| 9125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9126 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9127 | 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] | 9128 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9129 | Py_ssize_t j; |
| 9130 | int final_sigma; |
| 9131 | Py_UCS4 c; |
| 9132 | /* 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] | 9133 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9134 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9135 | |
| 9136 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9137 | */ |
| 9138 | for (j = i - 1; j >= 0; j--) { |
| 9139 | c = PyUnicode_READ(kind, data, j); |
| 9140 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9141 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9142 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9143 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9144 | if (final_sigma) { |
| 9145 | for (j = i + 1; j < length; j++) { |
| 9146 | c = PyUnicode_READ(kind, data, j); |
| 9147 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9148 | break; |
| 9149 | } |
| 9150 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9151 | } |
| 9152 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9153 | } |
| 9154 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9155 | static int |
| 9156 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9157 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9158 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9159 | /* Obscure special case. */ |
| 9160 | if (c == 0x3A3) { |
| 9161 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9162 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9163 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9164 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9165 | } |
| 9166 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9167 | static Py_ssize_t |
| 9168 | 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] | 9169 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9170 | Py_ssize_t i, k = 0; |
| 9171 | int n_res, j; |
| 9172 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9173 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9174 | c = PyUnicode_READ(kind, data, 0); |
| 9175 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9176 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9177 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9178 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9179 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9180 | for (i = 1; i < length; i++) { |
| 9181 | c = PyUnicode_READ(kind, data, i); |
| 9182 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9183 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9184 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9185 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9186 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9187 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9188 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9189 | } |
| 9190 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9191 | static Py_ssize_t |
| 9192 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9193 | Py_ssize_t i, k = 0; |
| 9194 | |
| 9195 | for (i = 0; i < length; i++) { |
| 9196 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9197 | int n_res, j; |
| 9198 | if (Py_UNICODE_ISUPPER(c)) { |
| 9199 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9200 | } |
| 9201 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9202 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9203 | } |
| 9204 | else { |
| 9205 | n_res = 1; |
| 9206 | mapped[0] = c; |
| 9207 | } |
| 9208 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9209 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9210 | res[k++] = mapped[j]; |
| 9211 | } |
| 9212 | } |
| 9213 | return k; |
| 9214 | } |
| 9215 | |
| 9216 | static Py_ssize_t |
| 9217 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9218 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9219 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9220 | Py_ssize_t i, k = 0; |
| 9221 | |
| 9222 | for (i = 0; i < length; i++) { |
| 9223 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9224 | int n_res, j; |
| 9225 | if (lower) |
| 9226 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9227 | else |
| 9228 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9229 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9230 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9231 | res[k++] = mapped[j]; |
| 9232 | } |
| 9233 | } |
| 9234 | return k; |
| 9235 | } |
| 9236 | |
| 9237 | static Py_ssize_t |
| 9238 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9239 | { |
| 9240 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9241 | } |
| 9242 | |
| 9243 | static Py_ssize_t |
| 9244 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9245 | { |
| 9246 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9247 | } |
| 9248 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9249 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9250 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9251 | { |
| 9252 | Py_ssize_t i, k = 0; |
| 9253 | |
| 9254 | for (i = 0; i < length; i++) { |
| 9255 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9256 | Py_UCS4 mapped[3]; |
| 9257 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9258 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9259 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9260 | res[k++] = mapped[j]; |
| 9261 | } |
| 9262 | } |
| 9263 | return k; |
| 9264 | } |
| 9265 | |
| 9266 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9267 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9268 | { |
| 9269 | Py_ssize_t i, k = 0; |
| 9270 | int previous_is_cased; |
| 9271 | |
| 9272 | previous_is_cased = 0; |
| 9273 | for (i = 0; i < length; i++) { |
| 9274 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9275 | Py_UCS4 mapped[3]; |
| 9276 | int n_res, j; |
| 9277 | |
| 9278 | if (previous_is_cased) |
| 9279 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9280 | else |
| 9281 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9282 | |
| 9283 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9284 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9285 | res[k++] = mapped[j]; |
| 9286 | } |
| 9287 | |
| 9288 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9289 | } |
| 9290 | return k; |
| 9291 | } |
| 9292 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9293 | static PyObject * |
| 9294 | case_operation(PyObject *self, |
| 9295 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9296 | { |
| 9297 | PyObject *res = NULL; |
| 9298 | Py_ssize_t length, newlength = 0; |
| 9299 | int kind, outkind; |
| 9300 | void *data, *outdata; |
| 9301 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9302 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9303 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9304 | |
| 9305 | kind = PyUnicode_KIND(self); |
| 9306 | data = PyUnicode_DATA(self); |
| 9307 | length = PyUnicode_GET_LENGTH(self); |
| 9308 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9309 | if (tmp == NULL) |
| 9310 | return PyErr_NoMemory(); |
| 9311 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9312 | res = PyUnicode_New(newlength, maxchar); |
| 9313 | if (res == NULL) |
| 9314 | goto leave; |
| 9315 | tmpend = tmp + newlength; |
| 9316 | outdata = PyUnicode_DATA(res); |
| 9317 | outkind = PyUnicode_KIND(res); |
| 9318 | switch (outkind) { |
| 9319 | case PyUnicode_1BYTE_KIND: |
| 9320 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9321 | break; |
| 9322 | case PyUnicode_2BYTE_KIND: |
| 9323 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9324 | break; |
| 9325 | case PyUnicode_4BYTE_KIND: |
| 9326 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9327 | break; |
| 9328 | default: |
| 9329 | assert(0); |
| 9330 | break; |
| 9331 | } |
| 9332 | leave: |
| 9333 | PyMem_FREE(tmp); |
| 9334 | return res; |
| 9335 | } |
| 9336 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9337 | PyObject * |
| 9338 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9339 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9340 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9341 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9342 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9343 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9344 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9345 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9346 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9347 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9348 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9349 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9350 | int use_memcpy; |
| 9351 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9352 | PyObject *last_obj; |
| 9353 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9354 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9355 | fseq = PySequence_Fast(seq, ""); |
| 9356 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9357 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9358 | } |
| 9359 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9360 | /* NOTE: the following code can't call back into Python code, |
| 9361 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9362 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9363 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9364 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9365 | /* If empty sequence, return u"". */ |
| 9366 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9367 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9368 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9369 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9370 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9371 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9372 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9373 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9374 | if (seqlen == 1) { |
| 9375 | if (PyUnicode_CheckExact(items[0])) { |
| 9376 | res = items[0]; |
| 9377 | Py_INCREF(res); |
| 9378 | Py_DECREF(fseq); |
| 9379 | return res; |
| 9380 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9381 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9382 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9383 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9384 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9385 | /* Set up sep and seplen */ |
| 9386 | if (separator == NULL) { |
| 9387 | /* fall back to a blank space separator */ |
| 9388 | sep = PyUnicode_FromOrdinal(' '); |
| 9389 | if (!sep) |
| 9390 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9391 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9392 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9393 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9394 | else { |
| 9395 | if (!PyUnicode_Check(separator)) { |
| 9396 | PyErr_Format(PyExc_TypeError, |
| 9397 | "separator: expected str instance," |
| 9398 | " %.80s found", |
| 9399 | Py_TYPE(separator)->tp_name); |
| 9400 | goto onError; |
| 9401 | } |
| 9402 | if (PyUnicode_READY(separator)) |
| 9403 | goto onError; |
| 9404 | sep = separator; |
| 9405 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9406 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9407 | /* inc refcount to keep this code path symmetric with the |
| 9408 | above case of a blank separator */ |
| 9409 | Py_INCREF(sep); |
| 9410 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9411 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9412 | } |
| 9413 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9414 | /* There are at least two things to join, or else we have a subclass |
| 9415 | * of str in the sequence. |
| 9416 | * Do a pre-pass to figure out the total amount of space we'll |
| 9417 | * need (sz), and see whether all argument are strings. |
| 9418 | */ |
| 9419 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9420 | #ifdef Py_DEBUG |
| 9421 | use_memcpy = 0; |
| 9422 | #else |
| 9423 | use_memcpy = 1; |
| 9424 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9425 | for (i = 0; i < seqlen; i++) { |
| 9426 | const Py_ssize_t old_sz = sz; |
| 9427 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9428 | if (!PyUnicode_Check(item)) { |
| 9429 | PyErr_Format(PyExc_TypeError, |
| 9430 | "sequence item %zd: expected str instance," |
| 9431 | " %.80s found", |
| 9432 | i, Py_TYPE(item)->tp_name); |
| 9433 | goto onError; |
| 9434 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9435 | if (PyUnicode_READY(item) == -1) |
| 9436 | goto onError; |
| 9437 | sz += PyUnicode_GET_LENGTH(item); |
| 9438 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9439 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9440 | if (i != 0) |
| 9441 | sz += seplen; |
| 9442 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9443 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9444 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9445 | goto onError; |
| 9446 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9447 | if (use_memcpy && last_obj != NULL) { |
| 9448 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9449 | use_memcpy = 0; |
| 9450 | } |
| 9451 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9452 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9453 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9454 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9455 | if (res == NULL) |
| 9456 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9457 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9458 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9459 | #ifdef Py_DEBUG |
| 9460 | use_memcpy = 0; |
| 9461 | #else |
| 9462 | if (use_memcpy) { |
| 9463 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9464 | kind = PyUnicode_KIND(res); |
| 9465 | if (seplen != 0) |
| 9466 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9467 | } |
| 9468 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9469 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9470 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9471 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9472 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9473 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9474 | if (use_memcpy) { |
| 9475 | Py_MEMCPY(res_data, |
| 9476 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9477 | kind * seplen); |
| 9478 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9479 | } |
| 9480 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9481 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9482 | res_offset += seplen; |
| 9483 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9484 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9485 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9486 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9487 | if (use_memcpy) { |
| 9488 | Py_MEMCPY(res_data, |
| 9489 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9490 | kind * itemlen); |
| 9491 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9492 | } |
| 9493 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9494 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9495 | res_offset += itemlen; |
| 9496 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9497 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9498 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9499 | if (use_memcpy) |
| 9500 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9501 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9502 | else |
| 9503 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9504 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9505 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9506 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9507 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9508 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9509 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9510 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9511 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9512 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9513 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9514 | return NULL; |
| 9515 | } |
| 9516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9517 | #define FILL(kind, data, value, start, length) \ |
| 9518 | do { \ |
| 9519 | Py_ssize_t i_ = 0; \ |
| 9520 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9521 | switch ((kind)) { \ |
| 9522 | case PyUnicode_1BYTE_KIND: { \ |
| 9523 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9524 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9525 | break; \ |
| 9526 | } \ |
| 9527 | case PyUnicode_2BYTE_KIND: { \ |
| 9528 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9529 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9530 | break; \ |
| 9531 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9532 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9533 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9534 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9535 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9536 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9537 | } \ |
| 9538 | } \ |
| 9539 | } while (0) |
| 9540 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9541 | void |
| 9542 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9543 | Py_UCS4 fill_char) |
| 9544 | { |
| 9545 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9546 | const void *data = PyUnicode_DATA(unicode); |
| 9547 | assert(PyUnicode_IS_READY(unicode)); |
| 9548 | assert(unicode_modifiable(unicode)); |
| 9549 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9550 | assert(start >= 0); |
| 9551 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9552 | FILL(kind, data, fill_char, start, length); |
| 9553 | } |
| 9554 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9555 | Py_ssize_t |
| 9556 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9557 | Py_UCS4 fill_char) |
| 9558 | { |
| 9559 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9560 | |
| 9561 | if (!PyUnicode_Check(unicode)) { |
| 9562 | PyErr_BadInternalCall(); |
| 9563 | return -1; |
| 9564 | } |
| 9565 | if (PyUnicode_READY(unicode) == -1) |
| 9566 | return -1; |
| 9567 | if (unicode_check_modifiable(unicode)) |
| 9568 | return -1; |
| 9569 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9570 | if (start < 0) { |
| 9571 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9572 | return -1; |
| 9573 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9574 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9575 | PyErr_SetString(PyExc_ValueError, |
| 9576 | "fill character is bigger than " |
| 9577 | "the string maximum character"); |
| 9578 | return -1; |
| 9579 | } |
| 9580 | |
| 9581 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9582 | length = Py_MIN(maxlen, length); |
| 9583 | if (length <= 0) |
| 9584 | return 0; |
| 9585 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9586 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9587 | return length; |
| 9588 | } |
| 9589 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9590 | static PyObject * |
| 9591 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9592 | Py_ssize_t left, |
| 9593 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9594 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9595 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9596 | PyObject *u; |
| 9597 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9598 | int kind; |
| 9599 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9600 | |
| 9601 | if (left < 0) |
| 9602 | left = 0; |
| 9603 | if (right < 0) |
| 9604 | right = 0; |
| 9605 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9606 | if (left == 0 && right == 0) |
| 9607 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9608 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9609 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9610 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9611 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9612 | return NULL; |
| 9613 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9614 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9615 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9616 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9617 | if (!u) |
| 9618 | return NULL; |
| 9619 | |
| 9620 | kind = PyUnicode_KIND(u); |
| 9621 | data = PyUnicode_DATA(u); |
| 9622 | if (left) |
| 9623 | FILL(kind, data, fill, 0, left); |
| 9624 | if (right) |
| 9625 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9626 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9627 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9628 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9629 | } |
| 9630 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9631 | PyObject * |
| 9632 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9633 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9634 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9635 | |
| 9636 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9637 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9638 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9639 | if (PyUnicode_READY(string) == -1) { |
| 9640 | Py_DECREF(string); |
| 9641 | return NULL; |
| 9642 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9643 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9644 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9645 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9646 | if (PyUnicode_IS_ASCII(string)) |
| 9647 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9648 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9649 | PyUnicode_GET_LENGTH(string), keepends); |
| 9650 | else |
| 9651 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9652 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9653 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9654 | break; |
| 9655 | case PyUnicode_2BYTE_KIND: |
| 9656 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9657 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9658 | PyUnicode_GET_LENGTH(string), keepends); |
| 9659 | break; |
| 9660 | case PyUnicode_4BYTE_KIND: |
| 9661 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9662 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9663 | PyUnicode_GET_LENGTH(string), keepends); |
| 9664 | break; |
| 9665 | default: |
| 9666 | assert(0); |
| 9667 | list = 0; |
| 9668 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9669 | Py_DECREF(string); |
| 9670 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9671 | } |
| 9672 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9673 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9674 | split(PyObject *self, |
| 9675 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9676 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9677 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9678 | int kind1, kind2, kind; |
| 9679 | void *buf1, *buf2; |
| 9680 | Py_ssize_t len1, len2; |
| 9681 | PyObject* out; |
| 9682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9683 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9684 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9685 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9686 | if (PyUnicode_READY(self) == -1) |
| 9687 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9688 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9689 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9690 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9691 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9692 | if (PyUnicode_IS_ASCII(self)) |
| 9693 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9694 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9695 | PyUnicode_GET_LENGTH(self), maxcount |
| 9696 | ); |
| 9697 | else |
| 9698 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9699 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9700 | PyUnicode_GET_LENGTH(self), maxcount |
| 9701 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9702 | case PyUnicode_2BYTE_KIND: |
| 9703 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9704 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9705 | PyUnicode_GET_LENGTH(self), maxcount |
| 9706 | ); |
| 9707 | case PyUnicode_4BYTE_KIND: |
| 9708 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9709 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9710 | PyUnicode_GET_LENGTH(self), maxcount |
| 9711 | ); |
| 9712 | default: |
| 9713 | assert(0); |
| 9714 | return NULL; |
| 9715 | } |
| 9716 | |
| 9717 | if (PyUnicode_READY(substring) == -1) |
| 9718 | return NULL; |
| 9719 | |
| 9720 | kind1 = PyUnicode_KIND(self); |
| 9721 | kind2 = PyUnicode_KIND(substring); |
| 9722 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9723 | buf1 = PyUnicode_DATA(self); |
| 9724 | buf2 = PyUnicode_DATA(substring); |
| 9725 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9726 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9727 | if (!buf1) |
| 9728 | return NULL; |
| 9729 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9730 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9731 | if (!buf2) { |
| 9732 | if (kind1 != kind) PyMem_Free(buf1); |
| 9733 | return NULL; |
| 9734 | } |
| 9735 | len1 = PyUnicode_GET_LENGTH(self); |
| 9736 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9737 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9738 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9739 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9740 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9741 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9742 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9743 | else |
| 9744 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9745 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9746 | break; |
| 9747 | case PyUnicode_2BYTE_KIND: |
| 9748 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9749 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9750 | break; |
| 9751 | case PyUnicode_4BYTE_KIND: |
| 9752 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9753 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9754 | break; |
| 9755 | default: |
| 9756 | out = NULL; |
| 9757 | } |
| 9758 | if (kind1 != kind) |
| 9759 | PyMem_Free(buf1); |
| 9760 | if (kind2 != kind) |
| 9761 | PyMem_Free(buf2); |
| 9762 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9763 | } |
| 9764 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9765 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9766 | rsplit(PyObject *self, |
| 9767 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9768 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9769 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9770 | int kind1, kind2, kind; |
| 9771 | void *buf1, *buf2; |
| 9772 | Py_ssize_t len1, len2; |
| 9773 | PyObject* out; |
| 9774 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9775 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9776 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9777 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9778 | if (PyUnicode_READY(self) == -1) |
| 9779 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9780 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9781 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9782 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9783 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9784 | if (PyUnicode_IS_ASCII(self)) |
| 9785 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9786 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9787 | PyUnicode_GET_LENGTH(self), maxcount |
| 9788 | ); |
| 9789 | else |
| 9790 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9791 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9792 | PyUnicode_GET_LENGTH(self), maxcount |
| 9793 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9794 | case PyUnicode_2BYTE_KIND: |
| 9795 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9796 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9797 | PyUnicode_GET_LENGTH(self), maxcount |
| 9798 | ); |
| 9799 | case PyUnicode_4BYTE_KIND: |
| 9800 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9801 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9802 | PyUnicode_GET_LENGTH(self), maxcount |
| 9803 | ); |
| 9804 | default: |
| 9805 | assert(0); |
| 9806 | return NULL; |
| 9807 | } |
| 9808 | |
| 9809 | if (PyUnicode_READY(substring) == -1) |
| 9810 | return NULL; |
| 9811 | |
| 9812 | kind1 = PyUnicode_KIND(self); |
| 9813 | kind2 = PyUnicode_KIND(substring); |
| 9814 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9815 | buf1 = PyUnicode_DATA(self); |
| 9816 | buf2 = PyUnicode_DATA(substring); |
| 9817 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9818 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9819 | if (!buf1) |
| 9820 | return NULL; |
| 9821 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9822 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9823 | if (!buf2) { |
| 9824 | if (kind1 != kind) PyMem_Free(buf1); |
| 9825 | return NULL; |
| 9826 | } |
| 9827 | len1 = PyUnicode_GET_LENGTH(self); |
| 9828 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9829 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9830 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9831 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9832 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9833 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9834 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9835 | else |
| 9836 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9837 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9838 | break; |
| 9839 | case PyUnicode_2BYTE_KIND: |
| 9840 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9841 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9842 | break; |
| 9843 | case PyUnicode_4BYTE_KIND: |
| 9844 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9845 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9846 | break; |
| 9847 | default: |
| 9848 | out = NULL; |
| 9849 | } |
| 9850 | if (kind1 != kind) |
| 9851 | PyMem_Free(buf1); |
| 9852 | if (kind2 != kind) |
| 9853 | PyMem_Free(buf2); |
| 9854 | return out; |
| 9855 | } |
| 9856 | |
| 9857 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9858 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9859 | 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] | 9860 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9861 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9862 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9863 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9864 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9865 | else |
| 9866 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9867 | case PyUnicode_2BYTE_KIND: |
| 9868 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9869 | case PyUnicode_4BYTE_KIND: |
| 9870 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9871 | } |
| 9872 | assert(0); |
| 9873 | return -1; |
| 9874 | } |
| 9875 | |
| 9876 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9877 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9878 | 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] | 9879 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 9880 | switch (kind) { |
| 9881 | case PyUnicode_1BYTE_KIND: |
| 9882 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9883 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9884 | else |
| 9885 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9886 | case PyUnicode_2BYTE_KIND: |
| 9887 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9888 | case PyUnicode_4BYTE_KIND: |
| 9889 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9890 | } |
| 9891 | assert(0); |
| 9892 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9893 | } |
| 9894 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9895 | static void |
| 9896 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 9897 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 9898 | { |
| 9899 | int kind = PyUnicode_KIND(u); |
| 9900 | void *data = PyUnicode_DATA(u); |
| 9901 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 9902 | if (kind == PyUnicode_1BYTE_KIND) { |
| 9903 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 9904 | (Py_UCS1 *)data + len, |
| 9905 | u1, u2, maxcount); |
| 9906 | } |
| 9907 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 9908 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 9909 | (Py_UCS2 *)data + len, |
| 9910 | u1, u2, maxcount); |
| 9911 | } |
| 9912 | else { |
| 9913 | assert(kind == PyUnicode_4BYTE_KIND); |
| 9914 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 9915 | (Py_UCS4 *)data + len, |
| 9916 | u1, u2, maxcount); |
| 9917 | } |
| 9918 | } |
| 9919 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9920 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | replace(PyObject *self, PyObject *str1, |
| 9922 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9924 | PyObject *u; |
| 9925 | char *sbuf = PyUnicode_DATA(self); |
| 9926 | char *buf1 = PyUnicode_DATA(str1); |
| 9927 | char *buf2 = PyUnicode_DATA(str2); |
| 9928 | int srelease = 0, release1 = 0, release2 = 0; |
| 9929 | int skind = PyUnicode_KIND(self); |
| 9930 | int kind1 = PyUnicode_KIND(str1); |
| 9931 | int kind2 = PyUnicode_KIND(str2); |
| 9932 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9933 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9934 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9935 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9936 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9937 | |
| 9938 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9939 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9940 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9941 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9942 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9943 | if (str1 == str2) |
| 9944 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9945 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9946 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9947 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 9948 | if (maxchar < maxchar_str1) |
| 9949 | /* substring too wide to be present */ |
| 9950 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9951 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 9952 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 9953 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9954 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9955 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9957 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9958 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9959 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9960 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9961 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9962 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9963 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9964 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9965 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 9966 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9967 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9968 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9969 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 9970 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9971 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9972 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9973 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9974 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 9975 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 9976 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9977 | } |
| 9978 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9979 | int rkind = skind; |
| 9980 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9981 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9983 | if (kind1 < rkind) { |
| 9984 | /* widen substring */ |
| 9985 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9986 | if (!buf1) goto error; |
| 9987 | release1 = 1; |
| 9988 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9989 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9990 | if (i < 0) |
| 9991 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9992 | if (rkind > kind2) { |
| 9993 | /* widen replacement */ |
| 9994 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9995 | if (!buf2) goto error; |
| 9996 | release2 = 1; |
| 9997 | } |
| 9998 | else if (rkind < kind2) { |
| 9999 | /* widen self and buf1 */ |
| 10000 | rkind = kind2; |
| 10001 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10002 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10003 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10004 | if (!sbuf) goto error; |
| 10005 | srelease = 1; |
| 10006 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10007 | if (!buf1) goto error; |
| 10008 | release1 = 1; |
| 10009 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10010 | u = PyUnicode_New(slen, maxchar); |
| 10011 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10012 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10013 | assert(PyUnicode_KIND(u) == rkind); |
| 10014 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10015 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10016 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10017 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10018 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10019 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10020 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10021 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10022 | |
| 10023 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10024 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10025 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10026 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10027 | if (i == -1) |
| 10028 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10029 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10030 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10031 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10032 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10033 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10034 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10035 | } |
| 10036 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10037 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10038 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10039 | int rkind = skind; |
| 10040 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10041 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10042 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10043 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10044 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10045 | if (!buf1) goto error; |
| 10046 | release1 = 1; |
| 10047 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10048 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10049 | if (n == 0) |
| 10050 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10052 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10053 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10054 | if (!buf2) goto error; |
| 10055 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10056 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10057 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10058 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | rkind = kind2; |
| 10060 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10061 | if (!sbuf) goto error; |
| 10062 | srelease = 1; |
| 10063 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10064 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10065 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10066 | if (!buf1) goto error; |
| 10067 | release1 = 1; |
| 10068 | } |
| 10069 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10070 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10071 | 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] | 10072 | PyErr_SetString(PyExc_OverflowError, |
| 10073 | "replace string is too long"); |
| 10074 | goto error; |
| 10075 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10076 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10077 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10078 | _Py_INCREF_UNICODE_EMPTY(); |
| 10079 | if (!unicode_empty) |
| 10080 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10081 | u = unicode_empty; |
| 10082 | goto done; |
| 10083 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10084 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | PyErr_SetString(PyExc_OverflowError, |
| 10086 | "replace string is too long"); |
| 10087 | goto error; |
| 10088 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10089 | u = PyUnicode_New(new_size, maxchar); |
| 10090 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10091 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10092 | assert(PyUnicode_KIND(u) == rkind); |
| 10093 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10094 | ires = i = 0; |
| 10095 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10096 | while (n-- > 0) { |
| 10097 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10098 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10099 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10100 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10101 | if (j == -1) |
| 10102 | break; |
| 10103 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10104 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10105 | memcpy(res + rkind * ires, |
| 10106 | sbuf + rkind * i, |
| 10107 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10108 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10109 | } |
| 10110 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10111 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10112 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10113 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10114 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10115 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10116 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10117 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10118 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10119 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10120 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10121 | memcpy(res + rkind * ires, |
| 10122 | sbuf + rkind * i, |
| 10123 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10124 | } |
| 10125 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10126 | /* interleave */ |
| 10127 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10128 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10129 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10130 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10131 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10132 | if (--n <= 0) |
| 10133 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10134 | memcpy(res + rkind * ires, |
| 10135 | sbuf + rkind * i, |
| 10136 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10137 | ires++; |
| 10138 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10139 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10140 | memcpy(res + rkind * ires, |
| 10141 | sbuf + rkind * i, |
| 10142 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10143 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10144 | } |
| 10145 | |
| 10146 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10147 | unicode_adjust_maxchar(&u); |
| 10148 | if (u == NULL) |
| 10149 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10150 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10151 | |
| 10152 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10153 | if (srelease) |
| 10154 | PyMem_FREE(sbuf); |
| 10155 | if (release1) |
| 10156 | PyMem_FREE(buf1); |
| 10157 | if (release2) |
| 10158 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10159 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10160 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10161 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10162 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10163 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10164 | if (srelease) |
| 10165 | PyMem_FREE(sbuf); |
| 10166 | if (release1) |
| 10167 | PyMem_FREE(buf1); |
| 10168 | if (release2) |
| 10169 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10170 | return unicode_result_unchanged(self); |
| 10171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | error: |
| 10173 | if (srelease && sbuf) |
| 10174 | PyMem_FREE(sbuf); |
| 10175 | if (release1 && buf1) |
| 10176 | PyMem_FREE(buf1); |
| 10177 | if (release2 && buf2) |
| 10178 | PyMem_FREE(buf2); |
| 10179 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10180 | } |
| 10181 | |
| 10182 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10184 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10185 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10186 | \n\ |
| 10187 | 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] | 10188 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10189 | |
| 10190 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10191 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10192 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10193 | if (PyUnicode_READY(self) == -1) |
| 10194 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10195 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10196 | } |
| 10197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10198 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10199 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10200 | \n\ |
| 10201 | 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] | 10202 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10203 | |
| 10204 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10205 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10206 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10207 | if (PyUnicode_READY(self) == -1) |
| 10208 | return NULL; |
| 10209 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10210 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10211 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10212 | } |
| 10213 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10214 | PyDoc_STRVAR(casefold__doc__, |
| 10215 | "S.casefold() -> str\n\ |
| 10216 | \n\ |
| 10217 | Return a version of S suitable for caseless comparisons."); |
| 10218 | |
| 10219 | static PyObject * |
| 10220 | unicode_casefold(PyObject *self) |
| 10221 | { |
| 10222 | if (PyUnicode_READY(self) == -1) |
| 10223 | return NULL; |
| 10224 | if (PyUnicode_IS_ASCII(self)) |
| 10225 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10226 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10227 | } |
| 10228 | |
| 10229 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10230 | /* Argument converter. Coerces to a single unicode character */ |
| 10231 | |
| 10232 | static int |
| 10233 | convert_uc(PyObject *obj, void *addr) |
| 10234 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10235 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10236 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10237 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10238 | uniobj = PyUnicode_FromObject(obj); |
| 10239 | if (uniobj == NULL) { |
| 10240 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10241 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10242 | return 0; |
| 10243 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10244 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10245 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10246 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10247 | Py_DECREF(uniobj); |
| 10248 | return 0; |
| 10249 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10250 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10251 | Py_DECREF(uniobj); |
| 10252 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10253 | } |
| 10254 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10255 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10256 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10257 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10258 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10259 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10260 | |
| 10261 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10262 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10263 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10264 | Py_ssize_t marg, left; |
| 10265 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | Py_UCS4 fillchar = ' '; |
| 10267 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10268 | 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] | 10269 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10270 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10271 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10272 | return NULL; |
| 10273 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10274 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10275 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10276 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10277 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10278 | left = marg / 2 + (marg & width & 1); |
| 10279 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10280 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10281 | } |
| 10282 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10284 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10285 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10286 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10287 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10288 | #define COMPARE(TYPE1, TYPE2) \ |
| 10289 | do { \ |
| 10290 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10291 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10292 | TYPE1* end = p1 + len; \ |
| 10293 | Py_UCS4 c1, c2; \ |
| 10294 | for (; p1 != end; p1++, p2++) { \ |
| 10295 | c1 = *p1; \ |
| 10296 | c2 = *p2; \ |
| 10297 | if (c1 != c2) \ |
| 10298 | return (c1 < c2) ? -1 : 1; \ |
| 10299 | } \ |
| 10300 | } \ |
| 10301 | while (0) |
| 10302 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10303 | int kind1, kind2; |
| 10304 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10305 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10306 | |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10307 | /* a string is equal to itself */ |
| 10308 | if (str1 == str2) |
| 10309 | return 0; |
| 10310 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10311 | kind1 = PyUnicode_KIND(str1); |
| 10312 | kind2 = PyUnicode_KIND(str2); |
| 10313 | data1 = PyUnicode_DATA(str1); |
| 10314 | data2 = PyUnicode_DATA(str2); |
| 10315 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10316 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10317 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10318 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10319 | switch(kind1) { |
| 10320 | case PyUnicode_1BYTE_KIND: |
| 10321 | { |
| 10322 | switch(kind2) { |
| 10323 | case PyUnicode_1BYTE_KIND: |
| 10324 | { |
| 10325 | int cmp = memcmp(data1, data2, len); |
| 10326 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10327 | if (cmp < 0) |
| 10328 | return -1; |
| 10329 | if (cmp > 0) |
| 10330 | return 1; |
| 10331 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10332 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10333 | case PyUnicode_2BYTE_KIND: |
| 10334 | COMPARE(Py_UCS1, Py_UCS2); |
| 10335 | break; |
| 10336 | case PyUnicode_4BYTE_KIND: |
| 10337 | COMPARE(Py_UCS1, Py_UCS4); |
| 10338 | break; |
| 10339 | default: |
| 10340 | assert(0); |
| 10341 | } |
| 10342 | break; |
| 10343 | } |
| 10344 | case PyUnicode_2BYTE_KIND: |
| 10345 | { |
| 10346 | switch(kind2) { |
| 10347 | case PyUnicode_1BYTE_KIND: |
| 10348 | COMPARE(Py_UCS2, Py_UCS1); |
| 10349 | break; |
| 10350 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10351 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10352 | COMPARE(Py_UCS2, Py_UCS2); |
| 10353 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10354 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10355 | case PyUnicode_4BYTE_KIND: |
| 10356 | COMPARE(Py_UCS2, Py_UCS4); |
| 10357 | break; |
| 10358 | default: |
| 10359 | assert(0); |
| 10360 | } |
| 10361 | break; |
| 10362 | } |
| 10363 | case PyUnicode_4BYTE_KIND: |
| 10364 | { |
| 10365 | switch(kind2) { |
| 10366 | case PyUnicode_1BYTE_KIND: |
| 10367 | COMPARE(Py_UCS4, Py_UCS1); |
| 10368 | break; |
| 10369 | case PyUnicode_2BYTE_KIND: |
| 10370 | COMPARE(Py_UCS4, Py_UCS2); |
| 10371 | break; |
| 10372 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10373 | { |
| 10374 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10375 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10376 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10377 | if (cmp < 0) |
| 10378 | return -1; |
| 10379 | if (cmp > 0) |
| 10380 | return 1; |
| 10381 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10382 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10383 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10384 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10385 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10386 | default: |
| 10387 | assert(0); |
| 10388 | } |
| 10389 | break; |
| 10390 | } |
| 10391 | default: |
| 10392 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10393 | } |
| 10394 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10395 | if (len1 == len2) |
| 10396 | return 0; |
| 10397 | if (len1 < len2) |
| 10398 | return -1; |
| 10399 | else |
| 10400 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10401 | |
| 10402 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10403 | } |
| 10404 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10405 | static int |
| 10406 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10407 | { |
| 10408 | int kind; |
| 10409 | void *data1, *data2; |
| 10410 | Py_ssize_t len; |
| 10411 | int cmp; |
| 10412 | |
| 10413 | /* a string is equal to itself */ |
| 10414 | if (str1 == str2) |
| 10415 | return 1; |
| 10416 | |
| 10417 | len = PyUnicode_GET_LENGTH(str1); |
| 10418 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10419 | return 0; |
| 10420 | kind = PyUnicode_KIND(str1); |
| 10421 | if (PyUnicode_KIND(str2) != kind) |
| 10422 | return 0; |
| 10423 | data1 = PyUnicode_DATA(str1); |
| 10424 | data2 = PyUnicode_DATA(str2); |
| 10425 | |
| 10426 | cmp = memcmp(data1, data2, len * kind); |
| 10427 | return (cmp == 0); |
| 10428 | } |
| 10429 | |
| 10430 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10431 | int |
| 10432 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10433 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10434 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10435 | if (PyUnicode_READY(left) == -1 || |
| 10436 | PyUnicode_READY(right) == -1) |
| 10437 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10438 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10439 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10440 | PyErr_Format(PyExc_TypeError, |
| 10441 | "Can't compare %.100s and %.100s", |
| 10442 | left->ob_type->tp_name, |
| 10443 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10444 | return -1; |
| 10445 | } |
| 10446 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10447 | int |
| 10448 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10449 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10450 | Py_ssize_t i; |
| 10451 | int kind; |
| 10452 | void *data; |
| 10453 | Py_UCS4 chr; |
| 10454 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10455 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10456 | if (PyUnicode_READY(uni) == -1) |
| 10457 | return -1; |
| 10458 | kind = PyUnicode_KIND(uni); |
| 10459 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10460 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10461 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10462 | if (chr != str[i]) |
| 10463 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10464 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10465 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10466 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10467 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10468 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10469 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10470 | return 0; |
| 10471 | } |
| 10472 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10473 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10474 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10475 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10476 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10477 | PyObject * |
| 10478 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10479 | { |
| 10480 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10481 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10482 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10483 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10484 | Py_RETURN_NOTIMPLEMENTED; |
| 10485 | |
| 10486 | if (PyUnicode_READY(left) == -1 || |
| 10487 | PyUnicode_READY(right) == -1) |
| 10488 | return NULL; |
| 10489 | |
| 10490 | if (op == Py_EQ || op == Py_NE) { |
| 10491 | result = unicode_compare_eq(left, right); |
| 10492 | if (op == Py_EQ) |
| 10493 | v = TEST_COND(result); |
| 10494 | else |
| 10495 | v = TEST_COND(!result); |
| 10496 | } |
| 10497 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10498 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10499 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10500 | /* Convert the return value to a Boolean */ |
| 10501 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10502 | case Py_LE: |
| 10503 | v = TEST_COND(result <= 0); |
| 10504 | break; |
| 10505 | case Py_GE: |
| 10506 | v = TEST_COND(result >= 0); |
| 10507 | break; |
| 10508 | case Py_LT: |
| 10509 | v = TEST_COND(result == -1); |
| 10510 | break; |
| 10511 | case Py_GT: |
| 10512 | v = TEST_COND(result == 1); |
| 10513 | break; |
| 10514 | default: |
| 10515 | PyErr_BadArgument(); |
| 10516 | return NULL; |
| 10517 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10518 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10519 | Py_INCREF(v); |
| 10520 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10521 | } |
| 10522 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10523 | int |
| 10524 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10525 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10526 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10527 | int kind1, kind2, kind; |
| 10528 | void *buf1, *buf2; |
| 10529 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10530 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10531 | |
| 10532 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10533 | sub = PyUnicode_FromObject(element); |
| 10534 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10535 | PyErr_Format(PyExc_TypeError, |
| 10536 | "'in <string>' requires string as left operand, not %s", |
| 10537 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10538 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10539 | } |
| 10540 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10541 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10542 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10543 | Py_DECREF(sub); |
| 10544 | return -1; |
| 10545 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10546 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10547 | Py_DECREF(sub); |
| 10548 | Py_DECREF(str); |
| 10549 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10550 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10551 | kind1 = PyUnicode_KIND(str); |
| 10552 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10553 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10554 | buf1 = PyUnicode_DATA(str); |
| 10555 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10556 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10557 | if (kind2 > kind) { |
| 10558 | Py_DECREF(sub); |
| 10559 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10560 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10561 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10562 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10563 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10564 | if (!buf2) { |
| 10565 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10566 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10567 | return -1; |
| 10568 | } |
| 10569 | len1 = PyUnicode_GET_LENGTH(str); |
| 10570 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10571 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10572 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10573 | case PyUnicode_1BYTE_KIND: |
| 10574 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10575 | break; |
| 10576 | case PyUnicode_2BYTE_KIND: |
| 10577 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10578 | break; |
| 10579 | case PyUnicode_4BYTE_KIND: |
| 10580 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10581 | break; |
| 10582 | default: |
| 10583 | result = -1; |
| 10584 | assert(0); |
| 10585 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10586 | |
| 10587 | Py_DECREF(str); |
| 10588 | Py_DECREF(sub); |
| 10589 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10590 | if (kind2 != kind) |
| 10591 | PyMem_Free(buf2); |
| 10592 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10593 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10594 | } |
| 10595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10596 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10597 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10598 | PyObject * |
| 10599 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10600 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10601 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10602 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10603 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10604 | |
| 10605 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10606 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10607 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10608 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10609 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10610 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10611 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10612 | |
| 10613 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10614 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10615 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10616 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10617 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10618 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10619 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10620 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10621 | } |
| 10622 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10623 | u_len = PyUnicode_GET_LENGTH(u); |
| 10624 | v_len = PyUnicode_GET_LENGTH(v); |
| 10625 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10626 | PyErr_SetString(PyExc_OverflowError, |
| 10627 | "strings are too large to concat"); |
| 10628 | goto onError; |
| 10629 | } |
| 10630 | new_len = u_len + v_len; |
| 10631 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10632 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10633 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10634 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10636 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10637 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10638 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10639 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10640 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10641 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10642 | Py_DECREF(u); |
| 10643 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10644 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10645 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10646 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10647 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10648 | Py_XDECREF(u); |
| 10649 | Py_XDECREF(v); |
| 10650 | return NULL; |
| 10651 | } |
| 10652 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10653 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10654 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10655 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10656 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10657 | Py_UCS4 maxchar, maxchar2; |
| 10658 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10659 | |
| 10660 | if (p_left == NULL) { |
| 10661 | if (!PyErr_Occurred()) |
| 10662 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10663 | return; |
| 10664 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10665 | left = *p_left; |
Serhiy Storchaka | 6c83e73 | 2013-01-04 12:39:34 +0200 | [diff] [blame] | 10666 | if (right == NULL || left == NULL || !PyUnicode_Check(left)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10667 | if (!PyErr_Occurred()) |
| 10668 | PyErr_BadInternalCall(); |
| 10669 | goto error; |
| 10670 | } |
| 10671 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10672 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10673 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10674 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10675 | goto error; |
| 10676 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10677 | /* Shortcuts */ |
| 10678 | if (left == unicode_empty) { |
| 10679 | Py_DECREF(left); |
| 10680 | Py_INCREF(right); |
| 10681 | *p_left = right; |
| 10682 | return; |
| 10683 | } |
| 10684 | if (right == unicode_empty) |
| 10685 | return; |
| 10686 | |
| 10687 | left_len = PyUnicode_GET_LENGTH(left); |
| 10688 | right_len = PyUnicode_GET_LENGTH(right); |
| 10689 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10690 | PyErr_SetString(PyExc_OverflowError, |
| 10691 | "strings are too large to concat"); |
| 10692 | goto error; |
| 10693 | } |
| 10694 | new_len = left_len + right_len; |
| 10695 | |
| 10696 | if (unicode_modifiable(left) |
| 10697 | && PyUnicode_CheckExact(right) |
| 10698 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10699 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10700 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10701 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10702 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10703 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10704 | { |
| 10705 | /* append inplace */ |
| 10706 | if (unicode_resize(p_left, new_len) != 0) { |
| 10707 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10708 | * deallocated so it cannot be put back into |
| 10709 | * 'variable'. The MemoryError is raised when there |
| 10710 | * is no value in 'variable', which might (very |
| 10711 | * remotely) be a cause of incompatibilities. |
| 10712 | */ |
| 10713 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10714 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10715 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10716 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10717 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10718 | else { |
| 10719 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10720 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10721 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10722 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10723 | /* Concat the two Unicode strings */ |
| 10724 | res = PyUnicode_New(new_len, maxchar); |
| 10725 | if (res == NULL) |
| 10726 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10727 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10728 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10729 | Py_DECREF(left); |
| 10730 | *p_left = res; |
| 10731 | } |
| 10732 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10733 | return; |
| 10734 | |
| 10735 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10736 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10737 | } |
| 10738 | |
| 10739 | void |
| 10740 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10741 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10742 | PyUnicode_Append(pleft, right); |
| 10743 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10744 | } |
| 10745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10746 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10747 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10748 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10749 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10750 | 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] | 10751 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10752 | |
| 10753 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10754 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10755 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10756 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10757 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10758 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10759 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10760 | int kind1, kind2, kind; |
| 10761 | void *buf1, *buf2; |
| 10762 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10764 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10765 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10766 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10768 | kind1 = PyUnicode_KIND(self); |
| 10769 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10770 | if (kind2 > kind1) |
| 10771 | return PyLong_FromLong(0); |
| 10772 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10773 | buf1 = PyUnicode_DATA(self); |
| 10774 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10775 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10776 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10777 | if (!buf2) { |
| 10778 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10779 | return NULL; |
| 10780 | } |
| 10781 | len1 = PyUnicode_GET_LENGTH(self); |
| 10782 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10783 | |
| 10784 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10785 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10786 | case PyUnicode_1BYTE_KIND: |
| 10787 | iresult = ucs1lib_count( |
| 10788 | ((Py_UCS1*)buf1) + start, end - start, |
| 10789 | buf2, len2, PY_SSIZE_T_MAX |
| 10790 | ); |
| 10791 | break; |
| 10792 | case PyUnicode_2BYTE_KIND: |
| 10793 | iresult = ucs2lib_count( |
| 10794 | ((Py_UCS2*)buf1) + start, end - start, |
| 10795 | buf2, len2, PY_SSIZE_T_MAX |
| 10796 | ); |
| 10797 | break; |
| 10798 | case PyUnicode_4BYTE_KIND: |
| 10799 | iresult = ucs4lib_count( |
| 10800 | ((Py_UCS4*)buf1) + start, end - start, |
| 10801 | buf2, len2, PY_SSIZE_T_MAX |
| 10802 | ); |
| 10803 | break; |
| 10804 | default: |
| 10805 | assert(0); iresult = 0; |
| 10806 | } |
| 10807 | |
| 10808 | result = PyLong_FromSsize_t(iresult); |
| 10809 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10810 | if (kind2 != kind) |
| 10811 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10812 | |
| 10813 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10814 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10815 | return result; |
| 10816 | } |
| 10817 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10818 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10819 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10820 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10821 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10822 | 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] | 10823 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10824 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10825 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10826 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10827 | |
| 10828 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10829 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10830 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10831 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10832 | char *encoding = NULL; |
| 10833 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10834 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10835 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10836 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10837 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10838 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10839 | } |
| 10840 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10841 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10842 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10843 | \n\ |
| 10844 | 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] | 10845 | 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] | 10846 | |
| 10847 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10848 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10849 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10850 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10851 | Py_UCS4 ch; |
| 10852 | PyObject *u; |
| 10853 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10854 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10855 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10856 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10857 | |
| 10858 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10859 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10860 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10861 | if (PyUnicode_READY(self) == -1) |
| 10862 | return NULL; |
| 10863 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10864 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10865 | src_len = PyUnicode_GET_LENGTH(self); |
| 10866 | i = j = line_pos = 0; |
| 10867 | kind = PyUnicode_KIND(self); |
| 10868 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10869 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10870 | for (; i < src_len; i++) { |
| 10871 | ch = PyUnicode_READ(kind, src_data, i); |
| 10872 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10873 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10874 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10875 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10876 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10877 | goto overflow; |
| 10878 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10879 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10880 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10881 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10882 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10883 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10884 | goto overflow; |
| 10885 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10886 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10887 | if (ch == '\n' || ch == '\r') |
| 10888 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10889 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10890 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10891 | if (!found) |
| 10892 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10893 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10894 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10895 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10896 | if (!u) |
| 10897 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10898 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10900 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10901 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10902 | for (; i < src_len; i++) { |
| 10903 | ch = PyUnicode_READ(kind, src_data, i); |
| 10904 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10905 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10906 | incr = tabsize - (line_pos % tabsize); |
| 10907 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10908 | FILL(kind, dest_data, ' ', j, incr); |
| 10909 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10910 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10911 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10912 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10913 | line_pos++; |
| 10914 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10915 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10916 | if (ch == '\n' || ch == '\r') |
| 10917 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10918 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10919 | } |
| 10920 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10921 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10922 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10923 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10924 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10925 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10926 | } |
| 10927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10928 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10929 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10930 | \n\ |
| 10931 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10932 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10933 | arguments start and end are interpreted as in slice notation.\n\ |
| 10934 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10935 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10936 | |
| 10937 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10938 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10939 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10940 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10941 | Py_ssize_t start; |
| 10942 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10943 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10944 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10945 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10946 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10947 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10948 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10949 | if (PyUnicode_READY(self) == -1) |
| 10950 | return NULL; |
| 10951 | if (PyUnicode_READY(substring) == -1) |
| 10952 | return NULL; |
| 10953 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10954 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10955 | |
| 10956 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10957 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10958 | if (result == -2) |
| 10959 | return NULL; |
| 10960 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10961 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | } |
| 10963 | |
| 10964 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10965 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10967 | void *data; |
| 10968 | enum PyUnicode_Kind kind; |
| 10969 | Py_UCS4 ch; |
| 10970 | PyObject *res; |
| 10971 | |
| 10972 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 10973 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10974 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10975 | } |
| 10976 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 10977 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10978 | return NULL; |
| 10979 | } |
| 10980 | kind = PyUnicode_KIND(self); |
| 10981 | data = PyUnicode_DATA(self); |
| 10982 | ch = PyUnicode_READ(kind, data, index); |
| 10983 | if (ch < 256) |
| 10984 | return get_latin1_char(ch); |
| 10985 | |
| 10986 | res = PyUnicode_New(1, ch); |
| 10987 | if (res == NULL) |
| 10988 | return NULL; |
| 10989 | kind = PyUnicode_KIND(res); |
| 10990 | data = PyUnicode_DATA(res); |
| 10991 | PyUnicode_WRITE(kind, data, 0, ch); |
| 10992 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 10993 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | } |
| 10995 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10996 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10997 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10998 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10999 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11000 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11001 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11002 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11003 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11004 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11005 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11006 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11007 | if (_PyUnicode_HASH(self) != -1) |
| 11008 | return _PyUnicode_HASH(self); |
| 11009 | if (PyUnicode_READY(self) == -1) |
| 11010 | return -1; |
| 11011 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11012 | /* |
| 11013 | We make the hash of the empty string be 0, rather than using |
| 11014 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11015 | */ |
| 11016 | if (len == 0) { |
| 11017 | _PyUnicode_HASH(self) = 0; |
| 11018 | return 0; |
| 11019 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11020 | |
| 11021 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11022 | #define HASH(P) \ |
| 11023 | x ^= (Py_uhash_t) *P << 7; \ |
| 11024 | while (--len >= 0) \ |
| 11025 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11026 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11027 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11028 | switch (PyUnicode_KIND(self)) { |
| 11029 | case PyUnicode_1BYTE_KIND: { |
| 11030 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11031 | HASH(c); |
| 11032 | break; |
| 11033 | } |
| 11034 | case PyUnicode_2BYTE_KIND: { |
| 11035 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11036 | HASH(s); |
| 11037 | break; |
| 11038 | } |
| 11039 | default: { |
| 11040 | Py_UCS4 *l; |
| 11041 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11042 | "Impossible switch case in unicode_hash"); |
| 11043 | l = PyUnicode_4BYTE_DATA(self); |
| 11044 | HASH(l); |
| 11045 | break; |
| 11046 | } |
| 11047 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11048 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11049 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11050 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11051 | if (x == -1) |
| 11052 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11053 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11054 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11055 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11056 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11058 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11059 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11060 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11061 | 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] | 11062 | |
| 11063 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11064 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11065 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11066 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11067 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11068 | Py_ssize_t start; |
| 11069 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11070 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11071 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11072 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11073 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11074 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11075 | if (PyUnicode_READY(self) == -1) |
| 11076 | return NULL; |
| 11077 | if (PyUnicode_READY(substring) == -1) |
| 11078 | return NULL; |
| 11079 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11080 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11081 | |
| 11082 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11083 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11084 | if (result == -2) |
| 11085 | return NULL; |
| 11086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11087 | if (result < 0) { |
| 11088 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11089 | return NULL; |
| 11090 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11091 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11092 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11093 | } |
| 11094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11095 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11096 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11097 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11098 | 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] | 11099 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11100 | |
| 11101 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11102 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11103 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11104 | Py_ssize_t i, length; |
| 11105 | int kind; |
| 11106 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11107 | int cased; |
| 11108 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11109 | if (PyUnicode_READY(self) == -1) |
| 11110 | return NULL; |
| 11111 | length = PyUnicode_GET_LENGTH(self); |
| 11112 | kind = PyUnicode_KIND(self); |
| 11113 | data = PyUnicode_DATA(self); |
| 11114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11116 | if (length == 1) |
| 11117 | return PyBool_FromLong( |
| 11118 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11119 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11120 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11121 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11122 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11123 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11124 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11125 | for (i = 0; i < length; i++) { |
| 11126 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11127 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11128 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11129 | return PyBool_FromLong(0); |
| 11130 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11131 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11132 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11133 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11134 | } |
| 11135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11136 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11137 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11138 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11139 | 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] | 11140 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11141 | |
| 11142 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11143 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11144 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11145 | Py_ssize_t i, length; |
| 11146 | int kind; |
| 11147 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11148 | int cased; |
| 11149 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11150 | if (PyUnicode_READY(self) == -1) |
| 11151 | return NULL; |
| 11152 | length = PyUnicode_GET_LENGTH(self); |
| 11153 | kind = PyUnicode_KIND(self); |
| 11154 | data = PyUnicode_DATA(self); |
| 11155 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11156 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11157 | if (length == 1) |
| 11158 | return PyBool_FromLong( |
| 11159 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11160 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11161 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11162 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11163 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11166 | for (i = 0; i < length; i++) { |
| 11167 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11168 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11169 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11170 | return PyBool_FromLong(0); |
| 11171 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11172 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11174 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | } |
| 11176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11177 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11178 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11179 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11180 | Return True if S is a titlecased string and there is at least one\n\ |
| 11181 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11182 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11183 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | |
| 11185 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11186 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11187 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11188 | Py_ssize_t i, length; |
| 11189 | int kind; |
| 11190 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11191 | int cased, previous_is_cased; |
| 11192 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11193 | if (PyUnicode_READY(self) == -1) |
| 11194 | return NULL; |
| 11195 | length = PyUnicode_GET_LENGTH(self); |
| 11196 | kind = PyUnicode_KIND(self); |
| 11197 | data = PyUnicode_DATA(self); |
| 11198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11200 | if (length == 1) { |
| 11201 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11202 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11203 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11204 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11205 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11206 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11207 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11208 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11209 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | cased = 0; |
| 11211 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11212 | for (i = 0; i < length; i++) { |
| 11213 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11214 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11215 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11216 | if (previous_is_cased) |
| 11217 | return PyBool_FromLong(0); |
| 11218 | previous_is_cased = 1; |
| 11219 | cased = 1; |
| 11220 | } |
| 11221 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11222 | if (!previous_is_cased) |
| 11223 | return PyBool_FromLong(0); |
| 11224 | previous_is_cased = 1; |
| 11225 | cased = 1; |
| 11226 | } |
| 11227 | else |
| 11228 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11229 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11230 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11231 | } |
| 11232 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11233 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11234 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11236 | Return True if all characters in S are whitespace\n\ |
| 11237 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 | |
| 11239 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11240 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11241 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11242 | Py_ssize_t i, length; |
| 11243 | int kind; |
| 11244 | void *data; |
| 11245 | |
| 11246 | if (PyUnicode_READY(self) == -1) |
| 11247 | return NULL; |
| 11248 | length = PyUnicode_GET_LENGTH(self); |
| 11249 | kind = PyUnicode_KIND(self); |
| 11250 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11253 | if (length == 1) |
| 11254 | return PyBool_FromLong( |
| 11255 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11256 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11257 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11258 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11259 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11260 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11261 | for (i = 0; i < length; i++) { |
| 11262 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11263 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11264 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11265 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11266 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11267 | } |
| 11268 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11269 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11270 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11271 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11272 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11273 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11274 | |
| 11275 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11276 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11277 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11278 | Py_ssize_t i, length; |
| 11279 | int kind; |
| 11280 | void *data; |
| 11281 | |
| 11282 | if (PyUnicode_READY(self) == -1) |
| 11283 | return NULL; |
| 11284 | length = PyUnicode_GET_LENGTH(self); |
| 11285 | kind = PyUnicode_KIND(self); |
| 11286 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11287 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11288 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11289 | if (length == 1) |
| 11290 | return PyBool_FromLong( |
| 11291 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11292 | |
| 11293 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11294 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11295 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11296 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11297 | for (i = 0; i < length; i++) { |
| 11298 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11299 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11300 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11301 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11302 | } |
| 11303 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11304 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11305 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11306 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11307 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11308 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11309 | |
| 11310 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11311 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11312 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11313 | int kind; |
| 11314 | void *data; |
| 11315 | Py_ssize_t len, i; |
| 11316 | |
| 11317 | if (PyUnicode_READY(self) == -1) |
| 11318 | return NULL; |
| 11319 | |
| 11320 | kind = PyUnicode_KIND(self); |
| 11321 | data = PyUnicode_DATA(self); |
| 11322 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11323 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11324 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11325 | if (len == 1) { |
| 11326 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11327 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11328 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11329 | |
| 11330 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11331 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11332 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11333 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11334 | for (i = 0; i < len; i++) { |
| 11335 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11336 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11337 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11338 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11339 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11340 | } |
| 11341 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11342 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11343 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11344 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11345 | 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] | 11346 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11347 | |
| 11348 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11349 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11351 | Py_ssize_t i, length; |
| 11352 | int kind; |
| 11353 | void *data; |
| 11354 | |
| 11355 | if (PyUnicode_READY(self) == -1) |
| 11356 | return NULL; |
| 11357 | length = PyUnicode_GET_LENGTH(self); |
| 11358 | kind = PyUnicode_KIND(self); |
| 11359 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11360 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11361 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | if (length == 1) |
| 11363 | return PyBool_FromLong( |
| 11364 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11365 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11366 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11367 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11368 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11369 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11370 | for (i = 0; i < length; i++) { |
| 11371 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11372 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11374 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | } |
| 11376 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11377 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11378 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11379 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11380 | Return True if all characters in S are digits\n\ |
| 11381 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11382 | |
| 11383 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11384 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11385 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11386 | Py_ssize_t i, length; |
| 11387 | int kind; |
| 11388 | void *data; |
| 11389 | |
| 11390 | if (PyUnicode_READY(self) == -1) |
| 11391 | return NULL; |
| 11392 | length = PyUnicode_GET_LENGTH(self); |
| 11393 | kind = PyUnicode_KIND(self); |
| 11394 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11396 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11397 | if (length == 1) { |
| 11398 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11399 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11400 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11401 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11402 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11403 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11404 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11405 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11406 | for (i = 0; i < length; i++) { |
| 11407 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11408 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11410 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11411 | } |
| 11412 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11413 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11414 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11416 | 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] | 11417 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | |
| 11419 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11420 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11422 | Py_ssize_t i, length; |
| 11423 | int kind; |
| 11424 | void *data; |
| 11425 | |
| 11426 | if (PyUnicode_READY(self) == -1) |
| 11427 | return NULL; |
| 11428 | length = PyUnicode_GET_LENGTH(self); |
| 11429 | kind = PyUnicode_KIND(self); |
| 11430 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11432 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11433 | if (length == 1) |
| 11434 | return PyBool_FromLong( |
| 11435 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11436 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11437 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11438 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11439 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11440 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11441 | for (i = 0; i < length; i++) { |
| 11442 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11443 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11445 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11446 | } |
| 11447 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11448 | int |
| 11449 | PyUnicode_IsIdentifier(PyObject *self) |
| 11450 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11451 | int kind; |
| 11452 | void *data; |
| 11453 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11454 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11455 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11456 | if (PyUnicode_READY(self) == -1) { |
| 11457 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11458 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11459 | } |
| 11460 | |
| 11461 | /* Special case for empty strings */ |
| 11462 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11463 | return 0; |
| 11464 | kind = PyUnicode_KIND(self); |
| 11465 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11466 | |
| 11467 | /* PEP 3131 says that the first character must be in |
| 11468 | XID_Start and subsequent characters in XID_Continue, |
| 11469 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11470 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11471 | letters, digits, underscore). However, given the current |
| 11472 | definition of XID_Start and XID_Continue, it is sufficient |
| 11473 | to check just for these, except that _ must be allowed |
| 11474 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11475 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11476 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11477 | return 0; |
| 11478 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11479 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11480 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11481 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11482 | return 1; |
| 11483 | } |
| 11484 | |
| 11485 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11486 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11487 | \n\ |
| 11488 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11489 | to the language definition.\n\ |
| 11490 | \n\ |
| 11491 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11492 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11493 | |
| 11494 | static PyObject* |
| 11495 | unicode_isidentifier(PyObject *self) |
| 11496 | { |
| 11497 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11498 | } |
| 11499 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11500 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11501 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11502 | \n\ |
| 11503 | Return True if all characters in S are considered\n\ |
| 11504 | printable in repr() or S is empty, False otherwise."); |
| 11505 | |
| 11506 | static PyObject* |
| 11507 | unicode_isprintable(PyObject *self) |
| 11508 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11509 | Py_ssize_t i, length; |
| 11510 | int kind; |
| 11511 | void *data; |
| 11512 | |
| 11513 | if (PyUnicode_READY(self) == -1) |
| 11514 | return NULL; |
| 11515 | length = PyUnicode_GET_LENGTH(self); |
| 11516 | kind = PyUnicode_KIND(self); |
| 11517 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11518 | |
| 11519 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11520 | if (length == 1) |
| 11521 | return PyBool_FromLong( |
| 11522 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11523 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | for (i = 0; i < length; i++) { |
| 11525 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11526 | Py_RETURN_FALSE; |
| 11527 | } |
| 11528 | } |
| 11529 | Py_RETURN_TRUE; |
| 11530 | } |
| 11531 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11532 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11533 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11534 | \n\ |
| 11535 | 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] | 11536 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11537 | |
| 11538 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11539 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11540 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11541 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11542 | } |
| 11543 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11544 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11545 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11546 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11547 | if (PyUnicode_READY(self) == -1) |
| 11548 | return -1; |
| 11549 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11550 | } |
| 11551 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11552 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11553 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11554 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11555 | 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] | 11556 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11557 | |
| 11558 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11559 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11560 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11561 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | Py_UCS4 fillchar = ' '; |
| 11563 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11564 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | return NULL; |
| 11566 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11567 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11568 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11569 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11570 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11571 | return unicode_result_unchanged(self); |
| 11572 | |
| 11573 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11574 | } |
| 11575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11576 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11577 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11578 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11579 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11580 | |
| 11581 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11582 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11583 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11584 | if (PyUnicode_READY(self) == -1) |
| 11585 | return NULL; |
| 11586 | if (PyUnicode_IS_ASCII(self)) |
| 11587 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11588 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11589 | } |
| 11590 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11591 | #define LEFTSTRIP 0 |
| 11592 | #define RIGHTSTRIP 1 |
| 11593 | #define BOTHSTRIP 2 |
| 11594 | |
| 11595 | /* Arrays indexed by above */ |
| 11596 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11597 | |
| 11598 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11599 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11600 | /* externally visible for str.strip(unicode) */ |
| 11601 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11602 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11603 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11604 | void *data; |
| 11605 | int kind; |
| 11606 | Py_ssize_t i, j, len; |
| 11607 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11608 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11610 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11611 | return NULL; |
| 11612 | |
| 11613 | kind = PyUnicode_KIND(self); |
| 11614 | data = PyUnicode_DATA(self); |
| 11615 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11616 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11617 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11618 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11619 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11620 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11621 | i = 0; |
| 11622 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11623 | while (i < len) { |
| 11624 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11625 | if (!BLOOM(sepmask, ch)) |
| 11626 | break; |
| 11627 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11628 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11629 | i++; |
| 11630 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11631 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11632 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11633 | j = len; |
| 11634 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11635 | j--; |
| 11636 | while (j >= i) { |
| 11637 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11638 | if (!BLOOM(sepmask, ch)) |
| 11639 | break; |
| 11640 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11641 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11642 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11643 | } |
| 11644 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11645 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11646 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11647 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11648 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11649 | } |
| 11650 | |
| 11651 | PyObject* |
| 11652 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11653 | { |
| 11654 | unsigned char *data; |
| 11655 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11656 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11657 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11658 | if (PyUnicode_READY(self) == -1) |
| 11659 | return NULL; |
| 11660 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11661 | length = PyUnicode_GET_LENGTH(self); |
| 11662 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11663 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11664 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11665 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11666 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11667 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11668 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11669 | return NULL; |
| 11670 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11671 | if (start >= length || end < start) |
| 11672 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11673 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11674 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11675 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11676 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11677 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11678 | } |
| 11679 | else { |
| 11680 | kind = PyUnicode_KIND(self); |
| 11681 | data = PyUnicode_1BYTE_DATA(self); |
| 11682 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11683 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11684 | length); |
| 11685 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11686 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11687 | |
| 11688 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11689 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11690 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11691 | Py_ssize_t len, i, j; |
| 11692 | |
| 11693 | if (PyUnicode_READY(self) == -1) |
| 11694 | return NULL; |
| 11695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11697 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11698 | if (PyUnicode_IS_ASCII(self)) { |
| 11699 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 11700 | |
| 11701 | i = 0; |
| 11702 | if (striptype != RIGHTSTRIP) { |
| 11703 | while (i < len) { |
| 11704 | Py_UCS4 ch = data[i]; |
| 11705 | if (!_Py_ascii_whitespace[ch]) |
| 11706 | break; |
| 11707 | i++; |
| 11708 | } |
| 11709 | } |
| 11710 | |
| 11711 | j = len; |
| 11712 | if (striptype != LEFTSTRIP) { |
| 11713 | j--; |
| 11714 | while (j >= i) { |
| 11715 | Py_UCS4 ch = data[j]; |
| 11716 | if (!_Py_ascii_whitespace[ch]) |
| 11717 | break; |
| 11718 | j--; |
| 11719 | } |
| 11720 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11721 | } |
| 11722 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11723 | else { |
| 11724 | int kind = PyUnicode_KIND(self); |
| 11725 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11726 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11727 | i = 0; |
| 11728 | if (striptype != RIGHTSTRIP) { |
| 11729 | while (i < len) { |
| 11730 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11731 | if (!Py_UNICODE_ISSPACE(ch)) |
| 11732 | break; |
| 11733 | i++; |
| 11734 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 11735 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 11736 | |
| 11737 | j = len; |
| 11738 | if (striptype != LEFTSTRIP) { |
| 11739 | j--; |
| 11740 | while (j >= i) { |
| 11741 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11742 | if (!Py_UNICODE_ISSPACE(ch)) |
| 11743 | break; |
| 11744 | j--; |
| 11745 | } |
| 11746 | j++; |
| 11747 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11748 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11749 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11750 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11751 | } |
| 11752 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11753 | |
| 11754 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11755 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11756 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11757 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11758 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11759 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11760 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11761 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11762 | if (sep != NULL && sep != Py_None) { |
| 11763 | if (PyUnicode_Check(sep)) |
| 11764 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11765 | else { |
| 11766 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11767 | "%s arg must be None or str", |
| 11768 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11769 | return NULL; |
| 11770 | } |
| 11771 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11772 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11773 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11774 | } |
| 11775 | |
| 11776 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11777 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11778 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11779 | \n\ |
| 11780 | Return a copy of the string S with leading and trailing\n\ |
| 11781 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11782 | 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] | 11783 | |
| 11784 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11785 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11786 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11787 | if (PyTuple_GET_SIZE(args) == 0) |
| 11788 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11789 | else |
| 11790 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11791 | } |
| 11792 | |
| 11793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11794 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11795 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11796 | \n\ |
| 11797 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11798 | 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] | 11799 | |
| 11800 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11801 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11802 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11803 | if (PyTuple_GET_SIZE(args) == 0) |
| 11804 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11805 | else |
| 11806 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11807 | } |
| 11808 | |
| 11809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11810 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11811 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11812 | \n\ |
| 11813 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11814 | 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] | 11815 | |
| 11816 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11817 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11818 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11819 | if (PyTuple_GET_SIZE(args) == 0) |
| 11820 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11821 | else |
| 11822 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11823 | } |
| 11824 | |
| 11825 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11826 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11827 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11828 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11829 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11830 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11831 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 11832 | if (len < 1) |
| 11833 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11834 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11835 | /* no repeat, return original string */ |
| 11836 | if (len == 1) |
| 11837 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11838 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11839 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11840 | return NULL; |
| 11841 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11842 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11843 | PyErr_SetString(PyExc_OverflowError, |
| 11844 | "repeated string is too long"); |
| 11845 | return NULL; |
| 11846 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11847 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11848 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11849 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11850 | if (!u) |
| 11851 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11852 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11853 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11854 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11855 | const int kind = PyUnicode_KIND(str); |
| 11856 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11857 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11858 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11859 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11860 | } |
| 11861 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11862 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11863 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11864 | ucs2[n] = fill_char; |
| 11865 | } else { |
| 11866 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11867 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11868 | for (n = 0; n < len; ++n) |
| 11869 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11870 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11871 | } |
| 11872 | else { |
| 11873 | /* number of characters copied this far */ |
| 11874 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11875 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | char *to = (char *) PyUnicode_DATA(u); |
| 11877 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11878 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11879 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11880 | n = (done <= nchars-done) ? done : nchars-done; |
| 11881 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11882 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11883 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11884 | } |
| 11885 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11886 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11887 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11888 | } |
| 11889 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11890 | PyObject * |
| 11891 | PyUnicode_Replace(PyObject *obj, |
| 11892 | PyObject *subobj, |
| 11893 | PyObject *replobj, |
| 11894 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11895 | { |
| 11896 | PyObject *self; |
| 11897 | PyObject *str1; |
| 11898 | PyObject *str2; |
| 11899 | PyObject *result; |
| 11900 | |
| 11901 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11902 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11903 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11905 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11906 | Py_DECREF(self); |
| 11907 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11908 | } |
| 11909 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11910 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11911 | Py_DECREF(self); |
| 11912 | Py_DECREF(str1); |
| 11913 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11914 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11915 | if (PyUnicode_READY(self) == -1 || |
| 11916 | PyUnicode_READY(str1) == -1 || |
| 11917 | PyUnicode_READY(str2) == -1) |
| 11918 | result = NULL; |
| 11919 | else |
| 11920 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11921 | Py_DECREF(self); |
| 11922 | Py_DECREF(str1); |
| 11923 | Py_DECREF(str2); |
| 11924 | return result; |
| 11925 | } |
| 11926 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11927 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11928 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11929 | \n\ |
| 11930 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11931 | old replaced by new. If the optional argument count is\n\ |
| 11932 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11933 | |
| 11934 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11935 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11936 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11937 | PyObject *str1; |
| 11938 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11939 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11940 | PyObject *result; |
| 11941 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11942 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11944 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11945 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11946 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11947 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11948 | return NULL; |
| 11949 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11950 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11951 | Py_DECREF(str1); |
| 11952 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11953 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11954 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11955 | result = NULL; |
| 11956 | else |
| 11957 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11958 | |
| 11959 | Py_DECREF(str1); |
| 11960 | Py_DECREF(str2); |
| 11961 | return result; |
| 11962 | } |
| 11963 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11964 | static PyObject * |
| 11965 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11966 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11967 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11968 | Py_ssize_t isize; |
| 11969 | Py_ssize_t osize, squote, dquote, i, o; |
| 11970 | Py_UCS4 max, quote; |
| 11971 | int ikind, okind; |
| 11972 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11973 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11974 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11975 | return NULL; |
| 11976 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11977 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11978 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11979 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11980 | /* Compute length of output, quote characters, and |
| 11981 | maximum character */ |
| 11982 | osize = 2; /* quotes */ |
| 11983 | max = 127; |
| 11984 | squote = dquote = 0; |
| 11985 | ikind = PyUnicode_KIND(unicode); |
| 11986 | for (i = 0; i < isize; i++) { |
| 11987 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11988 | switch (ch) { |
| 11989 | case '\'': squote++; osize++; break; |
| 11990 | case '"': dquote++; osize++; break; |
| 11991 | case '\\': case '\t': case '\r': case '\n': |
| 11992 | osize += 2; break; |
| 11993 | default: |
| 11994 | /* Fast-path ASCII */ |
| 11995 | if (ch < ' ' || ch == 0x7f) |
| 11996 | osize += 4; /* \xHH */ |
| 11997 | else if (ch < 0x7f) |
| 11998 | osize++; |
| 11999 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 12000 | osize++; |
| 12001 | max = ch > max ? ch : max; |
| 12002 | } |
| 12003 | else if (ch < 0x100) |
| 12004 | osize += 4; /* \xHH */ |
| 12005 | else if (ch < 0x10000) |
| 12006 | osize += 6; /* \uHHHH */ |
| 12007 | else |
| 12008 | osize += 10; /* \uHHHHHHHH */ |
| 12009 | } |
| 12010 | } |
| 12011 | |
| 12012 | quote = '\''; |
| 12013 | if (squote) { |
| 12014 | if (dquote) |
| 12015 | /* Both squote and dquote present. Use squote, |
| 12016 | and escape them */ |
| 12017 | osize += squote; |
| 12018 | else |
| 12019 | quote = '"'; |
| 12020 | } |
| 12021 | |
| 12022 | repr = PyUnicode_New(osize, max); |
| 12023 | if (repr == NULL) |
| 12024 | return NULL; |
| 12025 | okind = PyUnicode_KIND(repr); |
| 12026 | odata = PyUnicode_DATA(repr); |
| 12027 | |
| 12028 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12029 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12030 | |
| 12031 | for (i = 0, o = 1; i < isize; i++) { |
| 12032 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12033 | |
| 12034 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12035 | if ((ch == quote) || (ch == '\\')) { |
| 12036 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12037 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12038 | continue; |
| 12039 | } |
| 12040 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12041 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12042 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12043 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12044 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12045 | } |
| 12046 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12047 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12048 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12049 | } |
| 12050 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12051 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12052 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12053 | } |
| 12054 | |
| 12055 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12056 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12057 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12058 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12059 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12060 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12061 | } |
| 12062 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12063 | /* Copy ASCII characters as-is */ |
| 12064 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12065 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12066 | } |
| 12067 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12068 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12069 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12070 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12071 | (categories Z* and C* except ASCII space) |
| 12072 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12073 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12074 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12075 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12076 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12077 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12078 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12079 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12080 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12081 | /* Map 16-bit characters to '\uxxxx' */ |
| 12082 | else if (ch <= 0xffff) { |
| 12083 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12084 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12085 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12086 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12087 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12088 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12089 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12090 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12091 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12092 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12093 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12094 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12095 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12096 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12097 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12098 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12099 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12100 | } |
| 12101 | } |
| 12102 | /* Copy characters as-is */ |
| 12103 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12104 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12105 | } |
| 12106 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12107 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12108 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12109 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12110 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12111 | } |
| 12112 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12113 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12114 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12115 | \n\ |
| 12116 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12117 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12118 | arguments start and end are interpreted as in slice notation.\n\ |
| 12119 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12120 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12121 | |
| 12122 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12123 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12124 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12125 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12126 | Py_ssize_t start; |
| 12127 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12128 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12129 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12130 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12131 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12132 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12133 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12134 | if (PyUnicode_READY(self) == -1) |
| 12135 | return NULL; |
| 12136 | if (PyUnicode_READY(substring) == -1) |
| 12137 | return NULL; |
| 12138 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12139 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12140 | |
| 12141 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12142 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12143 | if (result == -2) |
| 12144 | return NULL; |
| 12145 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12146 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12147 | } |
| 12148 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12149 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12150 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12151 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12152 | 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] | 12153 | |
| 12154 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12155 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12156 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12157 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12158 | Py_ssize_t start; |
| 12159 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12160 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12161 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12162 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12163 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12164 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12165 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12166 | if (PyUnicode_READY(self) == -1) |
| 12167 | return NULL; |
| 12168 | if (PyUnicode_READY(substring) == -1) |
| 12169 | return NULL; |
| 12170 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12171 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12172 | |
| 12173 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12174 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12175 | if (result == -2) |
| 12176 | return NULL; |
| 12177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12178 | if (result < 0) { |
| 12179 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12180 | return NULL; |
| 12181 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12183 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12184 | } |
| 12185 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12186 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12187 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12188 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12189 | 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] | 12190 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | |
| 12192 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12193 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12194 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12195 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12196 | Py_UCS4 fillchar = ' '; |
| 12197 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12198 | 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] | 12199 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12200 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12201 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12202 | return NULL; |
| 12203 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12204 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12205 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12206 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12207 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12208 | } |
| 12209 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12210 | PyObject * |
| 12211 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | { |
| 12213 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12215 | s = PyUnicode_FromObject(s); |
| 12216 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12217 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12218 | if (sep != NULL) { |
| 12219 | sep = PyUnicode_FromObject(sep); |
| 12220 | if (sep == NULL) { |
| 12221 | Py_DECREF(s); |
| 12222 | return NULL; |
| 12223 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12224 | } |
| 12225 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12226 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12227 | |
| 12228 | Py_DECREF(s); |
| 12229 | Py_XDECREF(sep); |
| 12230 | return result; |
| 12231 | } |
| 12232 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12233 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12234 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12235 | \n\ |
| 12236 | Return a list of the words in S, using sep as the\n\ |
| 12237 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12238 | 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] | 12239 | whitespace string is a separator and empty strings are\n\ |
| 12240 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12241 | |
| 12242 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12243 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12244 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12245 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12246 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12247 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12248 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12249 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12250 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12251 | return NULL; |
| 12252 | |
| 12253 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12254 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12255 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12256 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12258 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12259 | } |
| 12260 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12261 | PyObject * |
| 12262 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12263 | { |
| 12264 | PyObject* str_obj; |
| 12265 | PyObject* sep_obj; |
| 12266 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12267 | int kind1, kind2, kind; |
| 12268 | void *buf1 = NULL, *buf2 = NULL; |
| 12269 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12270 | |
| 12271 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12272 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12273 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12274 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12275 | if (!sep_obj) { |
| 12276 | Py_DECREF(str_obj); |
| 12277 | return NULL; |
| 12278 | } |
| 12279 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12280 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12281 | Py_DECREF(str_obj); |
| 12282 | return NULL; |
| 12283 | } |
| 12284 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12285 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12286 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12287 | kind = Py_MAX(kind1, kind2); |
| 12288 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12289 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12290 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12291 | if (!buf1) |
| 12292 | goto onError; |
| 12293 | buf2 = PyUnicode_DATA(sep_obj); |
| 12294 | if (kind2 != kind) |
| 12295 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12296 | if (!buf2) |
| 12297 | goto onError; |
| 12298 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12299 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12300 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12301 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12302 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12303 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12304 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12305 | else |
| 12306 | 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] | 12307 | break; |
| 12308 | case PyUnicode_2BYTE_KIND: |
| 12309 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12310 | break; |
| 12311 | case PyUnicode_4BYTE_KIND: |
| 12312 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12313 | break; |
| 12314 | default: |
| 12315 | assert(0); |
| 12316 | out = 0; |
| 12317 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12318 | |
| 12319 | Py_DECREF(sep_obj); |
| 12320 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12321 | if (kind1 != kind) |
| 12322 | PyMem_Free(buf1); |
| 12323 | if (kind2 != kind) |
| 12324 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12325 | |
| 12326 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12327 | onError: |
| 12328 | Py_DECREF(sep_obj); |
| 12329 | Py_DECREF(str_obj); |
| 12330 | if (kind1 != kind && buf1) |
| 12331 | PyMem_Free(buf1); |
| 12332 | if (kind2 != kind && buf2) |
| 12333 | PyMem_Free(buf2); |
| 12334 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12335 | } |
| 12336 | |
| 12337 | |
| 12338 | PyObject * |
| 12339 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12340 | { |
| 12341 | PyObject* str_obj; |
| 12342 | PyObject* sep_obj; |
| 12343 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12344 | int kind1, kind2, kind; |
| 12345 | void *buf1 = NULL, *buf2 = NULL; |
| 12346 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12347 | |
| 12348 | str_obj = PyUnicode_FromObject(str_in); |
| 12349 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12350 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12351 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12352 | if (!sep_obj) { |
| 12353 | Py_DECREF(str_obj); |
| 12354 | return NULL; |
| 12355 | } |
| 12356 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12357 | kind1 = PyUnicode_KIND(str_in); |
| 12358 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12359 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12360 | buf1 = PyUnicode_DATA(str_in); |
| 12361 | if (kind1 != kind) |
| 12362 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12363 | if (!buf1) |
| 12364 | goto onError; |
| 12365 | buf2 = PyUnicode_DATA(sep_obj); |
| 12366 | if (kind2 != kind) |
| 12367 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12368 | if (!buf2) |
| 12369 | goto onError; |
| 12370 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12371 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12372 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12373 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12374 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12375 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12376 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12377 | else |
| 12378 | 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] | 12379 | break; |
| 12380 | case PyUnicode_2BYTE_KIND: |
| 12381 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12382 | break; |
| 12383 | case PyUnicode_4BYTE_KIND: |
| 12384 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12385 | break; |
| 12386 | default: |
| 12387 | assert(0); |
| 12388 | out = 0; |
| 12389 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12390 | |
| 12391 | Py_DECREF(sep_obj); |
| 12392 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12393 | if (kind1 != kind) |
| 12394 | PyMem_Free(buf1); |
| 12395 | if (kind2 != kind) |
| 12396 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12397 | |
| 12398 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12399 | onError: |
| 12400 | Py_DECREF(sep_obj); |
| 12401 | Py_DECREF(str_obj); |
| 12402 | if (kind1 != kind && buf1) |
| 12403 | PyMem_Free(buf1); |
| 12404 | if (kind2 != kind && buf2) |
| 12405 | PyMem_Free(buf2); |
| 12406 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12407 | } |
| 12408 | |
| 12409 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12410 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12411 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12412 | 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] | 12413 | 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] | 12414 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12415 | |
| 12416 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12417 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12418 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12419 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12420 | } |
| 12421 | |
| 12422 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12423 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12424 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12425 | 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] | 12426 | 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] | 12427 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12428 | |
| 12429 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12430 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12431 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12432 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12433 | } |
| 12434 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12435 | PyObject * |
| 12436 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12437 | { |
| 12438 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12439 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12440 | s = PyUnicode_FromObject(s); |
| 12441 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12442 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12443 | if (sep != NULL) { |
| 12444 | sep = PyUnicode_FromObject(sep); |
| 12445 | if (sep == NULL) { |
| 12446 | Py_DECREF(s); |
| 12447 | return NULL; |
| 12448 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12449 | } |
| 12450 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12451 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12452 | |
| 12453 | Py_DECREF(s); |
| 12454 | Py_XDECREF(sep); |
| 12455 | return result; |
| 12456 | } |
| 12457 | |
| 12458 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12459 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12460 | \n\ |
| 12461 | Return a list of the words in S, using sep as the\n\ |
| 12462 | delimiter string, starting at the end of the string and\n\ |
| 12463 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12464 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12465 | is a separator."); |
| 12466 | |
| 12467 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12468 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12469 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12470 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12471 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12472 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12473 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12474 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12475 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12476 | return NULL; |
| 12477 | |
| 12478 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12479 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12480 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12481 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12482 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12483 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12484 | } |
| 12485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12486 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12487 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12488 | \n\ |
| 12489 | 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] | 12490 | 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] | 12491 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12492 | |
| 12493 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12494 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12495 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12496 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12497 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12498 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12499 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12500 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12501 | return NULL; |
| 12502 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12503 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12504 | } |
| 12505 | |
| 12506 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12507 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12508 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12509 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12510 | } |
| 12511 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12512 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12513 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12514 | \n\ |
| 12515 | 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] | 12516 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12517 | |
| 12518 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12519 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12520 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12521 | if (PyUnicode_READY(self) == -1) |
| 12522 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12523 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12524 | } |
| 12525 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12526 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12527 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12528 | \n\ |
| 12529 | Return a translation table usable for str.translate().\n\ |
| 12530 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12531 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12532 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12533 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12534 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12535 | character at the same position in y. If there is a third argument, it\n\ |
| 12536 | must be a string, whose characters will be mapped to None in the result."); |
| 12537 | |
| 12538 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12539 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12540 | { |
| 12541 | PyObject *x, *y = NULL, *z = NULL; |
| 12542 | PyObject *new = NULL, *key, *value; |
| 12543 | Py_ssize_t i = 0; |
| 12544 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12545 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12546 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12547 | return NULL; |
| 12548 | new = PyDict_New(); |
| 12549 | if (!new) |
| 12550 | return NULL; |
| 12551 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12552 | int x_kind, y_kind, z_kind; |
| 12553 | void *x_data, *y_data, *z_data; |
| 12554 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12555 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12556 | if (!PyUnicode_Check(x)) { |
| 12557 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12558 | "be a string if there is a second argument"); |
| 12559 | goto err; |
| 12560 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12561 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12562 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12563 | "arguments must have equal length"); |
| 12564 | goto err; |
| 12565 | } |
| 12566 | /* 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] | 12567 | x_kind = PyUnicode_KIND(x); |
| 12568 | y_kind = PyUnicode_KIND(y); |
| 12569 | x_data = PyUnicode_DATA(x); |
| 12570 | y_data = PyUnicode_DATA(y); |
| 12571 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12572 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12573 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12574 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12575 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12576 | if (!value) { |
| 12577 | Py_DECREF(key); |
| 12578 | goto err; |
| 12579 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12580 | res = PyDict_SetItem(new, key, value); |
| 12581 | Py_DECREF(key); |
| 12582 | Py_DECREF(value); |
| 12583 | if (res < 0) |
| 12584 | goto err; |
| 12585 | } |
| 12586 | /* create entries for deleting chars in z */ |
| 12587 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12588 | z_kind = PyUnicode_KIND(z); |
| 12589 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12590 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12591 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12592 | if (!key) |
| 12593 | goto err; |
| 12594 | res = PyDict_SetItem(new, key, Py_None); |
| 12595 | Py_DECREF(key); |
| 12596 | if (res < 0) |
| 12597 | goto err; |
| 12598 | } |
| 12599 | } |
| 12600 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12601 | int kind; |
| 12602 | void *data; |
| 12603 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12604 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12605 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12606 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12607 | "to maketrans it must be a dict"); |
| 12608 | goto err; |
| 12609 | } |
| 12610 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12611 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12612 | if (PyUnicode_Check(key)) { |
| 12613 | /* convert string keys to integer keys */ |
| 12614 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12615 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12616 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12617 | "table must be of length 1"); |
| 12618 | goto err; |
| 12619 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12620 | kind = PyUnicode_KIND(key); |
| 12621 | data = PyUnicode_DATA(key); |
| 12622 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12623 | if (!newkey) |
| 12624 | goto err; |
| 12625 | res = PyDict_SetItem(new, newkey, value); |
| 12626 | Py_DECREF(newkey); |
| 12627 | if (res < 0) |
| 12628 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12629 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12630 | /* just keep integer keys */ |
| 12631 | if (PyDict_SetItem(new, key, value) < 0) |
| 12632 | goto err; |
| 12633 | } else { |
| 12634 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12635 | "be strings or integers"); |
| 12636 | goto err; |
| 12637 | } |
| 12638 | } |
| 12639 | } |
| 12640 | return new; |
| 12641 | err: |
| 12642 | Py_DECREF(new); |
| 12643 | return NULL; |
| 12644 | } |
| 12645 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12646 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12647 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12648 | \n\ |
| 12649 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12650 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12651 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12652 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12653 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12654 | |
| 12655 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12656 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12658 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12659 | } |
| 12660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12661 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12662 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12663 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12664 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12665 | |
| 12666 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12667 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12668 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12669 | if (PyUnicode_READY(self) == -1) |
| 12670 | return NULL; |
| 12671 | if (PyUnicode_IS_ASCII(self)) |
| 12672 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12673 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12674 | } |
| 12675 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12676 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12677 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12678 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12679 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12680 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12681 | |
| 12682 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12683 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12684 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12685 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12686 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12687 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12688 | int kind; |
| 12689 | void *data; |
| 12690 | Py_UCS4 chr; |
| 12691 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12692 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12693 | return NULL; |
| 12694 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12695 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12696 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12697 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12698 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12699 | return unicode_result_unchanged(self); |
| 12700 | |
| 12701 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12702 | |
| 12703 | u = pad(self, fill, 0, '0'); |
| 12704 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12705 | if (u == NULL) |
| 12706 | return NULL; |
| 12707 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12708 | kind = PyUnicode_KIND(u); |
| 12709 | data = PyUnicode_DATA(u); |
| 12710 | chr = PyUnicode_READ(kind, data, fill); |
| 12711 | |
| 12712 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12713 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12714 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12715 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12716 | } |
| 12717 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12718 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12719 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12720 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12721 | |
| 12722 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12723 | static PyObject * |
| 12724 | unicode__decimal2ascii(PyObject *self) |
| 12725 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12726 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12727 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12728 | #endif |
| 12729 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12730 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12731 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12732 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12733 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12734 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12735 | With optional end, stop comparing S at that position.\n\ |
| 12736 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12737 | |
| 12738 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12739 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12740 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12741 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12742 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12743 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12744 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12745 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12746 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12747 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12748 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12749 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12750 | if (PyTuple_Check(subobj)) { |
| 12751 | Py_ssize_t i; |
| 12752 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12753 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12754 | if (substring == NULL) |
| 12755 | return NULL; |
| 12756 | result = tailmatch(self, substring, start, end, -1); |
| 12757 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12758 | if (result == -1) |
| 12759 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12760 | if (result) { |
| 12761 | Py_RETURN_TRUE; |
| 12762 | } |
| 12763 | } |
| 12764 | /* nothing matched */ |
| 12765 | Py_RETURN_FALSE; |
| 12766 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12767 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12768 | if (substring == NULL) { |
| 12769 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12770 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12771 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12772 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12773 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12774 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12775 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12776 | if (result == -1) |
| 12777 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12778 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12779 | } |
| 12780 | |
| 12781 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12782 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12783 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12784 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12785 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12786 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12787 | With optional end, stop comparing S at that position.\n\ |
| 12788 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12789 | |
| 12790 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12791 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12792 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12793 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12794 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12795 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12796 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12797 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12798 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12799 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12800 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12801 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12802 | if (PyTuple_Check(subobj)) { |
| 12803 | Py_ssize_t i; |
| 12804 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12805 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12806 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12807 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12808 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12809 | result = tailmatch(self, substring, start, end, +1); |
| 12810 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12811 | if (result == -1) |
| 12812 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12813 | if (result) { |
| 12814 | Py_RETURN_TRUE; |
| 12815 | } |
| 12816 | } |
| 12817 | Py_RETURN_FALSE; |
| 12818 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12819 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12820 | if (substring == NULL) { |
| 12821 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12822 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12823 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12824 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12825 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12826 | result = tailmatch(self, substring, start, end, +1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12827 | if (result == -1) |
| 12828 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12829 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12830 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12831 | } |
| 12832 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12833 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12834 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12835 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12836 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12837 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12838 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12839 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12840 | } |
| 12841 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12842 | void |
| 12843 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12844 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12845 | memset(writer, 0, sizeof(*writer)); |
| 12846 | #ifdef Py_DEBUG |
| 12847 | writer->kind = 5; /* invalid kind */ |
| 12848 | #endif |
| 12849 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12850 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12851 | } |
| 12852 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12853 | int |
| 12854 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12855 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12856 | { |
| 12857 | Py_ssize_t newlen; |
| 12858 | PyObject *newbuffer; |
| 12859 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12860 | assert(length > 0); |
| 12861 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12862 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12863 | PyErr_NoMemory(); |
| 12864 | return -1; |
| 12865 | } |
| 12866 | newlen = writer->pos + length; |
| 12867 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12868 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12869 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12870 | /* overallocate 25% to limit the number of resize */ |
| 12871 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12872 | newlen += newlen / 4; |
| 12873 | if (newlen < writer->min_length) |
| 12874 | newlen = writer->min_length; |
| 12875 | } |
| 12876 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12877 | if (writer->buffer == NULL) |
| 12878 | return -1; |
| 12879 | _PyUnicodeWriter_Update(writer); |
| 12880 | return 0; |
| 12881 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12882 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12883 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12884 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12885 | /* overallocate 25% to limit the number of resize */ |
| 12886 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12887 | newlen += newlen / 4; |
| 12888 | if (newlen < writer->min_length) |
| 12889 | newlen = writer->min_length; |
| 12890 | } |
| 12891 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12892 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12893 | /* resize + widen */ |
| 12894 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12895 | if (newbuffer == NULL) |
| 12896 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12897 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12898 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12899 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12900 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12901 | } |
| 12902 | else { |
| 12903 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12904 | if (newbuffer == NULL) |
| 12905 | return -1; |
| 12906 | } |
| 12907 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12908 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12909 | } |
| 12910 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12911 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12912 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12913 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12914 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12915 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12916 | writer->buffer, 0, writer->pos); |
| 12917 | Py_DECREF(writer->buffer); |
| 12918 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12919 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12920 | } |
| 12921 | return 0; |
| 12922 | } |
| 12923 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 12924 | Py_LOCAL_INLINE(int) |
| 12925 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 12926 | { |
| 12927 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 12928 | return -1; |
| 12929 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 12930 | writer->pos++; |
| 12931 | return 0; |
| 12932 | } |
| 12933 | |
| 12934 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 12935 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 12936 | { |
| 12937 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 12938 | } |
| 12939 | |
| 12940 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12941 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12942 | { |
| 12943 | Py_UCS4 maxchar; |
| 12944 | Py_ssize_t len; |
| 12945 | |
| 12946 | if (PyUnicode_READY(str) == -1) |
| 12947 | return -1; |
| 12948 | len = PyUnicode_GET_LENGTH(str); |
| 12949 | if (len == 0) |
| 12950 | return 0; |
| 12951 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12952 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12953 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12954 | Py_INCREF(str); |
| 12955 | writer->buffer = str; |
| 12956 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12957 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12958 | writer->size = 0; |
| 12959 | writer->pos += len; |
| 12960 | return 0; |
| 12961 | } |
| 12962 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12963 | return -1; |
| 12964 | } |
| 12965 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12966 | str, 0, len); |
| 12967 | writer->pos += len; |
| 12968 | return 0; |
| 12969 | } |
| 12970 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 12971 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 12972 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 12973 | Py_ssize_t start, Py_ssize_t end) |
| 12974 | { |
| 12975 | Py_UCS4 maxchar; |
| 12976 | Py_ssize_t len; |
| 12977 | |
| 12978 | if (PyUnicode_READY(str) == -1) |
| 12979 | return -1; |
| 12980 | |
| 12981 | assert(0 <= start); |
| 12982 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 12983 | assert(start <= end); |
| 12984 | |
| 12985 | if (end == 0) |
| 12986 | return 0; |
| 12987 | |
| 12988 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 12989 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 12990 | |
| 12991 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 12992 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 12993 | else |
| 12994 | maxchar = writer->maxchar; |
| 12995 | len = end - start; |
| 12996 | |
| 12997 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 12998 | return -1; |
| 12999 | |
| 13000 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13001 | str, start, len); |
| 13002 | writer->pos += len; |
| 13003 | return 0; |
| 13004 | } |
| 13005 | |
| 13006 | int |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13007 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) |
| 13008 | { |
| 13009 | Py_UCS4 maxchar; |
| 13010 | |
| 13011 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13012 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13013 | return -1; |
| 13014 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13015 | writer->pos += len; |
| 13016 | return 0; |
| 13017 | } |
| 13018 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13019 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13020 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13021 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13022 | if (writer->pos == 0) { |
| 13023 | Py_XDECREF(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13024 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13025 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13026 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13027 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 13028 | return writer->buffer; |
| 13029 | } |
| 13030 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13031 | PyObject *newbuffer; |
| 13032 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13033 | if (newbuffer == NULL) { |
| 13034 | Py_DECREF(writer->buffer); |
| 13035 | return NULL; |
| 13036 | } |
| 13037 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13038 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 13039 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 2cb16aa | 2013-03-06 19:28:37 +0100 | [diff] [blame] | 13040 | return unicode_result_ready(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13041 | } |
| 13042 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13043 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13044 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13045 | { |
| 13046 | Py_CLEAR(writer->buffer); |
| 13047 | } |
| 13048 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13049 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13050 | |
| 13051 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13052 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13053 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13054 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13055 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13056 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13057 | PyDoc_STRVAR(format_map__doc__, |
| 13058 | "S.format_map(mapping) -> str\n\ |
| 13059 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13060 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13061 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13062 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13063 | static PyObject * |
| 13064 | unicode__format__(PyObject* self, PyObject* args) |
| 13065 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13066 | PyObject *format_spec; |
| 13067 | _PyUnicodeWriter writer; |
| 13068 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13069 | |
| 13070 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13071 | return NULL; |
| 13072 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13073 | if (PyUnicode_READY(self) == -1) |
| 13074 | return NULL; |
| 13075 | _PyUnicodeWriter_Init(&writer, 0); |
| 13076 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13077 | self, format_spec, 0, |
| 13078 | PyUnicode_GET_LENGTH(format_spec)); |
| 13079 | if (ret == -1) { |
| 13080 | _PyUnicodeWriter_Dealloc(&writer); |
| 13081 | return NULL; |
| 13082 | } |
| 13083 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13084 | } |
| 13085 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13086 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13087 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13088 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13089 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13090 | |
| 13091 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13092 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13093 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13094 | Py_ssize_t size; |
| 13095 | |
| 13096 | /* If it's a compact object, account for base structure + |
| 13097 | character data. */ |
| 13098 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13099 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13100 | else if (PyUnicode_IS_COMPACT(v)) |
| 13101 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13102 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13103 | else { |
| 13104 | /* If it is a two-block object, account for base object, and |
| 13105 | for character block if present. */ |
| 13106 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13107 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13108 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13109 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13110 | } |
| 13111 | /* 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] | 13112 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13113 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13114 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13115 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13116 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13117 | |
| 13118 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13119 | } |
| 13120 | |
| 13121 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13122 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13123 | |
| 13124 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13125 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13126 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13127 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13128 | if (!copy) |
| 13129 | return NULL; |
| 13130 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13131 | } |
| 13132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13133 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13134 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13135 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13136 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13137 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13138 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13139 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13140 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13141 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13142 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13143 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13144 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13145 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13146 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13147 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13148 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13149 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13150 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13151 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13152 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13153 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13154 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13155 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13156 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13157 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13158 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13159 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13160 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13161 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13162 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13163 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13164 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13165 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13166 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13167 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13168 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13169 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13170 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13171 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13172 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13173 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13174 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13175 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13176 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13177 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13178 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13179 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13180 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13181 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13182 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13183 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13184 | #endif |
| 13185 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13186 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13187 | {NULL, NULL} |
| 13188 | }; |
| 13189 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13190 | static PyObject * |
| 13191 | unicode_mod(PyObject *v, PyObject *w) |
| 13192 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13193 | if (!PyUnicode_Check(v)) |
| 13194 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13195 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13196 | } |
| 13197 | |
| 13198 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13199 | 0, /*nb_add*/ |
| 13200 | 0, /*nb_subtract*/ |
| 13201 | 0, /*nb_multiply*/ |
| 13202 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13203 | }; |
| 13204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13205 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13206 | (lenfunc) unicode_length, /* sq_length */ |
| 13207 | PyUnicode_Concat, /* sq_concat */ |
| 13208 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13209 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13210 | 0, /* sq_slice */ |
| 13211 | 0, /* sq_ass_item */ |
| 13212 | 0, /* sq_ass_slice */ |
| 13213 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13214 | }; |
| 13215 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13216 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13217 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13218 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13219 | if (PyUnicode_READY(self) == -1) |
| 13220 | return NULL; |
| 13221 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13222 | if (PyIndex_Check(item)) { |
| 13223 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13224 | if (i == -1 && PyErr_Occurred()) |
| 13225 | return NULL; |
| 13226 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13227 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13228 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13229 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13230 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13231 | PyObject *result; |
| 13232 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13233 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13234 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13235 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13236 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13237 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13238 | return NULL; |
| 13239 | } |
| 13240 | |
| 13241 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13242 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13243 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13244 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13245 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13246 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13247 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13248 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13249 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13250 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13251 | src_kind = PyUnicode_KIND(self); |
| 13252 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13253 | if (!PyUnicode_IS_ASCII(self)) { |
| 13254 | kind_limit = kind_maxchar_limit(src_kind); |
| 13255 | max_char = 0; |
| 13256 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13257 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13258 | if (ch > max_char) { |
| 13259 | max_char = ch; |
| 13260 | if (max_char >= kind_limit) |
| 13261 | break; |
| 13262 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13263 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13264 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13265 | else |
| 13266 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13267 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13268 | if (result == NULL) |
| 13269 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13270 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13271 | dest_data = PyUnicode_DATA(result); |
| 13272 | |
| 13273 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13274 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13275 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13276 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13277 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13278 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13279 | } else { |
| 13280 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13281 | return NULL; |
| 13282 | } |
| 13283 | } |
| 13284 | |
| 13285 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13286 | (lenfunc)unicode_length, /* mp_length */ |
| 13287 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13288 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13289 | }; |
| 13290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13291 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13292 | /* Helpers for PyUnicode_Format() */ |
| 13293 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13294 | struct unicode_formatter_t { |
| 13295 | PyObject *args; |
| 13296 | int args_owned; |
| 13297 | Py_ssize_t arglen, argidx; |
| 13298 | PyObject *dict; |
| 13299 | |
| 13300 | enum PyUnicode_Kind fmtkind; |
| 13301 | Py_ssize_t fmtcnt, fmtpos; |
| 13302 | void *fmtdata; |
| 13303 | PyObject *fmtstr; |
| 13304 | |
| 13305 | _PyUnicodeWriter writer; |
| 13306 | }; |
| 13307 | |
| 13308 | struct unicode_format_arg_t { |
| 13309 | Py_UCS4 ch; |
| 13310 | int flags; |
| 13311 | Py_ssize_t width; |
| 13312 | int prec; |
| 13313 | int sign; |
| 13314 | }; |
| 13315 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13316 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13317 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13318 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13319 | Py_ssize_t argidx = ctx->argidx; |
| 13320 | |
| 13321 | if (argidx < ctx->arglen) { |
| 13322 | ctx->argidx++; |
| 13323 | if (ctx->arglen < 0) |
| 13324 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13325 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13326 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13327 | } |
| 13328 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13329 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13330 | return NULL; |
| 13331 | } |
| 13332 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13333 | /* 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] | 13334 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13335 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13336 | otherwise. |
| 13337 | |
| 13338 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13339 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13340 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13341 | PyObject **p_output, |
| 13342 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13343 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13344 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13345 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13346 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13347 | int prec; |
| 13348 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13349 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13350 | x = PyFloat_AsDouble(v); |
| 13351 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13352 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13353 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13354 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13355 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13356 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13357 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13358 | if (arg->flags & F_ALT) |
| 13359 | dtoa_flags = Py_DTSF_ALT; |
| 13360 | else |
| 13361 | dtoa_flags = 0; |
| 13362 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13363 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13364 | return -1; |
| 13365 | len = strlen(p); |
| 13366 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13367 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13368 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13369 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13370 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13371 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13372 | writer->pos += len; |
| 13373 | } |
| 13374 | else |
| 13375 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13376 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13377 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13378 | } |
| 13379 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13380 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13381 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13382 | * Python's regular ints. |
| 13383 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13384 | * The output string is of the form |
| 13385 | * "-"? ("0x" | "0X")? digit+ |
| 13386 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13387 | * set in flags. The case of hex digits will be correct, |
| 13388 | * There will be at least prec digits, zero-filled on the left if |
| 13389 | * necessary to get that many. |
| 13390 | * val object to be converted |
| 13391 | * flags bitmask of format flags; only F_ALT is looked at |
| 13392 | * prec minimum number of digits; 0-fill on left if needed |
| 13393 | * type a character in [duoxX]; u acts the same as d |
| 13394 | * |
| 13395 | * CAUTION: o, x and X conversions on regular ints can never |
| 13396 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13397 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13398 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13399 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13400 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13401 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13402 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13403 | Py_ssize_t i; |
| 13404 | int sign; /* 1 if '-', else 0 */ |
| 13405 | int len; /* number of characters */ |
| 13406 | Py_ssize_t llen; |
| 13407 | int numdigits; /* len == numnondigits + numdigits */ |
| 13408 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13409 | int prec = arg->prec; |
| 13410 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13411 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13412 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13413 | if (prec > INT_MAX-3) { |
| 13414 | PyErr_SetString(PyExc_OverflowError, |
| 13415 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13416 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13417 | } |
| 13418 | |
| 13419 | assert(PyLong_Check(val)); |
| 13420 | |
| 13421 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13422 | default: |
| 13423 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13424 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13425 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13426 | case 'u': |
| 13427 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13428 | if (PyBool_Check(val)) |
| 13429 | result = PyNumber_ToBase(val, 10); |
| 13430 | else |
| 13431 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13432 | break; |
| 13433 | case 'o': |
| 13434 | numnondigits = 2; |
| 13435 | result = PyNumber_ToBase(val, 8); |
| 13436 | break; |
| 13437 | case 'x': |
| 13438 | case 'X': |
| 13439 | numnondigits = 2; |
| 13440 | result = PyNumber_ToBase(val, 16); |
| 13441 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13442 | } |
| 13443 | if (!result) |
| 13444 | return NULL; |
| 13445 | |
| 13446 | assert(unicode_modifiable(result)); |
| 13447 | assert(PyUnicode_IS_READY(result)); |
| 13448 | assert(PyUnicode_IS_ASCII(result)); |
| 13449 | |
| 13450 | /* To modify the string in-place, there can only be one reference. */ |
| 13451 | if (Py_REFCNT(result) != 1) { |
| 13452 | PyErr_BadInternalCall(); |
| 13453 | return NULL; |
| 13454 | } |
| 13455 | buf = PyUnicode_DATA(result); |
| 13456 | llen = PyUnicode_GET_LENGTH(result); |
| 13457 | if (llen > INT_MAX) { |
| 13458 | PyErr_SetString(PyExc_ValueError, |
| 13459 | "string too large in _PyBytes_FormatLong"); |
| 13460 | return NULL; |
| 13461 | } |
| 13462 | len = (int)llen; |
| 13463 | sign = buf[0] == '-'; |
| 13464 | numnondigits += sign; |
| 13465 | numdigits = len - numnondigits; |
| 13466 | assert(numdigits > 0); |
| 13467 | |
| 13468 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13469 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13470 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13471 | assert(buf[sign] == '0'); |
| 13472 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13473 | buf[sign+1] == 'o'); |
| 13474 | numnondigits -= 2; |
| 13475 | buf += 2; |
| 13476 | len -= 2; |
| 13477 | if (sign) |
| 13478 | buf[0] = '-'; |
| 13479 | assert(len == numnondigits + numdigits); |
| 13480 | assert(numdigits > 0); |
| 13481 | } |
| 13482 | |
| 13483 | /* Fill with leading zeroes to meet minimum width. */ |
| 13484 | if (prec > numdigits) { |
| 13485 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13486 | numnondigits + prec); |
| 13487 | char *b1; |
| 13488 | if (!r1) { |
| 13489 | Py_DECREF(result); |
| 13490 | return NULL; |
| 13491 | } |
| 13492 | b1 = PyBytes_AS_STRING(r1); |
| 13493 | for (i = 0; i < numnondigits; ++i) |
| 13494 | *b1++ = *buf++; |
| 13495 | for (i = 0; i < prec - numdigits; i++) |
| 13496 | *b1++ = '0'; |
| 13497 | for (i = 0; i < numdigits; i++) |
| 13498 | *b1++ = *buf++; |
| 13499 | *b1 = '\0'; |
| 13500 | Py_DECREF(result); |
| 13501 | result = r1; |
| 13502 | buf = PyBytes_AS_STRING(result); |
| 13503 | len = numnondigits + prec; |
| 13504 | } |
| 13505 | |
| 13506 | /* Fix up case for hex conversions. */ |
| 13507 | if (type == 'X') { |
| 13508 | /* Need to convert all lower case letters to upper case. |
| 13509 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13510 | for (i = 0; i < len; i++) |
| 13511 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13512 | buf[i] -= 'a'-'A'; |
| 13513 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13514 | if (!PyUnicode_Check(result) |
| 13515 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13516 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13517 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13518 | Py_DECREF(result); |
| 13519 | result = unicode; |
| 13520 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13521 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13522 | if (PyUnicode_Resize(&result, len) < 0) |
| 13523 | Py_CLEAR(result); |
| 13524 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13525 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13526 | } |
| 13527 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13528 | /* Format an integer. |
| 13529 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13530 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13531 | * -1 and raise an exception on error */ |
| 13532 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13533 | mainformatlong(PyObject *v, |
| 13534 | struct unicode_format_arg_t *arg, |
| 13535 | PyObject **p_output, |
| 13536 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13537 | { |
| 13538 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13539 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13540 | |
| 13541 | if (!PyNumber_Check(v)) |
| 13542 | goto wrongtype; |
| 13543 | |
| 13544 | if (!PyLong_Check(v)) { |
| 13545 | iobj = PyNumber_Long(v); |
| 13546 | if (iobj == NULL) { |
| 13547 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13548 | goto wrongtype; |
| 13549 | return -1; |
| 13550 | } |
| 13551 | assert(PyLong_Check(iobj)); |
| 13552 | } |
| 13553 | else { |
| 13554 | iobj = v; |
| 13555 | Py_INCREF(iobj); |
| 13556 | } |
| 13557 | |
| 13558 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13559 | && arg->width == -1 && arg->prec == -1 |
| 13560 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 13561 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13562 | { |
| 13563 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13564 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13565 | int base; |
| 13566 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13567 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13568 | { |
| 13569 | default: |
| 13570 | assert(0 && "'type' not in [diuoxX]"); |
| 13571 | case 'd': |
| 13572 | case 'i': |
| 13573 | case 'u': |
| 13574 | base = 10; |
| 13575 | break; |
| 13576 | case 'o': |
| 13577 | base = 8; |
| 13578 | break; |
| 13579 | case 'x': |
| 13580 | case 'X': |
| 13581 | base = 16; |
| 13582 | break; |
| 13583 | } |
| 13584 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13585 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 13586 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13587 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13588 | } |
| 13589 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13590 | return 1; |
| 13591 | } |
| 13592 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13593 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13594 | Py_DECREF(iobj); |
| 13595 | if (res == NULL) |
| 13596 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13597 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13598 | return 0; |
| 13599 | |
| 13600 | wrongtype: |
| 13601 | PyErr_Format(PyExc_TypeError, |
| 13602 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13603 | "not %.200s", |
| 13604 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13605 | return -1; |
| 13606 | } |
| 13607 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13608 | static Py_UCS4 |
| 13609 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13610 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13611 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13612 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13613 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13614 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13615 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13616 | goto onError; |
| 13617 | } |
| 13618 | else { |
| 13619 | /* Integer input truncated to a character */ |
| 13620 | long x; |
| 13621 | x = PyLong_AsLong(v); |
| 13622 | if (x == -1 && PyErr_Occurred()) |
| 13623 | goto onError; |
| 13624 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13625 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13626 | PyErr_SetString(PyExc_OverflowError, |
| 13627 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13628 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13629 | } |
| 13630 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13631 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13632 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13633 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13634 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13635 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13636 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13637 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13638 | } |
| 13639 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13640 | /* Parse options of an argument: flags, width, precision. |
| 13641 | Handle also "%(name)" syntax. |
| 13642 | |
| 13643 | Return 0 if the argument has been formatted into arg->str. |
| 13644 | Return 1 if the argument has been written into ctx->writer, |
| 13645 | Raise an exception and return -1 on error. */ |
| 13646 | static int |
| 13647 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 13648 | struct unicode_format_arg_t *arg) |
| 13649 | { |
| 13650 | #define FORMAT_READ(ctx) \ |
| 13651 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 13652 | |
| 13653 | PyObject *v; |
| 13654 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13655 | if (arg->ch == '(') { |
| 13656 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 13657 | Py_ssize_t keystart; |
| 13658 | Py_ssize_t keylen; |
| 13659 | PyObject *key; |
| 13660 | int pcount = 1; |
| 13661 | |
| 13662 | if (ctx->dict == NULL) { |
| 13663 | PyErr_SetString(PyExc_TypeError, |
| 13664 | "format requires a mapping"); |
| 13665 | return -1; |
| 13666 | } |
| 13667 | ++ctx->fmtpos; |
| 13668 | --ctx->fmtcnt; |
| 13669 | keystart = ctx->fmtpos; |
| 13670 | /* Skip over balanced parentheses */ |
| 13671 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 13672 | arg->ch = FORMAT_READ(ctx); |
| 13673 | if (arg->ch == ')') |
| 13674 | --pcount; |
| 13675 | else if (arg->ch == '(') |
| 13676 | ++pcount; |
| 13677 | ctx->fmtpos++; |
| 13678 | } |
| 13679 | keylen = ctx->fmtpos - keystart - 1; |
| 13680 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 13681 | PyErr_SetString(PyExc_ValueError, |
| 13682 | "incomplete format key"); |
| 13683 | return -1; |
| 13684 | } |
| 13685 | key = PyUnicode_Substring(ctx->fmtstr, |
| 13686 | keystart, keystart + keylen); |
| 13687 | if (key == NULL) |
| 13688 | return -1; |
| 13689 | if (ctx->args_owned) { |
| 13690 | Py_DECREF(ctx->args); |
| 13691 | ctx->args_owned = 0; |
| 13692 | } |
| 13693 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 13694 | Py_DECREF(key); |
| 13695 | if (ctx->args == NULL) |
| 13696 | return -1; |
| 13697 | ctx->args_owned = 1; |
| 13698 | ctx->arglen = -1; |
| 13699 | ctx->argidx = -2; |
| 13700 | } |
| 13701 | |
| 13702 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13703 | while (--ctx->fmtcnt >= 0) { |
| 13704 | arg->ch = FORMAT_READ(ctx); |
| 13705 | ctx->fmtpos++; |
| 13706 | switch (arg->ch) { |
| 13707 | case '-': arg->flags |= F_LJUST; continue; |
| 13708 | case '+': arg->flags |= F_SIGN; continue; |
| 13709 | case ' ': arg->flags |= F_BLANK; continue; |
| 13710 | case '#': arg->flags |= F_ALT; continue; |
| 13711 | case '0': arg->flags |= F_ZERO; continue; |
| 13712 | } |
| 13713 | break; |
| 13714 | } |
| 13715 | |
| 13716 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13717 | if (arg->ch == '*') { |
| 13718 | v = unicode_format_getnextarg(ctx); |
| 13719 | if (v == NULL) |
| 13720 | return -1; |
| 13721 | if (!PyLong_Check(v)) { |
| 13722 | PyErr_SetString(PyExc_TypeError, |
| 13723 | "* wants int"); |
| 13724 | return -1; |
| 13725 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13726 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13727 | if (arg->width == -1 && PyErr_Occurred()) |
| 13728 | return -1; |
| 13729 | if (arg->width < 0) { |
| 13730 | arg->flags |= F_LJUST; |
| 13731 | arg->width = -arg->width; |
| 13732 | } |
| 13733 | if (--ctx->fmtcnt >= 0) { |
| 13734 | arg->ch = FORMAT_READ(ctx); |
| 13735 | ctx->fmtpos++; |
| 13736 | } |
| 13737 | } |
| 13738 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13739 | arg->width = arg->ch - '0'; |
| 13740 | while (--ctx->fmtcnt >= 0) { |
| 13741 | arg->ch = FORMAT_READ(ctx); |
| 13742 | ctx->fmtpos++; |
| 13743 | if (arg->ch < '0' || arg->ch > '9') |
| 13744 | break; |
| 13745 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 13746 | mixing signed and unsigned comparison. Since arg->ch is between |
| 13747 | '0' and '9', casting to int is safe. */ |
| 13748 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 13749 | PyErr_SetString(PyExc_ValueError, |
| 13750 | "width too big"); |
| 13751 | return -1; |
| 13752 | } |
| 13753 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 13754 | } |
| 13755 | } |
| 13756 | |
| 13757 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13758 | if (arg->ch == '.') { |
| 13759 | arg->prec = 0; |
| 13760 | if (--ctx->fmtcnt >= 0) { |
| 13761 | arg->ch = FORMAT_READ(ctx); |
| 13762 | ctx->fmtpos++; |
| 13763 | } |
| 13764 | if (arg->ch == '*') { |
| 13765 | v = unicode_format_getnextarg(ctx); |
| 13766 | if (v == NULL) |
| 13767 | return -1; |
| 13768 | if (!PyLong_Check(v)) { |
| 13769 | PyErr_SetString(PyExc_TypeError, |
| 13770 | "* wants int"); |
| 13771 | return -1; |
| 13772 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13773 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13774 | if (arg->prec == -1 && PyErr_Occurred()) |
| 13775 | return -1; |
| 13776 | if (arg->prec < 0) |
| 13777 | arg->prec = 0; |
| 13778 | if (--ctx->fmtcnt >= 0) { |
| 13779 | arg->ch = FORMAT_READ(ctx); |
| 13780 | ctx->fmtpos++; |
| 13781 | } |
| 13782 | } |
| 13783 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13784 | arg->prec = arg->ch - '0'; |
| 13785 | while (--ctx->fmtcnt >= 0) { |
| 13786 | arg->ch = FORMAT_READ(ctx); |
| 13787 | ctx->fmtpos++; |
| 13788 | if (arg->ch < '0' || arg->ch > '9') |
| 13789 | break; |
| 13790 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 13791 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 13792 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13793 | return -1; |
| 13794 | } |
| 13795 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 13796 | } |
| 13797 | } |
| 13798 | } |
| 13799 | |
| 13800 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 13801 | if (ctx->fmtcnt >= 0) { |
| 13802 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 13803 | if (--ctx->fmtcnt >= 0) { |
| 13804 | arg->ch = FORMAT_READ(ctx); |
| 13805 | ctx->fmtpos++; |
| 13806 | } |
| 13807 | } |
| 13808 | } |
| 13809 | if (ctx->fmtcnt < 0) { |
| 13810 | PyErr_SetString(PyExc_ValueError, |
| 13811 | "incomplete format"); |
| 13812 | return -1; |
| 13813 | } |
| 13814 | return 0; |
| 13815 | |
| 13816 | #undef FORMAT_READ |
| 13817 | } |
| 13818 | |
| 13819 | /* Format one argument. Supported conversion specifiers: |
| 13820 | |
| 13821 | - "s", "r", "a": any type |
| 13822 | - "i", "d", "u", "o", "x", "X": int |
| 13823 | - "e", "E", "f", "F", "g", "G": float |
| 13824 | - "c": int or str (1 character) |
| 13825 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13826 | When possible, the output is written directly into the Unicode writer |
| 13827 | (ctx->writer). A string is created when padding is required. |
| 13828 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13829 | Return 0 if the argument has been formatted into *p_str, |
| 13830 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13831 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13832 | static int |
| 13833 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 13834 | struct unicode_format_arg_t *arg, |
| 13835 | PyObject **p_str) |
| 13836 | { |
| 13837 | PyObject *v; |
| 13838 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13839 | |
| 13840 | if (ctx->fmtcnt == 0) |
| 13841 | ctx->writer.overallocate = 0; |
| 13842 | |
| 13843 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13844 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13845 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13846 | return 1; |
| 13847 | } |
| 13848 | |
| 13849 | v = unicode_format_getnextarg(ctx); |
| 13850 | if (v == NULL) |
| 13851 | return -1; |
| 13852 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13853 | |
| 13854 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13855 | case 's': |
| 13856 | case 'r': |
| 13857 | case 'a': |
| 13858 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 13859 | /* Fast path */ |
| 13860 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 13861 | return -1; |
| 13862 | return 1; |
| 13863 | } |
| 13864 | |
| 13865 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 13866 | *p_str = v; |
| 13867 | Py_INCREF(*p_str); |
| 13868 | } |
| 13869 | else { |
| 13870 | if (arg->ch == 's') |
| 13871 | *p_str = PyObject_Str(v); |
| 13872 | else if (arg->ch == 'r') |
| 13873 | *p_str = PyObject_Repr(v); |
| 13874 | else |
| 13875 | *p_str = PyObject_ASCII(v); |
| 13876 | } |
| 13877 | break; |
| 13878 | |
| 13879 | case 'i': |
| 13880 | case 'd': |
| 13881 | case 'u': |
| 13882 | case 'o': |
| 13883 | case 'x': |
| 13884 | case 'X': |
| 13885 | { |
| 13886 | int ret = mainformatlong(v, arg, p_str, writer); |
| 13887 | if (ret != 0) |
| 13888 | return ret; |
| 13889 | arg->sign = 1; |
| 13890 | break; |
| 13891 | } |
| 13892 | |
| 13893 | case 'e': |
| 13894 | case 'E': |
| 13895 | case 'f': |
| 13896 | case 'F': |
| 13897 | case 'g': |
| 13898 | case 'G': |
| 13899 | if (arg->width == -1 && arg->prec == -1 |
| 13900 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13901 | { |
| 13902 | /* Fast path */ |
| 13903 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 13904 | return -1; |
| 13905 | return 1; |
| 13906 | } |
| 13907 | |
| 13908 | arg->sign = 1; |
| 13909 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 13910 | return -1; |
| 13911 | break; |
| 13912 | |
| 13913 | case 'c': |
| 13914 | { |
| 13915 | Py_UCS4 ch = formatchar(v); |
| 13916 | if (ch == (Py_UCS4) -1) |
| 13917 | return -1; |
| 13918 | if (arg->width == -1 && arg->prec == -1) { |
| 13919 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13920 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13921 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13922 | return 1; |
| 13923 | } |
| 13924 | *p_str = PyUnicode_FromOrdinal(ch); |
| 13925 | break; |
| 13926 | } |
| 13927 | |
| 13928 | default: |
| 13929 | PyErr_Format(PyExc_ValueError, |
| 13930 | "unsupported format character '%c' (0x%x) " |
| 13931 | "at index %zd", |
| 13932 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 13933 | (int)arg->ch, |
| 13934 | ctx->fmtpos - 1); |
| 13935 | return -1; |
| 13936 | } |
| 13937 | if (*p_str == NULL) |
| 13938 | return -1; |
| 13939 | assert (PyUnicode_Check(*p_str)); |
| 13940 | return 0; |
| 13941 | } |
| 13942 | |
| 13943 | static int |
| 13944 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 13945 | struct unicode_format_arg_t *arg, |
| 13946 | PyObject *str) |
| 13947 | { |
| 13948 | Py_ssize_t len; |
| 13949 | enum PyUnicode_Kind kind; |
| 13950 | void *pbuf; |
| 13951 | Py_ssize_t pindex; |
| 13952 | Py_UCS4 signchar; |
| 13953 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13954 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13955 | Py_ssize_t sublen; |
| 13956 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13957 | Py_UCS4 fill; |
| 13958 | |
| 13959 | fill = ' '; |
| 13960 | if (arg->sign && arg->flags & F_ZERO) |
| 13961 | fill = '0'; |
| 13962 | |
| 13963 | if (PyUnicode_READY(str) == -1) |
| 13964 | return -1; |
| 13965 | |
| 13966 | len = PyUnicode_GET_LENGTH(str); |
| 13967 | if ((arg->width == -1 || arg->width <= len) |
| 13968 | && (arg->prec == -1 || arg->prec >= len) |
| 13969 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13970 | { |
| 13971 | /* Fast path */ |
| 13972 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 13973 | return -1; |
| 13974 | return 0; |
| 13975 | } |
| 13976 | |
| 13977 | /* Truncate the string for "s", "r" and "a" formats |
| 13978 | if the precision is set */ |
| 13979 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 13980 | if (arg->prec >= 0 && len > arg->prec) |
| 13981 | len = arg->prec; |
| 13982 | } |
| 13983 | |
| 13984 | /* Adjust sign and width */ |
| 13985 | kind = PyUnicode_KIND(str); |
| 13986 | pbuf = PyUnicode_DATA(str); |
| 13987 | pindex = 0; |
| 13988 | signchar = '\0'; |
| 13989 | if (arg->sign) { |
| 13990 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13991 | if (ch == '-' || ch == '+') { |
| 13992 | signchar = ch; |
| 13993 | len--; |
| 13994 | pindex++; |
| 13995 | } |
| 13996 | else if (arg->flags & F_SIGN) |
| 13997 | signchar = '+'; |
| 13998 | else if (arg->flags & F_BLANK) |
| 13999 | signchar = ' '; |
| 14000 | else |
| 14001 | arg->sign = 0; |
| 14002 | } |
| 14003 | if (arg->width < len) |
| 14004 | arg->width = len; |
| 14005 | |
| 14006 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14007 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14008 | if (!(arg->flags & F_LJUST)) { |
| 14009 | if (arg->sign) { |
| 14010 | if ((arg->width-1) > len) |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14011 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14012 | } |
| 14013 | else { |
| 14014 | if (arg->width > len) |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14015 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14016 | } |
| 14017 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14018 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14019 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
| 14020 | maxchar = MAX_MAXCHAR(maxchar, strmaxchar); |
| 14021 | } |
| 14022 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14023 | buflen = arg->width; |
| 14024 | if (arg->sign && len == arg->width) |
| 14025 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14026 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14027 | return -1; |
| 14028 | |
| 14029 | /* Write the sign if needed */ |
| 14030 | if (arg->sign) { |
| 14031 | if (fill != ' ') { |
| 14032 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14033 | writer->pos += 1; |
| 14034 | } |
| 14035 | if (arg->width > len) |
| 14036 | arg->width--; |
| 14037 | } |
| 14038 | |
| 14039 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14040 | if the alternate form is used. |
| 14041 | For example, write "0x" for the "%#x" format. */ |
| 14042 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14043 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14044 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14045 | if (fill != ' ') { |
| 14046 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14047 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14048 | writer->pos += 2; |
| 14049 | pindex += 2; |
| 14050 | } |
| 14051 | arg->width -= 2; |
| 14052 | if (arg->width < 0) |
| 14053 | arg->width = 0; |
| 14054 | len -= 2; |
| 14055 | } |
| 14056 | |
| 14057 | /* Pad left with the fill character if needed */ |
| 14058 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14059 | sublen = arg->width - len; |
| 14060 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14061 | writer->pos += sublen; |
| 14062 | arg->width = len; |
| 14063 | } |
| 14064 | |
| 14065 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14066 | the alternate form is used */ |
| 14067 | if (fill == ' ') { |
| 14068 | if (arg->sign) { |
| 14069 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14070 | writer->pos += 1; |
| 14071 | } |
| 14072 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14073 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14074 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14075 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14076 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14077 | writer->pos += 2; |
| 14078 | pindex += 2; |
| 14079 | } |
| 14080 | } |
| 14081 | |
| 14082 | /* Write characters */ |
| 14083 | if (len) { |
| 14084 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14085 | str, pindex, len); |
| 14086 | writer->pos += len; |
| 14087 | } |
| 14088 | |
| 14089 | /* Pad right with the fill character if needed */ |
| 14090 | if (arg->width > len) { |
| 14091 | sublen = arg->width - len; |
| 14092 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14093 | writer->pos += sublen; |
| 14094 | } |
| 14095 | return 0; |
| 14096 | } |
| 14097 | |
| 14098 | /* Helper of PyUnicode_Format(): format one arg. |
| 14099 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14100 | static int |
| 14101 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14102 | { |
| 14103 | struct unicode_format_arg_t arg; |
| 14104 | PyObject *str; |
| 14105 | int ret; |
| 14106 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14107 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14108 | arg.flags = 0; |
| 14109 | arg.width = -1; |
| 14110 | arg.prec = -1; |
| 14111 | arg.sign = 0; |
| 14112 | str = NULL; |
| 14113 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14114 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14115 | if (ret == -1) |
| 14116 | return -1; |
| 14117 | |
| 14118 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14119 | if (ret == -1) |
| 14120 | return -1; |
| 14121 | |
| 14122 | if (ret != 1) { |
| 14123 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14124 | Py_DECREF(str); |
| 14125 | if (ret == -1) |
| 14126 | return -1; |
| 14127 | } |
| 14128 | |
| 14129 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14130 | PyErr_SetString(PyExc_TypeError, |
| 14131 | "not all arguments converted during string formatting"); |
| 14132 | return -1; |
| 14133 | } |
| 14134 | return 0; |
| 14135 | } |
| 14136 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14137 | PyObject * |
| 14138 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14139 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14140 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14142 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14143 | PyErr_BadInternalCall(); |
| 14144 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14145 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14146 | |
| 14147 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14148 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14149 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14150 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14151 | Py_DECREF(ctx.fmtstr); |
| 14152 | return NULL; |
| 14153 | } |
| 14154 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14155 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14156 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14157 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14158 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14159 | _PyUnicodeWriter_Init(&ctx.writer, ctx.fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14161 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14162 | ctx.arglen = PyTuple_Size(args); |
| 14163 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14164 | } |
| 14165 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14166 | ctx.arglen = -1; |
| 14167 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14168 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14169 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14170 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14171 | ctx.dict = args; |
| 14172 | else |
| 14173 | ctx.dict = NULL; |
| 14174 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14175 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14176 | while (--ctx.fmtcnt >= 0) { |
| 14177 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14178 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14179 | |
| 14180 | nonfmtpos = ctx.fmtpos++; |
| 14181 | while (ctx.fmtcnt >= 0 && |
| 14182 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14183 | ctx.fmtpos++; |
| 14184 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14185 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14186 | if (ctx.fmtcnt < 0) { |
| 14187 | ctx.fmtpos--; |
| 14188 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14189 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14190 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14191 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14192 | nonfmtpos, ctx.fmtpos) < 0) |
| 14193 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14194 | } |
| 14195 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14196 | ctx.fmtpos++; |
| 14197 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14198 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14199 | } |
| 14200 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14201 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14202 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14203 | PyErr_SetString(PyExc_TypeError, |
| 14204 | "not all arguments converted during string formatting"); |
| 14205 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14206 | } |
| 14207 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14208 | if (ctx.args_owned) { |
| 14209 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14210 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14211 | Py_DECREF(ctx.fmtstr); |
| 14212 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14213 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14214 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14215 | Py_DECREF(ctx.fmtstr); |
| 14216 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14217 | if (ctx.args_owned) { |
| 14218 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14219 | } |
| 14220 | return NULL; |
| 14221 | } |
| 14222 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14223 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14224 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14225 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14226 | static PyObject * |
| 14227 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14228 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14229 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14230 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14231 | char *encoding = NULL; |
| 14232 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14233 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14234 | if (type != &PyUnicode_Type) |
| 14235 | return unicode_subtype_new(type, args, kwds); |
| 14236 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14237 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14238 | return NULL; |
| 14239 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14240 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14241 | if (encoding == NULL && errors == NULL) |
| 14242 | return PyObject_Str(x); |
| 14243 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14244 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14245 | } |
| 14246 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14247 | static PyObject * |
| 14248 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14249 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14250 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14251 | Py_ssize_t length, char_size; |
| 14252 | int share_wstr, share_utf8; |
| 14253 | unsigned int kind; |
| 14254 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14255 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14256 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14257 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14258 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14259 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14260 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14261 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14262 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14263 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14264 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14265 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14266 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14267 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14268 | if (self == NULL) { |
| 14269 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14270 | return NULL; |
| 14271 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14272 | kind = PyUnicode_KIND(unicode); |
| 14273 | length = PyUnicode_GET_LENGTH(unicode); |
| 14274 | |
| 14275 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14276 | #ifdef Py_DEBUG |
| 14277 | _PyUnicode_HASH(self) = -1; |
| 14278 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14279 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14280 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14281 | _PyUnicode_STATE(self).interned = 0; |
| 14282 | _PyUnicode_STATE(self).kind = kind; |
| 14283 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14284 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14285 | _PyUnicode_STATE(self).ready = 1; |
| 14286 | _PyUnicode_WSTR(self) = NULL; |
| 14287 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14288 | _PyUnicode_UTF8(self) = NULL; |
| 14289 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14290 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14291 | |
| 14292 | share_utf8 = 0; |
| 14293 | share_wstr = 0; |
| 14294 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14295 | char_size = 1; |
| 14296 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14297 | share_utf8 = 1; |
| 14298 | } |
| 14299 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14300 | char_size = 2; |
| 14301 | if (sizeof(wchar_t) == 2) |
| 14302 | share_wstr = 1; |
| 14303 | } |
| 14304 | else { |
| 14305 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14306 | char_size = 4; |
| 14307 | if (sizeof(wchar_t) == 4) |
| 14308 | share_wstr = 1; |
| 14309 | } |
| 14310 | |
| 14311 | /* Ensure we won't overflow the length. */ |
| 14312 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14313 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14314 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14315 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14316 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14317 | if (data == NULL) { |
| 14318 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14319 | goto onError; |
| 14320 | } |
| 14321 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14322 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14323 | if (share_utf8) { |
| 14324 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14325 | _PyUnicode_UTF8(self) = data; |
| 14326 | } |
| 14327 | if (share_wstr) { |
| 14328 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14329 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14330 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14331 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14332 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14333 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14334 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14335 | #ifdef Py_DEBUG |
| 14336 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14337 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14338 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14339 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14340 | |
| 14341 | onError: |
| 14342 | Py_DECREF(unicode); |
| 14343 | Py_DECREF(self); |
| 14344 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14345 | } |
| 14346 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14347 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14348 | "str(object='') -> str\n\ |
| 14349 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14350 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14351 | Create a new string object from the given object. If encoding or\n\ |
| 14352 | errors is specified, then the object must expose a data buffer\n\ |
| 14353 | that will be decoded using the given encoding and error handler.\n\ |
| 14354 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14355 | or repr(object).\n\ |
| 14356 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14357 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14358 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14359 | static PyObject *unicode_iter(PyObject *seq); |
| 14360 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14361 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14362 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14363 | "str", /* tp_name */ |
| 14364 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14365 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14366 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14367 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14368 | 0, /* tp_print */ |
| 14369 | 0, /* tp_getattr */ |
| 14370 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14371 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14372 | unicode_repr, /* tp_repr */ |
| 14373 | &unicode_as_number, /* tp_as_number */ |
| 14374 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14375 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14376 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14377 | 0, /* tp_call*/ |
| 14378 | (reprfunc) unicode_str, /* tp_str */ |
| 14379 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14380 | 0, /* tp_setattro */ |
| 14381 | 0, /* tp_as_buffer */ |
| 14382 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14383 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14384 | unicode_doc, /* tp_doc */ |
| 14385 | 0, /* tp_traverse */ |
| 14386 | 0, /* tp_clear */ |
| 14387 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14388 | 0, /* tp_weaklistoffset */ |
| 14389 | unicode_iter, /* tp_iter */ |
| 14390 | 0, /* tp_iternext */ |
| 14391 | unicode_methods, /* tp_methods */ |
| 14392 | 0, /* tp_members */ |
| 14393 | 0, /* tp_getset */ |
| 14394 | &PyBaseObject_Type, /* tp_base */ |
| 14395 | 0, /* tp_dict */ |
| 14396 | 0, /* tp_descr_get */ |
| 14397 | 0, /* tp_descr_set */ |
| 14398 | 0, /* tp_dictoffset */ |
| 14399 | 0, /* tp_init */ |
| 14400 | 0, /* tp_alloc */ |
| 14401 | unicode_new, /* tp_new */ |
| 14402 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14403 | }; |
| 14404 | |
| 14405 | /* Initialize the Unicode implementation */ |
| 14406 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14407 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14408 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14409 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14410 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14411 | 0x000A, /* LINE FEED */ |
| 14412 | 0x000D, /* CARRIAGE RETURN */ |
| 14413 | 0x001C, /* FILE SEPARATOR */ |
| 14414 | 0x001D, /* GROUP SEPARATOR */ |
| 14415 | 0x001E, /* RECORD SEPARATOR */ |
| 14416 | 0x0085, /* NEXT LINE */ |
| 14417 | 0x2028, /* LINE SEPARATOR */ |
| 14418 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14419 | }; |
| 14420 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14421 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14422 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14423 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14424 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14425 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14426 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14427 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14428 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14429 | |
| 14430 | /* initialize the linebreak bloom filter */ |
| 14431 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14432 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14433 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14434 | |
| 14435 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14436 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14437 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14438 | Py_FatalError("Can't initialize field name iterator type"); |
| 14439 | |
| 14440 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14441 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14442 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14443 | #ifdef HAVE_MBCS |
| 14444 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14445 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14446 | PyErr_SetFromWindowsErr(0); |
| 14447 | return -1; |
| 14448 | } |
| 14449 | #endif |
| 14450 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14451 | } |
| 14452 | |
| 14453 | /* Finalize the Unicode implementation */ |
| 14454 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14455 | int |
| 14456 | PyUnicode_ClearFreeList(void) |
| 14457 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14458 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14459 | } |
| 14460 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14461 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14462 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14463 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14464 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14465 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14466 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14467 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14468 | for (i = 0; i < 256; i++) |
| 14469 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14470 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14471 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14472 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14473 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14474 | void |
| 14475 | PyUnicode_InternInPlace(PyObject **p) |
| 14476 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14477 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14478 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14479 | #ifdef Py_DEBUG |
| 14480 | assert(s != NULL); |
| 14481 | assert(_PyUnicode_CHECK(s)); |
| 14482 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14483 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14484 | return; |
| 14485 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14486 | /* If it's a subclass, we don't really know what putting |
| 14487 | it in the interned dict might do. */ |
| 14488 | if (!PyUnicode_CheckExact(s)) |
| 14489 | return; |
| 14490 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14491 | return; |
| 14492 | if (interned == NULL) { |
| 14493 | interned = PyDict_New(); |
| 14494 | if (interned == NULL) { |
| 14495 | PyErr_Clear(); /* Don't leave an exception */ |
| 14496 | return; |
| 14497 | } |
| 14498 | } |
| 14499 | /* It might be that the GetItem call fails even |
| 14500 | though the key is present in the dictionary, |
| 14501 | namely when this happens during a stack overflow. */ |
| 14502 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14503 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14504 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14505 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14506 | if (t) { |
| 14507 | Py_INCREF(t); |
| 14508 | Py_DECREF(*p); |
| 14509 | *p = t; |
| 14510 | return; |
| 14511 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14512 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14513 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14514 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14515 | PyErr_Clear(); |
| 14516 | PyThreadState_GET()->recursion_critical = 0; |
| 14517 | return; |
| 14518 | } |
| 14519 | PyThreadState_GET()->recursion_critical = 0; |
| 14520 | /* The two references in interned are not counted by refcnt. |
| 14521 | The deallocator will take care of this */ |
| 14522 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14523 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14524 | } |
| 14525 | |
| 14526 | void |
| 14527 | PyUnicode_InternImmortal(PyObject **p) |
| 14528 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14529 | PyUnicode_InternInPlace(p); |
| 14530 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14531 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14532 | Py_INCREF(*p); |
| 14533 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14534 | } |
| 14535 | |
| 14536 | PyObject * |
| 14537 | PyUnicode_InternFromString(const char *cp) |
| 14538 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14539 | PyObject *s = PyUnicode_FromString(cp); |
| 14540 | if (s == NULL) |
| 14541 | return NULL; |
| 14542 | PyUnicode_InternInPlace(&s); |
| 14543 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14544 | } |
| 14545 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14546 | void |
| 14547 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14548 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14549 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14550 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14551 | Py_ssize_t i, n; |
| 14552 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14553 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14554 | if (interned == NULL || !PyDict_Check(interned)) |
| 14555 | return; |
| 14556 | keys = PyDict_Keys(interned); |
| 14557 | if (keys == NULL || !PyList_Check(keys)) { |
| 14558 | PyErr_Clear(); |
| 14559 | return; |
| 14560 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14561 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14562 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14563 | detector, interned unicode strings are not forcibly deallocated; |
| 14564 | rather, we give them their stolen references back, and then clear |
| 14565 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14566 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14567 | n = PyList_GET_SIZE(keys); |
| 14568 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14569 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14570 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14571 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14572 | if (PyUnicode_READY(s) == -1) { |
| 14573 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14574 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14575 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14576 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14577 | case SSTATE_NOT_INTERNED: |
| 14578 | /* XXX Shouldn't happen */ |
| 14579 | break; |
| 14580 | case SSTATE_INTERNED_IMMORTAL: |
| 14581 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14582 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14583 | break; |
| 14584 | case SSTATE_INTERNED_MORTAL: |
| 14585 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14586 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14587 | break; |
| 14588 | default: |
| 14589 | Py_FatalError("Inconsistent interned string state."); |
| 14590 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14591 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14592 | } |
| 14593 | fprintf(stderr, "total size of all interned strings: " |
| 14594 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14595 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14596 | Py_DECREF(keys); |
| 14597 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14598 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14599 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14600 | |
| 14601 | |
| 14602 | /********************* Unicode Iterator **************************/ |
| 14603 | |
| 14604 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14605 | PyObject_HEAD |
| 14606 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14607 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14608 | } unicodeiterobject; |
| 14609 | |
| 14610 | static void |
| 14611 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14612 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14613 | _PyObject_GC_UNTRACK(it); |
| 14614 | Py_XDECREF(it->it_seq); |
| 14615 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14616 | } |
| 14617 | |
| 14618 | static int |
| 14619 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14620 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14621 | Py_VISIT(it->it_seq); |
| 14622 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14623 | } |
| 14624 | |
| 14625 | static PyObject * |
| 14626 | unicodeiter_next(unicodeiterobject *it) |
| 14627 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14628 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14629 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14630 | assert(it != NULL); |
| 14631 | seq = it->it_seq; |
| 14632 | if (seq == NULL) |
| 14633 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14634 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14635 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14636 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14637 | int kind = PyUnicode_KIND(seq); |
| 14638 | void *data = PyUnicode_DATA(seq); |
| 14639 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14640 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14641 | if (item != NULL) |
| 14642 | ++it->it_index; |
| 14643 | return item; |
| 14644 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14645 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14646 | Py_DECREF(seq); |
| 14647 | it->it_seq = NULL; |
| 14648 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14649 | } |
| 14650 | |
| 14651 | static PyObject * |
| 14652 | unicodeiter_len(unicodeiterobject *it) |
| 14653 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14654 | Py_ssize_t len = 0; |
| 14655 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14656 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14657 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14658 | } |
| 14659 | |
| 14660 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14661 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14662 | static PyObject * |
| 14663 | unicodeiter_reduce(unicodeiterobject *it) |
| 14664 | { |
| 14665 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14666 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14667 | it->it_seq, it->it_index); |
| 14668 | } else { |
| 14669 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14670 | if (u == NULL) |
| 14671 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14672 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14673 | } |
| 14674 | } |
| 14675 | |
| 14676 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14677 | |
| 14678 | static PyObject * |
| 14679 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14680 | { |
| 14681 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14682 | if (index == -1 && PyErr_Occurred()) |
| 14683 | return NULL; |
| 14684 | if (index < 0) |
| 14685 | index = 0; |
| 14686 | it->it_index = index; |
| 14687 | Py_RETURN_NONE; |
| 14688 | } |
| 14689 | |
| 14690 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14691 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14692 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14693 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14694 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14695 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14696 | reduce_doc}, |
| 14697 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14698 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14699 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14700 | }; |
| 14701 | |
| 14702 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14703 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14704 | "str_iterator", /* tp_name */ |
| 14705 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14706 | 0, /* tp_itemsize */ |
| 14707 | /* methods */ |
| 14708 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14709 | 0, /* tp_print */ |
| 14710 | 0, /* tp_getattr */ |
| 14711 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14712 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14713 | 0, /* tp_repr */ |
| 14714 | 0, /* tp_as_number */ |
| 14715 | 0, /* tp_as_sequence */ |
| 14716 | 0, /* tp_as_mapping */ |
| 14717 | 0, /* tp_hash */ |
| 14718 | 0, /* tp_call */ |
| 14719 | 0, /* tp_str */ |
| 14720 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14721 | 0, /* tp_setattro */ |
| 14722 | 0, /* tp_as_buffer */ |
| 14723 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14724 | 0, /* tp_doc */ |
| 14725 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14726 | 0, /* tp_clear */ |
| 14727 | 0, /* tp_richcompare */ |
| 14728 | 0, /* tp_weaklistoffset */ |
| 14729 | PyObject_SelfIter, /* tp_iter */ |
| 14730 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14731 | unicodeiter_methods, /* tp_methods */ |
| 14732 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14733 | }; |
| 14734 | |
| 14735 | static PyObject * |
| 14736 | unicode_iter(PyObject *seq) |
| 14737 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14738 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14739 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14740 | if (!PyUnicode_Check(seq)) { |
| 14741 | PyErr_BadInternalCall(); |
| 14742 | return NULL; |
| 14743 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14744 | if (PyUnicode_READY(seq) == -1) |
| 14745 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14746 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14747 | if (it == NULL) |
| 14748 | return NULL; |
| 14749 | it->it_index = 0; |
| 14750 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14751 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14752 | _PyObject_GC_TRACK(it); |
| 14753 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14754 | } |
| 14755 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14756 | |
| 14757 | size_t |
| 14758 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14759 | { |
| 14760 | int res = 0; |
| 14761 | while(*u++) |
| 14762 | res++; |
| 14763 | return res; |
| 14764 | } |
| 14765 | |
| 14766 | Py_UNICODE* |
| 14767 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14768 | { |
| 14769 | Py_UNICODE *u = s1; |
| 14770 | while ((*u++ = *s2++)); |
| 14771 | return s1; |
| 14772 | } |
| 14773 | |
| 14774 | Py_UNICODE* |
| 14775 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14776 | { |
| 14777 | Py_UNICODE *u = s1; |
| 14778 | while ((*u++ = *s2++)) |
| 14779 | if (n-- == 0) |
| 14780 | break; |
| 14781 | return s1; |
| 14782 | } |
| 14783 | |
| 14784 | Py_UNICODE* |
| 14785 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14786 | { |
| 14787 | Py_UNICODE *u1 = s1; |
| 14788 | u1 += Py_UNICODE_strlen(u1); |
| 14789 | Py_UNICODE_strcpy(u1, s2); |
| 14790 | return s1; |
| 14791 | } |
| 14792 | |
| 14793 | int |
| 14794 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14795 | { |
| 14796 | while (*s1 && *s2 && *s1 == *s2) |
| 14797 | s1++, s2++; |
| 14798 | if (*s1 && *s2) |
| 14799 | return (*s1 < *s2) ? -1 : +1; |
| 14800 | if (*s1) |
| 14801 | return 1; |
| 14802 | if (*s2) |
| 14803 | return -1; |
| 14804 | return 0; |
| 14805 | } |
| 14806 | |
| 14807 | int |
| 14808 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14809 | { |
| 14810 | register Py_UNICODE u1, u2; |
| 14811 | for (; n != 0; n--) { |
| 14812 | u1 = *s1; |
| 14813 | u2 = *s2; |
| 14814 | if (u1 != u2) |
| 14815 | return (u1 < u2) ? -1 : +1; |
| 14816 | if (u1 == '\0') |
| 14817 | return 0; |
| 14818 | s1++; |
| 14819 | s2++; |
| 14820 | } |
| 14821 | return 0; |
| 14822 | } |
| 14823 | |
| 14824 | Py_UNICODE* |
| 14825 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14826 | { |
| 14827 | const Py_UNICODE *p; |
| 14828 | for (p = s; *p; p++) |
| 14829 | if (*p == c) |
| 14830 | return (Py_UNICODE*)p; |
| 14831 | return NULL; |
| 14832 | } |
| 14833 | |
| 14834 | Py_UNICODE* |
| 14835 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14836 | { |
| 14837 | const Py_UNICODE *p; |
| 14838 | p = s + Py_UNICODE_strlen(s); |
| 14839 | while (p != s) { |
| 14840 | p--; |
| 14841 | if (*p == c) |
| 14842 | return (Py_UNICODE*)p; |
| 14843 | } |
| 14844 | return NULL; |
| 14845 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14846 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14847 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14848 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14849 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14850 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14851 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14852 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14853 | if (!PyUnicode_Check(unicode)) { |
| 14854 | PyErr_BadArgument(); |
| 14855 | return NULL; |
| 14856 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14857 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14858 | if (u == NULL) |
| 14859 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14860 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14861 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14862 | PyErr_NoMemory(); |
| 14863 | return NULL; |
| 14864 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14865 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14866 | size *= sizeof(Py_UNICODE); |
| 14867 | copy = PyMem_Malloc(size); |
| 14868 | if (copy == NULL) { |
| 14869 | PyErr_NoMemory(); |
| 14870 | return NULL; |
| 14871 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14872 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14873 | return copy; |
| 14874 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14875 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14876 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14877 | to the string.Formatter class implemented in Python. */ |
| 14878 | |
| 14879 | static PyMethodDef _string_methods[] = { |
| 14880 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14881 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14882 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14883 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14884 | {NULL, NULL} |
| 14885 | }; |
| 14886 | |
| 14887 | static struct PyModuleDef _string_module = { |
| 14888 | PyModuleDef_HEAD_INIT, |
| 14889 | "_string", |
| 14890 | PyDoc_STR("string helper module"), |
| 14891 | 0, |
| 14892 | _string_methods, |
| 14893 | NULL, |
| 14894 | NULL, |
| 14895 | NULL, |
| 14896 | NULL |
| 14897 | }; |
| 14898 | |
| 14899 | PyMODINIT_FUNC |
| 14900 | PyInit__string(void) |
| 14901 | { |
| 14902 | return PyModule_Create(&_string_module); |
| 14903 | } |
| 14904 | |
| 14905 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14906 | #ifdef __cplusplus |
| 14907 | } |
| 14908 | #endif |