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 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 199 | /* List of static strings. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 200 | static _Py_Identifier *static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 201 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 202 | /* Single character Unicode strings in the Latin-1 range are being |
| 203 | shared as well. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 204 | static PyObject *unicode_latin1[256] = {NULL}; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 205 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 206 | /* Fast detection of the most frequent whitespace characters */ |
| 207 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 208 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 209 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 210 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 211 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 212 | /* case 0x000C: * FORM FEED */ |
| 213 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 214 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 215 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 216 | /* case 0x001C: * FILE SEPARATOR */ |
| 217 | /* case 0x001D: * GROUP SEPARATOR */ |
| 218 | /* case 0x001E: * RECORD SEPARATOR */ |
| 219 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 220 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 221 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 222 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 223 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 224 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 225 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 226 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 227 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 228 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 229 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 230 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 231 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 237 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 238 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 239 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 240 | static int unicode_modifiable(PyObject *unicode); |
| 241 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 242 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 243 | static PyObject * |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 244 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 245 | static PyObject * |
| 246 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 247 | static PyObject * |
| 248 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 249 | |
| 250 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 251 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 252 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 253 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 254 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 255 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 256 | static void |
| 257 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 258 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 259 | PyObject *unicode, |
| 260 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 261 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 262 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 263 | /* Same for linebreaks */ |
| 264 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 265 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 266 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 267 | /* 0x000B, * LINE TABULATION */ |
| 268 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 269 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 270 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 271 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 272 | /* 0x001C, * FILE SEPARATOR */ |
| 273 | /* 0x001D, * GROUP SEPARATOR */ |
| 274 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 275 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 276 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 278 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 279 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 280 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 281 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 282 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 283 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 284 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 291 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 292 | 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] | 293 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 294 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 295 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 296 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 297 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 298 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 299 | /* This is actually an illegal character, so it should |
| 300 | not be passed to unichr. */ |
| 301 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 302 | #endif |
| 303 | } |
| 304 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 305 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 306 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 307 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 308 | { |
| 309 | PyASCIIObject *ascii; |
| 310 | unsigned int kind; |
| 311 | |
| 312 | assert(PyUnicode_Check(op)); |
| 313 | |
| 314 | ascii = (PyASCIIObject *)op; |
| 315 | kind = ascii->state.kind; |
| 316 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 317 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 318 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 319 | assert(ascii->state.ready == 1); |
| 320 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 321 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 322 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 323 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 324 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 325 | if (ascii->state.compact == 1) { |
| 326 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 327 | assert(kind == PyUnicode_1BYTE_KIND |
| 328 | || kind == PyUnicode_2BYTE_KIND |
| 329 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 330 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 331 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 332 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 333 | } |
| 334 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 335 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 336 | |
| 337 | data = unicode->data.any; |
| 338 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 339 | assert(ascii->length == 0); |
| 340 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 341 | assert(ascii->state.compact == 0); |
| 342 | assert(ascii->state.ascii == 0); |
| 343 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 344 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 345 | assert(ascii->wstr != NULL); |
| 346 | assert(data == NULL); |
| 347 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 348 | } |
| 349 | else { |
| 350 | assert(kind == PyUnicode_1BYTE_KIND |
| 351 | || kind == PyUnicode_2BYTE_KIND |
| 352 | || kind == PyUnicode_4BYTE_KIND); |
| 353 | assert(ascii->state.compact == 0); |
| 354 | assert(ascii->state.ready == 1); |
| 355 | assert(data != NULL); |
| 356 | if (ascii->state.ascii) { |
| 357 | assert (compact->utf8 == data); |
| 358 | assert (compact->utf8_length == ascii->length); |
| 359 | } |
| 360 | else |
| 361 | assert (compact->utf8 != data); |
| 362 | } |
| 363 | } |
| 364 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 365 | if ( |
| 366 | #if SIZEOF_WCHAR_T == 2 |
| 367 | kind == PyUnicode_2BYTE_KIND |
| 368 | #else |
| 369 | kind == PyUnicode_4BYTE_KIND |
| 370 | #endif |
| 371 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 372 | { |
| 373 | assert(ascii->wstr == data); |
| 374 | assert(compact->wstr_length == ascii->length); |
| 375 | } else |
| 376 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 377 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 378 | |
| 379 | if (compact->utf8 == NULL) |
| 380 | assert(compact->utf8_length == 0); |
| 381 | if (ascii->wstr == NULL) |
| 382 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 383 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 384 | /* check that the best kind is used */ |
| 385 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 386 | { |
| 387 | Py_ssize_t i; |
| 388 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 389 | void *data; |
| 390 | Py_UCS4 ch; |
| 391 | |
| 392 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 393 | for (i=0; i < ascii->length; i++) |
| 394 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 395 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 396 | if (ch > maxchar) |
| 397 | maxchar = ch; |
| 398 | } |
| 399 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 400 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 401 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 402 | assert(maxchar <= 255); |
| 403 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 404 | else |
| 405 | assert(maxchar < 128); |
| 406 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 407 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 408 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 409 | assert(maxchar <= 0xFFFF); |
| 410 | } |
| 411 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 412 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 413 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 414 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 415 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 416 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 417 | return 1; |
| 418 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 419 | #endif |
| 420 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 421 | static PyObject* |
| 422 | unicode_result_wchar(PyObject *unicode) |
| 423 | { |
| 424 | #ifndef Py_DEBUG |
| 425 | Py_ssize_t len; |
| 426 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 427 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 428 | if (len == 0) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 429 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 430 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | if (len == 1) { |
| 434 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 435 | if ((Py_UCS4)ch < 256) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 436 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 437 | Py_DECREF(unicode); |
| 438 | return latin1_char; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (_PyUnicode_Ready(unicode) < 0) { |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 443 | Py_DECREF(unicode); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 444 | return NULL; |
| 445 | } |
| 446 | #else |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 447 | assert(Py_REFCNT(unicode) == 1); |
| 448 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 449 | /* don't make the result ready in debug mode to ensure that the caller |
| 450 | makes the string ready before using it */ |
| 451 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 452 | #endif |
| 453 | return unicode; |
| 454 | } |
| 455 | |
| 456 | static PyObject* |
| 457 | unicode_result_ready(PyObject *unicode) |
| 458 | { |
| 459 | Py_ssize_t length; |
| 460 | |
| 461 | length = PyUnicode_GET_LENGTH(unicode); |
| 462 | if (length == 0) { |
| 463 | if (unicode != unicode_empty) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 464 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 465 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 466 | } |
| 467 | return unicode_empty; |
| 468 | } |
| 469 | |
| 470 | if (length == 1) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 471 | void *data = PyUnicode_DATA(unicode); |
| 472 | int kind = PyUnicode_KIND(unicode); |
| 473 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 474 | if (ch < 256) { |
| 475 | PyObject *latin1_char = unicode_latin1[ch]; |
| 476 | if (latin1_char != NULL) { |
| 477 | if (unicode != latin1_char) { |
| 478 | Py_INCREF(latin1_char); |
| 479 | Py_DECREF(unicode); |
| 480 | } |
| 481 | return latin1_char; |
| 482 | } |
| 483 | else { |
| 484 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 485 | Py_INCREF(unicode); |
| 486 | unicode_latin1[ch] = unicode; |
| 487 | return unicode; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 493 | return unicode; |
| 494 | } |
| 495 | |
| 496 | static PyObject* |
| 497 | unicode_result(PyObject *unicode) |
| 498 | { |
| 499 | assert(_PyUnicode_CHECK(unicode)); |
| 500 | if (PyUnicode_IS_READY(unicode)) |
| 501 | return unicode_result_ready(unicode); |
| 502 | else |
| 503 | return unicode_result_wchar(unicode); |
| 504 | } |
| 505 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 506 | static PyObject* |
| 507 | unicode_result_unchanged(PyObject *unicode) |
| 508 | { |
| 509 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 510 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 511 | return NULL; |
| 512 | Py_INCREF(unicode); |
| 513 | return unicode; |
| 514 | } |
| 515 | else |
| 516 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 517 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 520 | #ifdef HAVE_MBCS |
| 521 | static OSVERSIONINFOEX winver; |
| 522 | #endif |
| 523 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 524 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 525 | |
| 526 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 527 | to keep things simple, we use a single bitmask, using the least 5 |
| 528 | bits from each unicode characters as the bit index. */ |
| 529 | |
| 530 | /* the linebreak mask is set up by Unicode_Init below */ |
| 531 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 532 | #if LONG_BIT >= 128 |
| 533 | #define BLOOM_WIDTH 128 |
| 534 | #elif LONG_BIT >= 64 |
| 535 | #define BLOOM_WIDTH 64 |
| 536 | #elif LONG_BIT >= 32 |
| 537 | #define BLOOM_WIDTH 32 |
| 538 | #else |
| 539 | #error "LONG_BIT is smaller than 32" |
| 540 | #endif |
| 541 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 542 | #define BLOOM_MASK unsigned long |
| 543 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 544 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 546 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 548 | #define BLOOM_LINEBREAK(ch) \ |
| 549 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 550 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 551 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 552 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 553 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 554 | { |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame^] | 555 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN) \ |
| 556 | do { \ |
| 557 | TYPE *data = (TYPE *)PTR; \ |
| 558 | TYPE *end = data + LEN; \ |
| 559 | Py_UCS4 ch; \ |
| 560 | for (; data != end; data++) { \ |
| 561 | ch = *data; \ |
| 562 | MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ |
| 563 | } \ |
| 564 | break; \ |
| 565 | } while (0) |
| 566 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 567 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 568 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 569 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 570 | |
| 571 | mask = 0; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame^] | 572 | switch (kind) { |
| 573 | case PyUnicode_1BYTE_KIND: |
| 574 | BLOOM_UPDATE(Py_UCS1, mask, ptr, len); |
| 575 | break; |
| 576 | case PyUnicode_2BYTE_KIND: |
| 577 | BLOOM_UPDATE(Py_UCS2, mask, ptr, len); |
| 578 | break; |
| 579 | case PyUnicode_4BYTE_KIND: |
| 580 | BLOOM_UPDATE(Py_UCS4, mask, ptr, len); |
| 581 | break; |
| 582 | default: |
| 583 | assert(0); |
| 584 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 585 | return mask; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame^] | 586 | |
| 587 | #undef BLOOM_UPDATE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 590 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 591 | (BLOOM(mask, chr) \ |
| 592 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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" |
| 612 | #include "stringlib/find_max_char.h" |
| 613 | #include "stringlib/localeutil.h" |
| 614 | #include "stringlib/undef.h" |
| 615 | |
| 616 | #include "stringlib/ucs2lib.h" |
| 617 | #include "stringlib/fastsearch.h" |
| 618 | #include "stringlib/partition.h" |
| 619 | #include "stringlib/split.h" |
| 620 | #include "stringlib/count.h" |
| 621 | #include "stringlib/find.h" |
| 622 | #include "stringlib/find_max_char.h" |
| 623 | #include "stringlib/localeutil.h" |
| 624 | #include "stringlib/undef.h" |
| 625 | |
| 626 | #include "stringlib/ucs4lib.h" |
| 627 | #include "stringlib/fastsearch.h" |
| 628 | #include "stringlib/partition.h" |
| 629 | #include "stringlib/split.h" |
| 630 | #include "stringlib/count.h" |
| 631 | #include "stringlib/find.h" |
| 632 | #include "stringlib/find_max_char.h" |
| 633 | #include "stringlib/localeutil.h" |
| 634 | #include "stringlib/undef.h" |
| 635 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 636 | #include "stringlib/unicodedefs.h" |
| 637 | #include "stringlib/fastsearch.h" |
| 638 | #include "stringlib/count.h" |
| 639 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 640 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 641 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 642 | /* --- Unicode Object ----------------------------------------------------- */ |
| 643 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 644 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 645 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 646 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 647 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 648 | Py_ssize_t size, Py_UCS4 ch, |
| 649 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 650 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 651 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 652 | |
| 653 | switch (kind) { |
| 654 | case PyUnicode_1BYTE_KIND: |
| 655 | { |
| 656 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 657 | if (ch1 == ch) |
| 658 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 659 | else |
| 660 | return -1; |
| 661 | } |
| 662 | case PyUnicode_2BYTE_KIND: |
| 663 | { |
| 664 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 665 | if (ch2 == ch) |
| 666 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 667 | else |
| 668 | return -1; |
| 669 | } |
| 670 | case PyUnicode_4BYTE_KIND: |
| 671 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 672 | default: |
| 673 | assert(0); |
| 674 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 675 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 676 | } |
| 677 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 678 | #ifdef Py_DEBUG |
| 679 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 680 | earlier. |
| 681 | |
| 682 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 683 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 684 | invalid character in Unicode 6.0. */ |
| 685 | static void |
| 686 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 687 | { |
| 688 | int kind = PyUnicode_KIND(unicode); |
| 689 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 690 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 691 | if (length <= old_length) |
| 692 | return; |
| 693 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 694 | } |
| 695 | #endif |
| 696 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 697 | static PyObject* |
| 698 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 699 | { |
| 700 | Py_ssize_t char_size; |
| 701 | Py_ssize_t struct_size; |
| 702 | Py_ssize_t new_size; |
| 703 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 704 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 705 | #ifdef Py_DEBUG |
| 706 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 707 | #endif |
| 708 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 709 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 710 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 711 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 712 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 713 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 714 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 715 | struct_size = sizeof(PyASCIIObject); |
| 716 | else |
| 717 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 718 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 719 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 720 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 721 | PyErr_NoMemory(); |
| 722 | return NULL; |
| 723 | } |
| 724 | new_size = (struct_size + (length + 1) * char_size); |
| 725 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 726 | _Py_DEC_REFTOTAL; |
| 727 | _Py_ForgetReference(unicode); |
| 728 | |
| 729 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 730 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 731 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 732 | PyErr_NoMemory(); |
| 733 | return NULL; |
| 734 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 735 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 737 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 738 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 739 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 740 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 741 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 742 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 743 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 744 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 745 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 746 | _PyUnicode_WSTR(unicode) = NULL; |
| 747 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 748 | #ifdef Py_DEBUG |
| 749 | unicode_fill_invalid(unicode, old_length); |
| 750 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 751 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 752 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 753 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 754 | return unicode; |
| 755 | } |
| 756 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 757 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 758 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 759 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 760 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 761 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 762 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 763 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 764 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 765 | if (PyUnicode_IS_READY(unicode)) { |
| 766 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 767 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 768 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 769 | #ifdef Py_DEBUG |
| 770 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 771 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 772 | |
| 773 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 774 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 775 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 776 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 777 | |
| 778 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 779 | PyErr_NoMemory(); |
| 780 | return -1; |
| 781 | } |
| 782 | new_size = (length + 1) * char_size; |
| 783 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 784 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 785 | { |
| 786 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 787 | _PyUnicode_UTF8(unicode) = NULL; |
| 788 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 789 | } |
| 790 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 791 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 792 | if (data == NULL) { |
| 793 | PyErr_NoMemory(); |
| 794 | return -1; |
| 795 | } |
| 796 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 797 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 798 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 799 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 800 | } |
| 801 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 802 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 803 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 804 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 805 | _PyUnicode_LENGTH(unicode) = length; |
| 806 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 807 | #ifdef Py_DEBUG |
| 808 | unicode_fill_invalid(unicode, old_length); |
| 809 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 810 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 811 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 812 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 813 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 814 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 815 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 816 | |
| 817 | /* check for integer overflow */ |
| 818 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 819 | PyErr_NoMemory(); |
| 820 | return -1; |
| 821 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 822 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 823 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 824 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 825 | if (!wstr) { |
| 826 | PyErr_NoMemory(); |
| 827 | return -1; |
| 828 | } |
| 829 | _PyUnicode_WSTR(unicode) = wstr; |
| 830 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 831 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 832 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 836 | static PyObject* |
| 837 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 838 | { |
| 839 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 840 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 841 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 842 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 843 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 844 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 845 | |
| 846 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 847 | if (copy == NULL) |
| 848 | return NULL; |
| 849 | |
| 850 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 851 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 852 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 853 | } |
| 854 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 855 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 856 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 857 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 858 | if (w == NULL) |
| 859 | return NULL; |
| 860 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 861 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 862 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 863 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 864 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 868 | /* 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] | 869 | Ux0000 terminated; some code (e.g. new_identifier) |
| 870 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 871 | |
| 872 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 873 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 874 | |
| 875 | */ |
| 876 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 877 | static PyUnicodeObject * |
| 878 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 879 | { |
| 880 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 881 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 882 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 883 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 884 | if (length == 0 && unicode_empty != NULL) { |
| 885 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 886 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 889 | /* Ensure we won't overflow the size. */ |
| 890 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 891 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 892 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 893 | if (length < 0) { |
| 894 | PyErr_SetString(PyExc_SystemError, |
| 895 | "Negative size passed to _PyUnicode_New"); |
| 896 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 899 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 900 | if (unicode == NULL) |
| 901 | return NULL; |
| 902 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 903 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 904 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 905 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 906 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 907 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 908 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 909 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 910 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 911 | * the caller fails before initializing str -- unicode_resize() |
| 912 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 913 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 914 | * We don't want unicode_resize to read uninitialized memory in |
| 915 | * that case. |
| 916 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 917 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 918 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 919 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 920 | _PyUnicode_HASH(unicode) = -1; |
| 921 | _PyUnicode_STATE(unicode).interned = 0; |
| 922 | _PyUnicode_STATE(unicode).kind = 0; |
| 923 | _PyUnicode_STATE(unicode).compact = 0; |
| 924 | _PyUnicode_STATE(unicode).ready = 0; |
| 925 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 926 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 927 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 928 | _PyUnicode_UTF8(unicode) = NULL; |
| 929 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 930 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 931 | return unicode; |
| 932 | } |
| 933 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 934 | static const char* |
| 935 | unicode_kind_name(PyObject *unicode) |
| 936 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 937 | /* don't check consistency: unicode_kind_name() is called from |
| 938 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 939 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 940 | { |
| 941 | if (!PyUnicode_IS_READY(unicode)) |
| 942 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 943 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 944 | { |
| 945 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 946 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 947 | return "legacy ascii"; |
| 948 | else |
| 949 | return "legacy latin1"; |
| 950 | case PyUnicode_2BYTE_KIND: |
| 951 | return "legacy UCS2"; |
| 952 | case PyUnicode_4BYTE_KIND: |
| 953 | return "legacy UCS4"; |
| 954 | default: |
| 955 | return "<legacy invalid kind>"; |
| 956 | } |
| 957 | } |
| 958 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 959 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 960 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 961 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 962 | return "ascii"; |
| 963 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 964 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 965 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 966 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 967 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 968 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 969 | default: |
| 970 | return "<invalid compact kind>"; |
| 971 | } |
| 972 | } |
| 973 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 975 | /* Functions wrapping macros for use in debugger */ |
| 976 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 977 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | void *_PyUnicode_compact_data(void *unicode) { |
| 981 | return _PyUnicode_COMPACT_DATA(unicode); |
| 982 | } |
| 983 | void *_PyUnicode_data(void *unicode){ |
| 984 | printf("obj %p\n", unicode); |
| 985 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 986 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 987 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 988 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 989 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 990 | return PyUnicode_DATA(unicode); |
| 991 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 992 | |
| 993 | void |
| 994 | _PyUnicode_Dump(PyObject *op) |
| 995 | { |
| 996 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 997 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 998 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 999 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1000 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1001 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1002 | { |
| 1003 | if (ascii->state.ascii) |
| 1004 | data = (ascii + 1); |
| 1005 | else |
| 1006 | data = (compact + 1); |
| 1007 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1008 | else |
| 1009 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1010 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 1011 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1012 | if (ascii->wstr == data) |
| 1013 | printf("shared "); |
| 1014 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1015 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1016 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1017 | printf(" (%zu), ", compact->wstr_length); |
| 1018 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1019 | printf("shared "); |
| 1020 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1021 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1022 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1023 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1024 | #endif |
| 1025 | |
| 1026 | PyObject * |
| 1027 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1028 | { |
| 1029 | PyObject *obj; |
| 1030 | PyCompactUnicodeObject *unicode; |
| 1031 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1032 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1033 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1034 | Py_ssize_t char_size; |
| 1035 | Py_ssize_t struct_size; |
| 1036 | |
| 1037 | /* Optimization for empty strings */ |
| 1038 | if (size == 0 && unicode_empty != NULL) { |
| 1039 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1040 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1043 | is_ascii = 0; |
| 1044 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1045 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1046 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1047 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | char_size = 1; |
| 1049 | is_ascii = 1; |
| 1050 | struct_size = sizeof(PyASCIIObject); |
| 1051 | } |
| 1052 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1053 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1054 | char_size = 1; |
| 1055 | } |
| 1056 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1057 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1058 | char_size = 2; |
| 1059 | if (sizeof(wchar_t) == 2) |
| 1060 | is_sharing = 1; |
| 1061 | } |
| 1062 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1063 | if (maxchar > MAX_UNICODE) { |
| 1064 | PyErr_SetString(PyExc_SystemError, |
| 1065 | "invalid maximum character passed to PyUnicode_New"); |
| 1066 | return NULL; |
| 1067 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1068 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1069 | char_size = 4; |
| 1070 | if (sizeof(wchar_t) == 4) |
| 1071 | is_sharing = 1; |
| 1072 | } |
| 1073 | |
| 1074 | /* Ensure we won't overflow the size. */ |
| 1075 | if (size < 0) { |
| 1076 | PyErr_SetString(PyExc_SystemError, |
| 1077 | "Negative size passed to PyUnicode_New"); |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1081 | return PyErr_NoMemory(); |
| 1082 | |
| 1083 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1084 | * PyObject_New() so we are able to allocate space for the object and |
| 1085 | * it's data buffer. |
| 1086 | */ |
| 1087 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1088 | if (obj == NULL) |
| 1089 | return PyErr_NoMemory(); |
| 1090 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1091 | if (obj == NULL) |
| 1092 | return NULL; |
| 1093 | |
| 1094 | unicode = (PyCompactUnicodeObject *)obj; |
| 1095 | if (is_ascii) |
| 1096 | data = ((PyASCIIObject*)obj) + 1; |
| 1097 | else |
| 1098 | data = unicode + 1; |
| 1099 | _PyUnicode_LENGTH(unicode) = size; |
| 1100 | _PyUnicode_HASH(unicode) = -1; |
| 1101 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1102 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1103 | _PyUnicode_STATE(unicode).compact = 1; |
| 1104 | _PyUnicode_STATE(unicode).ready = 1; |
| 1105 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1106 | if (is_ascii) { |
| 1107 | ((char*)data)[size] = 0; |
| 1108 | _PyUnicode_WSTR(unicode) = NULL; |
| 1109 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1110 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1111 | ((char*)data)[size] = 0; |
| 1112 | _PyUnicode_WSTR(unicode) = NULL; |
| 1113 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1114 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1115 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1116 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1117 | else { |
| 1118 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1119 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1120 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1121 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1122 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1123 | ((Py_UCS4*)data)[size] = 0; |
| 1124 | if (is_sharing) { |
| 1125 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1126 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1127 | } |
| 1128 | else { |
| 1129 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1130 | _PyUnicode_WSTR(unicode) = NULL; |
| 1131 | } |
| 1132 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1133 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1134 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1135 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1136 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1137 | return obj; |
| 1138 | } |
| 1139 | |
| 1140 | #if SIZEOF_WCHAR_T == 2 |
| 1141 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1142 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1143 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | |
| 1145 | This function assumes that unicode can hold one more code point than wstr |
| 1146 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1147 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | 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] | 1149 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | { |
| 1151 | const wchar_t *iter; |
| 1152 | Py_UCS4 *ucs4_out; |
| 1153 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1154 | assert(unicode != NULL); |
| 1155 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1157 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1158 | |
| 1159 | for (iter = begin; iter < end; ) { |
| 1160 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1161 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1162 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1163 | && (iter+1) < end |
| 1164 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1165 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1166 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1167 | iter += 2; |
| 1168 | } |
| 1169 | else { |
| 1170 | *ucs4_out++ = *iter; |
| 1171 | iter++; |
| 1172 | } |
| 1173 | } |
| 1174 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1175 | _PyUnicode_GET_LENGTH(unicode))); |
| 1176 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1177 | } |
| 1178 | #endif |
| 1179 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1180 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1181 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1182 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1183 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1184 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1185 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1186 | return -1; |
| 1187 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1188 | return 0; |
| 1189 | } |
| 1190 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1191 | static int |
| 1192 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1193 | PyObject *from, Py_ssize_t from_start, |
| 1194 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1195 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1196 | unsigned int from_kind, to_kind; |
| 1197 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1198 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1199 | assert(0 <= how_many); |
| 1200 | assert(0 <= from_start); |
| 1201 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1202 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1203 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1204 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1205 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1206 | assert(PyUnicode_Check(to)); |
| 1207 | assert(PyUnicode_IS_READY(to)); |
| 1208 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1209 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1210 | if (how_many == 0) |
| 1211 | return 0; |
| 1212 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1213 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1214 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1215 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1216 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1217 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1218 | #ifdef Py_DEBUG |
| 1219 | if (!check_maxchar |
| 1220 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1221 | { |
| 1222 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1223 | Py_UCS4 ch; |
| 1224 | Py_ssize_t i; |
| 1225 | for (i=0; i < how_many; i++) { |
| 1226 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1227 | assert(ch <= to_maxchar); |
| 1228 | } |
| 1229 | } |
| 1230 | #endif |
| 1231 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1232 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1233 | if (check_maxchar |
| 1234 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1235 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1236 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1237 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1238 | Py_UCS4 max_char; |
| 1239 | max_char = ucs1lib_find_max_char(from_data, |
| 1240 | (Py_UCS1*)from_data + how_many); |
| 1241 | if (max_char >= 128) |
| 1242 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1243 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1244 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1245 | (char*)from_data + from_kind * from_start, |
| 1246 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1247 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1248 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1249 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1250 | { |
| 1251 | _PyUnicode_CONVERT_BYTES( |
| 1252 | Py_UCS1, Py_UCS2, |
| 1253 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1254 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1255 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1256 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1257 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1258 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1259 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1260 | { |
| 1261 | _PyUnicode_CONVERT_BYTES( |
| 1262 | Py_UCS1, Py_UCS4, |
| 1263 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1264 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1265 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1266 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1267 | } |
| 1268 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1269 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1270 | { |
| 1271 | _PyUnicode_CONVERT_BYTES( |
| 1272 | Py_UCS2, Py_UCS4, |
| 1273 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1274 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1275 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1276 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1277 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1278 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1279 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1280 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1281 | if (!check_maxchar) { |
| 1282 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1283 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1284 | { |
| 1285 | _PyUnicode_CONVERT_BYTES( |
| 1286 | Py_UCS2, Py_UCS1, |
| 1287 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1288 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1289 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1290 | ); |
| 1291 | } |
| 1292 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1293 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1294 | { |
| 1295 | _PyUnicode_CONVERT_BYTES( |
| 1296 | Py_UCS4, Py_UCS1, |
| 1297 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1298 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1299 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1300 | ); |
| 1301 | } |
| 1302 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1303 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1304 | { |
| 1305 | _PyUnicode_CONVERT_BYTES( |
| 1306 | Py_UCS4, Py_UCS2, |
| 1307 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1308 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1309 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1310 | ); |
| 1311 | } |
| 1312 | else { |
| 1313 | assert(0); |
| 1314 | return -1; |
| 1315 | } |
| 1316 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1317 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1318 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1319 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1320 | Py_ssize_t i; |
| 1321 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1322 | for (i=0; i < how_many; i++) { |
| 1323 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1324 | if (ch > to_maxchar) |
| 1325 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1326 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1327 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1328 | } |
| 1329 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1330 | return 0; |
| 1331 | } |
| 1332 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1333 | void |
| 1334 | _PyUnicode_FastCopyCharacters( |
| 1335 | PyObject *to, Py_ssize_t to_start, |
| 1336 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1337 | { |
| 1338 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1339 | } |
| 1340 | |
| 1341 | Py_ssize_t |
| 1342 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1343 | PyObject *from, Py_ssize_t from_start, |
| 1344 | Py_ssize_t how_many) |
| 1345 | { |
| 1346 | int err; |
| 1347 | |
| 1348 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1349 | PyErr_BadInternalCall(); |
| 1350 | return -1; |
| 1351 | } |
| 1352 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1353 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1354 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1355 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1356 | return -1; |
| 1357 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1358 | if (from_start < 0) { |
| 1359 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1360 | return -1; |
| 1361 | } |
| 1362 | if (to_start < 0) { |
| 1363 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1364 | return -1; |
| 1365 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1366 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1367 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1368 | PyErr_Format(PyExc_SystemError, |
| 1369 | "Cannot write %zi characters at %zi " |
| 1370 | "in a string of %zi characters", |
| 1371 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1372 | return -1; |
| 1373 | } |
| 1374 | |
| 1375 | if (how_many == 0) |
| 1376 | return 0; |
| 1377 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1378 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1379 | return -1; |
| 1380 | |
| 1381 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1382 | if (err) { |
| 1383 | PyErr_Format(PyExc_SystemError, |
| 1384 | "Cannot copy %s characters " |
| 1385 | "into a string of %s characters", |
| 1386 | unicode_kind_name(from), |
| 1387 | unicode_kind_name(to)); |
| 1388 | return -1; |
| 1389 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1390 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1391 | } |
| 1392 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1393 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1394 | correct string length can be computed before converting a string to UCS4. |
| 1395 | This function counts single surrogates as a character and not as a pair. |
| 1396 | |
| 1397 | Return 0 on success, or -1 on error. */ |
| 1398 | static int |
| 1399 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1400 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1401 | { |
| 1402 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1403 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1404 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1405 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1406 | *num_surrogates = 0; |
| 1407 | *maxchar = 0; |
| 1408 | |
| 1409 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1410 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1411 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1412 | && (iter+1) < end |
| 1413 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1414 | { |
| 1415 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1416 | ++(*num_surrogates); |
| 1417 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1418 | } |
| 1419 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1420 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1421 | { |
| 1422 | ch = *iter; |
| 1423 | iter++; |
| 1424 | } |
| 1425 | if (ch > *maxchar) { |
| 1426 | *maxchar = ch; |
| 1427 | if (*maxchar > MAX_UNICODE) { |
| 1428 | PyErr_Format(PyExc_ValueError, |
| 1429 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1430 | ch); |
| 1431 | return -1; |
| 1432 | } |
| 1433 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1434 | } |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1438 | int |
| 1439 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1440 | { |
| 1441 | wchar_t *end; |
| 1442 | Py_UCS4 maxchar = 0; |
| 1443 | Py_ssize_t num_surrogates; |
| 1444 | #if SIZEOF_WCHAR_T == 2 |
| 1445 | Py_ssize_t length_wo_surrogates; |
| 1446 | #endif |
| 1447 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1448 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1449 | strings were created using _PyObject_New() and where no canonical |
| 1450 | representation (the str field) has been set yet aka strings |
| 1451 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1452 | assert(_PyUnicode_CHECK(unicode)); |
| 1453 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1454 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1455 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1456 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1457 | /* Actually, it should neither be interned nor be anything else: */ |
| 1458 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1460 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1461 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1462 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1463 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1464 | |
| 1465 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1466 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1467 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | PyErr_NoMemory(); |
| 1469 | return -1; |
| 1470 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1471 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1472 | _PyUnicode_WSTR(unicode), end, |
| 1473 | PyUnicode_1BYTE_DATA(unicode)); |
| 1474 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1475 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1476 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1477 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1478 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1479 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1480 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1481 | } |
| 1482 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1483 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1484 | _PyUnicode_UTF8(unicode) = NULL; |
| 1485 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1486 | } |
| 1487 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1488 | _PyUnicode_WSTR(unicode) = NULL; |
| 1489 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1490 | } |
| 1491 | /* In this case we might have to convert down from 4-byte native |
| 1492 | wchar_t to 2-byte unicode. */ |
| 1493 | else if (maxchar < 65536) { |
| 1494 | assert(num_surrogates == 0 && |
| 1495 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1496 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1497 | #if SIZEOF_WCHAR_T == 2 |
| 1498 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1499 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1500 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1501 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1502 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1503 | _PyUnicode_UTF8(unicode) = NULL; |
| 1504 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1505 | #else |
| 1506 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1507 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1508 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1509 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1510 | PyErr_NoMemory(); |
| 1511 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1512 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1513 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1514 | _PyUnicode_WSTR(unicode), end, |
| 1515 | PyUnicode_2BYTE_DATA(unicode)); |
| 1516 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1517 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1518 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1519 | _PyUnicode_UTF8(unicode) = NULL; |
| 1520 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1521 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1522 | _PyUnicode_WSTR(unicode) = NULL; |
| 1523 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1524 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1525 | } |
| 1526 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1527 | else { |
| 1528 | #if SIZEOF_WCHAR_T == 2 |
| 1529 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1530 | new normalized 4-byte version. */ |
| 1531 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1532 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1533 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1534 | PyErr_NoMemory(); |
| 1535 | return -1; |
| 1536 | } |
| 1537 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1538 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1539 | _PyUnicode_UTF8(unicode) = NULL; |
| 1540 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1541 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1542 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1543 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1544 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1545 | _PyUnicode_WSTR(unicode) = NULL; |
| 1546 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1547 | #else |
| 1548 | assert(num_surrogates == 0); |
| 1549 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1550 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1551 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1552 | _PyUnicode_UTF8(unicode) = NULL; |
| 1553 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1554 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1555 | #endif |
| 1556 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1557 | } |
| 1558 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1559 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1560 | return 0; |
| 1561 | } |
| 1562 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1563 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1564 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1565 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1566 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1567 | case SSTATE_NOT_INTERNED: |
| 1568 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1569 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1570 | case SSTATE_INTERNED_MORTAL: |
| 1571 | /* revive dead object temporarily for DelItem */ |
| 1572 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1573 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1574 | Py_FatalError( |
| 1575 | "deletion of interned string failed"); |
| 1576 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1577 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1578 | case SSTATE_INTERNED_IMMORTAL: |
| 1579 | Py_FatalError("Immortal interned string died."); |
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 | default: |
| 1582 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1585 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1586 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1587 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1588 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1589 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1590 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1591 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1592 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1595 | #ifdef Py_DEBUG |
| 1596 | static int |
| 1597 | unicode_is_singleton(PyObject *unicode) |
| 1598 | { |
| 1599 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1600 | if (unicode == unicode_empty) |
| 1601 | return 1; |
| 1602 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1603 | { |
| 1604 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1605 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1606 | return 1; |
| 1607 | } |
| 1608 | return 0; |
| 1609 | } |
| 1610 | #endif |
| 1611 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1612 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1613 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1614 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1615 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1616 | if (Py_REFCNT(unicode) != 1) |
| 1617 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1618 | if (_PyUnicode_HASH(unicode) != -1) |
| 1619 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1620 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1621 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1622 | if (!PyUnicode_CheckExact(unicode)) |
| 1623 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1624 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1625 | /* singleton refcount is greater than 1 */ |
| 1626 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1627 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1628 | return 1; |
| 1629 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1630 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1631 | static int |
| 1632 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1633 | { |
| 1634 | PyObject *unicode; |
| 1635 | Py_ssize_t old_length; |
| 1636 | |
| 1637 | assert(p_unicode != NULL); |
| 1638 | unicode = *p_unicode; |
| 1639 | |
| 1640 | assert(unicode != NULL); |
| 1641 | assert(PyUnicode_Check(unicode)); |
| 1642 | assert(0 <= length); |
| 1643 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1644 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1645 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1646 | else |
| 1647 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1648 | if (old_length == length) |
| 1649 | return 0; |
| 1650 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1651 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1652 | _Py_INCREF_UNICODE_EMPTY(); |
| 1653 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1654 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1655 | Py_DECREF(*p_unicode); |
| 1656 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1657 | return 0; |
| 1658 | } |
| 1659 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1660 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1661 | PyObject *copy = resize_copy(unicode, length); |
| 1662 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1663 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1664 | Py_DECREF(*p_unicode); |
| 1665 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1666 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1669 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1670 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1671 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1672 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1673 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1674 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1675 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1676 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1679 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1680 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1681 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1682 | PyObject *unicode; |
| 1683 | if (p_unicode == NULL) { |
| 1684 | PyErr_BadInternalCall(); |
| 1685 | return -1; |
| 1686 | } |
| 1687 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1688 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1689 | { |
| 1690 | PyErr_BadInternalCall(); |
| 1691 | return -1; |
| 1692 | } |
| 1693 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1694 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1695 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1696 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1697 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1698 | WARNING: The function doesn't copy the terminating null character and |
| 1699 | doesn't check the maximum character (may write a latin1 character in an |
| 1700 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1701 | static void |
| 1702 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1703 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1704 | { |
| 1705 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1706 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1707 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1708 | |
| 1709 | switch (kind) { |
| 1710 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1711 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1712 | #ifdef Py_DEBUG |
| 1713 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1714 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1715 | (const Py_UCS1*)str, |
| 1716 | (const Py_UCS1*)str + len); |
| 1717 | assert(maxchar < 128); |
| 1718 | } |
| 1719 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1720 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1721 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1722 | } |
| 1723 | case PyUnicode_2BYTE_KIND: { |
| 1724 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1725 | Py_UCS2 *ucs2 = start; |
| 1726 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1727 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1728 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1729 | *ucs2 = (Py_UCS2)*str; |
| 1730 | |
| 1731 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1732 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1733 | } |
| 1734 | default: { |
| 1735 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1736 | Py_UCS4 *ucs4 = start; |
| 1737 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1738 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1739 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1740 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1741 | *ucs4 = (Py_UCS4)*str; |
| 1742 | |
| 1743 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1749 | static PyObject* |
| 1750 | get_latin1_char(unsigned char ch) |
| 1751 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1752 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1753 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1754 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1755 | if (!unicode) |
| 1756 | return NULL; |
| 1757 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1758 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1759 | unicode_latin1[ch] = unicode; |
| 1760 | } |
| 1761 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1762 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1763 | } |
| 1764 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1765 | PyObject * |
| 1766 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1767 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1768 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1769 | Py_UCS4 maxchar = 0; |
| 1770 | Py_ssize_t num_surrogates; |
| 1771 | |
| 1772 | if (u == NULL) |
| 1773 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1774 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1775 | /* If the Unicode data is known at construction time, we can apply |
| 1776 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1777 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1778 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1779 | if (size == 0) |
| 1780 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1781 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1782 | /* Single character Unicode objects in the Latin-1 range are |
| 1783 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1784 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1785 | return get_latin1_char((unsigned char)*u); |
| 1786 | |
| 1787 | /* If not empty and not single character, copy the Unicode data |
| 1788 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1789 | if (find_maxchar_surrogates(u, u + size, |
| 1790 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1791 | return NULL; |
| 1792 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1793 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1794 | if (!unicode) |
| 1795 | return NULL; |
| 1796 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1797 | switch (PyUnicode_KIND(unicode)) { |
| 1798 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1799 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1800 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1801 | break; |
| 1802 | case PyUnicode_2BYTE_KIND: |
| 1803 | #if Py_UNICODE_SIZE == 2 |
| 1804 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1805 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1806 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1807 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1808 | #endif |
| 1809 | break; |
| 1810 | case PyUnicode_4BYTE_KIND: |
| 1811 | #if SIZEOF_WCHAR_T == 2 |
| 1812 | /* This is the only case which has to process surrogates, thus |
| 1813 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1814 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1815 | #else |
| 1816 | assert(num_surrogates == 0); |
| 1817 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1818 | #endif |
| 1819 | break; |
| 1820 | default: |
| 1821 | assert(0 && "Impossible state"); |
| 1822 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1823 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1824 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1827 | PyObject * |
| 1828 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1829 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1830 | if (size < 0) { |
| 1831 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1832 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1833 | return NULL; |
| 1834 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1835 | if (u != NULL) |
| 1836 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1837 | else |
| 1838 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1841 | PyObject * |
| 1842 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1843 | { |
| 1844 | size_t size = strlen(u); |
| 1845 | if (size > PY_SSIZE_T_MAX) { |
| 1846 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1847 | return NULL; |
| 1848 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1849 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1852 | PyObject * |
| 1853 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1854 | { |
| 1855 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1856 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1857 | strlen(id->string), |
| 1858 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1859 | if (!id->object) |
| 1860 | return NULL; |
| 1861 | PyUnicode_InternInPlace(&id->object); |
| 1862 | assert(!id->next); |
| 1863 | id->next = static_strings; |
| 1864 | static_strings = id; |
| 1865 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1866 | return id->object; |
| 1867 | } |
| 1868 | |
| 1869 | void |
| 1870 | _PyUnicode_ClearStaticStrings() |
| 1871 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1872 | _Py_Identifier *tmp, *s = static_strings; |
| 1873 | while (s) { |
| 1874 | Py_DECREF(s->object); |
| 1875 | s->object = NULL; |
| 1876 | tmp = s->next; |
| 1877 | s->next = NULL; |
| 1878 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1879 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1880 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1881 | } |
| 1882 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1883 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1884 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1885 | PyObject* |
| 1886 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1887 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1888 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1889 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1890 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1891 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1892 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1893 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1894 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1895 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1896 | unicode = PyUnicode_New(size, 127); |
| 1897 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1898 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1899 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1900 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1901 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1902 | } |
| 1903 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1904 | static Py_UCS4 |
| 1905 | kind_maxchar_limit(unsigned int kind) |
| 1906 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1907 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1908 | case PyUnicode_1BYTE_KIND: |
| 1909 | return 0x80; |
| 1910 | case PyUnicode_2BYTE_KIND: |
| 1911 | return 0x100; |
| 1912 | case PyUnicode_4BYTE_KIND: |
| 1913 | return 0x10000; |
| 1914 | default: |
| 1915 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1916 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1920 | Py_LOCAL_INLINE(Py_UCS4) |
| 1921 | align_maxchar(Py_UCS4 maxchar) |
| 1922 | { |
| 1923 | if (maxchar <= 127) |
| 1924 | return 127; |
| 1925 | else if (maxchar <= 255) |
| 1926 | return 255; |
| 1927 | else if (maxchar <= 65535) |
| 1928 | return 65535; |
| 1929 | else |
| 1930 | return MAX_UNICODE; |
| 1931 | } |
| 1932 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1933 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1934 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1935 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1936 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1937 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1938 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1939 | if (size == 0) |
| 1940 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1941 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1942 | if (size == 1) |
| 1943 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1944 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1945 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1946 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1947 | if (!res) |
| 1948 | return NULL; |
| 1949 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1950 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1951 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1954 | static PyObject* |
| 1955 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1956 | { |
| 1957 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1958 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1959 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1960 | if (size == 0) |
| 1961 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1962 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1963 | if (size == 1) { |
| 1964 | Py_UCS4 ch = u[0]; |
| 1965 | if (ch < 256) |
| 1966 | return get_latin1_char((unsigned char)ch); |
| 1967 | |
| 1968 | res = PyUnicode_New(1, ch); |
| 1969 | if (res == NULL) |
| 1970 | return NULL; |
| 1971 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1972 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1973 | return res; |
| 1974 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1975 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1976 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1977 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1978 | if (!res) |
| 1979 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1980 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1982 | else { |
| 1983 | _PyUnicode_CONVERT_BYTES( |
| 1984 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1985 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1986 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1987 | return res; |
| 1988 | } |
| 1989 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1990 | static PyObject* |
| 1991 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | { |
| 1993 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1994 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1995 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1996 | if (size == 0) |
| 1997 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1998 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1999 | if (size == 1) { |
| 2000 | Py_UCS4 ch = u[0]; |
| 2001 | if (ch < 256) |
| 2002 | return get_latin1_char((unsigned char)ch); |
| 2003 | |
| 2004 | res = PyUnicode_New(1, ch); |
| 2005 | if (res == NULL) |
| 2006 | return NULL; |
| 2007 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 2008 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 2009 | return res; |
| 2010 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2011 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2012 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2013 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2014 | if (!res) |
| 2015 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2016 | if (max_char < 256) |
| 2017 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2018 | PyUnicode_1BYTE_DATA(res)); |
| 2019 | else if (max_char < 0x10000) |
| 2020 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2021 | PyUnicode_2BYTE_DATA(res)); |
| 2022 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2023 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2024 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2025 | return res; |
| 2026 | } |
| 2027 | |
| 2028 | PyObject* |
| 2029 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2030 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2031 | if (size < 0) { |
| 2032 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2033 | return NULL; |
| 2034 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2035 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2036 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2037 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2038 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2039 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2040 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2041 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2042 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2043 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2044 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2046 | } |
| 2047 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2048 | Py_UCS4 |
| 2049 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2050 | { |
| 2051 | enum PyUnicode_Kind kind; |
| 2052 | void *startptr, *endptr; |
| 2053 | |
| 2054 | assert(PyUnicode_IS_READY(unicode)); |
| 2055 | assert(0 <= start); |
| 2056 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2057 | assert(start <= end); |
| 2058 | |
| 2059 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2060 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2061 | |
| 2062 | if (start == end) |
| 2063 | return 127; |
| 2064 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2065 | if (PyUnicode_IS_ASCII(unicode)) |
| 2066 | return 127; |
| 2067 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2068 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2069 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2070 | endptr = (char *)startptr + end * kind; |
| 2071 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2072 | switch(kind) { |
| 2073 | case PyUnicode_1BYTE_KIND: |
| 2074 | return ucs1lib_find_max_char(startptr, endptr); |
| 2075 | case PyUnicode_2BYTE_KIND: |
| 2076 | return ucs2lib_find_max_char(startptr, endptr); |
| 2077 | case PyUnicode_4BYTE_KIND: |
| 2078 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2079 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2080 | assert(0); |
| 2081 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2082 | } |
| 2083 | } |
| 2084 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2085 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2086 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2087 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2088 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2089 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2090 | { |
| 2091 | PyObject *unicode, *copy; |
| 2092 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2093 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2094 | unsigned int kind; |
| 2095 | |
| 2096 | assert(p_unicode != NULL); |
| 2097 | unicode = *p_unicode; |
| 2098 | assert(PyUnicode_IS_READY(unicode)); |
| 2099 | if (PyUnicode_IS_ASCII(unicode)) |
| 2100 | return; |
| 2101 | |
| 2102 | len = PyUnicode_GET_LENGTH(unicode); |
| 2103 | kind = PyUnicode_KIND(unicode); |
| 2104 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2105 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2106 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2107 | if (max_char >= 128) |
| 2108 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2109 | } |
| 2110 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2111 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2112 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2113 | if (max_char >= 256) |
| 2114 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2115 | } |
| 2116 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2117 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2118 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2119 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2120 | if (max_char >= 0x10000) |
| 2121 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2122 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2123 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2124 | if (copy != NULL) |
| 2125 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2126 | Py_DECREF(unicode); |
| 2127 | *p_unicode = copy; |
| 2128 | } |
| 2129 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2130 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2131 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2132 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2133 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2134 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2135 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2136 | if (!PyUnicode_Check(unicode)) { |
| 2137 | PyErr_BadInternalCall(); |
| 2138 | return NULL; |
| 2139 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2140 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2141 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2142 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2143 | length = PyUnicode_GET_LENGTH(unicode); |
| 2144 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2145 | if (!copy) |
| 2146 | return NULL; |
| 2147 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2148 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2149 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2150 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2151 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2152 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2153 | } |
| 2154 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2155 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2156 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2157 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2158 | |
| 2159 | void* |
| 2160 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2161 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2162 | Py_ssize_t len; |
| 2163 | void *result; |
| 2164 | unsigned int skind; |
| 2165 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2166 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2167 | return NULL; |
| 2168 | |
| 2169 | len = PyUnicode_GET_LENGTH(s); |
| 2170 | skind = PyUnicode_KIND(s); |
| 2171 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2172 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2173 | return NULL; |
| 2174 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2175 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2176 | case PyUnicode_2BYTE_KIND: |
| 2177 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2178 | if (!result) |
| 2179 | return PyErr_NoMemory(); |
| 2180 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2181 | _PyUnicode_CONVERT_BYTES( |
| 2182 | Py_UCS1, Py_UCS2, |
| 2183 | PyUnicode_1BYTE_DATA(s), |
| 2184 | PyUnicode_1BYTE_DATA(s) + len, |
| 2185 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2186 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2187 | case PyUnicode_4BYTE_KIND: |
| 2188 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2189 | if (!result) |
| 2190 | return PyErr_NoMemory(); |
| 2191 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2192 | _PyUnicode_CONVERT_BYTES( |
| 2193 | Py_UCS2, Py_UCS4, |
| 2194 | PyUnicode_2BYTE_DATA(s), |
| 2195 | PyUnicode_2BYTE_DATA(s) + len, |
| 2196 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2197 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2198 | else { |
| 2199 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2200 | _PyUnicode_CONVERT_BYTES( |
| 2201 | Py_UCS1, Py_UCS4, |
| 2202 | PyUnicode_1BYTE_DATA(s), |
| 2203 | PyUnicode_1BYTE_DATA(s) + len, |
| 2204 | result); |
| 2205 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2206 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2207 | default: |
| 2208 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2209 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2210 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2211 | return NULL; |
| 2212 | } |
| 2213 | |
| 2214 | static Py_UCS4* |
| 2215 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2216 | int copy_null) |
| 2217 | { |
| 2218 | int kind; |
| 2219 | void *data; |
| 2220 | Py_ssize_t len, targetlen; |
| 2221 | if (PyUnicode_READY(string) == -1) |
| 2222 | return NULL; |
| 2223 | kind = PyUnicode_KIND(string); |
| 2224 | data = PyUnicode_DATA(string); |
| 2225 | len = PyUnicode_GET_LENGTH(string); |
| 2226 | targetlen = len; |
| 2227 | if (copy_null) |
| 2228 | targetlen++; |
| 2229 | if (!target) { |
| 2230 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2231 | PyErr_NoMemory(); |
| 2232 | return NULL; |
| 2233 | } |
| 2234 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2235 | if (!target) { |
| 2236 | PyErr_NoMemory(); |
| 2237 | return NULL; |
| 2238 | } |
| 2239 | } |
| 2240 | else { |
| 2241 | if (targetsize < targetlen) { |
| 2242 | PyErr_Format(PyExc_SystemError, |
| 2243 | "string is longer than the buffer"); |
| 2244 | if (copy_null && 0 < targetsize) |
| 2245 | target[0] = 0; |
| 2246 | return NULL; |
| 2247 | } |
| 2248 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2249 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2250 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2251 | _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] | 2252 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2253 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2254 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2255 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2256 | } |
| 2257 | else { |
| 2258 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2259 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2260 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2261 | if (copy_null) |
| 2262 | target[len] = 0; |
| 2263 | return target; |
| 2264 | } |
| 2265 | |
| 2266 | Py_UCS4* |
| 2267 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2268 | int copy_null) |
| 2269 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2270 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2271 | PyErr_BadInternalCall(); |
| 2272 | return NULL; |
| 2273 | } |
| 2274 | return as_ucs4(string, target, targetsize, copy_null); |
| 2275 | } |
| 2276 | |
| 2277 | Py_UCS4* |
| 2278 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2279 | { |
| 2280 | return as_ucs4(string, NULL, 0, 1); |
| 2281 | } |
| 2282 | |
| 2283 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2284 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2285 | PyObject * |
| 2286 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2287 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2288 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2289 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2290 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2291 | PyErr_BadInternalCall(); |
| 2292 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2295 | if (size == -1) { |
| 2296 | size = wcslen(w); |
| 2297 | } |
| 2298 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2299 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2302 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2303 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2304 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2305 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2306 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2307 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2308 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2309 | if (longflag) |
| 2310 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2311 | else if (longlongflag) { |
| 2312 | /* longlongflag should only ever be nonzero on machines with |
| 2313 | HAVE_LONG_LONG defined */ |
| 2314 | #ifdef HAVE_LONG_LONG |
| 2315 | char *f = PY_FORMAT_LONG_LONG; |
| 2316 | while (*f) |
| 2317 | *fmt++ = *f++; |
| 2318 | #else |
| 2319 | /* we shouldn't ever get here */ |
| 2320 | assert(0); |
| 2321 | *fmt++ = 'l'; |
| 2322 | #endif |
| 2323 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2324 | else if (size_tflag) { |
| 2325 | char *f = PY_FORMAT_SIZE_T; |
| 2326 | while (*f) |
| 2327 | *fmt++ = *f++; |
| 2328 | } |
| 2329 | *fmt++ = c; |
| 2330 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2333 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2334 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2335 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2336 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2337 | |
| 2338 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2339 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2340 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2341 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2342 | const char *p; |
| 2343 | Py_ssize_t len; |
| 2344 | int zeropad; |
| 2345 | int width; |
| 2346 | int precision; |
| 2347 | int longflag; |
| 2348 | int longlongflag; |
| 2349 | int size_tflag; |
| 2350 | int fill; |
| 2351 | |
| 2352 | p = f; |
| 2353 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2354 | zeropad = 0; |
| 2355 | if (*f == '0') { |
| 2356 | zeropad = 1; |
| 2357 | f++; |
| 2358 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2359 | |
| 2360 | /* 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] | 2361 | width = 0; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2362 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2363 | if (width > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2364 | PyErr_SetString(PyExc_ValueError, |
| 2365 | "width too big"); |
| 2366 | return NULL; |
| 2367 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2368 | width = (width*10) + (*f - '0'); |
| 2369 | f++; |
| 2370 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2371 | precision = 0; |
| 2372 | if (*f == '.') { |
| 2373 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2374 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2375 | if (precision > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2376 | PyErr_SetString(PyExc_ValueError, |
| 2377 | "precision too big"); |
| 2378 | return NULL; |
| 2379 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2380 | precision = (precision*10) + (*f - '0'); |
| 2381 | f++; |
| 2382 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2383 | if (*f == '%') { |
| 2384 | /* "%.3%s" => f points to "3" */ |
| 2385 | f--; |
| 2386 | } |
| 2387 | } |
| 2388 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2389 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2390 | f--; |
| 2391 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2392 | |
| 2393 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2394 | longflag = 0; |
| 2395 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2396 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2397 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2398 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2399 | longflag = 1; |
| 2400 | ++f; |
| 2401 | } |
| 2402 | #ifdef HAVE_LONG_LONG |
| 2403 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2404 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2405 | longlongflag = 1; |
| 2406 | f += 2; |
| 2407 | } |
| 2408 | #endif |
| 2409 | } |
| 2410 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2411 | 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] | 2412 | size_tflag = 1; |
| 2413 | ++f; |
| 2414 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2415 | |
| 2416 | if (f[1] == '\0') |
| 2417 | writer->overallocate = 0; |
| 2418 | |
| 2419 | switch (*f) { |
| 2420 | case 'c': |
| 2421 | { |
| 2422 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2423 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
| 2424 | PyErr_SetString(PyExc_ValueError, |
| 2425 | "character argument not in range(0x110000)"); |
| 2426 | return NULL; |
| 2427 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2428 | if (_PyUnicodeWriter_Prepare(writer, 1, ordinal) == -1) |
| 2429 | return NULL; |
| 2430 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ordinal); |
| 2431 | writer->pos++; |
| 2432 | break; |
| 2433 | } |
| 2434 | |
| 2435 | case 'i': |
| 2436 | case 'd': |
| 2437 | case 'u': |
| 2438 | case 'x': |
| 2439 | { |
| 2440 | /* used by sprintf */ |
| 2441 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2442 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2443 | |
| 2444 | if (*f == 'u') { |
| 2445 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2446 | |
| 2447 | if (longflag) |
| 2448 | len = sprintf(buffer, fmt, |
| 2449 | va_arg(*vargs, unsigned long)); |
| 2450 | #ifdef HAVE_LONG_LONG |
| 2451 | else if (longlongflag) |
| 2452 | len = sprintf(buffer, fmt, |
| 2453 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2454 | #endif |
| 2455 | else if (size_tflag) |
| 2456 | len = sprintf(buffer, fmt, |
| 2457 | va_arg(*vargs, size_t)); |
| 2458 | else |
| 2459 | len = sprintf(buffer, fmt, |
| 2460 | va_arg(*vargs, unsigned int)); |
| 2461 | } |
| 2462 | else if (*f == 'x') { |
| 2463 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2464 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2465 | } |
| 2466 | else { |
| 2467 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2468 | |
| 2469 | if (longflag) |
| 2470 | len = sprintf(buffer, fmt, |
| 2471 | va_arg(*vargs, long)); |
| 2472 | #ifdef HAVE_LONG_LONG |
| 2473 | else if (longlongflag) |
| 2474 | len = sprintf(buffer, fmt, |
| 2475 | va_arg(*vargs, PY_LONG_LONG)); |
| 2476 | #endif |
| 2477 | else if (size_tflag) |
| 2478 | len = sprintf(buffer, fmt, |
| 2479 | va_arg(*vargs, Py_ssize_t)); |
| 2480 | else |
| 2481 | len = sprintf(buffer, fmt, |
| 2482 | va_arg(*vargs, int)); |
| 2483 | } |
| 2484 | assert(len >= 0); |
| 2485 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2486 | if (precision < len) |
| 2487 | precision = len; |
| 2488 | if (width > precision) { |
| 2489 | Py_UCS4 fillchar; |
| 2490 | fill = width - precision; |
| 2491 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2492 | if (_PyUnicodeWriter_Prepare(writer, fill, fillchar) == -1) |
| 2493 | return NULL; |
| 2494 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2495 | return NULL; |
| 2496 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2497 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2498 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2499 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2500 | if (_PyUnicodeWriter_Prepare(writer, fill, '0') == -1) |
| 2501 | return NULL; |
| 2502 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2503 | return NULL; |
| 2504 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2505 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2506 | if (_PyUnicodeWriter_WriteCstr(writer, buffer, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2507 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2508 | break; |
| 2509 | } |
| 2510 | |
| 2511 | case 'p': |
| 2512 | { |
| 2513 | char number[MAX_LONG_LONG_CHARS]; |
| 2514 | |
| 2515 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2516 | assert(len >= 0); |
| 2517 | |
| 2518 | /* %p is ill-defined: ensure leading 0x. */ |
| 2519 | if (number[1] == 'X') |
| 2520 | number[1] = 'x'; |
| 2521 | else if (number[1] != 'x') { |
| 2522 | memmove(number + 2, number, |
| 2523 | strlen(number) + 1); |
| 2524 | number[0] = '0'; |
| 2525 | number[1] = 'x'; |
| 2526 | len += 2; |
| 2527 | } |
| 2528 | |
| 2529 | if (_PyUnicodeWriter_WriteCstr(writer, number, len) == -1) |
| 2530 | return NULL; |
| 2531 | break; |
| 2532 | } |
| 2533 | |
| 2534 | case 's': |
| 2535 | { |
| 2536 | /* UTF-8 */ |
| 2537 | const char *s = va_arg(*vargs, const char*); |
| 2538 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
| 2539 | if (!str) |
| 2540 | return NULL; |
| 2541 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2542 | Py_DECREF(str); |
| 2543 | return NULL; |
| 2544 | } |
| 2545 | Py_DECREF(str); |
| 2546 | break; |
| 2547 | } |
| 2548 | |
| 2549 | case 'U': |
| 2550 | { |
| 2551 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2552 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2553 | |
| 2554 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2555 | return NULL; |
| 2556 | break; |
| 2557 | } |
| 2558 | |
| 2559 | case 'V': |
| 2560 | { |
| 2561 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2562 | const char *str = va_arg(*vargs, const char *); |
| 2563 | PyObject *str_obj; |
| 2564 | assert(obj || str); |
| 2565 | if (obj) { |
| 2566 | assert(_PyUnicode_CHECK(obj)); |
| 2567 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2568 | return NULL; |
| 2569 | } |
| 2570 | else { |
| 2571 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
| 2572 | if (!str_obj) |
| 2573 | return NULL; |
| 2574 | if (_PyUnicodeWriter_WriteStr(writer, str_obj) == -1) { |
| 2575 | Py_DECREF(str_obj); |
| 2576 | return NULL; |
| 2577 | } |
| 2578 | Py_DECREF(str_obj); |
| 2579 | } |
| 2580 | break; |
| 2581 | } |
| 2582 | |
| 2583 | case 'S': |
| 2584 | { |
| 2585 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2586 | PyObject *str; |
| 2587 | assert(obj); |
| 2588 | str = PyObject_Str(obj); |
| 2589 | if (!str) |
| 2590 | return NULL; |
| 2591 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2592 | Py_DECREF(str); |
| 2593 | return NULL; |
| 2594 | } |
| 2595 | Py_DECREF(str); |
| 2596 | break; |
| 2597 | } |
| 2598 | |
| 2599 | case 'R': |
| 2600 | { |
| 2601 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2602 | PyObject *repr; |
| 2603 | assert(obj); |
| 2604 | repr = PyObject_Repr(obj); |
| 2605 | if (!repr) |
| 2606 | return NULL; |
| 2607 | if (_PyUnicodeWriter_WriteStr(writer, repr) == -1) { |
| 2608 | Py_DECREF(repr); |
| 2609 | return NULL; |
| 2610 | } |
| 2611 | Py_DECREF(repr); |
| 2612 | break; |
| 2613 | } |
| 2614 | |
| 2615 | case 'A': |
| 2616 | { |
| 2617 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2618 | PyObject *ascii; |
| 2619 | assert(obj); |
| 2620 | ascii = PyObject_ASCII(obj); |
| 2621 | if (!ascii) |
| 2622 | return NULL; |
| 2623 | if (_PyUnicodeWriter_WriteStr(writer, ascii) == -1) { |
| 2624 | Py_DECREF(ascii); |
| 2625 | return NULL; |
| 2626 | } |
| 2627 | Py_DECREF(ascii); |
| 2628 | break; |
| 2629 | } |
| 2630 | |
| 2631 | case '%': |
| 2632 | if (_PyUnicodeWriter_Prepare(writer, 1, '%') == 1) |
| 2633 | return NULL; |
| 2634 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%'); |
| 2635 | writer->pos++; |
| 2636 | break; |
| 2637 | |
| 2638 | default: |
| 2639 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2640 | of the format string to the output string. (we cannot just |
| 2641 | skip the code, since there's no way to know what's in the |
| 2642 | argument list) */ |
| 2643 | len = strlen(p); |
| 2644 | if (_PyUnicodeWriter_WriteCstr(writer, p, len) == -1) |
| 2645 | return NULL; |
| 2646 | f = p+len; |
| 2647 | return f; |
| 2648 | } |
| 2649 | |
| 2650 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2651 | return f; |
| 2652 | } |
| 2653 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2654 | PyObject * |
| 2655 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2656 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2657 | va_list vargs2; |
| 2658 | const char *f; |
| 2659 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2660 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2661 | _PyUnicodeWriter_Init(&writer, strlen(format) + 100); |
| 2662 | |
| 2663 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2664 | Copy it to be able to pass a reference to a subfunction. */ |
| 2665 | Py_VA_COPY(vargs2, vargs); |
| 2666 | |
| 2667 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2668 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2669 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2670 | if (f == NULL) |
| 2671 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2672 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2673 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2674 | const char *p; |
| 2675 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2676 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2677 | p = f; |
| 2678 | do |
| 2679 | { |
| 2680 | if ((unsigned char)*p > 127) { |
| 2681 | PyErr_Format(PyExc_ValueError, |
| 2682 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2683 | "string, got a non-ASCII byte: 0x%02x", |
| 2684 | (unsigned char)*p); |
| 2685 | return NULL; |
| 2686 | } |
| 2687 | p++; |
| 2688 | } |
| 2689 | while (*p != '\0' && *p != '%'); |
| 2690 | len = p - f; |
| 2691 | |
| 2692 | if (*p == '\0') |
| 2693 | writer.overallocate = 0; |
| 2694 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
| 2695 | goto fail; |
| 2696 | unicode_write_cstr(writer.buffer, writer.pos, f, len); |
| 2697 | writer.pos += len; |
| 2698 | |
| 2699 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2700 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2701 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2702 | return _PyUnicodeWriter_Finish(&writer); |
| 2703 | |
| 2704 | fail: |
| 2705 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2706 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2709 | PyObject * |
| 2710 | PyUnicode_FromFormat(const char *format, ...) |
| 2711 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2712 | PyObject* ret; |
| 2713 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2714 | |
| 2715 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2716 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2717 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2718 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2719 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2720 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2721 | va_end(vargs); |
| 2722 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2725 | #ifdef HAVE_WCHAR_H |
| 2726 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2727 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2728 | convert a Unicode object to a wide character string. |
| 2729 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2730 | - 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] | 2731 | character) required to convert the unicode object. Ignore size argument. |
| 2732 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2733 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2734 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2735 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2736 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2737 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2738 | wchar_t *w, |
| 2739 | Py_ssize_t size) |
| 2740 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2741 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2742 | const wchar_t *wstr; |
| 2743 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2744 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2745 | if (wstr == NULL) |
| 2746 | return -1; |
| 2747 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2748 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2749 | if (size > res) |
| 2750 | size = res + 1; |
| 2751 | else |
| 2752 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2753 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2754 | return res; |
| 2755 | } |
| 2756 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2757 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2761 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2762 | wchar_t *w, |
| 2763 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2764 | { |
| 2765 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2766 | PyErr_BadInternalCall(); |
| 2767 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2768 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2769 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2772 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2773 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2774 | Py_ssize_t *size) |
| 2775 | { |
| 2776 | wchar_t* buffer; |
| 2777 | Py_ssize_t buflen; |
| 2778 | |
| 2779 | if (unicode == NULL) { |
| 2780 | PyErr_BadInternalCall(); |
| 2781 | return NULL; |
| 2782 | } |
| 2783 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2784 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2785 | if (buflen == -1) |
| 2786 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2787 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2788 | PyErr_NoMemory(); |
| 2789 | return NULL; |
| 2790 | } |
| 2791 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2792 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2793 | if (buffer == NULL) { |
| 2794 | PyErr_NoMemory(); |
| 2795 | return NULL; |
| 2796 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2797 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2798 | if (buflen == -1) { |
| 2799 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2800 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2801 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2802 | if (size != NULL) |
| 2803 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2804 | return buffer; |
| 2805 | } |
| 2806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2807 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2808 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2809 | PyObject * |
| 2810 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2811 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2812 | PyObject *v; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2813 | void *data; |
| 2814 | int kind; |
| 2815 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2816 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2817 | PyErr_SetString(PyExc_ValueError, |
| 2818 | "chr() arg not in range(0x110000)"); |
| 2819 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2820 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2821 | |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2822 | if ((Py_UCS4)ordinal < 256) |
| 2823 | return get_latin1_char((unsigned char)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2824 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2825 | v = PyUnicode_New(1, ordinal); |
| 2826 | if (v == NULL) |
| 2827 | return NULL; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 2828 | kind = PyUnicode_KIND(v); |
| 2829 | data = PyUnicode_DATA(v); |
| 2830 | PyUnicode_WRITE(kind, data, 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2831 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2832 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2835 | PyObject * |
| 2836 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2837 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2838 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2839 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2840 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2841 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2842 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2843 | Py_INCREF(obj); |
| 2844 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2845 | } |
| 2846 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2847 | /* For a Unicode subtype that's not a Unicode object, |
| 2848 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2849 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2850 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2851 | PyErr_Format(PyExc_TypeError, |
| 2852 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2853 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2854 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2857 | PyObject * |
| 2858 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2859 | const char *encoding, |
| 2860 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2861 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2862 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2863 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2865 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2866 | PyErr_BadInternalCall(); |
| 2867 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2868 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2869 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2870 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2871 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2872 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2873 | _Py_RETURN_UNICODE_EMPTY(); |
| 2874 | v = PyUnicode_Decode( |
| 2875 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2876 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2877 | return v; |
| 2878 | } |
| 2879 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2880 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2881 | PyErr_SetString(PyExc_TypeError, |
| 2882 | "decoding str is not supported"); |
| 2883 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2884 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2885 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2886 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2887 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2888 | PyErr_Format(PyExc_TypeError, |
| 2889 | "coercing to str: need bytes, bytearray " |
| 2890 | "or buffer-like object, %.80s found", |
| 2891 | Py_TYPE(obj)->tp_name); |
| 2892 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2893 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2894 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2895 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2896 | PyBuffer_Release(&buffer); |
| 2897 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2899 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2900 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2901 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2902 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2905 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2906 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2907 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2908 | int |
| 2909 | _Py_normalize_encoding(const char *encoding, |
| 2910 | char *lower, |
| 2911 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2912 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2913 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2914 | char *l; |
| 2915 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2916 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2917 | if (encoding == NULL) { |
| 2918 | strcpy(lower, "utf-8"); |
| 2919 | return 1; |
| 2920 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2921 | e = encoding; |
| 2922 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2923 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2924 | while (*e) { |
| 2925 | if (l == l_end) |
| 2926 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2927 | if (Py_ISUPPER(*e)) { |
| 2928 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2929 | } |
| 2930 | else if (*e == '_') { |
| 2931 | *l++ = '-'; |
| 2932 | e++; |
| 2933 | } |
| 2934 | else { |
| 2935 | *l++ = *e++; |
| 2936 | } |
| 2937 | } |
| 2938 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2939 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2942 | PyObject * |
| 2943 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2944 | Py_ssize_t size, |
| 2945 | const char *encoding, |
| 2946 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2947 | { |
| 2948 | PyObject *buffer = NULL, *unicode; |
| 2949 | Py_buffer info; |
| 2950 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2951 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2952 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2953 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2954 | if ((strcmp(lower, "utf-8") == 0) || |
| 2955 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2956 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2957 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2958 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2959 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2960 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2961 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2962 | else if (strcmp(lower, "mbcs") == 0) |
| 2963 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2964 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2965 | else if (strcmp(lower, "ascii") == 0) |
| 2966 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2967 | else if (strcmp(lower, "utf-16") == 0) |
| 2968 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2969 | else if (strcmp(lower, "utf-32") == 0) |
| 2970 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2971 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2972 | |
| 2973 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2974 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2975 | 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] | 2976 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2977 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2978 | if (buffer == NULL) |
| 2979 | goto onError; |
| 2980 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2981 | if (unicode == NULL) |
| 2982 | goto onError; |
| 2983 | if (!PyUnicode_Check(unicode)) { |
| 2984 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2985 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2986 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2987 | Py_DECREF(unicode); |
| 2988 | goto onError; |
| 2989 | } |
| 2990 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2991 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2992 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2993 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2994 | Py_XDECREF(buffer); |
| 2995 | return NULL; |
| 2996 | } |
| 2997 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2998 | PyObject * |
| 2999 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3000 | const char *encoding, |
| 3001 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3002 | { |
| 3003 | PyObject *v; |
| 3004 | |
| 3005 | if (!PyUnicode_Check(unicode)) { |
| 3006 | PyErr_BadArgument(); |
| 3007 | goto onError; |
| 3008 | } |
| 3009 | |
| 3010 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3011 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3012 | |
| 3013 | /* Decode via the codec registry */ |
| 3014 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3015 | if (v == NULL) |
| 3016 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3017 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3018 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3019 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3020 | return NULL; |
| 3021 | } |
| 3022 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3023 | PyObject * |
| 3024 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3025 | const char *encoding, |
| 3026 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3027 | { |
| 3028 | PyObject *v; |
| 3029 | |
| 3030 | if (!PyUnicode_Check(unicode)) { |
| 3031 | PyErr_BadArgument(); |
| 3032 | goto onError; |
| 3033 | } |
| 3034 | |
| 3035 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3036 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3037 | |
| 3038 | /* Decode via the codec registry */ |
| 3039 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3040 | if (v == NULL) |
| 3041 | goto onError; |
| 3042 | if (!PyUnicode_Check(v)) { |
| 3043 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3044 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3045 | Py_TYPE(v)->tp_name); |
| 3046 | Py_DECREF(v); |
| 3047 | goto onError; |
| 3048 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3049 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3050 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3051 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3052 | return NULL; |
| 3053 | } |
| 3054 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3055 | PyObject * |
| 3056 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3057 | Py_ssize_t size, |
| 3058 | const char *encoding, |
| 3059 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | { |
| 3061 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3062 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3063 | unicode = PyUnicode_FromUnicode(s, size); |
| 3064 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3065 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3066 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3067 | Py_DECREF(unicode); |
| 3068 | return v; |
| 3069 | } |
| 3070 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3071 | PyObject * |
| 3072 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3073 | const char *encoding, |
| 3074 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3075 | { |
| 3076 | PyObject *v; |
| 3077 | |
| 3078 | if (!PyUnicode_Check(unicode)) { |
| 3079 | PyErr_BadArgument(); |
| 3080 | goto onError; |
| 3081 | } |
| 3082 | |
| 3083 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3084 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3085 | |
| 3086 | /* Encode via the codec registry */ |
| 3087 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3088 | if (v == NULL) |
| 3089 | goto onError; |
| 3090 | return v; |
| 3091 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3092 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3093 | return NULL; |
| 3094 | } |
| 3095 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3096 | static size_t |
| 3097 | wcstombs_errorpos(const wchar_t *wstr) |
| 3098 | { |
| 3099 | size_t len; |
| 3100 | #if SIZEOF_WCHAR_T == 2 |
| 3101 | wchar_t buf[3]; |
| 3102 | #else |
| 3103 | wchar_t buf[2]; |
| 3104 | #endif |
| 3105 | char outbuf[MB_LEN_MAX]; |
| 3106 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3107 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3108 | #if SIZEOF_WCHAR_T == 2 |
| 3109 | buf[2] = 0; |
| 3110 | #else |
| 3111 | buf[1] = 0; |
| 3112 | #endif |
| 3113 | start = wstr; |
| 3114 | while (*wstr != L'\0') |
| 3115 | { |
| 3116 | previous = wstr; |
| 3117 | #if SIZEOF_WCHAR_T == 2 |
| 3118 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3119 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3120 | { |
| 3121 | buf[0] = wstr[0]; |
| 3122 | buf[1] = wstr[1]; |
| 3123 | wstr += 2; |
| 3124 | } |
| 3125 | else { |
| 3126 | buf[0] = *wstr; |
| 3127 | buf[1] = 0; |
| 3128 | wstr++; |
| 3129 | } |
| 3130 | #else |
| 3131 | buf[0] = *wstr; |
| 3132 | wstr++; |
| 3133 | #endif |
| 3134 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3135 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3136 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3140 | return 0; |
| 3141 | } |
| 3142 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3143 | static int |
| 3144 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3145 | { |
| 3146 | if (errors == NULL) { |
| 3147 | *surrogateescape = 0; |
| 3148 | return 0; |
| 3149 | } |
| 3150 | |
| 3151 | if (strcmp(errors, "strict") == 0) { |
| 3152 | *surrogateescape = 0; |
| 3153 | return 0; |
| 3154 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3155 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3156 | *surrogateescape = 1; |
| 3157 | return 0; |
| 3158 | } |
| 3159 | PyErr_Format(PyExc_ValueError, |
| 3160 | "only 'strict' and 'surrogateescape' error handlers " |
| 3161 | "are supported, not '%s'", |
| 3162 | errors); |
| 3163 | return -1; |
| 3164 | } |
| 3165 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3166 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3167 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3168 | { |
| 3169 | Py_ssize_t wlen, wlen2; |
| 3170 | wchar_t *wstr; |
| 3171 | PyObject *bytes = NULL; |
| 3172 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3173 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3174 | PyObject *exc; |
| 3175 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3176 | int surrogateescape; |
| 3177 | |
| 3178 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3179 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3180 | |
| 3181 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3182 | if (wstr == NULL) |
| 3183 | return NULL; |
| 3184 | |
| 3185 | wlen2 = wcslen(wstr); |
| 3186 | if (wlen2 != wlen) { |
| 3187 | PyMem_Free(wstr); |
| 3188 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3189 | return NULL; |
| 3190 | } |
| 3191 | |
| 3192 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3193 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3194 | char *str; |
| 3195 | |
| 3196 | str = _Py_wchar2char(wstr, &error_pos); |
| 3197 | if (str == NULL) { |
| 3198 | if (error_pos == (size_t)-1) { |
| 3199 | PyErr_NoMemory(); |
| 3200 | PyMem_Free(wstr); |
| 3201 | return NULL; |
| 3202 | } |
| 3203 | else { |
| 3204 | goto encode_error; |
| 3205 | } |
| 3206 | } |
| 3207 | PyMem_Free(wstr); |
| 3208 | |
| 3209 | bytes = PyBytes_FromString(str); |
| 3210 | PyMem_Free(str); |
| 3211 | } |
| 3212 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3213 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3214 | size_t len, len2; |
| 3215 | |
| 3216 | len = wcstombs(NULL, wstr, 0); |
| 3217 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3218 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3219 | goto encode_error; |
| 3220 | } |
| 3221 | |
| 3222 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3223 | if (bytes == NULL) { |
| 3224 | PyMem_Free(wstr); |
| 3225 | return NULL; |
| 3226 | } |
| 3227 | |
| 3228 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3229 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3230 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3231 | goto encode_error; |
| 3232 | } |
| 3233 | PyMem_Free(wstr); |
| 3234 | } |
| 3235 | return bytes; |
| 3236 | |
| 3237 | encode_error: |
| 3238 | errmsg = strerror(errno); |
| 3239 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3240 | |
| 3241 | if (error_pos == (size_t)-1) |
| 3242 | error_pos = wcstombs_errorpos(wstr); |
| 3243 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3244 | PyMem_Free(wstr); |
| 3245 | Py_XDECREF(bytes); |
| 3246 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3247 | if (errmsg != NULL) { |
| 3248 | size_t errlen; |
| 3249 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3250 | if (wstr != NULL) { |
| 3251 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3252 | PyMem_Free(wstr); |
| 3253 | } else |
| 3254 | errmsg = NULL; |
| 3255 | } |
| 3256 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3257 | reason = PyUnicode_FromString( |
| 3258 | "wcstombs() encountered an unencodable " |
| 3259 | "wide character"); |
| 3260 | if (reason == NULL) |
| 3261 | return NULL; |
| 3262 | |
| 3263 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3264 | "locale", unicode, |
| 3265 | (Py_ssize_t)error_pos, |
| 3266 | (Py_ssize_t)(error_pos+1), |
| 3267 | reason); |
| 3268 | Py_DECREF(reason); |
| 3269 | if (exc != NULL) { |
| 3270 | PyCodec_StrictErrors(exc); |
| 3271 | Py_XDECREF(exc); |
| 3272 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3273 | return NULL; |
| 3274 | } |
| 3275 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3276 | PyObject * |
| 3277 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3278 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3279 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3280 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3281 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3282 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3283 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3284 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3285 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3286 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3287 | the Python codec requires to encode at least its own filename. Use the C |
| 3288 | version of the locale codec until the codec registry is initialized and |
| 3289 | the Python codec is loaded. |
| 3290 | |
| 3291 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3292 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3293 | subinterpreters. */ |
| 3294 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3295 | return PyUnicode_AsEncodedString(unicode, |
| 3296 | Py_FileSystemDefaultEncoding, |
| 3297 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3298 | } |
| 3299 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3300 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3301 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3302 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3305 | PyObject * |
| 3306 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3307 | const char *encoding, |
| 3308 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3309 | { |
| 3310 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3311 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3312 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3313 | if (!PyUnicode_Check(unicode)) { |
| 3314 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3315 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3316 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3317 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3318 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3319 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3320 | if ((strcmp(lower, "utf-8") == 0) || |
| 3321 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3322 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3323 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3324 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3325 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3326 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3327 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3328 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3329 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3330 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3331 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3332 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3333 | else if (strcmp(lower, "mbcs") == 0) |
| 3334 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3335 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3336 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3337 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3338 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3339 | |
| 3340 | /* Encode via the codec registry */ |
| 3341 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3342 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3343 | return NULL; |
| 3344 | |
| 3345 | /* The normal path */ |
| 3346 | if (PyBytes_Check(v)) |
| 3347 | return v; |
| 3348 | |
| 3349 | /* 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] | 3350 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3351 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3352 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3353 | |
| 3354 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3355 | "encoder %s returned bytearray instead of bytes", |
| 3356 | encoding); |
| 3357 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3358 | Py_DECREF(v); |
| 3359 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3360 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3361 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3362 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3363 | Py_DECREF(v); |
| 3364 | return b; |
| 3365 | } |
| 3366 | |
| 3367 | PyErr_Format(PyExc_TypeError, |
| 3368 | "encoder did not return a bytes object (type=%.400s)", |
| 3369 | Py_TYPE(v)->tp_name); |
| 3370 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3371 | return NULL; |
| 3372 | } |
| 3373 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3374 | PyObject * |
| 3375 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3376 | const char *encoding, |
| 3377 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3378 | { |
| 3379 | PyObject *v; |
| 3380 | |
| 3381 | if (!PyUnicode_Check(unicode)) { |
| 3382 | PyErr_BadArgument(); |
| 3383 | goto onError; |
| 3384 | } |
| 3385 | |
| 3386 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3387 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3388 | |
| 3389 | /* Encode via the codec registry */ |
| 3390 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3391 | if (v == NULL) |
| 3392 | goto onError; |
| 3393 | if (!PyUnicode_Check(v)) { |
| 3394 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3395 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3396 | Py_TYPE(v)->tp_name); |
| 3397 | Py_DECREF(v); |
| 3398 | goto onError; |
| 3399 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3400 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3401 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3402 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3403 | return NULL; |
| 3404 | } |
| 3405 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3406 | static size_t |
| 3407 | mbstowcs_errorpos(const char *str, size_t len) |
| 3408 | { |
| 3409 | #ifdef HAVE_MBRTOWC |
| 3410 | const char *start = str; |
| 3411 | mbstate_t mbs; |
| 3412 | size_t converted; |
| 3413 | wchar_t ch; |
| 3414 | |
| 3415 | memset(&mbs, 0, sizeof mbs); |
| 3416 | while (len) |
| 3417 | { |
| 3418 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3419 | if (converted == 0) |
| 3420 | /* Reached end of string */ |
| 3421 | break; |
| 3422 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3423 | /* Conversion error or incomplete character */ |
| 3424 | return str - start; |
| 3425 | } |
| 3426 | else { |
| 3427 | str += converted; |
| 3428 | len -= converted; |
| 3429 | } |
| 3430 | } |
| 3431 | /* failed to find the undecodable byte sequence */ |
| 3432 | return 0; |
| 3433 | #endif |
| 3434 | return 0; |
| 3435 | } |
| 3436 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3437 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3438 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3439 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3440 | { |
| 3441 | wchar_t smallbuf[256]; |
| 3442 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3443 | wchar_t *wstr; |
| 3444 | size_t wlen, wlen2; |
| 3445 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3446 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3447 | size_t error_pos; |
| 3448 | char *errmsg; |
| 3449 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3450 | |
| 3451 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3452 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3453 | |
| 3454 | if (str[len] != '\0' || len != strlen(str)) { |
| 3455 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3456 | return NULL; |
| 3457 | } |
| 3458 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3459 | if (surrogateescape) { |
| 3460 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3461 | wstr = _Py_char2wchar(str, &wlen); |
| 3462 | if (wstr == NULL) { |
| 3463 | if (wlen == (size_t)-1) |
| 3464 | PyErr_NoMemory(); |
| 3465 | else |
| 3466 | PyErr_SetFromErrno(PyExc_OSError); |
| 3467 | return NULL; |
| 3468 | } |
| 3469 | |
| 3470 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3471 | PyMem_Free(wstr); |
| 3472 | } |
| 3473 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3474 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3475 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3476 | wlen = mbstowcs(NULL, str, 0); |
| 3477 | #else |
| 3478 | wlen = len; |
| 3479 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3480 | if (wlen == (size_t)-1) |
| 3481 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3482 | if (wlen+1 <= smallbuf_len) { |
| 3483 | wstr = smallbuf; |
| 3484 | } |
| 3485 | else { |
| 3486 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3487 | return PyErr_NoMemory(); |
| 3488 | |
| 3489 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3490 | if (!wstr) |
| 3491 | return PyErr_NoMemory(); |
| 3492 | } |
| 3493 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3494 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3495 | if (wlen2 == (size_t)-1) { |
| 3496 | if (wstr != smallbuf) |
| 3497 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3498 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3499 | } |
| 3500 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3501 | assert(wlen2 == wlen); |
| 3502 | #endif |
| 3503 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3504 | if (wstr != smallbuf) |
| 3505 | PyMem_Free(wstr); |
| 3506 | } |
| 3507 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3508 | |
| 3509 | decode_error: |
| 3510 | errmsg = strerror(errno); |
| 3511 | assert(errmsg != NULL); |
| 3512 | |
| 3513 | error_pos = mbstowcs_errorpos(str, len); |
| 3514 | if (errmsg != NULL) { |
| 3515 | size_t errlen; |
| 3516 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3517 | if (wstr != NULL) { |
| 3518 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3519 | PyMem_Free(wstr); |
| 3520 | } else |
| 3521 | errmsg = NULL; |
| 3522 | } |
| 3523 | if (errmsg == NULL) |
| 3524 | reason = PyUnicode_FromString( |
| 3525 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3526 | if (reason == NULL) |
| 3527 | return NULL; |
| 3528 | |
| 3529 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3530 | "locale", str, len, |
| 3531 | (Py_ssize_t)error_pos, |
| 3532 | (Py_ssize_t)(error_pos+1), |
| 3533 | reason); |
| 3534 | Py_DECREF(reason); |
| 3535 | if (exc != NULL) { |
| 3536 | PyCodec_StrictErrors(exc); |
| 3537 | Py_XDECREF(exc); |
| 3538 | } |
| 3539 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3543 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3544 | { |
| 3545 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3546 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | |
| 3550 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3551 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3552 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3553 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3554 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3555 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3556 | PyObject* |
| 3557 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3558 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3559 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3560 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3561 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3562 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3563 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3564 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3565 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3566 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3567 | the Python codec requires to encode at least its own filename. Use the C |
| 3568 | version of the locale codec until the codec registry is initialized and |
| 3569 | the Python codec is loaded. |
| 3570 | |
| 3571 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3572 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3573 | subinterpreters. */ |
| 3574 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3575 | return PyUnicode_Decode(s, size, |
| 3576 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3577 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3578 | } |
| 3579 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3580 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3581 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3582 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3585 | |
| 3586 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3587 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3588 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3589 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3590 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3591 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3592 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3593 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3594 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3595 | if (pos == -1) |
| 3596 | return 0; |
| 3597 | else |
| 3598 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3599 | } |
| 3600 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3601 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3602 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3603 | { |
| 3604 | PyObject *output = NULL; |
| 3605 | Py_ssize_t size; |
| 3606 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3607 | if (arg == NULL) { |
| 3608 | Py_DECREF(*(PyObject**)addr); |
| 3609 | return 1; |
| 3610 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3611 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3612 | output = arg; |
| 3613 | Py_INCREF(output); |
| 3614 | } |
| 3615 | else { |
| 3616 | arg = PyUnicode_FromObject(arg); |
| 3617 | if (!arg) |
| 3618 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3619 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3620 | Py_DECREF(arg); |
| 3621 | if (!output) |
| 3622 | return 0; |
| 3623 | if (!PyBytes_Check(output)) { |
| 3624 | Py_DECREF(output); |
| 3625 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3626 | return 0; |
| 3627 | } |
| 3628 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3629 | size = PyBytes_GET_SIZE(output); |
| 3630 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3631 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3632 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3633 | Py_DECREF(output); |
| 3634 | return 0; |
| 3635 | } |
| 3636 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3637 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3641 | int |
| 3642 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3643 | { |
| 3644 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3645 | if (arg == NULL) { |
| 3646 | Py_DECREF(*(PyObject**)addr); |
| 3647 | return 1; |
| 3648 | } |
| 3649 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3650 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3651 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3652 | output = arg; |
| 3653 | Py_INCREF(output); |
| 3654 | } |
| 3655 | else { |
| 3656 | arg = PyBytes_FromObject(arg); |
| 3657 | if (!arg) |
| 3658 | return 0; |
| 3659 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3660 | PyBytes_GET_SIZE(arg)); |
| 3661 | Py_DECREF(arg); |
| 3662 | if (!output) |
| 3663 | return 0; |
| 3664 | if (!PyUnicode_Check(output)) { |
| 3665 | Py_DECREF(output); |
| 3666 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3667 | return 0; |
| 3668 | } |
| 3669 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3670 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3671 | Py_DECREF(output); |
| 3672 | return 0; |
| 3673 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3674 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3675 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3676 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3677 | Py_DECREF(output); |
| 3678 | return 0; |
| 3679 | } |
| 3680 | *(PyObject**)addr = output; |
| 3681 | return Py_CLEANUP_SUPPORTED; |
| 3682 | } |
| 3683 | |
| 3684 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3685 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3686 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3687 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3688 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3689 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3690 | if (!PyUnicode_Check(unicode)) { |
| 3691 | PyErr_BadArgument(); |
| 3692 | return NULL; |
| 3693 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3694 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3695 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3696 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3697 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3698 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3699 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3700 | if (bytes == NULL) |
| 3701 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3702 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3703 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3704 | Py_DECREF(bytes); |
| 3705 | return NULL; |
| 3706 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3707 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3708 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3709 | PyBytes_AS_STRING(bytes), |
| 3710 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3711 | Py_DECREF(bytes); |
| 3712 | } |
| 3713 | |
| 3714 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3715 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3716 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3720 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3721 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3722 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3723 | } |
| 3724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3725 | Py_UNICODE * |
| 3726 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3727 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3728 | const unsigned char *one_byte; |
| 3729 | #if SIZEOF_WCHAR_T == 4 |
| 3730 | const Py_UCS2 *two_bytes; |
| 3731 | #else |
| 3732 | const Py_UCS4 *four_bytes; |
| 3733 | const Py_UCS4 *ucs4_end; |
| 3734 | Py_ssize_t num_surrogates; |
| 3735 | #endif |
| 3736 | wchar_t *w; |
| 3737 | wchar_t *wchar_end; |
| 3738 | |
| 3739 | if (!PyUnicode_Check(unicode)) { |
| 3740 | PyErr_BadArgument(); |
| 3741 | return NULL; |
| 3742 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3743 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3744 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3745 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3746 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3747 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3748 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3749 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3750 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3751 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3752 | num_surrogates = 0; |
| 3753 | |
| 3754 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3755 | if (*four_bytes > 0xFFFF) |
| 3756 | ++num_surrogates; |
| 3757 | } |
| 3758 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3759 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3760 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3761 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3762 | PyErr_NoMemory(); |
| 3763 | return NULL; |
| 3764 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3765 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3766 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3767 | w = _PyUnicode_WSTR(unicode); |
| 3768 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3769 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3770 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3771 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3772 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3773 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3774 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3775 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3776 | } |
| 3777 | else |
| 3778 | *w = *four_bytes; |
| 3779 | |
| 3780 | if (w > wchar_end) { |
| 3781 | assert(0 && "Miscalculated string end"); |
| 3782 | } |
| 3783 | } |
| 3784 | *w = 0; |
| 3785 | #else |
| 3786 | /* sizeof(wchar_t) == 4 */ |
| 3787 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3788 | "should share memory already."); |
| 3789 | return NULL; |
| 3790 | #endif |
| 3791 | } |
| 3792 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3793 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3794 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3795 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3796 | PyErr_NoMemory(); |
| 3797 | return NULL; |
| 3798 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3799 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3800 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3801 | w = _PyUnicode_WSTR(unicode); |
| 3802 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3803 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3804 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3805 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3806 | for (; w < wchar_end; ++one_byte, ++w) |
| 3807 | *w = *one_byte; |
| 3808 | /* null-terminate the wstr */ |
| 3809 | *w = 0; |
| 3810 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3811 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3813 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3814 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3815 | *w = *two_bytes; |
| 3816 | /* null-terminate the wstr */ |
| 3817 | *w = 0; |
| 3818 | #else |
| 3819 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3820 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3821 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3822 | Py_FatalError("Impossible unicode object state, wstr " |
| 3823 | "and str should share memory already."); |
| 3824 | return NULL; |
| 3825 | #endif |
| 3826 | } |
| 3827 | else { |
| 3828 | assert(0 && "This should never happen."); |
| 3829 | } |
| 3830 | } |
| 3831 | } |
| 3832 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3833 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3834 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3837 | Py_UNICODE * |
| 3838 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3839 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3840 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3844 | Py_ssize_t |
| 3845 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3846 | { |
| 3847 | if (!PyUnicode_Check(unicode)) { |
| 3848 | PyErr_BadArgument(); |
| 3849 | goto onError; |
| 3850 | } |
| 3851 | return PyUnicode_GET_SIZE(unicode); |
| 3852 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3853 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3854 | return -1; |
| 3855 | } |
| 3856 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3857 | Py_ssize_t |
| 3858 | PyUnicode_GetLength(PyObject *unicode) |
| 3859 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3860 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3861 | PyErr_BadArgument(); |
| 3862 | return -1; |
| 3863 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3864 | if (PyUnicode_READY(unicode) == -1) |
| 3865 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3866 | return PyUnicode_GET_LENGTH(unicode); |
| 3867 | } |
| 3868 | |
| 3869 | Py_UCS4 |
| 3870 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3871 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3872 | void *data; |
| 3873 | int kind; |
| 3874 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3875 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3876 | PyErr_BadArgument(); |
| 3877 | return (Py_UCS4)-1; |
| 3878 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3879 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3880 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3881 | return (Py_UCS4)-1; |
| 3882 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3883 | data = PyUnicode_DATA(unicode); |
| 3884 | kind = PyUnicode_KIND(unicode); |
| 3885 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | int |
| 3889 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3890 | { |
| 3891 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3892 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3893 | return -1; |
| 3894 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3895 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3896 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3897 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3898 | return -1; |
| 3899 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3900 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3901 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3902 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3903 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3904 | return -1; |
| 3905 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3906 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3907 | index, ch); |
| 3908 | return 0; |
| 3909 | } |
| 3910 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3911 | const char * |
| 3912 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3913 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3914 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3917 | /* create or adjust a UnicodeDecodeError */ |
| 3918 | static void |
| 3919 | make_decode_exception(PyObject **exceptionObject, |
| 3920 | const char *encoding, |
| 3921 | const char *input, Py_ssize_t length, |
| 3922 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3923 | const char *reason) |
| 3924 | { |
| 3925 | if (*exceptionObject == NULL) { |
| 3926 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3927 | encoding, input, length, startpos, endpos, reason); |
| 3928 | } |
| 3929 | else { |
| 3930 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3931 | goto onError; |
| 3932 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3933 | goto onError; |
| 3934 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3935 | goto onError; |
| 3936 | } |
| 3937 | return; |
| 3938 | |
| 3939 | onError: |
| 3940 | Py_DECREF(*exceptionObject); |
| 3941 | *exceptionObject = NULL; |
| 3942 | } |
| 3943 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3944 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3945 | /* error handling callback helper: |
| 3946 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3947 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3948 | and adjust various state variables. |
| 3949 | return 0 on success, -1 on error |
| 3950 | */ |
| 3951 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3952 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3953 | unicode_decode_call_errorhandler_wchar( |
| 3954 | const char *errors, PyObject **errorHandler, |
| 3955 | const char *encoding, const char *reason, |
| 3956 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3957 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3958 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3959 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3960 | 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] | 3961 | |
| 3962 | PyObject *restuple = NULL; |
| 3963 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3964 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3965 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3966 | Py_ssize_t requiredsize; |
| 3967 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3968 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3969 | wchar_t *repwstr; |
| 3970 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3971 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3972 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 3973 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3974 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3975 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3976 | *errorHandler = PyCodec_LookupError(errors); |
| 3977 | if (*errorHandler == NULL) |
| 3978 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3981 | make_decode_exception(exceptionObject, |
| 3982 | encoding, |
| 3983 | *input, *inend - *input, |
| 3984 | *startinpos, *endinpos, |
| 3985 | reason); |
| 3986 | if (*exceptionObject == NULL) |
| 3987 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3988 | |
| 3989 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3990 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3991 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3992 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3993 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3994 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3995 | } |
| 3996 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3997 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3998 | |
| 3999 | /* Copy back the bytes variables, which might have been modified by the |
| 4000 | callback */ |
| 4001 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4002 | if (!inputobj) |
| 4003 | goto onError; |
| 4004 | if (!PyBytes_Check(inputobj)) { |
| 4005 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4006 | } |
| 4007 | *input = PyBytes_AS_STRING(inputobj); |
| 4008 | insize = PyBytes_GET_SIZE(inputobj); |
| 4009 | *inend = *input + insize; |
| 4010 | /* we can DECREF safely, as the exception has another reference, |
| 4011 | so the object won't go away. */ |
| 4012 | Py_DECREF(inputobj); |
| 4013 | |
| 4014 | if (newpos<0) |
| 4015 | newpos = insize+newpos; |
| 4016 | if (newpos<0 || newpos>insize) { |
| 4017 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4018 | goto onError; |
| 4019 | } |
| 4020 | |
| 4021 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4022 | if (repwstr == NULL) |
| 4023 | goto onError; |
| 4024 | /* need more space? (at least enough for what we |
| 4025 | have+the replacement+the rest of the string (starting |
| 4026 | at the new input position), so we won't have to check space |
| 4027 | when there are no errors in the rest of the string) */ |
| 4028 | requiredsize = *outpos + repwlen + insize-newpos; |
| 4029 | if (requiredsize > outsize) { |
| 4030 | if (requiredsize < 2*outsize) |
| 4031 | requiredsize = 2*outsize; |
| 4032 | if (unicode_resize(output, requiredsize) < 0) |
| 4033 | goto onError; |
| 4034 | } |
| 4035 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4036 | *outpos += repwlen; |
| 4037 | |
| 4038 | *endinpos = newpos; |
| 4039 | *inptr = *input + newpos; |
| 4040 | |
| 4041 | /* we made it! */ |
| 4042 | Py_XDECREF(restuple); |
| 4043 | return 0; |
| 4044 | |
| 4045 | onError: |
| 4046 | Py_XDECREF(restuple); |
| 4047 | return -1; |
| 4048 | } |
| 4049 | #endif /* HAVE_MBCS */ |
| 4050 | |
| 4051 | static int |
| 4052 | unicode_decode_call_errorhandler_writer( |
| 4053 | const char *errors, PyObject **errorHandler, |
| 4054 | const char *encoding, const char *reason, |
| 4055 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4056 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4057 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4058 | { |
| 4059 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4060 | |
| 4061 | PyObject *restuple = NULL; |
| 4062 | PyObject *repunicode = NULL; |
| 4063 | Py_ssize_t insize; |
| 4064 | Py_ssize_t newpos; |
| 4065 | PyObject *inputobj = NULL; |
| 4066 | |
| 4067 | if (*errorHandler == NULL) { |
| 4068 | *errorHandler = PyCodec_LookupError(errors); |
| 4069 | if (*errorHandler == NULL) |
| 4070 | goto onError; |
| 4071 | } |
| 4072 | |
| 4073 | make_decode_exception(exceptionObject, |
| 4074 | encoding, |
| 4075 | *input, *inend - *input, |
| 4076 | *startinpos, *endinpos, |
| 4077 | reason); |
| 4078 | if (*exceptionObject == NULL) |
| 4079 | goto onError; |
| 4080 | |
| 4081 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4082 | if (restuple == NULL) |
| 4083 | goto onError; |
| 4084 | if (!PyTuple_Check(restuple)) { |
| 4085 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4086 | goto onError; |
| 4087 | } |
| 4088 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4089 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4090 | |
| 4091 | /* Copy back the bytes variables, which might have been modified by the |
| 4092 | callback */ |
| 4093 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4094 | if (!inputobj) |
| 4095 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4096 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4097 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4098 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4099 | *input = PyBytes_AS_STRING(inputobj); |
| 4100 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4101 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4102 | /* we can DECREF safely, as the exception has another reference, |
| 4103 | so the object won't go away. */ |
| 4104 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4105 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4106 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4107 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4108 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4109 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4110 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4111 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4112 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4113 | writer->overallocate = 1; |
| 4114 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
| 4115 | return |
| 4116 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4117 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4118 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4119 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4120 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4121 | Py_XDECREF(restuple); |
| 4122 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4123 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4124 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4125 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4126 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4129 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4130 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4131 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4132 | |
| 4133 | /* Three simple macros defining base-64. */ |
| 4134 | |
| 4135 | /* Is c a base-64 character? */ |
| 4136 | |
| 4137 | #define IS_BASE64(c) \ |
| 4138 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4139 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4140 | ((c) >= '0' && (c) <= '9') || \ |
| 4141 | (c) == '+' || (c) == '/') |
| 4142 | |
| 4143 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4144 | |
| 4145 | #define FROM_BASE64(c) \ |
| 4146 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4147 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4148 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4149 | (c) == '+' ? 62 : 63) |
| 4150 | |
| 4151 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4152 | |
| 4153 | #define TO_BASE64(n) \ |
| 4154 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4155 | |
| 4156 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4157 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4158 | * byte not decoding to itself is the + which begins a base64 |
| 4159 | * string. */ |
| 4160 | |
| 4161 | #define DECODE_DIRECT(c) \ |
| 4162 | ((c) <= 127 && (c) != '+') |
| 4163 | |
| 4164 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4165 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4166 | * the above). See RFC2152. This array identifies these different |
| 4167 | * sets: |
| 4168 | * 0 : "Set D" |
| 4169 | * alphanumeric and '(),-./:? |
| 4170 | * 1 : "Set O" |
| 4171 | * !"#$%&*;<=>@[]^_`{|} |
| 4172 | * 2 : "whitespace" |
| 4173 | * ht nl cr sp |
| 4174 | * 3 : special (must be base64 encoded) |
| 4175 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4176 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4177 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4178 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4179 | char utf7_category[128] = { |
| 4180 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4181 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4182 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4183 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4184 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4185 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4186 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4187 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4188 | /* @ A B C D E F G H I J K L M N O */ |
| 4189 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4190 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4191 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4192 | /* ` a b c d e f g h i j k l m n o */ |
| 4193 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4194 | /* p q r s t u v w x y z { | } ~ del */ |
| 4195 | 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] | 4196 | }; |
| 4197 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4198 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4199 | * answer depends on whether we are encoding set O as itself, and also |
| 4200 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4201 | * clear that the answers to these questions vary between |
| 4202 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4203 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4204 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4205 | ((c) < 128 && (c) > 0 && \ |
| 4206 | ((utf7_category[(c)] == 0) || \ |
| 4207 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4208 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4209 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4210 | PyObject * |
| 4211 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4212 | Py_ssize_t size, |
| 4213 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4214 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4215 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4216 | } |
| 4217 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4218 | /* The decoder. The only state we preserve is our read position, |
| 4219 | * i.e. how many characters we have consumed. So if we end in the |
| 4220 | * middle of a shift sequence we have to back off the read position |
| 4221 | * and the output to the beginning of the sequence, otherwise we lose |
| 4222 | * all the shift state (seen bits, number of bits seen, high |
| 4223 | * surrogate). */ |
| 4224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4225 | PyObject * |
| 4226 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4227 | Py_ssize_t size, |
| 4228 | const char *errors, |
| 4229 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4230 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4231 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4232 | Py_ssize_t startinpos; |
| 4233 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4234 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4235 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4236 | const char *errmsg = ""; |
| 4237 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4238 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4239 | unsigned int base64bits = 0; |
| 4240 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4241 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4242 | PyObject *errorHandler = NULL; |
| 4243 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4244 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4245 | if (size == 0) { |
| 4246 | if (consumed) |
| 4247 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4248 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4249 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4250 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4251 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4252 | _PyUnicodeWriter_Init(&writer, 0); |
| 4253 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4254 | goto onError; |
| 4255 | |
| 4256 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4257 | e = s + size; |
| 4258 | |
| 4259 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4260 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4261 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4262 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4263 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4264 | if (inShift) { /* in a base-64 section */ |
| 4265 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4266 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4267 | base64bits += 6; |
| 4268 | s++; |
| 4269 | if (base64bits >= 16) { |
| 4270 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4271 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4272 | base64bits -= 16; |
| 4273 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4274 | if (surrogate) { |
| 4275 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4276 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4277 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4278 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch2) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4279 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4280 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch2); |
| 4281 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4282 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4283 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4284 | } |
| 4285 | else { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4286 | if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4287 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4288 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate); |
| 4289 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4290 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4291 | } |
| 4292 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4293 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4294 | /* first surrogate */ |
| 4295 | surrogate = outCh; |
| 4296 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4297 | else { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4298 | if (_PyUnicodeWriter_Prepare(&writer, 1, outCh) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4299 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4300 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, outCh); |
| 4301 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4302 | } |
| 4303 | } |
| 4304 | } |
| 4305 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4306 | inShift = 0; |
| 4307 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4308 | if (surrogate) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4309 | if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4310 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4311 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate); |
| 4312 | writer.pos++; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4313 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4314 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4315 | if (base64bits > 0) { /* left-over bits */ |
| 4316 | if (base64bits >= 6) { |
| 4317 | /* We've seen at least one base-64 character */ |
| 4318 | errmsg = "partial character in shift sequence"; |
| 4319 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4320 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4321 | else { |
| 4322 | /* Some bits remain; they should be zero */ |
| 4323 | if (base64buffer != 0) { |
| 4324 | errmsg = "non-zero padding bits in shift sequence"; |
| 4325 | goto utf7Error; |
| 4326 | } |
| 4327 | } |
| 4328 | } |
| 4329 | if (ch != '-') { |
| 4330 | /* '-' is absorbed; other terminating |
| 4331 | characters are preserved */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4332 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4333 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4334 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4335 | writer.pos++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4336 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4337 | } |
| 4338 | } |
| 4339 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4340 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4341 | s++; /* consume '+' */ |
| 4342 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4343 | s++; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4344 | if (_PyUnicodeWriter_Prepare(&writer, 1, '+') == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4345 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4346 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '+'); |
| 4347 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4348 | } |
| 4349 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4350 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4351 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4352 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4353 | } |
| 4354 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4355 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4356 | s++; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4357 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
| 4358 | goto onError; |
| 4359 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4360 | writer.pos++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4361 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4362 | else { |
| 4363 | startinpos = s-starts; |
| 4364 | s++; |
| 4365 | errmsg = "unexpected special character"; |
| 4366 | goto utf7Error; |
| 4367 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4368 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4370 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4371 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4372 | errors, &errorHandler, |
| 4373 | "utf7", errmsg, |
| 4374 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4375 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4376 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4379 | /* end of string */ |
| 4380 | |
| 4381 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4382 | /* if we're in an inconsistent state, that's an error */ |
| 4383 | if (surrogate || |
| 4384 | (base64bits >= 6) || |
| 4385 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4386 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4387 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4388 | errors, &errorHandler, |
| 4389 | "utf7", "unterminated shift sequence", |
| 4390 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4391 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4392 | goto onError; |
| 4393 | if (s < e) |
| 4394 | goto restart; |
| 4395 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4396 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4397 | |
| 4398 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4399 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4400 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4401 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4402 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4403 | } |
| 4404 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4405 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4406 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4407 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4408 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4409 | Py_XDECREF(errorHandler); |
| 4410 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4411 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4412 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4413 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4414 | Py_XDECREF(errorHandler); |
| 4415 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4416 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 | return NULL; |
| 4418 | } |
| 4419 | |
| 4420 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4421 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4422 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4423 | int base64SetO, |
| 4424 | int base64WhiteSpace, |
| 4425 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4426 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4427 | int kind; |
| 4428 | void *data; |
| 4429 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4430 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4431 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4432 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | unsigned int base64bits = 0; |
| 4434 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4435 | char * out; |
| 4436 | char * start; |
| 4437 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4438 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4439 | return NULL; |
| 4440 | kind = PyUnicode_KIND(str); |
| 4441 | data = PyUnicode_DATA(str); |
| 4442 | len = PyUnicode_GET_LENGTH(str); |
| 4443 | |
| 4444 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4445 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4446 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4447 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4448 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4449 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4450 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4451 | if (v == NULL) |
| 4452 | return NULL; |
| 4453 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4454 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4455 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4456 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4457 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4458 | if (inShift) { |
| 4459 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4460 | /* shifting out */ |
| 4461 | if (base64bits) { /* output remaining bits */ |
| 4462 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4463 | base64buffer = 0; |
| 4464 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4465 | } |
| 4466 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4467 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4468 | so no '-' is required, except if the character is itself a '-' */ |
| 4469 | if (IS_BASE64(ch) || ch == '-') { |
| 4470 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4471 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4472 | *out++ = (char) ch; |
| 4473 | } |
| 4474 | else { |
| 4475 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4476 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4477 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4478 | else { /* not in a shift sequence */ |
| 4479 | if (ch == '+') { |
| 4480 | *out++ = '+'; |
| 4481 | *out++ = '-'; |
| 4482 | } |
| 4483 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4484 | *out++ = (char) ch; |
| 4485 | } |
| 4486 | else { |
| 4487 | *out++ = '+'; |
| 4488 | inShift = 1; |
| 4489 | goto encode_char; |
| 4490 | } |
| 4491 | } |
| 4492 | continue; |
| 4493 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4494 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4495 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4496 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4497 | /* code first surrogate */ |
| 4498 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4499 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4500 | while (base64bits >= 6) { |
| 4501 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4502 | base64bits -= 6; |
| 4503 | } |
| 4504 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4505 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4506 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4507 | base64bits += 16; |
| 4508 | base64buffer = (base64buffer << 16) | ch; |
| 4509 | while (base64bits >= 6) { |
| 4510 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4511 | base64bits -= 6; |
| 4512 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4513 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4514 | if (base64bits) |
| 4515 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4516 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4517 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4518 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4519 | return NULL; |
| 4520 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4521 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4522 | PyObject * |
| 4523 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4524 | Py_ssize_t size, |
| 4525 | int base64SetO, |
| 4526 | int base64WhiteSpace, |
| 4527 | const char *errors) |
| 4528 | { |
| 4529 | PyObject *result; |
| 4530 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4531 | if (tmp == NULL) |
| 4532 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4533 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4534 | base64WhiteSpace, errors); |
| 4535 | Py_DECREF(tmp); |
| 4536 | return result; |
| 4537 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4538 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4539 | #undef IS_BASE64 |
| 4540 | #undef FROM_BASE64 |
| 4541 | #undef TO_BASE64 |
| 4542 | #undef DECODE_DIRECT |
| 4543 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4544 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4545 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4546 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4547 | PyObject * |
| 4548 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4549 | Py_ssize_t size, |
| 4550 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4551 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4552 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4553 | } |
| 4554 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4555 | #include "stringlib/asciilib.h" |
| 4556 | #include "stringlib/codecs.h" |
| 4557 | #include "stringlib/undef.h" |
| 4558 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4559 | #include "stringlib/ucs1lib.h" |
| 4560 | #include "stringlib/codecs.h" |
| 4561 | #include "stringlib/undef.h" |
| 4562 | |
| 4563 | #include "stringlib/ucs2lib.h" |
| 4564 | #include "stringlib/codecs.h" |
| 4565 | #include "stringlib/undef.h" |
| 4566 | |
| 4567 | #include "stringlib/ucs4lib.h" |
| 4568 | #include "stringlib/codecs.h" |
| 4569 | #include "stringlib/undef.h" |
| 4570 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4571 | /* Mask to quickly check whether a C 'long' contains a |
| 4572 | non-ASCII, UTF8-encoded char. */ |
| 4573 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4574 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4575 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4576 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4577 | #else |
| 4578 | # error C 'long' size should be either 4 or 8! |
| 4579 | #endif |
| 4580 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4581 | static Py_ssize_t |
| 4582 | ascii_decode(const char *start, const char *end, Py_UCS1 *dest) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4583 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4584 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4585 | const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4586 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4587 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4588 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4589 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4590 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4591 | an explanation. */ |
| 4592 | /* Help register allocation */ |
| 4593 | register const char *_p = p; |
| 4594 | register Py_UCS1 * q = dest; |
| 4595 | while (_p < aligned_end) { |
| 4596 | unsigned long value = *(const unsigned long *) _p; |
| 4597 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4598 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4599 | *((unsigned long *)q) = value; |
| 4600 | _p += SIZEOF_LONG; |
| 4601 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4602 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4603 | p = _p; |
| 4604 | while (p < end) { |
| 4605 | if ((unsigned char)*p & 0x80) |
| 4606 | break; |
| 4607 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4608 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4609 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4610 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4611 | #endif |
| 4612 | while (p < end) { |
| 4613 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4614 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4615 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4616 | /* Help register allocation */ |
| 4617 | register const char *_p = p; |
| 4618 | while (_p < aligned_end) { |
| 4619 | unsigned long value = *(unsigned long *) _p; |
| 4620 | if (value & ASCII_CHAR_MASK) |
| 4621 | break; |
| 4622 | _p += SIZEOF_LONG; |
| 4623 | } |
| 4624 | p = _p; |
| 4625 | if (_p == end) |
| 4626 | break; |
| 4627 | } |
| 4628 | if ((unsigned char)*p & 0x80) |
| 4629 | break; |
| 4630 | ++p; |
| 4631 | } |
| 4632 | memcpy(dest, start, p - start); |
| 4633 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4634 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4635 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4636 | PyObject * |
| 4637 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4638 | Py_ssize_t size, |
| 4639 | const char *errors, |
| 4640 | Py_ssize_t *consumed) |
| 4641 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4642 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4643 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4644 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4645 | |
| 4646 | Py_ssize_t startinpos; |
| 4647 | Py_ssize_t endinpos; |
| 4648 | const char *errmsg = ""; |
| 4649 | PyObject *errorHandler = NULL; |
| 4650 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4651 | |
| 4652 | if (size == 0) { |
| 4653 | if (consumed) |
| 4654 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4655 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4656 | } |
| 4657 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4658 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4659 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4660 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4661 | *consumed = 1; |
| 4662 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4663 | } |
| 4664 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4665 | _PyUnicodeWriter_Init(&writer, 0); |
| 4666 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4667 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4668 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4669 | writer.pos = ascii_decode(s, end, writer.data); |
| 4670 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4671 | while (s < end) { |
| 4672 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4673 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4674 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4675 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4676 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4677 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4678 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4679 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4680 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4681 | } else { |
| 4682 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4683 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | switch (ch) { |
| 4687 | case 0: |
| 4688 | if (s == end || consumed) |
| 4689 | goto End; |
| 4690 | errmsg = "unexpected end of data"; |
| 4691 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4692 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4693 | break; |
| 4694 | case 1: |
| 4695 | errmsg = "invalid start byte"; |
| 4696 | startinpos = s - starts; |
| 4697 | endinpos = startinpos + 1; |
| 4698 | break; |
| 4699 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4700 | case 3: |
| 4701 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4702 | errmsg = "invalid continuation byte"; |
| 4703 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4704 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4705 | break; |
| 4706 | default: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4707 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4708 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4709 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4710 | writer.pos++; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4711 | continue; |
| 4712 | } |
| 4713 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4714 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4715 | errors, &errorHandler, |
| 4716 | "utf-8", errmsg, |
| 4717 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4718 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4719 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4720 | } |
| 4721 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4722 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4723 | if (consumed) |
| 4724 | *consumed = s - starts; |
| 4725 | |
| 4726 | Py_XDECREF(errorHandler); |
| 4727 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4728 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4729 | |
| 4730 | onError: |
| 4731 | Py_XDECREF(errorHandler); |
| 4732 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4733 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4734 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4735 | } |
| 4736 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4737 | #ifdef __APPLE__ |
| 4738 | |
| 4739 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4740 | used to decode the command line arguments on Mac OS X. |
| 4741 | |
| 4742 | Return a pointer to a newly allocated wide character string (use |
| 4743 | PyMem_Free() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4744 | |
| 4745 | wchar_t* |
| 4746 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4747 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4748 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4749 | wchar_t *unicode; |
| 4750 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4751 | |
| 4752 | /* Note: size will always be longer than the resulting Unicode |
| 4753 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4754 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4755 | return NULL; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4756 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4757 | if (!unicode) |
| 4758 | return NULL; |
| 4759 | |
| 4760 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4761 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4762 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4763 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4764 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4765 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4766 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4767 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4768 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4769 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4770 | if (ch > 0xFF) { |
| 4771 | #if SIZEOF_WCHAR_T == 4 |
| 4772 | assert(0); |
| 4773 | #else |
| 4774 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4775 | /* compute and append the two surrogates: */ |
| 4776 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4777 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4778 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4779 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4780 | else { |
| 4781 | if (!ch && s == e) |
| 4782 | break; |
| 4783 | /* surrogateescape */ |
| 4784 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4785 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4786 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4787 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4788 | return unicode; |
| 4789 | } |
| 4790 | |
| 4791 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4793 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4794 | |
| 4795 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4796 | and allocate exactly as much space needed at the end. Else allocate the |
| 4797 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4798 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4799 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4800 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4801 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4802 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4803 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4804 | void *data; |
| 4805 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4807 | if (!PyUnicode_Check(unicode)) { |
| 4808 | PyErr_BadArgument(); |
| 4809 | return NULL; |
| 4810 | } |
| 4811 | |
| 4812 | if (PyUnicode_READY(unicode) == -1) |
| 4813 | return NULL; |
| 4814 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4815 | if (PyUnicode_UTF8(unicode)) |
| 4816 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4817 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4818 | |
| 4819 | kind = PyUnicode_KIND(unicode); |
| 4820 | data = PyUnicode_DATA(unicode); |
| 4821 | size = PyUnicode_GET_LENGTH(unicode); |
| 4822 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4823 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4824 | default: |
| 4825 | assert(0); |
| 4826 | case PyUnicode_1BYTE_KIND: |
| 4827 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4828 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4829 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4830 | case PyUnicode_2BYTE_KIND: |
| 4831 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4832 | case PyUnicode_4BYTE_KIND: |
| 4833 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4834 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4837 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4838 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4839 | Py_ssize_t size, |
| 4840 | const char *errors) |
| 4841 | { |
| 4842 | PyObject *v, *unicode; |
| 4843 | |
| 4844 | unicode = PyUnicode_FromUnicode(s, size); |
| 4845 | if (unicode == NULL) |
| 4846 | return NULL; |
| 4847 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4848 | Py_DECREF(unicode); |
| 4849 | return v; |
| 4850 | } |
| 4851 | |
| 4852 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4853 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4854 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4855 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4856 | } |
| 4857 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4858 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4859 | |
| 4860 | PyObject * |
| 4861 | PyUnicode_DecodeUTF32(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) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4865 | { |
| 4866 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4867 | } |
| 4868 | |
| 4869 | PyObject * |
| 4870 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4871 | Py_ssize_t size, |
| 4872 | const char *errors, |
| 4873 | int *byteorder, |
| 4874 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4875 | { |
| 4876 | const char *starts = s; |
| 4877 | Py_ssize_t startinpos; |
| 4878 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4879 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4880 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4881 | int le, bo = 0; /* assume native ordering by default */ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4882 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4883 | PyObject *errorHandler = NULL; |
| 4884 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4885 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4886 | q = (unsigned char *)s; |
| 4887 | e = q + size; |
| 4888 | |
| 4889 | if (byteorder) |
| 4890 | bo = *byteorder; |
| 4891 | |
| 4892 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4893 | byte order setting accordingly. In native mode, the leading BOM |
| 4894 | mark is skipped, in all other modes, it is copied to the output |
| 4895 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4896 | if (bo == 0 && size >= 4) { |
| 4897 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4898 | if (bom == 0x0000FEFF) { |
| 4899 | bo = -1; |
| 4900 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4901 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4902 | else if (bom == 0xFFFE0000) { |
| 4903 | bo = 1; |
| 4904 | q += 4; |
| 4905 | } |
| 4906 | if (byteorder) |
| 4907 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4910 | if (q == e) { |
| 4911 | if (consumed) |
| 4912 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4913 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4916 | #ifdef WORDS_BIGENDIAN |
| 4917 | le = bo < 0; |
| 4918 | #else |
| 4919 | le = bo <= 0; |
| 4920 | #endif |
| 4921 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4922 | _PyUnicodeWriter_Init(&writer, 0); |
| 4923 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 3) / 4, 127) == -1) |
| 4924 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4925 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4926 | while (1) { |
| 4927 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4928 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4929 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4930 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4931 | enum PyUnicode_Kind kind = writer.kind; |
| 4932 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4933 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4934 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4935 | if (le) { |
| 4936 | do { |
| 4937 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4938 | if (ch > maxch) |
| 4939 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4940 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4941 | q += 4; |
| 4942 | } while (q <= last); |
| 4943 | } |
| 4944 | else { |
| 4945 | do { |
| 4946 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 4947 | if (ch > maxch) |
| 4948 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4949 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4950 | q += 4; |
| 4951 | } while (q <= last); |
| 4952 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4953 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | if (ch <= maxch) { |
| 4957 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4958 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4959 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4960 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4961 | startinpos = ((const char *)q) - starts; |
| 4962 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4963 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4964 | else { |
| 4965 | if (ch < 0x110000) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4966 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4967 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4968 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4969 | writer.pos++; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4970 | q += 4; |
| 4971 | continue; |
| 4972 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4973 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4974 | startinpos = ((const char *)q) - starts; |
| 4975 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4976 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4977 | |
| 4978 | /* The remaining input chars are ignored if the callback |
| 4979 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4980 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4981 | errors, &errorHandler, |
| 4982 | "utf32", errmsg, |
| 4983 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4984 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4985 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4988 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4989 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4990 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4991 | Py_XDECREF(errorHandler); |
| 4992 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4993 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4994 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4995 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4996 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4997 | Py_XDECREF(errorHandler); |
| 4998 | Py_XDECREF(exc); |
| 4999 | return NULL; |
| 5000 | } |
| 5001 | |
| 5002 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5003 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5004 | const char *errors, |
| 5005 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5006 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5007 | int kind; |
| 5008 | void *data; |
| 5009 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5010 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5011 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5012 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5013 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5014 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5015 | int iorder[] = {0, 1, 2, 3}; |
| 5016 | #else |
| 5017 | int iorder[] = {3, 2, 1, 0}; |
| 5018 | #endif |
| 5019 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5020 | #define STORECHAR(CH) \ |
| 5021 | do { \ |
| 5022 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5023 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5024 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5025 | p[iorder[0]] = (CH) & 0xff; \ |
| 5026 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5027 | } while(0) |
| 5028 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5029 | if (!PyUnicode_Check(str)) { |
| 5030 | PyErr_BadArgument(); |
| 5031 | return NULL; |
| 5032 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5033 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5034 | return NULL; |
| 5035 | kind = PyUnicode_KIND(str); |
| 5036 | data = PyUnicode_DATA(str); |
| 5037 | len = PyUnicode_GET_LENGTH(str); |
| 5038 | |
| 5039 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5040 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5041 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5042 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5043 | if (v == NULL) |
| 5044 | return NULL; |
| 5045 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5046 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5047 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5048 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5049 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5050 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5051 | |
| 5052 | if (byteorder == -1) { |
| 5053 | /* force LE */ |
| 5054 | iorder[0] = 0; |
| 5055 | iorder[1] = 1; |
| 5056 | iorder[2] = 2; |
| 5057 | iorder[3] = 3; |
| 5058 | } |
| 5059 | else if (byteorder == 1) { |
| 5060 | /* force BE */ |
| 5061 | iorder[0] = 3; |
| 5062 | iorder[1] = 2; |
| 5063 | iorder[2] = 1; |
| 5064 | iorder[3] = 0; |
| 5065 | } |
| 5066 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5067 | for (i = 0; i < len; i++) |
| 5068 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5069 | |
| 5070 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5071 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5072 | #undef STORECHAR |
| 5073 | } |
| 5074 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5075 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5076 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5077 | Py_ssize_t size, |
| 5078 | const char *errors, |
| 5079 | int byteorder) |
| 5080 | { |
| 5081 | PyObject *result; |
| 5082 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5083 | if (tmp == NULL) |
| 5084 | return NULL; |
| 5085 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5086 | Py_DECREF(tmp); |
| 5087 | return result; |
| 5088 | } |
| 5089 | |
| 5090 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5091 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5092 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5093 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5094 | } |
| 5095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5097 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5098 | PyObject * |
| 5099 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5100 | Py_ssize_t size, |
| 5101 | const char *errors, |
| 5102 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5103 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5104 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5105 | } |
| 5106 | |
| 5107 | PyObject * |
| 5108 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5109 | Py_ssize_t size, |
| 5110 | const char *errors, |
| 5111 | int *byteorder, |
| 5112 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5113 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5114 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5115 | Py_ssize_t startinpos; |
| 5116 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5117 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5118 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5119 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5120 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5121 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5122 | PyObject *errorHandler = NULL; |
| 5123 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5124 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5125 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5126 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5127 | |
| 5128 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5129 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5130 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5131 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5132 | byte order setting accordingly. In native mode, the leading BOM |
| 5133 | mark is skipped, in all other modes, it is copied to the output |
| 5134 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5135 | if (bo == 0 && size >= 2) { |
| 5136 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5137 | if (bom == 0xFEFF) { |
| 5138 | q += 2; |
| 5139 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5140 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5141 | else if (bom == 0xFFFE) { |
| 5142 | q += 2; |
| 5143 | bo = 1; |
| 5144 | } |
| 5145 | if (byteorder) |
| 5146 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5147 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5148 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5149 | if (q == e) { |
| 5150 | if (consumed) |
| 5151 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5152 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5153 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5154 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5155 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5156 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5157 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5158 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5159 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5160 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5161 | /* Note: size will always be longer than the resulting Unicode |
| 5162 | character count */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5163 | _PyUnicodeWriter_Init(&writer, 0); |
| 5164 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 1) / 2, 127) == -1) |
| 5165 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5166 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5167 | while (1) { |
| 5168 | Py_UCS4 ch = 0; |
| 5169 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5170 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5171 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5172 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5173 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5174 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5175 | native_ordering); |
| 5176 | else |
| 5177 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5178 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5179 | native_ordering); |
| 5180 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5181 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5182 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5183 | native_ordering); |
| 5184 | } else { |
| 5185 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5186 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5187 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5188 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5189 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5190 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5191 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5192 | switch (ch) |
| 5193 | { |
| 5194 | case 0: |
| 5195 | /* remaining byte at the end? (size should be even) */ |
| 5196 | if (q == e || consumed) |
| 5197 | goto End; |
| 5198 | errmsg = "truncated data"; |
| 5199 | startinpos = ((const char *)q) - starts; |
| 5200 | endinpos = ((const char *)e) - starts; |
| 5201 | break; |
| 5202 | /* The remaining input chars are ignored if the callback |
| 5203 | chooses to skip the input */ |
| 5204 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5205 | q -= 2; |
| 5206 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5207 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5208 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5209 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5210 | endinpos = ((const char *)e) - starts; |
| 5211 | break; |
| 5212 | case 2: |
| 5213 | errmsg = "illegal encoding"; |
| 5214 | startinpos = ((const char *)q) - 2 - starts; |
| 5215 | endinpos = startinpos + 2; |
| 5216 | break; |
| 5217 | case 3: |
| 5218 | errmsg = "illegal UTF-16 surrogate"; |
| 5219 | startinpos = ((const char *)q) - 4 - starts; |
| 5220 | endinpos = startinpos + 2; |
| 5221 | break; |
| 5222 | default: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5223 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5224 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5225 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 5226 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5227 | continue; |
| 5228 | } |
| 5229 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5230 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5231 | errors, |
| 5232 | &errorHandler, |
| 5233 | "utf16", errmsg, |
| 5234 | &starts, |
| 5235 | (const char **)&e, |
| 5236 | &startinpos, |
| 5237 | &endinpos, |
| 5238 | &exc, |
| 5239 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5240 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5241 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5242 | } |
| 5243 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5244 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5245 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5246 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5247 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5248 | Py_XDECREF(errorHandler); |
| 5249 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5250 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5251 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5252 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5253 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5254 | Py_XDECREF(errorHandler); |
| 5255 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5256 | return NULL; |
| 5257 | } |
| 5258 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5259 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5260 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5261 | const char *errors, |
| 5262 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5263 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5264 | enum PyUnicode_Kind kind; |
| 5265 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5266 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5267 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5268 | unsigned short *out; |
| 5269 | Py_ssize_t bytesize; |
| 5270 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5271 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5272 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5273 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5274 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5275 | #endif |
| 5276 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5277 | if (!PyUnicode_Check(str)) { |
| 5278 | PyErr_BadArgument(); |
| 5279 | return NULL; |
| 5280 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5281 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5282 | return NULL; |
| 5283 | kind = PyUnicode_KIND(str); |
| 5284 | data = PyUnicode_DATA(str); |
| 5285 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5286 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5287 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5288 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5289 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5290 | const Py_UCS4 *end = in + len; |
| 5291 | while (in < end) |
| 5292 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5293 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5294 | } |
| 5295 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5296 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5297 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5298 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5299 | if (v == NULL) |
| 5300 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5302 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5303 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5304 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5305 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5306 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5307 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5308 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5309 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5310 | switch (kind) { |
| 5311 | case PyUnicode_1BYTE_KIND: { |
| 5312 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5313 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5314 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5315 | case PyUnicode_2BYTE_KIND: { |
| 5316 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5317 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5318 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5319 | case PyUnicode_4BYTE_KIND: { |
| 5320 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5321 | break; |
| 5322 | } |
| 5323 | default: |
| 5324 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5325 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5326 | |
| 5327 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5328 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5329 | } |
| 5330 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5331 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5332 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5333 | Py_ssize_t size, |
| 5334 | const char *errors, |
| 5335 | int byteorder) |
| 5336 | { |
| 5337 | PyObject *result; |
| 5338 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5339 | if (tmp == NULL) |
| 5340 | return NULL; |
| 5341 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5342 | Py_DECREF(tmp); |
| 5343 | return result; |
| 5344 | } |
| 5345 | |
| 5346 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5347 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5348 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5349 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5353 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5354 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5355 | if all the escapes in the string make it still a valid ASCII string. |
| 5356 | Returns -1 if any escapes were found which cause the string to |
| 5357 | pop out of ASCII range. Otherwise returns the length of the |
| 5358 | required buffer to hold the string. |
| 5359 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5360 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5361 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5362 | { |
| 5363 | const unsigned char *p = (const unsigned char *)s; |
| 5364 | const unsigned char *end = p + size; |
| 5365 | Py_ssize_t length = 0; |
| 5366 | |
| 5367 | if (size < 0) |
| 5368 | return -1; |
| 5369 | |
| 5370 | for (; p < end; ++p) { |
| 5371 | if (*p > 127) { |
| 5372 | /* Non-ASCII */ |
| 5373 | return -1; |
| 5374 | } |
| 5375 | else if (*p != '\\') { |
| 5376 | /* Normal character */ |
| 5377 | ++length; |
| 5378 | } |
| 5379 | else { |
| 5380 | /* Backslash-escape, check next char */ |
| 5381 | ++p; |
| 5382 | /* Escape sequence reaches till end of string or |
| 5383 | non-ASCII follow-up. */ |
| 5384 | if (p >= end || *p > 127) |
| 5385 | return -1; |
| 5386 | switch (*p) { |
| 5387 | case '\n': |
| 5388 | /* backslash + \n result in zero characters */ |
| 5389 | break; |
| 5390 | case '\\': case '\'': case '\"': |
| 5391 | case 'b': case 'f': case 't': |
| 5392 | case 'n': case 'r': case 'v': case 'a': |
| 5393 | ++length; |
| 5394 | break; |
| 5395 | case '0': case '1': case '2': case '3': |
| 5396 | case '4': case '5': case '6': case '7': |
| 5397 | case 'x': case 'u': case 'U': case 'N': |
| 5398 | /* these do not guarantee ASCII characters */ |
| 5399 | return -1; |
| 5400 | default: |
| 5401 | /* count the backslash + the other character */ |
| 5402 | length += 2; |
| 5403 | } |
| 5404 | } |
| 5405 | } |
| 5406 | return length; |
| 5407 | } |
| 5408 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5409 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5410 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5411 | PyObject * |
| 5412 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5413 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5414 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5416 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5417 | Py_ssize_t startinpos; |
| 5418 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5419 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5421 | char* message; |
| 5422 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5423 | PyObject *errorHandler = NULL; |
| 5424 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5425 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5426 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5427 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5428 | if (len == 0) |
| 5429 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5430 | |
| 5431 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5432 | either the string is pure ASCII with named escapes like \n, etc. |
| 5433 | and we determined it's exact size (common case) |
| 5434 | or it contains \x, \u, ... escape sequences. then we create a |
| 5435 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5436 | _PyUnicodeWriter_Init(&writer, 0); |
| 5437 | if (len > 0) { |
| 5438 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5439 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5440 | assert(writer.kind == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5441 | } |
| 5442 | else { |
| 5443 | /* Escaped strings will always be longer than the resulting |
| 5444 | Unicode string, so we start with size here and then reduce the |
| 5445 | length after conversion to the true value. |
| 5446 | (but if the error callback returns a long replacement string |
| 5447 | we'll have to allocate more space) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5448 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5449 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5450 | } |
| 5451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5452 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5453 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5455 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5456 | while (s < end) { |
| 5457 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5458 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5459 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | |
| 5461 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5462 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5463 | x = (unsigned char)*s; |
| 5464 | s++; |
| 5465 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5466 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5467 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5468 | writer.pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | continue; |
| 5470 | } |
| 5471 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5472 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | /* \ - Escapes */ |
| 5474 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5475 | c = *s++; |
| 5476 | if (s > end) |
| 5477 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5478 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5479 | /* The only case in which i == ascii_length is a backslash |
| 5480 | followed by a newline. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5481 | 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] | 5482 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5483 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5485 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5486 | #define WRITECHAR(ch) \ |
| 5487 | do { \ |
| 5488 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) \ |
| 5489 | goto onError; \ |
| 5490 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); \ |
| 5491 | writer.pos++; \ |
| 5492 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5493 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5494 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5495 | case '\\': WRITECHAR('\\'); break; |
| 5496 | case '\'': WRITECHAR('\''); break; |
| 5497 | case '\"': WRITECHAR('\"'); break; |
| 5498 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5499 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5500 | case 'f': WRITECHAR('\014'); break; |
| 5501 | case 't': WRITECHAR('\t'); break; |
| 5502 | case 'n': WRITECHAR('\n'); break; |
| 5503 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5504 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5505 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5506 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5507 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5508 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5509 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5510 | case '0': case '1': case '2': case '3': |
| 5511 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5512 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5513 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5514 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5515 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5516 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5517 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5518 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5519 | break; |
| 5520 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5521 | /* hex escapes */ |
| 5522 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5523 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5524 | digits = 2; |
| 5525 | message = "truncated \\xXX escape"; |
| 5526 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5528 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5529 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5530 | digits = 4; |
| 5531 | message = "truncated \\uXXXX escape"; |
| 5532 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5533 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5534 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5535 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5536 | digits = 8; |
| 5537 | message = "truncated \\UXXXXXXXX escape"; |
| 5538 | hexescape: |
| 5539 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5540 | if (end - s < digits) { |
| 5541 | /* count only hex digits */ |
| 5542 | for (; s < end; ++s) { |
| 5543 | c = (unsigned char)*s; |
| 5544 | if (!Py_ISXDIGIT(c)) |
| 5545 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5546 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5547 | goto error; |
| 5548 | } |
| 5549 | for (; digits--; ++s) { |
| 5550 | c = (unsigned char)*s; |
| 5551 | if (!Py_ISXDIGIT(c)) |
| 5552 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5553 | chr = (chr<<4) & ~0xF; |
| 5554 | if (c >= '0' && c <= '9') |
| 5555 | chr += c - '0'; |
| 5556 | else if (c >= 'a' && c <= 'f') |
| 5557 | chr += 10 + c - 'a'; |
| 5558 | else |
| 5559 | chr += 10 + c - 'A'; |
| 5560 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5561 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5562 | /* _decoding_error will have already written into the |
| 5563 | target buffer. */ |
| 5564 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5565 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5566 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5567 | message = "illegal Unicode character"; |
| 5568 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5569 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5570 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5571 | break; |
| 5572 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5573 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5574 | case 'N': |
| 5575 | message = "malformed \\N character escape"; |
| 5576 | if (ucnhash_CAPI == NULL) { |
| 5577 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5578 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5579 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5580 | if (ucnhash_CAPI == NULL) |
| 5581 | goto ucnhashError; |
| 5582 | } |
| 5583 | if (*s == '{') { |
| 5584 | const char *start = s+1; |
| 5585 | /* look for the closing brace */ |
| 5586 | while (*s != '}' && s < end) |
| 5587 | s++; |
| 5588 | if (s > start && s < end && *s == '}') { |
| 5589 | /* found a name. look it up in the unicode database */ |
| 5590 | message = "unknown Unicode character name"; |
| 5591 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5592 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5593 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5594 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5595 | goto store; |
| 5596 | } |
| 5597 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5598 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5599 | |
| 5600 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5601 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5602 | message = "\\ at end of string"; |
| 5603 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5604 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5605 | } |
| 5606 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5607 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5608 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5609 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5610 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5612 | continue; |
| 5613 | |
| 5614 | error: |
| 5615 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5616 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5617 | errors, &errorHandler, |
| 5618 | "unicodeescape", message, |
| 5619 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5620 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5621 | goto onError; |
| 5622 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5623 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5624 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5625 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5626 | Py_XDECREF(errorHandler); |
| 5627 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5628 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5629 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5630 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5631 | PyErr_SetString( |
| 5632 | PyExc_UnicodeError, |
| 5633 | "\\N escapes not supported (can't load unicodedata module)" |
| 5634 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5635 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5636 | Py_XDECREF(errorHandler); |
| 5637 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5638 | return NULL; |
| 5639 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5640 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5641 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5642 | Py_XDECREF(errorHandler); |
| 5643 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5644 | return NULL; |
| 5645 | } |
| 5646 | |
| 5647 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5648 | |
| 5649 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5650 | appropriate. |
| 5651 | |
| 5652 | */ |
| 5653 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5654 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5655 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5656 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5657 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5658 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5659 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5660 | int kind; |
| 5661 | void *data; |
| 5662 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5664 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5665 | escape. |
| 5666 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5667 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5668 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5669 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5670 | */ |
| 5671 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5672 | if (!PyUnicode_Check(unicode)) { |
| 5673 | PyErr_BadArgument(); |
| 5674 | return NULL; |
| 5675 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5676 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5677 | return NULL; |
| 5678 | len = PyUnicode_GET_LENGTH(unicode); |
| 5679 | kind = PyUnicode_KIND(unicode); |
| 5680 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5681 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5682 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5683 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5684 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5685 | } |
| 5686 | |
| 5687 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5688 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5689 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5690 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5691 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5692 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5693 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5694 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5695 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5696 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | if (repr == NULL) |
| 5698 | return NULL; |
| 5699 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5700 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5701 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5702 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5703 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5704 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5705 | /* Escape backslashes */ |
| 5706 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5707 | *p++ = '\\'; |
| 5708 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5709 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5710 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5711 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5712 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5713 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5714 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5715 | *p++ = '\\'; |
| 5716 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5717 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5718 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5719 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5720 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5721 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5722 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5723 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5724 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5725 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5726 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5729 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5730 | *p++ = '\\'; |
| 5731 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5732 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5733 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5734 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5735 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5737 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5738 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5739 | else if (ch == '\t') { |
| 5740 | *p++ = '\\'; |
| 5741 | *p++ = 't'; |
| 5742 | } |
| 5743 | else if (ch == '\n') { |
| 5744 | *p++ = '\\'; |
| 5745 | *p++ = 'n'; |
| 5746 | } |
| 5747 | else if (ch == '\r') { |
| 5748 | *p++ = '\\'; |
| 5749 | *p++ = 'r'; |
| 5750 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5751 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5752 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5753 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5755 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5756 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5757 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5758 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | /* Copy everything else as-is */ |
| 5761 | else |
| 5762 | *p++ = (char) ch; |
| 5763 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5765 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5766 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5767 | return NULL; |
| 5768 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5771 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5772 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5773 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5774 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5775 | PyObject *result; |
| 5776 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5777 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5779 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5780 | Py_DECREF(tmp); |
| 5781 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
| 5784 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5785 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5786 | PyObject * |
| 5787 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5788 | Py_ssize_t size, |
| 5789 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5791 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5792 | Py_ssize_t startinpos; |
| 5793 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5794 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | const char *end; |
| 5796 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5797 | PyObject *errorHandler = NULL; |
| 5798 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5799 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5800 | if (size == 0) |
| 5801 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5802 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5803 | /* Escaped strings will always be longer than the resulting |
| 5804 | 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] | 5805 | length after conversion to the true value. (But decoding error |
| 5806 | handler might have to resize the string) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5807 | _PyUnicodeWriter_Init(&writer, 1); |
| 5808 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5809 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | end = s + size; |
| 5812 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5813 | unsigned char c; |
| 5814 | Py_UCS4 x; |
| 5815 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5816 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5817 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5818 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5819 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5820 | x = (unsigned char)*s++; |
| 5821 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5822 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5823 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5824 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5825 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5826 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5827 | startinpos = s-starts; |
| 5828 | |
| 5829 | /* \u-escapes are only interpreted iff the number of leading |
| 5830 | backslashes if odd */ |
| 5831 | bs = s; |
| 5832 | for (;s < end;) { |
| 5833 | if (*s != '\\') |
| 5834 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5835 | x = (unsigned char)*s++; |
| 5836 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5837 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5838 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5839 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5840 | } |
| 5841 | if (((s - bs) & 1) == 0 || |
| 5842 | s >= end || |
| 5843 | (*s != 'u' && *s != 'U')) { |
| 5844 | continue; |
| 5845 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5846 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5847 | count = *s=='u' ? 4 : 8; |
| 5848 | s++; |
| 5849 | |
| 5850 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5851 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5852 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5853 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5854 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5855 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5856 | errors, &errorHandler, |
| 5857 | "rawunicodeescape", "truncated \\uXXXX", |
| 5858 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5859 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5860 | goto onError; |
| 5861 | goto nextByte; |
| 5862 | } |
| 5863 | x = (x<<4) & ~0xF; |
| 5864 | if (c >= '0' && c <= '9') |
| 5865 | x += c - '0'; |
| 5866 | else if (c >= 'a' && c <= 'f') |
| 5867 | x += 10 + c - 'a'; |
| 5868 | else |
| 5869 | x += 10 + c - 'A'; |
| 5870 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5871 | if (x <= MAX_UNICODE) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5872 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5873 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5874 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5875 | writer.pos++; |
| 5876 | } |
| 5877 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5878 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5879 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5880 | errors, &errorHandler, |
| 5881 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5882 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5883 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5884 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5885 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5886 | nextByte: |
| 5887 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5889 | Py_XDECREF(errorHandler); |
| 5890 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5891 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5892 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5893 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5894 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5895 | Py_XDECREF(errorHandler); |
| 5896 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | return NULL; |
| 5898 | } |
| 5899 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5900 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5901 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5902 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5904 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | char *p; |
| 5906 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5907 | Py_ssize_t expandsize, pos; |
| 5908 | int kind; |
| 5909 | void *data; |
| 5910 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5912 | if (!PyUnicode_Check(unicode)) { |
| 5913 | PyErr_BadArgument(); |
| 5914 | return NULL; |
| 5915 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5916 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5917 | return NULL; |
| 5918 | kind = PyUnicode_KIND(unicode); |
| 5919 | data = PyUnicode_DATA(unicode); |
| 5920 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5921 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 5922 | bytes, and 1 byte characters 4. */ |
| 5923 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5924 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5925 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5926 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5927 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5928 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | if (repr == NULL) |
| 5930 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5931 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5932 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5934 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5935 | for (pos = 0; pos < len; pos++) { |
| 5936 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5937 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5938 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5939 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5940 | *p++ = '\\'; |
| 5941 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5942 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 5943 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 5944 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 5945 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 5946 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5947 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5948 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5949 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5950 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5952 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | *p++ = '\\'; |
| 5954 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5955 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5956 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5957 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5958 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5960 | /* Copy everything else as-is */ |
| 5961 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5962 | *p++ = (char) ch; |
| 5963 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5964 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5965 | assert(p > q); |
| 5966 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5967 | return NULL; |
| 5968 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5971 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5972 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 5973 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5974 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5975 | PyObject *result; |
| 5976 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5977 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5978 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5979 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 5980 | Py_DECREF(tmp); |
| 5981 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5984 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5985 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5986 | PyObject * |
| 5987 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5988 | Py_ssize_t size, |
| 5989 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5990 | { |
| 5991 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5992 | Py_ssize_t startinpos; |
| 5993 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5994 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5995 | const char *end; |
| 5996 | const char *reason; |
| 5997 | PyObject *errorHandler = NULL; |
| 5998 | PyObject *exc = NULL; |
| 5999 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6000 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6001 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6002 | 1)) |
| 6003 | return NULL; |
| 6004 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6005 | if (size == 0) |
| 6006 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6007 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6008 | /* XXX overflow detection missing */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6009 | _PyUnicodeWriter_Init(&writer, 0); |
| 6010 | 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] | 6011 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6012 | end = s + size; |
| 6013 | |
| 6014 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6015 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6016 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6017 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6018 | endinpos = end-starts; |
| 6019 | reason = "truncated input"; |
| 6020 | goto error; |
| 6021 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6022 | /* We copy the raw representation one byte at a time because the |
| 6023 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6024 | ((char *) &uch)[0] = s[0]; |
| 6025 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6026 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6027 | ((char *) &uch)[2] = s[2]; |
| 6028 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6029 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6030 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6031 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6032 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6033 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6034 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6035 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6036 | reason = "illegal code point (> 0x10FFFF)"; |
| 6037 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6038 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6039 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6040 | s += Py_UNICODE_SIZE; |
| 6041 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6042 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6043 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6044 | Py_UNICODE uch2; |
| 6045 | ((char *) &uch2)[0] = s[0]; |
| 6046 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6047 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6048 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6049 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6050 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6051 | } |
| 6052 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6053 | #endif |
| 6054 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6055 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6056 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6057 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 6058 | writer.pos++; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6059 | continue; |
| 6060 | |
| 6061 | error: |
| 6062 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6063 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6064 | errors, &errorHandler, |
| 6065 | "unicode_internal", reason, |
| 6066 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6067 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6068 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6069 | } |
| 6070 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6071 | Py_XDECREF(errorHandler); |
| 6072 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6073 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6075 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6076 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6077 | Py_XDECREF(errorHandler); |
| 6078 | Py_XDECREF(exc); |
| 6079 | return NULL; |
| 6080 | } |
| 6081 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6083 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6084 | PyObject * |
| 6085 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6086 | Py_ssize_t size, |
| 6087 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6090 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | } |
| 6092 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6093 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6094 | static void |
| 6095 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6096 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6097 | PyObject *unicode, |
| 6098 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6099 | const char *reason) |
| 6100 | { |
| 6101 | if (*exceptionObject == NULL) { |
| 6102 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6103 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6104 | encoding, unicode, startpos, endpos, reason); |
| 6105 | } |
| 6106 | else { |
| 6107 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6108 | goto onError; |
| 6109 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6110 | goto onError; |
| 6111 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6112 | goto onError; |
| 6113 | return; |
| 6114 | onError: |
| 6115 | Py_DECREF(*exceptionObject); |
| 6116 | *exceptionObject = NULL; |
| 6117 | } |
| 6118 | } |
| 6119 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6120 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6121 | static void |
| 6122 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6123 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6124 | PyObject *unicode, |
| 6125 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6126 | const char *reason) |
| 6127 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6128 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6129 | encoding, unicode, startpos, endpos, reason); |
| 6130 | if (*exceptionObject != NULL) |
| 6131 | PyCodec_StrictErrors(*exceptionObject); |
| 6132 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6133 | |
| 6134 | /* error handling callback helper: |
| 6135 | build arguments, call the callback and check the arguments, |
| 6136 | put the result into newpos and return the replacement string, which |
| 6137 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6138 | static PyObject * |
| 6139 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6140 | PyObject **errorHandler, |
| 6141 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6142 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6143 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6144 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6145 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6146 | 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] | 6147 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6148 | PyObject *restuple; |
| 6149 | PyObject *resunicode; |
| 6150 | |
| 6151 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6152 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6153 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6154 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6155 | } |
| 6156 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6157 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6158 | return NULL; |
| 6159 | len = PyUnicode_GET_LENGTH(unicode); |
| 6160 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6161 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6162 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6163 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6164 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6165 | |
| 6166 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6168 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6169 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6170 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6171 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | Py_DECREF(restuple); |
| 6173 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6174 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6175 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6176 | &resunicode, newpos)) { |
| 6177 | Py_DECREF(restuple); |
| 6178 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6179 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6180 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6181 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6182 | Py_DECREF(restuple); |
| 6183 | return NULL; |
| 6184 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6185 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6186 | *newpos = len + *newpos; |
| 6187 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6188 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6189 | Py_DECREF(restuple); |
| 6190 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6191 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6192 | Py_INCREF(resunicode); |
| 6193 | Py_DECREF(restuple); |
| 6194 | return resunicode; |
| 6195 | } |
| 6196 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6197 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6198 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6199 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6200 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6201 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6202 | /* input state */ |
| 6203 | Py_ssize_t pos=0, size; |
| 6204 | int kind; |
| 6205 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6206 | /* output object */ |
| 6207 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6208 | /* pointer into the output */ |
| 6209 | char *str; |
| 6210 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6211 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6212 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6213 | 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] | 6214 | PyObject *errorHandler = NULL; |
| 6215 | PyObject *exc = NULL; |
| 6216 | /* the following variable is used for caching string comparisons |
| 6217 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6218 | int known_errorHandler = -1; |
| 6219 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6220 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6221 | return NULL; |
| 6222 | size = PyUnicode_GET_LENGTH(unicode); |
| 6223 | kind = PyUnicode_KIND(unicode); |
| 6224 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6225 | /* allocate enough for a simple encoding without |
| 6226 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6227 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6228 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6229 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6230 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6231 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6232 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6233 | ressize = size; |
| 6234 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6235 | while (pos < size) { |
| 6236 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6237 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6238 | /* can we encode this? */ |
| 6239 | if (c<limit) { |
| 6240 | /* no overflow check, because we know that the space is enough */ |
| 6241 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6242 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6243 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6244 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6245 | Py_ssize_t requiredsize; |
| 6246 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6247 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6249 | Py_ssize_t collstart = pos; |
| 6250 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6251 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6252 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6253 | ++collend; |
| 6254 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6255 | if (known_errorHandler==-1) { |
| 6256 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6257 | known_errorHandler = 1; |
| 6258 | else if (!strcmp(errors, "replace")) |
| 6259 | known_errorHandler = 2; |
| 6260 | else if (!strcmp(errors, "ignore")) |
| 6261 | known_errorHandler = 3; |
| 6262 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6263 | known_errorHandler = 4; |
| 6264 | else |
| 6265 | known_errorHandler = 0; |
| 6266 | } |
| 6267 | switch (known_errorHandler) { |
| 6268 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6269 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6270 | goto onError; |
| 6271 | case 2: /* replace */ |
| 6272 | while (collstart++<collend) |
| 6273 | *str++ = '?'; /* fall through */ |
| 6274 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6275 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6276 | break; |
| 6277 | case 4: /* xmlcharrefreplace */ |
| 6278 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6279 | /* determine replacement size */ |
| 6280 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6281 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6282 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6284 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6286 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6287 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6288 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6289 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6290 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6292 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6293 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6294 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6295 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6297 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6298 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6299 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | if (requiredsize > ressize) { |
| 6301 | if (requiredsize<2*ressize) |
| 6302 | requiredsize = 2*ressize; |
| 6303 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6304 | goto onError; |
| 6305 | str = PyBytes_AS_STRING(res) + respos; |
| 6306 | ressize = requiredsize; |
| 6307 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6308 | /* generate replacement */ |
| 6309 | for (i = collstart; i < collend; ++i) { |
| 6310 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6311 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6312 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6313 | break; |
| 6314 | default: |
| 6315 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6316 | encoding, reason, unicode, &exc, |
| 6317 | collstart, collend, &newpos); |
| 6318 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6319 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6320 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6321 | if (PyBytes_Check(repunicode)) { |
| 6322 | /* Directly copy bytes result to output. */ |
| 6323 | repsize = PyBytes_Size(repunicode); |
| 6324 | if (repsize > 1) { |
| 6325 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6326 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6327 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6328 | Py_DECREF(repunicode); |
| 6329 | goto onError; |
| 6330 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6331 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6332 | ressize += repsize-1; |
| 6333 | } |
| 6334 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6335 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6336 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6337 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6338 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6339 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6340 | /* need more space? (at least enough for what we |
| 6341 | have+the replacement+the rest of the string, so |
| 6342 | we won't have to check space for encodable characters) */ |
| 6343 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6344 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6345 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6346 | if (requiredsize > ressize) { |
| 6347 | if (requiredsize<2*ressize) |
| 6348 | requiredsize = 2*ressize; |
| 6349 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6350 | Py_DECREF(repunicode); |
| 6351 | goto onError; |
| 6352 | } |
| 6353 | str = PyBytes_AS_STRING(res) + respos; |
| 6354 | ressize = requiredsize; |
| 6355 | } |
| 6356 | /* check if there is anything unencodable in the replacement |
| 6357 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6358 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6359 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6361 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6362 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6363 | Py_DECREF(repunicode); |
| 6364 | goto onError; |
| 6365 | } |
| 6366 | *str = (char)c; |
| 6367 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6368 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6369 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6370 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6371 | } |
| 6372 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6373 | /* Resize if we allocated to much */ |
| 6374 | size = str - PyBytes_AS_STRING(res); |
| 6375 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6376 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6377 | if (_PyBytes_Resize(&res, size) < 0) |
| 6378 | goto onError; |
| 6379 | } |
| 6380 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6381 | Py_XDECREF(errorHandler); |
| 6382 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6383 | return res; |
| 6384 | |
| 6385 | onError: |
| 6386 | Py_XDECREF(res); |
| 6387 | Py_XDECREF(errorHandler); |
| 6388 | Py_XDECREF(exc); |
| 6389 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6392 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6393 | PyObject * |
| 6394 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6395 | Py_ssize_t size, |
| 6396 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6398 | PyObject *result; |
| 6399 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6400 | if (unicode == NULL) |
| 6401 | return NULL; |
| 6402 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6403 | Py_DECREF(unicode); |
| 6404 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6407 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6408 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6409 | { |
| 6410 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | PyErr_BadArgument(); |
| 6412 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6414 | if (PyUnicode_READY(unicode) == -1) |
| 6415 | return NULL; |
| 6416 | /* Fast path: if it is a one-byte string, construct |
| 6417 | bytes object directly. */ |
| 6418 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6419 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6420 | PyUnicode_GET_LENGTH(unicode)); |
| 6421 | /* Non-Latin-1 characters present. Defer to above function to |
| 6422 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6423 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6424 | } |
| 6425 | |
| 6426 | PyObject* |
| 6427 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6428 | { |
| 6429 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6433 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6434 | PyObject * |
| 6435 | PyUnicode_DecodeASCII(const char *s, |
| 6436 | Py_ssize_t size, |
| 6437 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6438 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6439 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6440 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6441 | int kind; |
| 6442 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6443 | Py_ssize_t startinpos; |
| 6444 | Py_ssize_t endinpos; |
| 6445 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6446 | const char *e; |
| 6447 | PyObject *errorHandler = NULL; |
| 6448 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6450 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6451 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6453 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6454 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6455 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6456 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6457 | _PyUnicodeWriter_Init(&writer, 0); |
| 6458 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6460 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6461 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6462 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6463 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6464 | writer.pos = outpos; |
| 6465 | if (writer.pos == size) |
| 6466 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6467 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6468 | s += writer.pos; |
| 6469 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6470 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6471 | register unsigned char c = (unsigned char)*s; |
| 6472 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6473 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6474 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6475 | ++s; |
| 6476 | } |
| 6477 | else { |
| 6478 | startinpos = s-starts; |
| 6479 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6480 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | errors, &errorHandler, |
| 6482 | "ascii", "ordinal not in range(128)", |
| 6483 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6484 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6485 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6486 | kind = writer.kind; |
| 6487 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6490 | Py_XDECREF(errorHandler); |
| 6491 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6492 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6494 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6495 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6496 | Py_XDECREF(errorHandler); |
| 6497 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | return NULL; |
| 6499 | } |
| 6500 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6501 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6502 | PyObject * |
| 6503 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6504 | Py_ssize_t size, |
| 6505 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6507 | PyObject *result; |
| 6508 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6509 | if (unicode == NULL) |
| 6510 | return NULL; |
| 6511 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6512 | Py_DECREF(unicode); |
| 6513 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6514 | } |
| 6515 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6516 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6517 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6518 | { |
| 6519 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6520 | PyErr_BadArgument(); |
| 6521 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6522 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6523 | if (PyUnicode_READY(unicode) == -1) |
| 6524 | return NULL; |
| 6525 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6526 | directly. Else defer to above function to raise the exception. */ |
| 6527 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6528 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6529 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6530 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | PyObject * |
| 6534 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6535 | { |
| 6536 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 | } |
| 6538 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6539 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6540 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6541 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6542 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6543 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6544 | #define NEED_RETRY |
| 6545 | #endif |
| 6546 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6547 | #ifndef WC_ERR_INVALID_CHARS |
| 6548 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6549 | #endif |
| 6550 | |
| 6551 | static char* |
| 6552 | code_page_name(UINT code_page, PyObject **obj) |
| 6553 | { |
| 6554 | *obj = NULL; |
| 6555 | if (code_page == CP_ACP) |
| 6556 | return "mbcs"; |
| 6557 | if (code_page == CP_UTF7) |
| 6558 | return "CP_UTF7"; |
| 6559 | if (code_page == CP_UTF8) |
| 6560 | return "CP_UTF8"; |
| 6561 | |
| 6562 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6563 | if (*obj == NULL) |
| 6564 | return NULL; |
| 6565 | return PyBytes_AS_STRING(*obj); |
| 6566 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6567 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6568 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6569 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6570 | { |
| 6571 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6572 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6573 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6574 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6575 | return 0; |
| 6576 | |
| 6577 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6578 | if (prev == curr) |
| 6579 | return 1; |
| 6580 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6581 | as it assumes an incomplete character consists of a single |
| 6582 | byte. */ |
| 6583 | if (curr - prev == 2) |
| 6584 | return 1; |
| 6585 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6586 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6587 | return 0; |
| 6588 | } |
| 6589 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6590 | static DWORD |
| 6591 | decode_code_page_flags(UINT code_page) |
| 6592 | { |
| 6593 | if (code_page == CP_UTF7) { |
| 6594 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6595 | return 0; |
| 6596 | } |
| 6597 | else |
| 6598 | return MB_ERR_INVALID_CHARS; |
| 6599 | } |
| 6600 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6601 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6602 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6603 | * mode. |
| 6604 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6605 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6606 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6607 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6608 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6609 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6610 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6611 | const char *in, |
| 6612 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6613 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6614 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6615 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6616 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6617 | |
| 6618 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6619 | assert(insize > 0); |
| 6620 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6621 | if (outsize <= 0) |
| 6622 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6623 | |
| 6624 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6625 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6626 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6627 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6628 | if (*v == NULL) |
| 6629 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6630 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6631 | } |
| 6632 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6634 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6635 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6636 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6637 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6638 | } |
| 6639 | |
| 6640 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6641 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6642 | if (outsize <= 0) |
| 6643 | goto error; |
| 6644 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6645 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6646 | error: |
| 6647 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6648 | return -2; |
| 6649 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6650 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6653 | /* |
| 6654 | * Decode a byte string from a code page into unicode object with an error |
| 6655 | * handler. |
| 6656 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6657 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6658 | * UnicodeDecodeError exception and returns -1 on error. |
| 6659 | */ |
| 6660 | static int |
| 6661 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6662 | PyObject **v, |
| 6663 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6664 | const char *errors) |
| 6665 | { |
| 6666 | const char *startin = in; |
| 6667 | const char *endin = in + size; |
| 6668 | const DWORD flags = decode_code_page_flags(code_page); |
| 6669 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6670 | 2000 English version of the message. */ |
| 6671 | const char *reason = "No mapping for the Unicode character exists " |
| 6672 | "in the target code page."; |
| 6673 | /* each step cannot decode more than 1 character, but a character can be |
| 6674 | represented as a surrogate pair */ |
| 6675 | wchar_t buffer[2], *startout, *out; |
| 6676 | int insize, outsize; |
| 6677 | PyObject *errorHandler = NULL; |
| 6678 | PyObject *exc = NULL; |
| 6679 | PyObject *encoding_obj = NULL; |
| 6680 | char *encoding; |
| 6681 | DWORD err; |
| 6682 | int ret = -1; |
| 6683 | |
| 6684 | assert(size > 0); |
| 6685 | |
| 6686 | encoding = code_page_name(code_page, &encoding_obj); |
| 6687 | if (encoding == NULL) |
| 6688 | return -1; |
| 6689 | |
| 6690 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6691 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6692 | UnicodeDecodeError. */ |
| 6693 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6694 | if (exc != NULL) { |
| 6695 | PyCodec_StrictErrors(exc); |
| 6696 | Py_CLEAR(exc); |
| 6697 | } |
| 6698 | goto error; |
| 6699 | } |
| 6700 | |
| 6701 | if (*v == NULL) { |
| 6702 | /* Create unicode object */ |
| 6703 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6704 | PyErr_NoMemory(); |
| 6705 | goto error; |
| 6706 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6707 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6708 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6709 | if (*v == NULL) |
| 6710 | goto error; |
| 6711 | startout = PyUnicode_AS_UNICODE(*v); |
| 6712 | } |
| 6713 | else { |
| 6714 | /* Extend unicode object */ |
| 6715 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6716 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6717 | PyErr_NoMemory(); |
| 6718 | goto error; |
| 6719 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6720 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6721 | goto error; |
| 6722 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6723 | } |
| 6724 | |
| 6725 | /* Decode the byte string character per character */ |
| 6726 | out = startout; |
| 6727 | while (in < endin) |
| 6728 | { |
| 6729 | /* Decode a character */ |
| 6730 | insize = 1; |
| 6731 | do |
| 6732 | { |
| 6733 | outsize = MultiByteToWideChar(code_page, flags, |
| 6734 | in, insize, |
| 6735 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6736 | if (outsize > 0) |
| 6737 | break; |
| 6738 | err = GetLastError(); |
| 6739 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6740 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6741 | { |
| 6742 | PyErr_SetFromWindowsErr(0); |
| 6743 | goto error; |
| 6744 | } |
| 6745 | insize++; |
| 6746 | } |
| 6747 | /* 4=maximum length of a UTF-8 sequence */ |
| 6748 | while (insize <= 4 && (in + insize) <= endin); |
| 6749 | |
| 6750 | if (outsize <= 0) { |
| 6751 | Py_ssize_t startinpos, endinpos, outpos; |
| 6752 | |
| 6753 | startinpos = in - startin; |
| 6754 | endinpos = startinpos + 1; |
| 6755 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6756 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6757 | errors, &errorHandler, |
| 6758 | encoding, reason, |
| 6759 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6760 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6761 | { |
| 6762 | goto error; |
| 6763 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6764 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6765 | } |
| 6766 | else { |
| 6767 | in += insize; |
| 6768 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6769 | out += outsize; |
| 6770 | } |
| 6771 | } |
| 6772 | |
| 6773 | /* write a NUL character at the end */ |
| 6774 | *out = 0; |
| 6775 | |
| 6776 | /* Extend unicode object */ |
| 6777 | outsize = out - startout; |
| 6778 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6779 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6780 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6781 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6782 | |
| 6783 | error: |
| 6784 | Py_XDECREF(encoding_obj); |
| 6785 | Py_XDECREF(errorHandler); |
| 6786 | Py_XDECREF(exc); |
| 6787 | return ret; |
| 6788 | } |
| 6789 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6790 | static PyObject * |
| 6791 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6792 | const char *s, Py_ssize_t size, |
| 6793 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6794 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6795 | PyObject *v = NULL; |
| 6796 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6797 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6798 | if (code_page < 0) { |
| 6799 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6800 | return NULL; |
| 6801 | } |
| 6802 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6803 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6804 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6805 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6806 | do |
| 6807 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6808 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6809 | if (size > INT_MAX) { |
| 6810 | chunk_size = INT_MAX; |
| 6811 | final = 0; |
| 6812 | done = 0; |
| 6813 | } |
| 6814 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6815 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6816 | { |
| 6817 | chunk_size = (int)size; |
| 6818 | final = (consumed == NULL); |
| 6819 | done = 1; |
| 6820 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6821 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6822 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6823 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6824 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6825 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6826 | if (chunk_size == 0 && done) { |
| 6827 | if (v != NULL) |
| 6828 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6829 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6830 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6831 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6832 | |
| 6833 | converted = decode_code_page_strict(code_page, &v, |
| 6834 | s, chunk_size); |
| 6835 | if (converted == -2) |
| 6836 | converted = decode_code_page_errors(code_page, &v, |
| 6837 | s, chunk_size, |
| 6838 | errors); |
| 6839 | assert(converted != 0); |
| 6840 | |
| 6841 | if (converted < 0) { |
| 6842 | Py_XDECREF(v); |
| 6843 | return NULL; |
| 6844 | } |
| 6845 | |
| 6846 | if (consumed) |
| 6847 | *consumed += converted; |
| 6848 | |
| 6849 | s += converted; |
| 6850 | size -= converted; |
| 6851 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6852 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6853 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6856 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6857 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6858 | const char *s, |
| 6859 | Py_ssize_t size, |
| 6860 | const char *errors, |
| 6861 | Py_ssize_t *consumed) |
| 6862 | { |
| 6863 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6864 | } |
| 6865 | |
| 6866 | PyObject * |
| 6867 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6868 | Py_ssize_t size, |
| 6869 | const char *errors, |
| 6870 | Py_ssize_t *consumed) |
| 6871 | { |
| 6872 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6873 | } |
| 6874 | |
| 6875 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6876 | PyUnicode_DecodeMBCS(const char *s, |
| 6877 | Py_ssize_t size, |
| 6878 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6879 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6880 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6881 | } |
| 6882 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6883 | static DWORD |
| 6884 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6885 | { |
| 6886 | if (code_page == CP_UTF8) { |
| 6887 | if (winver.dwMajorVersion >= 6) |
| 6888 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6889 | and later */ |
| 6890 | return WC_ERR_INVALID_CHARS; |
| 6891 | else |
| 6892 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 6893 | return 0; |
| 6894 | } |
| 6895 | else if (code_page == CP_UTF7) { |
| 6896 | /* CP_UTF7 only supports flags=0 */ |
| 6897 | return 0; |
| 6898 | } |
| 6899 | else { |
| 6900 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 6901 | return 0; |
| 6902 | else |
| 6903 | return WC_NO_BEST_FIT_CHARS; |
| 6904 | } |
| 6905 | } |
| 6906 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6907 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6908 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 6909 | * mode. |
| 6910 | * |
| 6911 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6912 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6913 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6914 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6915 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6916 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6917 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6918 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6919 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6920 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 6921 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6922 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6923 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6924 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6925 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 6926 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6927 | /* Create a substring so that we can get the UTF-16 representation |
| 6928 | of just the slice under consideration. */ |
| 6929 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6930 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6931 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6932 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6933 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6934 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6935 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6936 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6937 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6938 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 6939 | if (substring == NULL) |
| 6940 | return -1; |
| 6941 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 6942 | if (p == NULL) { |
| 6943 | Py_DECREF(substring); |
| 6944 | return -1; |
| 6945 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6946 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6947 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6948 | outsize = WideCharToMultiByte(code_page, flags, |
| 6949 | p, size, |
| 6950 | NULL, 0, |
| 6951 | NULL, pusedDefaultChar); |
| 6952 | if (outsize <= 0) |
| 6953 | goto error; |
| 6954 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6955 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 6956 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6957 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6958 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6959 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6960 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6961 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6962 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6963 | if (*outbytes == NULL) { |
| 6964 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6965 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6966 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6967 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6968 | } |
| 6969 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6970 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6971 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 6972 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 6973 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6974 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6975 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6976 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6977 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 6978 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6979 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6980 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6981 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6985 | outsize = WideCharToMultiByte(code_page, flags, |
| 6986 | p, size, |
| 6987 | out, outsize, |
| 6988 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6989 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6990 | if (outsize <= 0) |
| 6991 | goto error; |
| 6992 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6993 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6994 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6995 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6996 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6997 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6998 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6999 | return -2; |
| 7000 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7001 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7002 | } |
| 7003 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7004 | /* |
| 7005 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7006 | * error handler. |
| 7007 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7008 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7009 | * -1 on other error. |
| 7010 | */ |
| 7011 | static int |
| 7012 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7013 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7014 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7015 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7016 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7017 | Py_ssize_t pos = unicode_offset; |
| 7018 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7019 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7020 | 2000 English version of the message. */ |
| 7021 | const char *reason = "invalid character"; |
| 7022 | /* 4=maximum length of a UTF-8 sequence */ |
| 7023 | char buffer[4]; |
| 7024 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7025 | Py_ssize_t outsize; |
| 7026 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7027 | PyObject *errorHandler = NULL; |
| 7028 | PyObject *exc = NULL; |
| 7029 | PyObject *encoding_obj = NULL; |
| 7030 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7031 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7032 | PyObject *rep; |
| 7033 | int ret = -1; |
| 7034 | |
| 7035 | assert(insize > 0); |
| 7036 | |
| 7037 | encoding = code_page_name(code_page, &encoding_obj); |
| 7038 | if (encoding == NULL) |
| 7039 | return -1; |
| 7040 | |
| 7041 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7042 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7043 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7044 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7045 | if (exc != NULL) { |
| 7046 | PyCodec_StrictErrors(exc); |
| 7047 | Py_DECREF(exc); |
| 7048 | } |
| 7049 | Py_XDECREF(encoding_obj); |
| 7050 | return -1; |
| 7051 | } |
| 7052 | |
| 7053 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7054 | pusedDefaultChar = &usedDefaultChar; |
| 7055 | else |
| 7056 | pusedDefaultChar = NULL; |
| 7057 | |
| 7058 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7059 | PyErr_NoMemory(); |
| 7060 | goto error; |
| 7061 | } |
| 7062 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7063 | |
| 7064 | if (*outbytes == NULL) { |
| 7065 | /* Create string object */ |
| 7066 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7067 | if (*outbytes == NULL) |
| 7068 | goto error; |
| 7069 | out = PyBytes_AS_STRING(*outbytes); |
| 7070 | } |
| 7071 | else { |
| 7072 | /* Extend string object */ |
| 7073 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7074 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7075 | PyErr_NoMemory(); |
| 7076 | goto error; |
| 7077 | } |
| 7078 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7079 | goto error; |
| 7080 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7081 | } |
| 7082 | |
| 7083 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7084 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7085 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7086 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7087 | wchar_t chars[2]; |
| 7088 | int charsize; |
| 7089 | if (ch < 0x10000) { |
| 7090 | chars[0] = (wchar_t)ch; |
| 7091 | charsize = 1; |
| 7092 | } |
| 7093 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7094 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7095 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7096 | charsize = 2; |
| 7097 | } |
| 7098 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7099 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7100 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7101 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7102 | NULL, pusedDefaultChar); |
| 7103 | if (outsize > 0) { |
| 7104 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7105 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7106 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7107 | memcpy(out, buffer, outsize); |
| 7108 | out += outsize; |
| 7109 | continue; |
| 7110 | } |
| 7111 | } |
| 7112 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7113 | PyErr_SetFromWindowsErr(0); |
| 7114 | goto error; |
| 7115 | } |
| 7116 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7117 | rep = unicode_encode_call_errorhandler( |
| 7118 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7119 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7120 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7121 | if (rep == NULL) |
| 7122 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7123 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7124 | |
| 7125 | if (PyBytes_Check(rep)) { |
| 7126 | outsize = PyBytes_GET_SIZE(rep); |
| 7127 | if (outsize != 1) { |
| 7128 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7129 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7130 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7131 | Py_DECREF(rep); |
| 7132 | goto error; |
| 7133 | } |
| 7134 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7135 | } |
| 7136 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7137 | out += outsize; |
| 7138 | } |
| 7139 | else { |
| 7140 | Py_ssize_t i; |
| 7141 | enum PyUnicode_Kind kind; |
| 7142 | void *data; |
| 7143 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7144 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7145 | Py_DECREF(rep); |
| 7146 | goto error; |
| 7147 | } |
| 7148 | |
| 7149 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7150 | if (outsize != 1) { |
| 7151 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7152 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7153 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7154 | Py_DECREF(rep); |
| 7155 | goto error; |
| 7156 | } |
| 7157 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7158 | } |
| 7159 | kind = PyUnicode_KIND(rep); |
| 7160 | data = PyUnicode_DATA(rep); |
| 7161 | for (i=0; i < outsize; i++) { |
| 7162 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7163 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7164 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7165 | encoding, unicode, |
| 7166 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7167 | "unable to encode error handler result to ASCII"); |
| 7168 | Py_DECREF(rep); |
| 7169 | goto error; |
| 7170 | } |
| 7171 | *out = (unsigned char)ch; |
| 7172 | out++; |
| 7173 | } |
| 7174 | } |
| 7175 | Py_DECREF(rep); |
| 7176 | } |
| 7177 | /* write a NUL byte */ |
| 7178 | *out = 0; |
| 7179 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7180 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7181 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7182 | goto error; |
| 7183 | ret = 0; |
| 7184 | |
| 7185 | error: |
| 7186 | Py_XDECREF(encoding_obj); |
| 7187 | Py_XDECREF(errorHandler); |
| 7188 | Py_XDECREF(exc); |
| 7189 | return ret; |
| 7190 | } |
| 7191 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7192 | static PyObject * |
| 7193 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7194 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7195 | const char *errors) |
| 7196 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7197 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7198 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7199 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7200 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7201 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7202 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7203 | return NULL; |
| 7204 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7205 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7206 | if (code_page < 0) { |
| 7207 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7208 | return NULL; |
| 7209 | } |
| 7210 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7211 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7212 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7213 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7214 | offset = 0; |
| 7215 | do |
| 7216 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7217 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7218 | /* 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] | 7219 | chunks. */ |
| 7220 | if (len > INT_MAX/2) { |
| 7221 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7222 | done = 0; |
| 7223 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7224 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7225 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7226 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7227 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7228 | done = 1; |
| 7229 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7230 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7231 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7232 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7233 | errors); |
| 7234 | if (ret == -2) |
| 7235 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7236 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7237 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7238 | if (ret < 0) { |
| 7239 | Py_XDECREF(outbytes); |
| 7240 | return NULL; |
| 7241 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7242 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7243 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7244 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7245 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7246 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7247 | return outbytes; |
| 7248 | } |
| 7249 | |
| 7250 | PyObject * |
| 7251 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7252 | Py_ssize_t size, |
| 7253 | const char *errors) |
| 7254 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7255 | PyObject *unicode, *res; |
| 7256 | unicode = PyUnicode_FromUnicode(p, size); |
| 7257 | if (unicode == NULL) |
| 7258 | return NULL; |
| 7259 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7260 | Py_DECREF(unicode); |
| 7261 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7262 | } |
| 7263 | |
| 7264 | PyObject * |
| 7265 | PyUnicode_EncodeCodePage(int code_page, |
| 7266 | PyObject *unicode, |
| 7267 | const char *errors) |
| 7268 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7269 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7270 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7271 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7272 | PyObject * |
| 7273 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7274 | { |
| 7275 | if (!PyUnicode_Check(unicode)) { |
| 7276 | PyErr_BadArgument(); |
| 7277 | return NULL; |
| 7278 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7279 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7282 | #undef NEED_RETRY |
| 7283 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7284 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7286 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7287 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7288 | PyObject * |
| 7289 | PyUnicode_DecodeCharmap(const char *s, |
| 7290 | Py_ssize_t size, |
| 7291 | PyObject *mapping, |
| 7292 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7293 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7294 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7295 | Py_ssize_t startinpos; |
| 7296 | Py_ssize_t endinpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7297 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7298 | _PyUnicodeWriter writer; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7299 | PyObject *errorHandler = NULL; |
| 7300 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | /* Default to Latin-1 */ |
| 7303 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7304 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7305 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7306 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7307 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7308 | _PyUnicodeWriter_Init(&writer, 0); |
| 7309 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7310 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7311 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7312 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7313 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7314 | Py_ssize_t maplen; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7315 | enum PyUnicode_Kind mapkind; |
| 7316 | void *mapdata; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7317 | Py_UCS4 x; |
Victor Stinner | 03c3e35 | 2013-04-09 21:53:09 +0200 | [diff] [blame] | 7318 | unsigned char ch; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7319 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7320 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7321 | return NULL; |
| 7322 | |
| 7323 | maplen = PyUnicode_GET_LENGTH(mapping); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7324 | mapdata = PyUnicode_DATA(mapping); |
| 7325 | mapkind = PyUnicode_KIND(mapping); |
Victor Stinner | 03c3e35 | 2013-04-09 21:53:09 +0200 | [diff] [blame] | 7326 | |
| 7327 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7328 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7329 | * is disabled in encoding aliases, latin1 is preferred because |
| 7330 | * its implementation is faster. */ |
| 7331 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7332 | Py_UCS1 *outdata = (Py_UCS1 *)writer.data; |
| 7333 | Py_UCS4 maxchar = writer.maxchar; |
| 7334 | |
| 7335 | assert (writer.kind == PyUnicode_1BYTE_KIND); |
| 7336 | while (s < e) { |
| 7337 | ch = *s; |
| 7338 | x = mapdata_ucs1[ch]; |
| 7339 | if (x > maxchar) { |
| 7340 | if (_PyUnicodeWriter_PrepareInternal(&writer, 1, 0xff) == -1) |
| 7341 | goto onError; |
| 7342 | maxchar = writer.maxchar; |
| 7343 | outdata = (Py_UCS1 *)writer.data; |
| 7344 | } |
| 7345 | outdata[writer.pos] = x; |
| 7346 | writer.pos++; |
| 7347 | ++s; |
| 7348 | } |
| 7349 | } |
| 7350 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7351 | while (s < e) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7352 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7353 | enum PyUnicode_Kind outkind = writer.kind; |
| 7354 | void *outdata = writer.data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7355 | if (outkind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7356 | Py_UCS4 maxchar = writer.maxchar; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7357 | while (s < e) { |
| 7358 | unsigned char ch = *s; |
| 7359 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7360 | if (x > maxchar) |
| 7361 | goto Error; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7362 | PyUnicode_WRITE(PyUnicode_1BYTE_KIND, outdata, writer.pos, x); |
| 7363 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7364 | ++s; |
| 7365 | } |
| 7366 | break; |
| 7367 | } |
| 7368 | else if (outkind == PyUnicode_2BYTE_KIND) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7369 | while (s < e) { |
| 7370 | unsigned char ch = *s; |
| 7371 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7372 | if (x == 0xFFFE) |
| 7373 | goto Error; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7374 | PyUnicode_WRITE(PyUnicode_2BYTE_KIND, outdata, writer.pos, x); |
| 7375 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7376 | ++s; |
| 7377 | } |
| 7378 | break; |
| 7379 | } |
| 7380 | } |
| 7381 | ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7382 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7383 | if (ch < maplen) |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7384 | x = PyUnicode_READ(mapkind, mapdata, ch); |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7385 | else |
| 7386 | x = 0xfffe; /* invalid value */ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7387 | Error: |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7388 | if (x == 0xfffe) |
| 7389 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7390 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7391 | startinpos = s-starts; |
| 7392 | endinpos = startinpos+1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7393 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7394 | errors, &errorHandler, |
| 7395 | "charmap", "character maps to <undefined>", |
| 7396 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7397 | &writer)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7398 | goto onError; |
| 7399 | } |
| 7400 | continue; |
| 7401 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7402 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7403 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7404 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7405 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 7406 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7407 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7408 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7409 | } |
| 7410 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7411 | while (s < e) { |
| 7412 | unsigned char ch = *s; |
| 7413 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7414 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7415 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7416 | w = PyLong_FromLong((long)ch); |
| 7417 | if (w == NULL) |
| 7418 | goto onError; |
| 7419 | x = PyObject_GetItem(mapping, w); |
| 7420 | Py_DECREF(w); |
| 7421 | if (x == NULL) { |
| 7422 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7423 | /* No mapping found means: mapping is undefined. */ |
| 7424 | PyErr_Clear(); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7425 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7426 | } else |
| 7427 | goto onError; |
| 7428 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7429 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7430 | /* Apply mapping */ |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7431 | if (x == Py_None) |
| 7432 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7433 | if (PyLong_Check(x)) { |
| 7434 | long value = PyLong_AS_LONG(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7435 | if (value == 0xFFFE) |
| 7436 | goto Undefined; |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 7437 | if (value < 0 || value > MAX_UNICODE) { |
| 7438 | PyErr_Format(PyExc_TypeError, |
| 7439 | "character mapping must be in range(0x%lx)", |
| 7440 | (unsigned long)MAX_UNICODE + 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7441 | Py_DECREF(x); |
| 7442 | goto onError; |
| 7443 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7444 | |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7445 | if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7446 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7447 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7448 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7449 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value); |
| 7450 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7451 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7452 | else if (PyUnicode_Check(x)) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7453 | if (PyUnicode_READY(x) == -1) { |
| 7454 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7455 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7456 | } |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7457 | if (PyUnicode_GET_LENGTH(x) == 1) { |
Serhiy Storchaka | 45d16d9 | 2013-01-15 15:01:20 +0200 | [diff] [blame] | 7458 | Py_UCS4 value = PyUnicode_READ_CHAR(x, 0); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7459 | if (value == 0xFFFE) |
| 7460 | goto Undefined; |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7461 | if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) { |
| 7462 | Py_DECREF(x); |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7463 | goto onError; |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7464 | } |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7465 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value); |
| 7466 | writer.pos++; |
| 7467 | } |
| 7468 | else { |
| 7469 | writer.overallocate = 1; |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7470 | if (_PyUnicodeWriter_WriteStr(&writer, x) == -1) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7471 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7472 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7473 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7474 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7475 | } |
| 7476 | else { |
| 7477 | /* wrong return value */ |
| 7478 | PyErr_SetString(PyExc_TypeError, |
| 7479 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7480 | Py_DECREF(x); |
| 7481 | goto onError; |
| 7482 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7483 | Py_DECREF(x); |
| 7484 | ++s; |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7485 | continue; |
| 7486 | Undefined: |
| 7487 | /* undefined mapping */ |
| 7488 | Py_XDECREF(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7489 | startinpos = s-starts; |
| 7490 | endinpos = startinpos+1; |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7491 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7492 | errors, &errorHandler, |
| 7493 | "charmap", "character maps to <undefined>", |
| 7494 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7495 | &writer)) { |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7496 | goto onError; |
| 7497 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7498 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7499 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7500 | Py_XDECREF(errorHandler); |
| 7501 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7502 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7503 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7504 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7505 | Py_XDECREF(errorHandler); |
| 7506 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7507 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7508 | return NULL; |
| 7509 | } |
| 7510 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7511 | /* Charmap encoding: the lookup table */ |
| 7512 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7513 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7514 | PyObject_HEAD |
| 7515 | unsigned char level1[32]; |
| 7516 | int count2, count3; |
| 7517 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7518 | }; |
| 7519 | |
| 7520 | static PyObject* |
| 7521 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7522 | { |
| 7523 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7524 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7525 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7526 | } |
| 7527 | |
| 7528 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7529 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7530 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7531 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7532 | }; |
| 7533 | |
| 7534 | static void |
| 7535 | encoding_map_dealloc(PyObject* o) |
| 7536 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7537 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7538 | } |
| 7539 | |
| 7540 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7541 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7542 | "EncodingMap", /*tp_name*/ |
| 7543 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7544 | 0, /*tp_itemsize*/ |
| 7545 | /* methods */ |
| 7546 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7547 | 0, /*tp_print*/ |
| 7548 | 0, /*tp_getattr*/ |
| 7549 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7550 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7551 | 0, /*tp_repr*/ |
| 7552 | 0, /*tp_as_number*/ |
| 7553 | 0, /*tp_as_sequence*/ |
| 7554 | 0, /*tp_as_mapping*/ |
| 7555 | 0, /*tp_hash*/ |
| 7556 | 0, /*tp_call*/ |
| 7557 | 0, /*tp_str*/ |
| 7558 | 0, /*tp_getattro*/ |
| 7559 | 0, /*tp_setattro*/ |
| 7560 | 0, /*tp_as_buffer*/ |
| 7561 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7562 | 0, /*tp_doc*/ |
| 7563 | 0, /*tp_traverse*/ |
| 7564 | 0, /*tp_clear*/ |
| 7565 | 0, /*tp_richcompare*/ |
| 7566 | 0, /*tp_weaklistoffset*/ |
| 7567 | 0, /*tp_iter*/ |
| 7568 | 0, /*tp_iternext*/ |
| 7569 | encoding_map_methods, /*tp_methods*/ |
| 7570 | 0, /*tp_members*/ |
| 7571 | 0, /*tp_getset*/ |
| 7572 | 0, /*tp_base*/ |
| 7573 | 0, /*tp_dict*/ |
| 7574 | 0, /*tp_descr_get*/ |
| 7575 | 0, /*tp_descr_set*/ |
| 7576 | 0, /*tp_dictoffset*/ |
| 7577 | 0, /*tp_init*/ |
| 7578 | 0, /*tp_alloc*/ |
| 7579 | 0, /*tp_new*/ |
| 7580 | 0, /*tp_free*/ |
| 7581 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7582 | }; |
| 7583 | |
| 7584 | PyObject* |
| 7585 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7586 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7587 | PyObject *result; |
| 7588 | struct encoding_map *mresult; |
| 7589 | int i; |
| 7590 | int need_dict = 0; |
| 7591 | unsigned char level1[32]; |
| 7592 | unsigned char level2[512]; |
| 7593 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7594 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7595 | int kind; |
| 7596 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7597 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7598 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7599 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7600 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7601 | PyErr_BadArgument(); |
| 7602 | return NULL; |
| 7603 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7604 | kind = PyUnicode_KIND(string); |
| 7605 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7606 | length = PyUnicode_GET_LENGTH(string); |
| 7607 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7608 | memset(level1, 0xFF, sizeof level1); |
| 7609 | memset(level2, 0xFF, sizeof level2); |
| 7610 | |
| 7611 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7612 | or if there are non-BMP characters, we need to use |
| 7613 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7614 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7615 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7616 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7617 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7618 | ch = PyUnicode_READ(kind, data, i); |
| 7619 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7620 | need_dict = 1; |
| 7621 | break; |
| 7622 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7623 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7624 | /* unmapped character */ |
| 7625 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7626 | l1 = ch >> 11; |
| 7627 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7628 | if (level1[l1] == 0xFF) |
| 7629 | level1[l1] = count2++; |
| 7630 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7631 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7632 | } |
| 7633 | |
| 7634 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7635 | need_dict = 1; |
| 7636 | |
| 7637 | if (need_dict) { |
| 7638 | PyObject *result = PyDict_New(); |
| 7639 | PyObject *key, *value; |
| 7640 | if (!result) |
| 7641 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7642 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7643 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7644 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7645 | if (!key || !value) |
| 7646 | goto failed1; |
| 7647 | if (PyDict_SetItem(result, key, value) == -1) |
| 7648 | goto failed1; |
| 7649 | Py_DECREF(key); |
| 7650 | Py_DECREF(value); |
| 7651 | } |
| 7652 | return result; |
| 7653 | failed1: |
| 7654 | Py_XDECREF(key); |
| 7655 | Py_XDECREF(value); |
| 7656 | Py_DECREF(result); |
| 7657 | return NULL; |
| 7658 | } |
| 7659 | |
| 7660 | /* Create a three-level trie */ |
| 7661 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7662 | 16*count2 + 128*count3 - 1); |
| 7663 | if (!result) |
| 7664 | return PyErr_NoMemory(); |
| 7665 | PyObject_Init(result, &EncodingMapType); |
| 7666 | mresult = (struct encoding_map*)result; |
| 7667 | mresult->count2 = count2; |
| 7668 | mresult->count3 = count3; |
| 7669 | mlevel1 = mresult->level1; |
| 7670 | mlevel2 = mresult->level23; |
| 7671 | mlevel3 = mresult->level23 + 16*count2; |
| 7672 | memcpy(mlevel1, level1, 32); |
| 7673 | memset(mlevel2, 0xFF, 16*count2); |
| 7674 | memset(mlevel3, 0, 128*count3); |
| 7675 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7676 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7677 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7678 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7679 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7680 | /* unmapped character */ |
| 7681 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7682 | o1 = ch>>11; |
| 7683 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7684 | i2 = 16*mlevel1[o1] + o2; |
| 7685 | if (mlevel2[i2] == 0xFF) |
| 7686 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7687 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7688 | i3 = 128*mlevel2[i2] + o3; |
| 7689 | mlevel3[i3] = i; |
| 7690 | } |
| 7691 | return result; |
| 7692 | } |
| 7693 | |
| 7694 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7695 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7696 | { |
| 7697 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7698 | int l1 = c>>11; |
| 7699 | int l2 = (c>>7) & 0xF; |
| 7700 | int l3 = c & 0x7F; |
| 7701 | int i; |
| 7702 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7703 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7704 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7705 | if (c == 0) |
| 7706 | return 0; |
| 7707 | /* level 1*/ |
| 7708 | i = map->level1[l1]; |
| 7709 | if (i == 0xFF) { |
| 7710 | return -1; |
| 7711 | } |
| 7712 | /* level 2*/ |
| 7713 | i = map->level23[16*i+l2]; |
| 7714 | if (i == 0xFF) { |
| 7715 | return -1; |
| 7716 | } |
| 7717 | /* level 3 */ |
| 7718 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7719 | if (i == 0) { |
| 7720 | return -1; |
| 7721 | } |
| 7722 | return i; |
| 7723 | } |
| 7724 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7725 | /* Lookup the character ch in the mapping. If the character |
| 7726 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7727 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7728 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7729 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7730 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7731 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7732 | PyObject *x; |
| 7733 | |
| 7734 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7735 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7736 | x = PyObject_GetItem(mapping, w); |
| 7737 | Py_DECREF(w); |
| 7738 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7739 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7740 | /* No mapping found means: mapping is undefined. */ |
| 7741 | PyErr_Clear(); |
| 7742 | x = Py_None; |
| 7743 | Py_INCREF(x); |
| 7744 | return x; |
| 7745 | } else |
| 7746 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7748 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7749 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7750 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7751 | long value = PyLong_AS_LONG(x); |
| 7752 | if (value < 0 || value > 255) { |
| 7753 | PyErr_SetString(PyExc_TypeError, |
| 7754 | "character mapping must be in range(256)"); |
| 7755 | Py_DECREF(x); |
| 7756 | return NULL; |
| 7757 | } |
| 7758 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7760 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7762 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7763 | /* wrong return value */ |
| 7764 | PyErr_Format(PyExc_TypeError, |
| 7765 | "character mapping must return integer, bytes or None, not %.400s", |
| 7766 | x->ob_type->tp_name); |
| 7767 | Py_DECREF(x); |
| 7768 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | } |
| 7770 | } |
| 7771 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7772 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7773 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7774 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7775 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7776 | /* exponentially overallocate to minimize reallocations */ |
| 7777 | if (requiredsize < 2*outsize) |
| 7778 | requiredsize = 2*outsize; |
| 7779 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7780 | return -1; |
| 7781 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7782 | } |
| 7783 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7784 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7785 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7786 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7787 | /* 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] | 7788 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7789 | space is available. Return a new reference to the object that |
| 7790 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7791 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7792 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7793 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7794 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7795 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7796 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7797 | PyObject *rep; |
| 7798 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7799 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7800 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7801 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7802 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7803 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7804 | if (res == -1) |
| 7805 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7806 | if (outsize<requiredsize) |
| 7807 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7808 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7809 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7810 | outstart[(*outpos)++] = (char)res; |
| 7811 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7812 | } |
| 7813 | |
| 7814 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7815 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7816 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7817 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7818 | Py_DECREF(rep); |
| 7819 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7820 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7821 | if (PyLong_Check(rep)) { |
| 7822 | Py_ssize_t requiredsize = *outpos+1; |
| 7823 | if (outsize<requiredsize) |
| 7824 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7825 | Py_DECREF(rep); |
| 7826 | return enc_EXCEPTION; |
| 7827 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7828 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7829 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7830 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7831 | else { |
| 7832 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7833 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7834 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7835 | if (outsize<requiredsize) |
| 7836 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7837 | Py_DECREF(rep); |
| 7838 | return enc_EXCEPTION; |
| 7839 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7840 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | memcpy(outstart + *outpos, repchars, repsize); |
| 7842 | *outpos += repsize; |
| 7843 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7844 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7845 | Py_DECREF(rep); |
| 7846 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7847 | } |
| 7848 | |
| 7849 | /* handle an error in PyUnicode_EncodeCharmap |
| 7850 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7851 | static int |
| 7852 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7853 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7854 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7855 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7856 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7857 | { |
| 7858 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7859 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7860 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7861 | enum PyUnicode_Kind kind; |
| 7862 | void *data; |
| 7863 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7864 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7865 | Py_ssize_t collstartpos = *inpos; |
| 7866 | Py_ssize_t collendpos = *inpos+1; |
| 7867 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7868 | char *encoding = "charmap"; |
| 7869 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7870 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7871 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7872 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7873 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7874 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7875 | return -1; |
| 7876 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7877 | /* find all unencodable characters */ |
| 7878 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7879 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7880 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7881 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7882 | val = encoding_map_lookup(ch, mapping); |
| 7883 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7884 | break; |
| 7885 | ++collendpos; |
| 7886 | continue; |
| 7887 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7888 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7889 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7890 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7891 | if (rep==NULL) |
| 7892 | return -1; |
| 7893 | else if (rep!=Py_None) { |
| 7894 | Py_DECREF(rep); |
| 7895 | break; |
| 7896 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7897 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7899 | } |
| 7900 | /* cache callback name lookup |
| 7901 | * (if not done yet, i.e. it's the first error) */ |
| 7902 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7904 | *known_errorHandler = 1; |
| 7905 | else if (!strcmp(errors, "replace")) |
| 7906 | *known_errorHandler = 2; |
| 7907 | else if (!strcmp(errors, "ignore")) |
| 7908 | *known_errorHandler = 3; |
| 7909 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7910 | *known_errorHandler = 4; |
| 7911 | else |
| 7912 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7913 | } |
| 7914 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7915 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7916 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7917 | return -1; |
| 7918 | case 2: /* replace */ |
| 7919 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7920 | x = charmapencode_output('?', mapping, res, respos); |
| 7921 | if (x==enc_EXCEPTION) { |
| 7922 | return -1; |
| 7923 | } |
| 7924 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7925 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7926 | return -1; |
| 7927 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7928 | } |
| 7929 | /* fall through */ |
| 7930 | case 3: /* ignore */ |
| 7931 | *inpos = collendpos; |
| 7932 | break; |
| 7933 | case 4: /* xmlcharrefreplace */ |
| 7934 | /* generate replacement (temporarily (mis)uses p) */ |
| 7935 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7936 | char buffer[2+29+1+1]; |
| 7937 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7938 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7939 | for (cp = buffer; *cp; ++cp) { |
| 7940 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7941 | if (x==enc_EXCEPTION) |
| 7942 | return -1; |
| 7943 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7944 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7945 | return -1; |
| 7946 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7947 | } |
| 7948 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7949 | *inpos = collendpos; |
| 7950 | break; |
| 7951 | default: |
| 7952 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7953 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7954 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7955 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7956 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7957 | if (PyBytes_Check(repunicode)) { |
| 7958 | /* Directly copy bytes result to output. */ |
| 7959 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7960 | Py_ssize_t requiredsize; |
| 7961 | repsize = PyBytes_Size(repunicode); |
| 7962 | requiredsize = *respos + repsize; |
| 7963 | if (requiredsize > outsize) |
| 7964 | /* Make room for all additional bytes. */ |
| 7965 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7966 | Py_DECREF(repunicode); |
| 7967 | return -1; |
| 7968 | } |
| 7969 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7970 | PyBytes_AsString(repunicode), repsize); |
| 7971 | *respos += repsize; |
| 7972 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7973 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7974 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7975 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7976 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7977 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7978 | Py_DECREF(repunicode); |
| 7979 | return -1; |
| 7980 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 7981 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7982 | data = PyUnicode_DATA(repunicode); |
| 7983 | kind = PyUnicode_KIND(repunicode); |
| 7984 | for (index = 0; index < repsize; index++) { |
| 7985 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 7986 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7987 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7988 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | return -1; |
| 7990 | } |
| 7991 | else if (x==enc_FAILED) { |
| 7992 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7993 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7994 | return -1; |
| 7995 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7996 | } |
| 7997 | *inpos = newpos; |
| 7998 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7999 | } |
| 8000 | return 0; |
| 8001 | } |
| 8002 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8003 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8004 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8005 | PyObject *mapping, |
| 8006 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8007 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8008 | /* output object */ |
| 8009 | PyObject *res = NULL; |
| 8010 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8011 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8012 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8013 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8014 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8015 | PyObject *errorHandler = NULL; |
| 8016 | PyObject *exc = NULL; |
| 8017 | /* the following variable is used for caching string comparisons |
| 8018 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8019 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8020 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8021 | void *data; |
| 8022 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8023 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8024 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8025 | return NULL; |
| 8026 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8027 | data = PyUnicode_DATA(unicode); |
| 8028 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | /* Default to Latin-1 */ |
| 8031 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8032 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8034 | /* allocate enough for a simple encoding without |
| 8035 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8036 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8037 | if (res == NULL) |
| 8038 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8039 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8040 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8041 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8042 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8043 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8044 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8045 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8046 | if (x==enc_EXCEPTION) /* error */ |
| 8047 | goto onError; |
| 8048 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8049 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | &exc, |
| 8051 | &known_errorHandler, &errorHandler, errors, |
| 8052 | &res, &respos)) { |
| 8053 | goto onError; |
| 8054 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8055 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8056 | else |
| 8057 | /* done with this character => adjust input position */ |
| 8058 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8059 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8060 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8061 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8062 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8063 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8064 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8065 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8066 | Py_XDECREF(exc); |
| 8067 | Py_XDECREF(errorHandler); |
| 8068 | return res; |
| 8069 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8070 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8071 | Py_XDECREF(res); |
| 8072 | Py_XDECREF(exc); |
| 8073 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8074 | return NULL; |
| 8075 | } |
| 8076 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8077 | /* Deprecated */ |
| 8078 | PyObject * |
| 8079 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8080 | Py_ssize_t size, |
| 8081 | PyObject *mapping, |
| 8082 | const char *errors) |
| 8083 | { |
| 8084 | PyObject *result; |
| 8085 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8086 | if (unicode == NULL) |
| 8087 | return NULL; |
| 8088 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8089 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8090 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8091 | } |
| 8092 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8093 | PyObject * |
| 8094 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8095 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8096 | { |
| 8097 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8098 | PyErr_BadArgument(); |
| 8099 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8100 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8101 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8102 | } |
| 8103 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8104 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8105 | static void |
| 8106 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8107 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8108 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8109 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8110 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8111 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8112 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8113 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8114 | } |
| 8115 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8116 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8117 | goto onError; |
| 8118 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8119 | goto onError; |
| 8120 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8121 | goto onError; |
| 8122 | return; |
| 8123 | onError: |
| 8124 | Py_DECREF(*exceptionObject); |
| 8125 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | } |
| 8127 | } |
| 8128 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8129 | /* error handling callback helper: |
| 8130 | build arguments, call the callback and check the arguments, |
| 8131 | put the result into newpos and return the replacement string, which |
| 8132 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8133 | static PyObject * |
| 8134 | unicode_translate_call_errorhandler(const char *errors, |
| 8135 | PyObject **errorHandler, |
| 8136 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8137 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8138 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8139 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8140 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8141 | 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] | 8142 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8143 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8144 | PyObject *restuple; |
| 8145 | PyObject *resunicode; |
| 8146 | |
| 8147 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8149 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8150 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8151 | } |
| 8152 | |
| 8153 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8154 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8155 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8156 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8157 | |
| 8158 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8159 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8160 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8161 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8162 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8163 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8164 | Py_DECREF(restuple); |
| 8165 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8166 | } |
| 8167 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | &resunicode, &i_newpos)) { |
| 8169 | Py_DECREF(restuple); |
| 8170 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8171 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8172 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8173 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8174 | else |
| 8175 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8176 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8177 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8178 | Py_DECREF(restuple); |
| 8179 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8180 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8181 | Py_INCREF(resunicode); |
| 8182 | Py_DECREF(restuple); |
| 8183 | return resunicode; |
| 8184 | } |
| 8185 | |
| 8186 | /* Lookup the character ch in the mapping and put the result in result, |
| 8187 | which must be decrefed by the caller. |
| 8188 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8189 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8190 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8191 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8192 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8193 | PyObject *x; |
| 8194 | |
| 8195 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8196 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8197 | x = PyObject_GetItem(mapping, w); |
| 8198 | Py_DECREF(w); |
| 8199 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8200 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8201 | /* No mapping found means: use 1:1 mapping. */ |
| 8202 | PyErr_Clear(); |
| 8203 | *result = NULL; |
| 8204 | return 0; |
| 8205 | } else |
| 8206 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8207 | } |
| 8208 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8209 | *result = x; |
| 8210 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8211 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8212 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8213 | long value = PyLong_AS_LONG(x); |
| 8214 | long max = PyUnicode_GetMax(); |
| 8215 | if (value < 0 || value > max) { |
| 8216 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8217 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8218 | Py_DECREF(x); |
| 8219 | return -1; |
| 8220 | } |
| 8221 | *result = x; |
| 8222 | return 0; |
| 8223 | } |
| 8224 | else if (PyUnicode_Check(x)) { |
| 8225 | *result = x; |
| 8226 | return 0; |
| 8227 | } |
| 8228 | else { |
| 8229 | /* wrong return value */ |
| 8230 | PyErr_SetString(PyExc_TypeError, |
| 8231 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8232 | Py_DECREF(x); |
| 8233 | return -1; |
| 8234 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8235 | } |
| 8236 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | if not reallocate and adjust various state variables. |
| 8238 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8239 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8240 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8241 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8242 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8243 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8244 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8245 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8246 | /* exponentially overallocate to minimize reallocations */ |
| 8247 | if (requiredsize < 2 * oldsize) |
| 8248 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8249 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8250 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8251 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8252 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8253 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8254 | } |
| 8255 | return 0; |
| 8256 | } |
| 8257 | /* lookup the character, put the result in the output string and adjust |
| 8258 | various state variables. Return a new reference to the object that |
| 8259 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8260 | undefined (in which case no character was written). |
| 8261 | The called must decref result. |
| 8262 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8263 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8264 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8265 | PyObject *mapping, Py_UCS4 **output, |
| 8266 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8267 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8268 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8269 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8270 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8272 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8273 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8274 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8275 | } |
| 8276 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8277 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8278 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8279 | /* 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] | 8280 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8281 | } |
| 8282 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8283 | Py_ssize_t repsize; |
| 8284 | if (PyUnicode_READY(*res) == -1) |
| 8285 | return -1; |
| 8286 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | if (repsize==1) { |
| 8288 | /* 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] | 8289 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8290 | } |
| 8291 | else if (repsize!=0) { |
| 8292 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8293 | Py_ssize_t requiredsize = *opos + |
| 8294 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8295 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8296 | Py_ssize_t i; |
| 8297 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8298 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8299 | for(i = 0; i < repsize; i++) |
| 8300 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8302 | } |
| 8303 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8305 | return 0; |
| 8306 | } |
| 8307 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8308 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8309 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8310 | PyObject *mapping, |
| 8311 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8312 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8313 | /* input object */ |
| 8314 | char *idata; |
| 8315 | Py_ssize_t size, i; |
| 8316 | int kind; |
| 8317 | /* output buffer */ |
| 8318 | Py_UCS4 *output = NULL; |
| 8319 | Py_ssize_t osize; |
| 8320 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8321 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8322 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8323 | char *reason = "character maps to <undefined>"; |
| 8324 | PyObject *errorHandler = NULL; |
| 8325 | PyObject *exc = NULL; |
| 8326 | /* the following variable is used for caching string comparisons |
| 8327 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8328 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8329 | int known_errorHandler = -1; |
| 8330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8332 | PyErr_BadArgument(); |
| 8333 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8336 | if (PyUnicode_READY(input) == -1) |
| 8337 | return NULL; |
| 8338 | idata = (char*)PyUnicode_DATA(input); |
| 8339 | kind = PyUnicode_KIND(input); |
| 8340 | size = PyUnicode_GET_LENGTH(input); |
| 8341 | i = 0; |
| 8342 | |
| 8343 | if (size == 0) { |
| 8344 | Py_INCREF(input); |
| 8345 | return input; |
| 8346 | } |
| 8347 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8348 | /* allocate enough for a simple 1:1 translation without |
| 8349 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8350 | osize = size; |
| 8351 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8352 | opos = 0; |
| 8353 | if (output == NULL) { |
| 8354 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8355 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8356 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8357 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8358 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8359 | /* try to encode it */ |
| 8360 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8361 | if (charmaptranslate_output(input, i, mapping, |
| 8362 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 | Py_XDECREF(x); |
| 8364 | goto onError; |
| 8365 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8366 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8367 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8368 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8369 | else { /* untranslatable character */ |
| 8370 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8371 | Py_ssize_t repsize; |
| 8372 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8373 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8374 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8375 | Py_ssize_t collstart = i; |
| 8376 | Py_ssize_t collend = i+1; |
| 8377 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8378 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8379 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8380 | while (collend < size) { |
| 8381 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8382 | goto onError; |
| 8383 | Py_XDECREF(x); |
| 8384 | if (x!=Py_None) |
| 8385 | break; |
| 8386 | ++collend; |
| 8387 | } |
| 8388 | /* cache callback name lookup |
| 8389 | * (if not done yet, i.e. it's the first error) */ |
| 8390 | if (known_errorHandler==-1) { |
| 8391 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8392 | known_errorHandler = 1; |
| 8393 | else if (!strcmp(errors, "replace")) |
| 8394 | known_errorHandler = 2; |
| 8395 | else if (!strcmp(errors, "ignore")) |
| 8396 | known_errorHandler = 3; |
| 8397 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8398 | known_errorHandler = 4; |
| 8399 | else |
| 8400 | known_errorHandler = 0; |
| 8401 | } |
| 8402 | switch (known_errorHandler) { |
| 8403 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8404 | make_translate_exception(&exc, |
| 8405 | input, collstart, collend, reason); |
| 8406 | if (exc != NULL) |
| 8407 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8408 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8409 | case 2: /* replace */ |
| 8410 | /* 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] | 8411 | for (coll = collstart; coll<collend; coll++) |
| 8412 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8413 | /* fall through */ |
| 8414 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8415 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8416 | break; |
| 8417 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8418 | /* generate replacement (temporarily (mis)uses i) */ |
| 8419 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8420 | char buffer[2+29+1+1]; |
| 8421 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8422 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8423 | if (charmaptranslate_makespace(&output, &osize, |
| 8424 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8425 | goto onError; |
| 8426 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8427 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8429 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8430 | break; |
| 8431 | default: |
| 8432 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8433 | reason, input, &exc, |
| 8434 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8435 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8436 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8437 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8438 | Py_DECREF(repunicode); |
| 8439 | goto onError; |
| 8440 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8441 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8442 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8443 | if (charmaptranslate_makespace(&output, &osize, |
| 8444 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8445 | Py_DECREF(repunicode); |
| 8446 | goto onError; |
| 8447 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8448 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8449 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8450 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8451 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8452 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8453 | } |
| 8454 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8455 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8456 | if (!res) |
| 8457 | goto onError; |
| 8458 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8459 | Py_XDECREF(exc); |
| 8460 | Py_XDECREF(errorHandler); |
| 8461 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8463 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8465 | Py_XDECREF(exc); |
| 8466 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8467 | return NULL; |
| 8468 | } |
| 8469 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8470 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8471 | PyObject * |
| 8472 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8473 | Py_ssize_t size, |
| 8474 | PyObject *mapping, |
| 8475 | const char *errors) |
| 8476 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8477 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8478 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8479 | if (!unicode) |
| 8480 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8481 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8482 | Py_DECREF(unicode); |
| 8483 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8484 | } |
| 8485 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8486 | PyObject * |
| 8487 | PyUnicode_Translate(PyObject *str, |
| 8488 | PyObject *mapping, |
| 8489 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8490 | { |
| 8491 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | str = PyUnicode_FromObject(str); |
| 8494 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8495 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8496 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8497 | Py_DECREF(str); |
| 8498 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8499 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8500 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8501 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8502 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8503 | { |
| 8504 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8505 | called as a callback from fixup() which does it already. */ |
| 8506 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8507 | const int kind = PyUnicode_KIND(self); |
| 8508 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8509 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8510 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8511 | Py_ssize_t i; |
| 8512 | |
| 8513 | for (i = 0; i < len; ++i) { |
| 8514 | ch = PyUnicode_READ(kind, data, i); |
| 8515 | fixed = 0; |
| 8516 | if (ch > 127) { |
| 8517 | if (Py_UNICODE_ISSPACE(ch)) |
| 8518 | fixed = ' '; |
| 8519 | else { |
| 8520 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8521 | if (decimal >= 0) |
| 8522 | fixed = '0' + decimal; |
| 8523 | } |
| 8524 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8525 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8526 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8527 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8528 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8529 | else |
| 8530 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8532 | } |
| 8533 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8534 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8535 | } |
| 8536 | |
| 8537 | PyObject * |
| 8538 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8539 | { |
| 8540 | if (!PyUnicode_Check(unicode)) { |
| 8541 | PyErr_BadInternalCall(); |
| 8542 | return NULL; |
| 8543 | } |
| 8544 | if (PyUnicode_READY(unicode) == -1) |
| 8545 | return NULL; |
| 8546 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8547 | /* If the string is already ASCII, just return the same string */ |
| 8548 | Py_INCREF(unicode); |
| 8549 | return unicode; |
| 8550 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8551 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8552 | } |
| 8553 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8554 | PyObject * |
| 8555 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8556 | Py_ssize_t length) |
| 8557 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8558 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8559 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8560 | Py_UCS4 maxchar; |
| 8561 | enum PyUnicode_Kind kind; |
| 8562 | void *data; |
| 8563 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8564 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8565 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8566 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8567 | if (ch > 127) { |
| 8568 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8569 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8570 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8571 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8572 | } |
| 8573 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8574 | |
| 8575 | /* Copy to a new string */ |
| 8576 | decimal = PyUnicode_New(length, maxchar); |
| 8577 | if (decimal == NULL) |
| 8578 | return decimal; |
| 8579 | kind = PyUnicode_KIND(decimal); |
| 8580 | data = PyUnicode_DATA(decimal); |
| 8581 | /* Iterate over code points */ |
| 8582 | for (i = 0; i < length; i++) { |
| 8583 | Py_UNICODE ch = s[i]; |
| 8584 | if (ch > 127) { |
| 8585 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8586 | if (decimal >= 0) |
| 8587 | ch = '0' + decimal; |
| 8588 | } |
| 8589 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8590 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8591 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8592 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8593 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8594 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8595 | int |
| 8596 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8597 | Py_ssize_t length, |
| 8598 | char *output, |
| 8599 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8600 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8601 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8602 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8603 | enum PyUnicode_Kind kind; |
| 8604 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8605 | |
| 8606 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8607 | PyErr_BadArgument(); |
| 8608 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8609 | } |
| 8610 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8611 | unicode = PyUnicode_FromUnicode(s, length); |
| 8612 | if (unicode == NULL) |
| 8613 | return -1; |
| 8614 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8615 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8616 | Py_DECREF(unicode); |
| 8617 | return -1; |
| 8618 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8619 | kind = PyUnicode_KIND(unicode); |
| 8620 | data = PyUnicode_DATA(unicode); |
| 8621 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8622 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8623 | PyObject *exc; |
| 8624 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8625 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8626 | Py_ssize_t startpos; |
| 8627 | |
| 8628 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8629 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8630 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8631 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8632 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8633 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8634 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8635 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8636 | if (decimal >= 0) { |
| 8637 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8638 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8639 | continue; |
| 8640 | } |
| 8641 | if (0 < ch && ch < 256) { |
| 8642 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8643 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8644 | continue; |
| 8645 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8646 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8647 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8648 | exc = NULL; |
| 8649 | raise_encode_exception(&exc, "decimal", unicode, |
| 8650 | startpos, startpos+1, |
| 8651 | "invalid decimal Unicode string"); |
| 8652 | Py_XDECREF(exc); |
| 8653 | Py_DECREF(unicode); |
| 8654 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8655 | } |
| 8656 | /* 0-terminate the output string */ |
| 8657 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8658 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8659 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8660 | } |
| 8661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8662 | /* --- Helpers ------------------------------------------------------------ */ |
| 8663 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8664 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8665 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8666 | Py_ssize_t start, |
| 8667 | Py_ssize_t end) |
| 8668 | { |
| 8669 | int kind1, kind2, kind; |
| 8670 | void *buf1, *buf2; |
| 8671 | Py_ssize_t len1, len2, result; |
| 8672 | |
| 8673 | kind1 = PyUnicode_KIND(s1); |
| 8674 | kind2 = PyUnicode_KIND(s2); |
| 8675 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8676 | buf1 = PyUnicode_DATA(s1); |
| 8677 | buf2 = PyUnicode_DATA(s2); |
| 8678 | if (kind1 != kind) |
| 8679 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8680 | if (!buf1) |
| 8681 | return -2; |
| 8682 | if (kind2 != kind) |
| 8683 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8684 | if (!buf2) { |
| 8685 | if (kind1 != kind) PyMem_Free(buf1); |
| 8686 | return -2; |
| 8687 | } |
| 8688 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8689 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8690 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8691 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8692 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8693 | case PyUnicode_1BYTE_KIND: |
| 8694 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8695 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8696 | else |
| 8697 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8698 | break; |
| 8699 | case PyUnicode_2BYTE_KIND: |
| 8700 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8701 | break; |
| 8702 | case PyUnicode_4BYTE_KIND: |
| 8703 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8704 | break; |
| 8705 | default: |
| 8706 | assert(0); result = -2; |
| 8707 | } |
| 8708 | } |
| 8709 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8710 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8711 | case PyUnicode_1BYTE_KIND: |
| 8712 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8713 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8714 | else |
| 8715 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8716 | break; |
| 8717 | case PyUnicode_2BYTE_KIND: |
| 8718 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8719 | break; |
| 8720 | case PyUnicode_4BYTE_KIND: |
| 8721 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8722 | break; |
| 8723 | default: |
| 8724 | assert(0); result = -2; |
| 8725 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8726 | } |
| 8727 | |
| 8728 | if (kind1 != kind) |
| 8729 | PyMem_Free(buf1); |
| 8730 | if (kind2 != kind) |
| 8731 | PyMem_Free(buf2); |
| 8732 | |
| 8733 | return result; |
| 8734 | } |
| 8735 | |
| 8736 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8737 | _PyUnicode_InsertThousandsGrouping( |
| 8738 | PyObject *unicode, Py_ssize_t index, |
| 8739 | Py_ssize_t n_buffer, |
| 8740 | void *digits, Py_ssize_t n_digits, |
| 8741 | Py_ssize_t min_width, |
| 8742 | const char *grouping, PyObject *thousands_sep, |
| 8743 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8744 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8745 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8746 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8747 | Py_ssize_t thousands_sep_len; |
| 8748 | Py_ssize_t len; |
| 8749 | |
| 8750 | if (unicode != NULL) { |
| 8751 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8752 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8753 | } |
| 8754 | else { |
| 8755 | kind = PyUnicode_1BYTE_KIND; |
| 8756 | data = NULL; |
| 8757 | } |
| 8758 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8759 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8760 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8761 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8762 | if (thousands_sep_kind < kind) { |
| 8763 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8764 | if (!thousands_sep_data) |
| 8765 | return -1; |
| 8766 | } |
| 8767 | else { |
| 8768 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8769 | if (!data) |
| 8770 | return -1; |
| 8771 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8772 | } |
| 8773 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8774 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8775 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8776 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8777 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8778 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8779 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8780 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8781 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8782 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8783 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8784 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8785 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8786 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8787 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8788 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8789 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8790 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8791 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8792 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8793 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8794 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8795 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8796 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8797 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8798 | break; |
| 8799 | default: |
| 8800 | assert(0); |
| 8801 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8802 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8803 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8804 | if (thousands_sep_kind < kind) |
| 8805 | PyMem_Free(thousands_sep_data); |
| 8806 | else |
| 8807 | PyMem_Free(data); |
| 8808 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8809 | if (unicode == NULL) { |
| 8810 | *maxchar = 127; |
| 8811 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8812 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8813 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8814 | } |
| 8815 | } |
| 8816 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8817 | } |
| 8818 | |
| 8819 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8820 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8821 | #define ADJUST_INDICES(start, end, len) \ |
| 8822 | if (end > len) \ |
| 8823 | end = len; \ |
| 8824 | else if (end < 0) { \ |
| 8825 | end += len; \ |
| 8826 | if (end < 0) \ |
| 8827 | end = 0; \ |
| 8828 | } \ |
| 8829 | if (start < 0) { \ |
| 8830 | start += len; \ |
| 8831 | if (start < 0) \ |
| 8832 | start = 0; \ |
| 8833 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8834 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8835 | Py_ssize_t |
| 8836 | PyUnicode_Count(PyObject *str, |
| 8837 | PyObject *substr, |
| 8838 | Py_ssize_t start, |
| 8839 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8840 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8841 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8842 | PyObject* str_obj; |
| 8843 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8844 | int kind1, kind2, kind; |
| 8845 | void *buf1 = NULL, *buf2 = NULL; |
| 8846 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8847 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8848 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8849 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8850 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8851 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8852 | if (!sub_obj) { |
| 8853 | Py_DECREF(str_obj); |
| 8854 | return -1; |
| 8855 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8856 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8857 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8858 | Py_DECREF(str_obj); |
| 8859 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8860 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8861 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8862 | kind1 = PyUnicode_KIND(str_obj); |
| 8863 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8864 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8865 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8866 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8867 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8868 | if (kind2 > kind) { |
| 8869 | Py_DECREF(sub_obj); |
| 8870 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8871 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8872 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8873 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8874 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 | if (!buf2) |
| 8876 | goto onError; |
| 8877 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8878 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8879 | |
| 8880 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8881 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8882 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8883 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8884 | result = asciilib_count( |
| 8885 | ((Py_UCS1*)buf1) + start, end - start, |
| 8886 | buf2, len2, PY_SSIZE_T_MAX |
| 8887 | ); |
| 8888 | else |
| 8889 | result = ucs1lib_count( |
| 8890 | ((Py_UCS1*)buf1) + start, end - start, |
| 8891 | buf2, len2, PY_SSIZE_T_MAX |
| 8892 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8893 | break; |
| 8894 | case PyUnicode_2BYTE_KIND: |
| 8895 | result = ucs2lib_count( |
| 8896 | ((Py_UCS2*)buf1) + start, end - start, |
| 8897 | buf2, len2, PY_SSIZE_T_MAX |
| 8898 | ); |
| 8899 | break; |
| 8900 | case PyUnicode_4BYTE_KIND: |
| 8901 | result = ucs4lib_count( |
| 8902 | ((Py_UCS4*)buf1) + start, end - start, |
| 8903 | buf2, len2, PY_SSIZE_T_MAX |
| 8904 | ); |
| 8905 | break; |
| 8906 | default: |
| 8907 | assert(0); result = 0; |
| 8908 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8909 | |
| 8910 | Py_DECREF(sub_obj); |
| 8911 | Py_DECREF(str_obj); |
| 8912 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8913 | if (kind2 != kind) |
| 8914 | PyMem_Free(buf2); |
| 8915 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8916 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8917 | onError: |
| 8918 | Py_DECREF(sub_obj); |
| 8919 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8920 | if (kind2 != kind && buf2) |
| 8921 | PyMem_Free(buf2); |
| 8922 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8923 | } |
| 8924 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8925 | Py_ssize_t |
| 8926 | PyUnicode_Find(PyObject *str, |
| 8927 | PyObject *sub, |
| 8928 | Py_ssize_t start, |
| 8929 | Py_ssize_t end, |
| 8930 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8931 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8932 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8933 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8934 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8935 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8936 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8937 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8938 | if (!sub) { |
| 8939 | Py_DECREF(str); |
| 8940 | return -2; |
| 8941 | } |
| 8942 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 8943 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8944 | Py_DECREF(str); |
| 8945 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8946 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8947 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8948 | result = any_find_slice(direction, |
| 8949 | str, sub, start, end |
| 8950 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8951 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8952 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8953 | Py_DECREF(sub); |
| 8954 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8955 | return result; |
| 8956 | } |
| 8957 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8958 | Py_ssize_t |
| 8959 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8960 | Py_ssize_t start, Py_ssize_t end, |
| 8961 | int direction) |
| 8962 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8963 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8964 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8965 | if (PyUnicode_READY(str) == -1) |
| 8966 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8967 | if (start < 0 || end < 0) { |
| 8968 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8969 | return -2; |
| 8970 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8971 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8972 | end = PyUnicode_GET_LENGTH(str); |
| 8973 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8974 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 8975 | kind, end-start, ch, direction); |
| 8976 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8977 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8978 | else |
| 8979 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8980 | } |
| 8981 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8982 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8983 | tailmatch(PyObject *self, |
| 8984 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8985 | Py_ssize_t start, |
| 8986 | Py_ssize_t end, |
| 8987 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8988 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8989 | int kind_self; |
| 8990 | int kind_sub; |
| 8991 | void *data_self; |
| 8992 | void *data_sub; |
| 8993 | Py_ssize_t offset; |
| 8994 | Py_ssize_t i; |
| 8995 | Py_ssize_t end_sub; |
| 8996 | |
| 8997 | if (PyUnicode_READY(self) == -1 || |
| 8998 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 8999 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9000 | |
| 9001 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | return 1; |
| 9003 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9004 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9005 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9006 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9007 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9008 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9009 | kind_self = PyUnicode_KIND(self); |
| 9010 | data_self = PyUnicode_DATA(self); |
| 9011 | kind_sub = PyUnicode_KIND(substring); |
| 9012 | data_sub = PyUnicode_DATA(substring); |
| 9013 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9014 | |
| 9015 | if (direction > 0) |
| 9016 | offset = end; |
| 9017 | else |
| 9018 | offset = start; |
| 9019 | |
| 9020 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9021 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9022 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9023 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9024 | /* If both are of the same kind, memcmp is sufficient */ |
| 9025 | if (kind_self == kind_sub) { |
| 9026 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9027 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9028 | data_sub, |
| 9029 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9030 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9031 | } |
| 9032 | /* otherwise we have to compare each character by first accesing it */ |
| 9033 | else { |
| 9034 | /* We do not need to compare 0 and len(substring)-1 because |
| 9035 | the if statement above ensured already that they are equal |
| 9036 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9037 | for (i = 1; i < end_sub; ++i) { |
| 9038 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9039 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9040 | return 0; |
| 9041 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9042 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9043 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9044 | } |
| 9045 | |
| 9046 | return 0; |
| 9047 | } |
| 9048 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9049 | Py_ssize_t |
| 9050 | PyUnicode_Tailmatch(PyObject *str, |
| 9051 | PyObject *substr, |
| 9052 | Py_ssize_t start, |
| 9053 | Py_ssize_t end, |
| 9054 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9055 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9056 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9058 | str = PyUnicode_FromObject(str); |
| 9059 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9060 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9061 | substr = PyUnicode_FromObject(substr); |
| 9062 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9063 | Py_DECREF(str); |
| 9064 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9065 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9066 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9067 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9068 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9069 | Py_DECREF(str); |
| 9070 | Py_DECREF(substr); |
| 9071 | return result; |
| 9072 | } |
| 9073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9074 | /* Apply fixfct filter to the Unicode object self and return a |
| 9075 | reference to the modified object */ |
| 9076 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9077 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9078 | fixup(PyObject *self, |
| 9079 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9080 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9081 | PyObject *u; |
| 9082 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9083 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9084 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9085 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9086 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9087 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9088 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9089 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9090 | /* fix functions return the new maximum character in a string, |
| 9091 | if the kind of the resulting unicode object does not change, |
| 9092 | everything is fine. Otherwise we need to change the string kind |
| 9093 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9094 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9095 | |
| 9096 | if (maxchar_new == 0) { |
| 9097 | /* no changes */; |
| 9098 | if (PyUnicode_CheckExact(self)) { |
| 9099 | Py_DECREF(u); |
| 9100 | Py_INCREF(self); |
| 9101 | return self; |
| 9102 | } |
| 9103 | else |
| 9104 | return u; |
| 9105 | } |
| 9106 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9107 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9108 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9109 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9110 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9111 | |
| 9112 | /* In case the maximum character changed, we need to |
| 9113 | convert the string to the new category. */ |
| 9114 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9115 | if (v == NULL) { |
| 9116 | Py_DECREF(u); |
| 9117 | return NULL; |
| 9118 | } |
| 9119 | if (maxchar_new > maxchar_old) { |
| 9120 | /* If the maxchar increased so that the kind changed, not all |
| 9121 | characters are representable anymore and we need to fix the |
| 9122 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9123 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9124 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9125 | maxchar_old = fixfct(v); |
| 9126 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9127 | } |
| 9128 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9129 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9130 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9131 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9132 | Py_DECREF(u); |
| 9133 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9134 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9135 | } |
| 9136 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9137 | static PyObject * |
| 9138 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9139 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9140 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9141 | char *resdata, *data = PyUnicode_DATA(self); |
| 9142 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9143 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9144 | res = PyUnicode_New(len, 127); |
| 9145 | if (res == NULL) |
| 9146 | return NULL; |
| 9147 | resdata = PyUnicode_DATA(res); |
| 9148 | if (lower) |
| 9149 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9150 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9151 | _Py_bytes_upper(resdata, data, len); |
| 9152 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9153 | } |
| 9154 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9155 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9156 | 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] | 9157 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9158 | Py_ssize_t j; |
| 9159 | int final_sigma; |
| 9160 | Py_UCS4 c; |
| 9161 | /* 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] | 9162 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9163 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9164 | |
| 9165 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9166 | */ |
| 9167 | for (j = i - 1; j >= 0; j--) { |
| 9168 | c = PyUnicode_READ(kind, data, j); |
| 9169 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9170 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9171 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9172 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9173 | if (final_sigma) { |
| 9174 | for (j = i + 1; j < length; j++) { |
| 9175 | c = PyUnicode_READ(kind, data, j); |
| 9176 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9177 | break; |
| 9178 | } |
| 9179 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9180 | } |
| 9181 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9182 | } |
| 9183 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9184 | static int |
| 9185 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9186 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9187 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9188 | /* Obscure special case. */ |
| 9189 | if (c == 0x3A3) { |
| 9190 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9191 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9192 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9193 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9194 | } |
| 9195 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9196 | static Py_ssize_t |
| 9197 | 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] | 9198 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9199 | Py_ssize_t i, k = 0; |
| 9200 | int n_res, j; |
| 9201 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9202 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9203 | c = PyUnicode_READ(kind, data, 0); |
| 9204 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9205 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9206 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9207 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9208 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9209 | for (i = 1; i < length; i++) { |
| 9210 | c = PyUnicode_READ(kind, data, i); |
| 9211 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9212 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9213 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9214 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9215 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9216 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9217 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9218 | } |
| 9219 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9220 | static Py_ssize_t |
| 9221 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9222 | Py_ssize_t i, k = 0; |
| 9223 | |
| 9224 | for (i = 0; i < length; i++) { |
| 9225 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9226 | int n_res, j; |
| 9227 | if (Py_UNICODE_ISUPPER(c)) { |
| 9228 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9229 | } |
| 9230 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9231 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9232 | } |
| 9233 | else { |
| 9234 | n_res = 1; |
| 9235 | mapped[0] = c; |
| 9236 | } |
| 9237 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9238 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9239 | res[k++] = mapped[j]; |
| 9240 | } |
| 9241 | } |
| 9242 | return k; |
| 9243 | } |
| 9244 | |
| 9245 | static Py_ssize_t |
| 9246 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9247 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9248 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9249 | Py_ssize_t i, k = 0; |
| 9250 | |
| 9251 | for (i = 0; i < length; i++) { |
| 9252 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9253 | int n_res, j; |
| 9254 | if (lower) |
| 9255 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9256 | else |
| 9257 | n_res = _PyUnicode_ToUpperFull(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 | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9260 | res[k++] = mapped[j]; |
| 9261 | } |
| 9262 | } |
| 9263 | return k; |
| 9264 | } |
| 9265 | |
| 9266 | static Py_ssize_t |
| 9267 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9268 | { |
| 9269 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9270 | } |
| 9271 | |
| 9272 | static Py_ssize_t |
| 9273 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9274 | { |
| 9275 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9276 | } |
| 9277 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9278 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9279 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9280 | { |
| 9281 | Py_ssize_t i, k = 0; |
| 9282 | |
| 9283 | for (i = 0; i < length; i++) { |
| 9284 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9285 | Py_UCS4 mapped[3]; |
| 9286 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9287 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9288 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9289 | res[k++] = mapped[j]; |
| 9290 | } |
| 9291 | } |
| 9292 | return k; |
| 9293 | } |
| 9294 | |
| 9295 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9296 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9297 | { |
| 9298 | Py_ssize_t i, k = 0; |
| 9299 | int previous_is_cased; |
| 9300 | |
| 9301 | previous_is_cased = 0; |
| 9302 | for (i = 0; i < length; i++) { |
| 9303 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9304 | Py_UCS4 mapped[3]; |
| 9305 | int n_res, j; |
| 9306 | |
| 9307 | if (previous_is_cased) |
| 9308 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9309 | else |
| 9310 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9311 | |
| 9312 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9313 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9314 | res[k++] = mapped[j]; |
| 9315 | } |
| 9316 | |
| 9317 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9318 | } |
| 9319 | return k; |
| 9320 | } |
| 9321 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9322 | static PyObject * |
| 9323 | case_operation(PyObject *self, |
| 9324 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9325 | { |
| 9326 | PyObject *res = NULL; |
| 9327 | Py_ssize_t length, newlength = 0; |
| 9328 | int kind, outkind; |
| 9329 | void *data, *outdata; |
| 9330 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9331 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9332 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9333 | |
| 9334 | kind = PyUnicode_KIND(self); |
| 9335 | data = PyUnicode_DATA(self); |
| 9336 | length = PyUnicode_GET_LENGTH(self); |
| 9337 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9338 | if (tmp == NULL) |
| 9339 | return PyErr_NoMemory(); |
| 9340 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9341 | res = PyUnicode_New(newlength, maxchar); |
| 9342 | if (res == NULL) |
| 9343 | goto leave; |
| 9344 | tmpend = tmp + newlength; |
| 9345 | outdata = PyUnicode_DATA(res); |
| 9346 | outkind = PyUnicode_KIND(res); |
| 9347 | switch (outkind) { |
| 9348 | case PyUnicode_1BYTE_KIND: |
| 9349 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9350 | break; |
| 9351 | case PyUnicode_2BYTE_KIND: |
| 9352 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9353 | break; |
| 9354 | case PyUnicode_4BYTE_KIND: |
| 9355 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9356 | break; |
| 9357 | default: |
| 9358 | assert(0); |
| 9359 | break; |
| 9360 | } |
| 9361 | leave: |
| 9362 | PyMem_FREE(tmp); |
| 9363 | return res; |
| 9364 | } |
| 9365 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9366 | PyObject * |
| 9367 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9368 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9369 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9370 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9371 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9372 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9373 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9374 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9375 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9376 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9377 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9378 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9379 | int use_memcpy; |
| 9380 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9381 | PyObject *last_obj; |
| 9382 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9383 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9384 | fseq = PySequence_Fast(seq, ""); |
| 9385 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9386 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9387 | } |
| 9388 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9389 | /* NOTE: the following code can't call back into Python code, |
| 9390 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9391 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9392 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9393 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9394 | /* If empty sequence, return u"". */ |
| 9395 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9396 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9397 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9398 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9399 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9400 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9401 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9402 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9403 | if (seqlen == 1) { |
| 9404 | if (PyUnicode_CheckExact(items[0])) { |
| 9405 | res = items[0]; |
| 9406 | Py_INCREF(res); |
| 9407 | Py_DECREF(fseq); |
| 9408 | return res; |
| 9409 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9410 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9411 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9412 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9413 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9414 | /* Set up sep and seplen */ |
| 9415 | if (separator == NULL) { |
| 9416 | /* fall back to a blank space separator */ |
| 9417 | sep = PyUnicode_FromOrdinal(' '); |
| 9418 | if (!sep) |
| 9419 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9420 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9421 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9422 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9423 | else { |
| 9424 | if (!PyUnicode_Check(separator)) { |
| 9425 | PyErr_Format(PyExc_TypeError, |
| 9426 | "separator: expected str instance," |
| 9427 | " %.80s found", |
| 9428 | Py_TYPE(separator)->tp_name); |
| 9429 | goto onError; |
| 9430 | } |
| 9431 | if (PyUnicode_READY(separator)) |
| 9432 | goto onError; |
| 9433 | sep = separator; |
| 9434 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9435 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9436 | /* inc refcount to keep this code path symmetric with the |
| 9437 | above case of a blank separator */ |
| 9438 | Py_INCREF(sep); |
| 9439 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9440 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9441 | } |
| 9442 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9443 | /* There are at least two things to join, or else we have a subclass |
| 9444 | * of str in the sequence. |
| 9445 | * Do a pre-pass to figure out the total amount of space we'll |
| 9446 | * need (sz), and see whether all argument are strings. |
| 9447 | */ |
| 9448 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9449 | #ifdef Py_DEBUG |
| 9450 | use_memcpy = 0; |
| 9451 | #else |
| 9452 | use_memcpy = 1; |
| 9453 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9454 | for (i = 0; i < seqlen; i++) { |
| 9455 | const Py_ssize_t old_sz = sz; |
| 9456 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9457 | if (!PyUnicode_Check(item)) { |
| 9458 | PyErr_Format(PyExc_TypeError, |
| 9459 | "sequence item %zd: expected str instance," |
| 9460 | " %.80s found", |
| 9461 | i, Py_TYPE(item)->tp_name); |
| 9462 | goto onError; |
| 9463 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9464 | if (PyUnicode_READY(item) == -1) |
| 9465 | goto onError; |
| 9466 | sz += PyUnicode_GET_LENGTH(item); |
| 9467 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9468 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9469 | if (i != 0) |
| 9470 | sz += seplen; |
| 9471 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9472 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9473 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9474 | goto onError; |
| 9475 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9476 | if (use_memcpy && last_obj != NULL) { |
| 9477 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9478 | use_memcpy = 0; |
| 9479 | } |
| 9480 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9481 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9482 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9483 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9484 | if (res == NULL) |
| 9485 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9486 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9487 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9488 | #ifdef Py_DEBUG |
| 9489 | use_memcpy = 0; |
| 9490 | #else |
| 9491 | if (use_memcpy) { |
| 9492 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9493 | kind = PyUnicode_KIND(res); |
| 9494 | if (seplen != 0) |
| 9495 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9496 | } |
| 9497 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9498 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9499 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9500 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9501 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9502 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9503 | if (use_memcpy) { |
| 9504 | Py_MEMCPY(res_data, |
| 9505 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9506 | kind * seplen); |
| 9507 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9508 | } |
| 9509 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9510 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9511 | res_offset += seplen; |
| 9512 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9513 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9514 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9515 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9516 | if (use_memcpy) { |
| 9517 | Py_MEMCPY(res_data, |
| 9518 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9519 | kind * itemlen); |
| 9520 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9521 | } |
| 9522 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9523 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9524 | res_offset += itemlen; |
| 9525 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9526 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9527 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9528 | if (use_memcpy) |
| 9529 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9530 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9531 | else |
| 9532 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9533 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9534 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9535 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9536 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9537 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9538 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9539 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9540 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9541 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9542 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9543 | return NULL; |
| 9544 | } |
| 9545 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9546 | #define FILL(kind, data, value, start, length) \ |
| 9547 | do { \ |
| 9548 | Py_ssize_t i_ = 0; \ |
| 9549 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9550 | switch ((kind)) { \ |
| 9551 | case PyUnicode_1BYTE_KIND: { \ |
| 9552 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9553 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9554 | break; \ |
| 9555 | } \ |
| 9556 | case PyUnicode_2BYTE_KIND: { \ |
| 9557 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9558 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9559 | break; \ |
| 9560 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9561 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9562 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9563 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9564 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9565 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9566 | } \ |
| 9567 | } \ |
| 9568 | } while (0) |
| 9569 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9570 | void |
| 9571 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9572 | Py_UCS4 fill_char) |
| 9573 | { |
| 9574 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9575 | const void *data = PyUnicode_DATA(unicode); |
| 9576 | assert(PyUnicode_IS_READY(unicode)); |
| 9577 | assert(unicode_modifiable(unicode)); |
| 9578 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9579 | assert(start >= 0); |
| 9580 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9581 | FILL(kind, data, fill_char, start, length); |
| 9582 | } |
| 9583 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9584 | Py_ssize_t |
| 9585 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9586 | Py_UCS4 fill_char) |
| 9587 | { |
| 9588 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9589 | |
| 9590 | if (!PyUnicode_Check(unicode)) { |
| 9591 | PyErr_BadInternalCall(); |
| 9592 | return -1; |
| 9593 | } |
| 9594 | if (PyUnicode_READY(unicode) == -1) |
| 9595 | return -1; |
| 9596 | if (unicode_check_modifiable(unicode)) |
| 9597 | return -1; |
| 9598 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9599 | if (start < 0) { |
| 9600 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9601 | return -1; |
| 9602 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9603 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9604 | PyErr_SetString(PyExc_ValueError, |
| 9605 | "fill character is bigger than " |
| 9606 | "the string maximum character"); |
| 9607 | return -1; |
| 9608 | } |
| 9609 | |
| 9610 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9611 | length = Py_MIN(maxlen, length); |
| 9612 | if (length <= 0) |
| 9613 | return 0; |
| 9614 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9615 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9616 | return length; |
| 9617 | } |
| 9618 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9619 | static PyObject * |
| 9620 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9621 | Py_ssize_t left, |
| 9622 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9623 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9624 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9625 | PyObject *u; |
| 9626 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9627 | int kind; |
| 9628 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9629 | |
| 9630 | if (left < 0) |
| 9631 | left = 0; |
| 9632 | if (right < 0) |
| 9633 | right = 0; |
| 9634 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9635 | if (left == 0 && right == 0) |
| 9636 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9637 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9638 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9639 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9640 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9641 | return NULL; |
| 9642 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9643 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9644 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9645 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9646 | if (!u) |
| 9647 | return NULL; |
| 9648 | |
| 9649 | kind = PyUnicode_KIND(u); |
| 9650 | data = PyUnicode_DATA(u); |
| 9651 | if (left) |
| 9652 | FILL(kind, data, fill, 0, left); |
| 9653 | if (right) |
| 9654 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9655 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9656 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9657 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9658 | } |
| 9659 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9660 | PyObject * |
| 9661 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9662 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9663 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9664 | |
| 9665 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9666 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9667 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9668 | if (PyUnicode_READY(string) == -1) { |
| 9669 | Py_DECREF(string); |
| 9670 | return NULL; |
| 9671 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9672 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9673 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9674 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9675 | if (PyUnicode_IS_ASCII(string)) |
| 9676 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9677 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9678 | PyUnicode_GET_LENGTH(string), keepends); |
| 9679 | else |
| 9680 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9681 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9682 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9683 | break; |
| 9684 | case PyUnicode_2BYTE_KIND: |
| 9685 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9686 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9687 | PyUnicode_GET_LENGTH(string), keepends); |
| 9688 | break; |
| 9689 | case PyUnicode_4BYTE_KIND: |
| 9690 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9691 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9692 | PyUnicode_GET_LENGTH(string), keepends); |
| 9693 | break; |
| 9694 | default: |
| 9695 | assert(0); |
| 9696 | list = 0; |
| 9697 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9698 | Py_DECREF(string); |
| 9699 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9700 | } |
| 9701 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9702 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9703 | split(PyObject *self, |
| 9704 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9705 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9706 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9707 | int kind1, kind2, kind; |
| 9708 | void *buf1, *buf2; |
| 9709 | Py_ssize_t len1, len2; |
| 9710 | PyObject* out; |
| 9711 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9712 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9713 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9714 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9715 | if (PyUnicode_READY(self) == -1) |
| 9716 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9717 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9718 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9719 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9720 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9721 | if (PyUnicode_IS_ASCII(self)) |
| 9722 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9723 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9724 | PyUnicode_GET_LENGTH(self), maxcount |
| 9725 | ); |
| 9726 | else |
| 9727 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9728 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9729 | PyUnicode_GET_LENGTH(self), maxcount |
| 9730 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9731 | case PyUnicode_2BYTE_KIND: |
| 9732 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9733 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9734 | PyUnicode_GET_LENGTH(self), maxcount |
| 9735 | ); |
| 9736 | case PyUnicode_4BYTE_KIND: |
| 9737 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9738 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9739 | PyUnicode_GET_LENGTH(self), maxcount |
| 9740 | ); |
| 9741 | default: |
| 9742 | assert(0); |
| 9743 | return NULL; |
| 9744 | } |
| 9745 | |
| 9746 | if (PyUnicode_READY(substring) == -1) |
| 9747 | return NULL; |
| 9748 | |
| 9749 | kind1 = PyUnicode_KIND(self); |
| 9750 | kind2 = PyUnicode_KIND(substring); |
| 9751 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9752 | buf1 = PyUnicode_DATA(self); |
| 9753 | buf2 = PyUnicode_DATA(substring); |
| 9754 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9755 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | if (!buf1) |
| 9757 | return NULL; |
| 9758 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9759 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9760 | if (!buf2) { |
| 9761 | if (kind1 != kind) PyMem_Free(buf1); |
| 9762 | return NULL; |
| 9763 | } |
| 9764 | len1 = PyUnicode_GET_LENGTH(self); |
| 9765 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9766 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9767 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9768 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9769 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9770 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9771 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9772 | else |
| 9773 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9774 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9775 | break; |
| 9776 | case PyUnicode_2BYTE_KIND: |
| 9777 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9778 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9779 | break; |
| 9780 | case PyUnicode_4BYTE_KIND: |
| 9781 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9782 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9783 | break; |
| 9784 | default: |
| 9785 | out = NULL; |
| 9786 | } |
| 9787 | if (kind1 != kind) |
| 9788 | PyMem_Free(buf1); |
| 9789 | if (kind2 != kind) |
| 9790 | PyMem_Free(buf2); |
| 9791 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9792 | } |
| 9793 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9794 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9795 | rsplit(PyObject *self, |
| 9796 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9797 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9798 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9799 | int kind1, kind2, kind; |
| 9800 | void *buf1, *buf2; |
| 9801 | Py_ssize_t len1, len2; |
| 9802 | PyObject* out; |
| 9803 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9804 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9805 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9807 | if (PyUnicode_READY(self) == -1) |
| 9808 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9809 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9810 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9811 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9812 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9813 | if (PyUnicode_IS_ASCII(self)) |
| 9814 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9815 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9816 | PyUnicode_GET_LENGTH(self), maxcount |
| 9817 | ); |
| 9818 | else |
| 9819 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9820 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9821 | PyUnicode_GET_LENGTH(self), maxcount |
| 9822 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9823 | case PyUnicode_2BYTE_KIND: |
| 9824 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9825 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9826 | PyUnicode_GET_LENGTH(self), maxcount |
| 9827 | ); |
| 9828 | case PyUnicode_4BYTE_KIND: |
| 9829 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9830 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9831 | PyUnicode_GET_LENGTH(self), maxcount |
| 9832 | ); |
| 9833 | default: |
| 9834 | assert(0); |
| 9835 | return NULL; |
| 9836 | } |
| 9837 | |
| 9838 | if (PyUnicode_READY(substring) == -1) |
| 9839 | return NULL; |
| 9840 | |
| 9841 | kind1 = PyUnicode_KIND(self); |
| 9842 | kind2 = PyUnicode_KIND(substring); |
| 9843 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9844 | buf1 = PyUnicode_DATA(self); |
| 9845 | buf2 = PyUnicode_DATA(substring); |
| 9846 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9847 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9848 | if (!buf1) |
| 9849 | return NULL; |
| 9850 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9851 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9852 | if (!buf2) { |
| 9853 | if (kind1 != kind) PyMem_Free(buf1); |
| 9854 | return NULL; |
| 9855 | } |
| 9856 | len1 = PyUnicode_GET_LENGTH(self); |
| 9857 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9858 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9859 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9860 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9861 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9862 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9863 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9864 | else |
| 9865 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9866 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9867 | break; |
| 9868 | case PyUnicode_2BYTE_KIND: |
| 9869 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9870 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9871 | break; |
| 9872 | case PyUnicode_4BYTE_KIND: |
| 9873 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9874 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 | break; |
| 9876 | default: |
| 9877 | out = NULL; |
| 9878 | } |
| 9879 | if (kind1 != kind) |
| 9880 | PyMem_Free(buf1); |
| 9881 | if (kind2 != kind) |
| 9882 | PyMem_Free(buf2); |
| 9883 | return out; |
| 9884 | } |
| 9885 | |
| 9886 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9887 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9888 | 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] | 9889 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9890 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9891 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9892 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9893 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9894 | else |
| 9895 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9896 | case PyUnicode_2BYTE_KIND: |
| 9897 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9898 | case PyUnicode_4BYTE_KIND: |
| 9899 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9900 | } |
| 9901 | assert(0); |
| 9902 | return -1; |
| 9903 | } |
| 9904 | |
| 9905 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9906 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9907 | 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] | 9908 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 9909 | switch (kind) { |
| 9910 | case PyUnicode_1BYTE_KIND: |
| 9911 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9912 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9913 | else |
| 9914 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9915 | case PyUnicode_2BYTE_KIND: |
| 9916 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9917 | case PyUnicode_4BYTE_KIND: |
| 9918 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9919 | } |
| 9920 | assert(0); |
| 9921 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9922 | } |
| 9923 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9924 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9925 | replace(PyObject *self, PyObject *str1, |
| 9926 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9927 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9928 | PyObject *u; |
| 9929 | char *sbuf = PyUnicode_DATA(self); |
| 9930 | char *buf1 = PyUnicode_DATA(str1); |
| 9931 | char *buf2 = PyUnicode_DATA(str2); |
| 9932 | int srelease = 0, release1 = 0, release2 = 0; |
| 9933 | int skind = PyUnicode_KIND(self); |
| 9934 | int kind1 = PyUnicode_KIND(str1); |
| 9935 | int kind2 = PyUnicode_KIND(str2); |
| 9936 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9937 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9938 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9939 | int mayshrink; |
| 9940 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9941 | |
| 9942 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9943 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9944 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9945 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9946 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9947 | if (str1 == str2) |
| 9948 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9949 | if (skind < kind1) |
| 9950 | /* substring too wide to be present */ |
| 9951 | goto nothing; |
| 9952 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9953 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9954 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 9955 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 9956 | result string. */ |
| 9957 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9958 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9959 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9960 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9961 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9962 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9963 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9964 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9965 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9966 | Py_UCS4 u1, u2; |
| 9967 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9968 | Py_ssize_t index, pos; |
| 9969 | char *src; |
| 9970 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 9971 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9972 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 9973 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9974 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 9975 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9976 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9977 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9978 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9979 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9980 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9981 | |
| 9982 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 9983 | index = 0; |
| 9984 | src = sbuf; |
| 9985 | while (--maxcount) |
| 9986 | { |
| 9987 | pos++; |
| 9988 | src += pos * PyUnicode_KIND(self); |
| 9989 | slen -= pos; |
| 9990 | index += pos; |
| 9991 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 9992 | if (pos < 0) |
| 9993 | break; |
| 9994 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 9995 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9996 | } |
| 9997 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | int rkind = skind; |
| 9999 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10000 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10002 | if (kind1 < rkind) { |
| 10003 | /* widen substring */ |
| 10004 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10005 | if (!buf1) goto error; |
| 10006 | release1 = 1; |
| 10007 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10008 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10009 | if (i < 0) |
| 10010 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10011 | if (rkind > kind2) { |
| 10012 | /* widen replacement */ |
| 10013 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10014 | if (!buf2) goto error; |
| 10015 | release2 = 1; |
| 10016 | } |
| 10017 | else if (rkind < kind2) { |
| 10018 | /* widen self and buf1 */ |
| 10019 | rkind = kind2; |
| 10020 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10021 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10022 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10023 | if (!sbuf) goto error; |
| 10024 | srelease = 1; |
| 10025 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10026 | if (!buf1) goto error; |
| 10027 | release1 = 1; |
| 10028 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10029 | u = PyUnicode_New(slen, maxchar); |
| 10030 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10031 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10032 | assert(PyUnicode_KIND(u) == rkind); |
| 10033 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10034 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10035 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10036 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10037 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10038 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10039 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10040 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10041 | |
| 10042 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10043 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10044 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10045 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10046 | if (i == -1) |
| 10047 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10048 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10049 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10050 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10052 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10053 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10054 | } |
| 10055 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10056 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10057 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10058 | int rkind = skind; |
| 10059 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10060 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10061 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10062 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10064 | if (!buf1) goto error; |
| 10065 | release1 = 1; |
| 10066 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10067 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10068 | if (n == 0) |
| 10069 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10070 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10071 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10072 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10073 | if (!buf2) goto error; |
| 10074 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10075 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10076 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10077 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10078 | rkind = kind2; |
| 10079 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10080 | if (!sbuf) goto error; |
| 10081 | srelease = 1; |
| 10082 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10083 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10084 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10085 | if (!buf1) goto error; |
| 10086 | release1 = 1; |
| 10087 | } |
| 10088 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10089 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10090 | 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] | 10091 | PyErr_SetString(PyExc_OverflowError, |
| 10092 | "replace string is too long"); |
| 10093 | goto error; |
| 10094 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10095 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10096 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10097 | _Py_INCREF_UNICODE_EMPTY(); |
| 10098 | if (!unicode_empty) |
| 10099 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10100 | u = unicode_empty; |
| 10101 | goto done; |
| 10102 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10103 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10104 | PyErr_SetString(PyExc_OverflowError, |
| 10105 | "replace string is too long"); |
| 10106 | goto error; |
| 10107 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10108 | u = PyUnicode_New(new_size, maxchar); |
| 10109 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10110 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10111 | assert(PyUnicode_KIND(u) == rkind); |
| 10112 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10113 | ires = i = 0; |
| 10114 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10115 | while (n-- > 0) { |
| 10116 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10117 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10118 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10119 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10120 | if (j == -1) |
| 10121 | break; |
| 10122 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10123 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10124 | memcpy(res + rkind * ires, |
| 10125 | sbuf + rkind * i, |
| 10126 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10127 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10128 | } |
| 10129 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10131 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10133 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10134 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10135 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10136 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10137 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10139 | /* copy tail [i:] */ |
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)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10143 | } |
| 10144 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10145 | /* interleave */ |
| 10146 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10147 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10148 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10149 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10151 | if (--n <= 0) |
| 10152 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10153 | memcpy(res + rkind * ires, |
| 10154 | sbuf + rkind * i, |
| 10155 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10156 | ires++; |
| 10157 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10158 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10159 | memcpy(res + rkind * ires, |
| 10160 | sbuf + rkind * i, |
| 10161 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10162 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10163 | } |
| 10164 | |
| 10165 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10166 | unicode_adjust_maxchar(&u); |
| 10167 | if (u == NULL) |
| 10168 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10169 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10170 | |
| 10171 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | if (srelease) |
| 10173 | PyMem_FREE(sbuf); |
| 10174 | if (release1) |
| 10175 | PyMem_FREE(buf1); |
| 10176 | if (release2) |
| 10177 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10178 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10180 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10181 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10182 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10183 | if (srelease) |
| 10184 | PyMem_FREE(sbuf); |
| 10185 | if (release1) |
| 10186 | PyMem_FREE(buf1); |
| 10187 | if (release2) |
| 10188 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10189 | return unicode_result_unchanged(self); |
| 10190 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | error: |
| 10192 | if (srelease && sbuf) |
| 10193 | PyMem_FREE(sbuf); |
| 10194 | if (release1 && buf1) |
| 10195 | PyMem_FREE(buf1); |
| 10196 | if (release2 && buf2) |
| 10197 | PyMem_FREE(buf2); |
| 10198 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10199 | } |
| 10200 | |
| 10201 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10203 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10204 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10205 | \n\ |
| 10206 | 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] | 10207 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10208 | |
| 10209 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10210 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10211 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10212 | if (PyUnicode_READY(self) == -1) |
| 10213 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10214 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10215 | } |
| 10216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10217 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10218 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10219 | \n\ |
| 10220 | 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] | 10221 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10222 | |
| 10223 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10224 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10225 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10226 | if (PyUnicode_READY(self) == -1) |
| 10227 | return NULL; |
| 10228 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10229 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10230 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10231 | } |
| 10232 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10233 | PyDoc_STRVAR(casefold__doc__, |
| 10234 | "S.casefold() -> str\n\ |
| 10235 | \n\ |
| 10236 | Return a version of S suitable for caseless comparisons."); |
| 10237 | |
| 10238 | static PyObject * |
| 10239 | unicode_casefold(PyObject *self) |
| 10240 | { |
| 10241 | if (PyUnicode_READY(self) == -1) |
| 10242 | return NULL; |
| 10243 | if (PyUnicode_IS_ASCII(self)) |
| 10244 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10245 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10246 | } |
| 10247 | |
| 10248 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10249 | /* Argument converter. Coerces to a single unicode character */ |
| 10250 | |
| 10251 | static int |
| 10252 | convert_uc(PyObject *obj, void *addr) |
| 10253 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10254 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10255 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10256 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10257 | uniobj = PyUnicode_FromObject(obj); |
| 10258 | if (uniobj == NULL) { |
| 10259 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10260 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10261 | return 0; |
| 10262 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10263 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10264 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10265 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10266 | Py_DECREF(uniobj); |
| 10267 | return 0; |
| 10268 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10269 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10270 | Py_DECREF(uniobj); |
| 10271 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10272 | } |
| 10273 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10274 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10275 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10276 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10277 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10278 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10279 | |
| 10280 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10281 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10282 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10283 | Py_ssize_t marg, left; |
| 10284 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | Py_UCS4 fillchar = ' '; |
| 10286 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10287 | 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] | 10288 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10289 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10290 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10291 | return NULL; |
| 10292 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10293 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10294 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10295 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10296 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10297 | left = marg / 2 + (marg & width & 1); |
| 10298 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10299 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10300 | } |
| 10301 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10302 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10303 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10304 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10305 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10306 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10307 | #define COMPARE(TYPE1, TYPE2) \ |
| 10308 | do { \ |
| 10309 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10310 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10311 | TYPE1* end = p1 + len; \ |
| 10312 | Py_UCS4 c1, c2; \ |
| 10313 | for (; p1 != end; p1++, p2++) { \ |
| 10314 | c1 = *p1; \ |
| 10315 | c2 = *p2; \ |
| 10316 | if (c1 != c2) \ |
| 10317 | return (c1 < c2) ? -1 : 1; \ |
| 10318 | } \ |
| 10319 | } \ |
| 10320 | while (0) |
| 10321 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10322 | int kind1, kind2; |
| 10323 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10324 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10325 | |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10326 | /* a string is equal to itself */ |
| 10327 | if (str1 == str2) |
| 10328 | return 0; |
| 10329 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10330 | kind1 = PyUnicode_KIND(str1); |
| 10331 | kind2 = PyUnicode_KIND(str2); |
| 10332 | data1 = PyUnicode_DATA(str1); |
| 10333 | data2 = PyUnicode_DATA(str2); |
| 10334 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10335 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10336 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10337 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10338 | switch(kind1) { |
| 10339 | case PyUnicode_1BYTE_KIND: |
| 10340 | { |
| 10341 | switch(kind2) { |
| 10342 | case PyUnicode_1BYTE_KIND: |
| 10343 | { |
| 10344 | int cmp = memcmp(data1, data2, len); |
| 10345 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10346 | if (cmp < 0) |
| 10347 | return -1; |
| 10348 | if (cmp > 0) |
| 10349 | return 1; |
| 10350 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10351 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10352 | case PyUnicode_2BYTE_KIND: |
| 10353 | COMPARE(Py_UCS1, Py_UCS2); |
| 10354 | break; |
| 10355 | case PyUnicode_4BYTE_KIND: |
| 10356 | COMPARE(Py_UCS1, Py_UCS4); |
| 10357 | break; |
| 10358 | default: |
| 10359 | assert(0); |
| 10360 | } |
| 10361 | break; |
| 10362 | } |
| 10363 | case PyUnicode_2BYTE_KIND: |
| 10364 | { |
| 10365 | switch(kind2) { |
| 10366 | case PyUnicode_1BYTE_KIND: |
| 10367 | COMPARE(Py_UCS2, Py_UCS1); |
| 10368 | break; |
| 10369 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10370 | { |
| 10371 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 2 |
| 10372 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10373 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10374 | if (cmp < 0) |
| 10375 | return -1; |
| 10376 | if (cmp > 0) |
| 10377 | return 1; |
| 10378 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10379 | COMPARE(Py_UCS2, Py_UCS2); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10380 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10381 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10382 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10383 | case PyUnicode_4BYTE_KIND: |
| 10384 | COMPARE(Py_UCS2, Py_UCS4); |
| 10385 | break; |
| 10386 | default: |
| 10387 | assert(0); |
| 10388 | } |
| 10389 | break; |
| 10390 | } |
| 10391 | case PyUnicode_4BYTE_KIND: |
| 10392 | { |
| 10393 | switch(kind2) { |
| 10394 | case PyUnicode_1BYTE_KIND: |
| 10395 | COMPARE(Py_UCS4, Py_UCS1); |
| 10396 | break; |
| 10397 | case PyUnicode_2BYTE_KIND: |
| 10398 | COMPARE(Py_UCS4, Py_UCS2); |
| 10399 | break; |
| 10400 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10401 | { |
| 10402 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10403 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10404 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10405 | if (cmp < 0) |
| 10406 | return -1; |
| 10407 | if (cmp > 0) |
| 10408 | return 1; |
| 10409 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10410 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10411 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10412 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10413 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10414 | default: |
| 10415 | assert(0); |
| 10416 | } |
| 10417 | break; |
| 10418 | } |
| 10419 | default: |
| 10420 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10421 | } |
| 10422 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10423 | if (len1 == len2) |
| 10424 | return 0; |
| 10425 | if (len1 < len2) |
| 10426 | return -1; |
| 10427 | else |
| 10428 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10429 | |
| 10430 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10431 | } |
| 10432 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10433 | static int |
| 10434 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10435 | { |
| 10436 | int kind; |
| 10437 | void *data1, *data2; |
| 10438 | Py_ssize_t len; |
| 10439 | int cmp; |
| 10440 | |
| 10441 | /* a string is equal to itself */ |
| 10442 | if (str1 == str2) |
| 10443 | return 1; |
| 10444 | |
| 10445 | len = PyUnicode_GET_LENGTH(str1); |
| 10446 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10447 | return 0; |
| 10448 | kind = PyUnicode_KIND(str1); |
| 10449 | if (PyUnicode_KIND(str2) != kind) |
| 10450 | return 0; |
| 10451 | data1 = PyUnicode_DATA(str1); |
| 10452 | data2 = PyUnicode_DATA(str2); |
| 10453 | |
| 10454 | cmp = memcmp(data1, data2, len * kind); |
| 10455 | return (cmp == 0); |
| 10456 | } |
| 10457 | |
| 10458 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10459 | int |
| 10460 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10461 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10462 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10463 | if (PyUnicode_READY(left) == -1 || |
| 10464 | PyUnicode_READY(right) == -1) |
| 10465 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10466 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10467 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10468 | PyErr_Format(PyExc_TypeError, |
| 10469 | "Can't compare %.100s and %.100s", |
| 10470 | left->ob_type->tp_name, |
| 10471 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10472 | return -1; |
| 10473 | } |
| 10474 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10475 | int |
| 10476 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10477 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10478 | Py_ssize_t i; |
| 10479 | int kind; |
| 10480 | void *data; |
| 10481 | Py_UCS4 chr; |
| 10482 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10483 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10484 | if (PyUnicode_READY(uni) == -1) |
| 10485 | return -1; |
| 10486 | kind = PyUnicode_KIND(uni); |
| 10487 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10488 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10490 | if (chr != str[i]) |
| 10491 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10492 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10493 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10495 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10496 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10497 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10498 | return 0; |
| 10499 | } |
| 10500 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10501 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10502 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10503 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10504 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10505 | PyObject * |
| 10506 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10507 | { |
| 10508 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10509 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10510 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10511 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10512 | Py_RETURN_NOTIMPLEMENTED; |
| 10513 | |
| 10514 | if (PyUnicode_READY(left) == -1 || |
| 10515 | PyUnicode_READY(right) == -1) |
| 10516 | return NULL; |
| 10517 | |
| 10518 | if (op == Py_EQ || op == Py_NE) { |
| 10519 | result = unicode_compare_eq(left, right); |
| 10520 | if (op == Py_EQ) |
| 10521 | v = TEST_COND(result); |
| 10522 | else |
| 10523 | v = TEST_COND(!result); |
| 10524 | } |
| 10525 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10526 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10527 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10528 | /* Convert the return value to a Boolean */ |
| 10529 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10530 | case Py_LE: |
| 10531 | v = TEST_COND(result <= 0); |
| 10532 | break; |
| 10533 | case Py_GE: |
| 10534 | v = TEST_COND(result >= 0); |
| 10535 | break; |
| 10536 | case Py_LT: |
| 10537 | v = TEST_COND(result == -1); |
| 10538 | break; |
| 10539 | case Py_GT: |
| 10540 | v = TEST_COND(result == 1); |
| 10541 | break; |
| 10542 | default: |
| 10543 | PyErr_BadArgument(); |
| 10544 | return NULL; |
| 10545 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10546 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10547 | Py_INCREF(v); |
| 10548 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10549 | } |
| 10550 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10551 | int |
| 10552 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10553 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10554 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10555 | int kind1, kind2, kind; |
| 10556 | void *buf1, *buf2; |
| 10557 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10558 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10559 | |
| 10560 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10561 | sub = PyUnicode_FromObject(element); |
| 10562 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10563 | PyErr_Format(PyExc_TypeError, |
| 10564 | "'in <string>' requires string as left operand, not %s", |
| 10565 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10566 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10567 | } |
| 10568 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10569 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10570 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10571 | Py_DECREF(sub); |
| 10572 | return -1; |
| 10573 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10574 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10575 | Py_DECREF(sub); |
| 10576 | Py_DECREF(str); |
| 10577 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10578 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10579 | kind1 = PyUnicode_KIND(str); |
| 10580 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10581 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10582 | buf1 = PyUnicode_DATA(str); |
| 10583 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10584 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10585 | if (kind2 > kind) { |
| 10586 | Py_DECREF(sub); |
| 10587 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10588 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10589 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10590 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10591 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10592 | if (!buf2) { |
| 10593 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10594 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10595 | return -1; |
| 10596 | } |
| 10597 | len1 = PyUnicode_GET_LENGTH(str); |
| 10598 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10599 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10600 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10601 | case PyUnicode_1BYTE_KIND: |
| 10602 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10603 | break; |
| 10604 | case PyUnicode_2BYTE_KIND: |
| 10605 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10606 | break; |
| 10607 | case PyUnicode_4BYTE_KIND: |
| 10608 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10609 | break; |
| 10610 | default: |
| 10611 | result = -1; |
| 10612 | assert(0); |
| 10613 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10614 | |
| 10615 | Py_DECREF(str); |
| 10616 | Py_DECREF(sub); |
| 10617 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | if (kind2 != kind) |
| 10619 | PyMem_Free(buf2); |
| 10620 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10621 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10622 | } |
| 10623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10624 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10625 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10626 | PyObject * |
| 10627 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10628 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10629 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10630 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10631 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10632 | |
| 10633 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10634 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10635 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10636 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10637 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10638 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10639 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10640 | |
| 10641 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10642 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10643 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10644 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10645 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10646 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10647 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10648 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10649 | } |
| 10650 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10651 | u_len = PyUnicode_GET_LENGTH(u); |
| 10652 | v_len = PyUnicode_GET_LENGTH(v); |
| 10653 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10654 | PyErr_SetString(PyExc_OverflowError, |
| 10655 | "strings are too large to concat"); |
| 10656 | goto onError; |
| 10657 | } |
| 10658 | new_len = u_len + v_len; |
| 10659 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10660 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10661 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10662 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10665 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10666 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10667 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10668 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10669 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10670 | Py_DECREF(u); |
| 10671 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10672 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10673 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10674 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10675 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10676 | Py_XDECREF(u); |
| 10677 | Py_XDECREF(v); |
| 10678 | return NULL; |
| 10679 | } |
| 10680 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10681 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10682 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10683 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10684 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10685 | Py_UCS4 maxchar, maxchar2; |
| 10686 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10687 | |
| 10688 | if (p_left == NULL) { |
| 10689 | if (!PyErr_Occurred()) |
| 10690 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10691 | return; |
| 10692 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10693 | left = *p_left; |
Serhiy Storchaka | 6c83e73 | 2013-01-04 12:39:34 +0200 | [diff] [blame] | 10694 | if (right == NULL || left == NULL || !PyUnicode_Check(left)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10695 | if (!PyErr_Occurred()) |
| 10696 | PyErr_BadInternalCall(); |
| 10697 | goto error; |
| 10698 | } |
| 10699 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10700 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10701 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10702 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10703 | goto error; |
| 10704 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10705 | /* Shortcuts */ |
| 10706 | if (left == unicode_empty) { |
| 10707 | Py_DECREF(left); |
| 10708 | Py_INCREF(right); |
| 10709 | *p_left = right; |
| 10710 | return; |
| 10711 | } |
| 10712 | if (right == unicode_empty) |
| 10713 | return; |
| 10714 | |
| 10715 | left_len = PyUnicode_GET_LENGTH(left); |
| 10716 | right_len = PyUnicode_GET_LENGTH(right); |
| 10717 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10718 | PyErr_SetString(PyExc_OverflowError, |
| 10719 | "strings are too large to concat"); |
| 10720 | goto error; |
| 10721 | } |
| 10722 | new_len = left_len + right_len; |
| 10723 | |
| 10724 | if (unicode_modifiable(left) |
| 10725 | && PyUnicode_CheckExact(right) |
| 10726 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10727 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10728 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10729 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10730 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10731 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10732 | { |
| 10733 | /* append inplace */ |
| 10734 | if (unicode_resize(p_left, new_len) != 0) { |
| 10735 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10736 | * deallocated so it cannot be put back into |
| 10737 | * 'variable'. The MemoryError is raised when there |
| 10738 | * is no value in 'variable', which might (very |
| 10739 | * remotely) be a cause of incompatibilities. |
| 10740 | */ |
| 10741 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10742 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10743 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10744 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10745 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10746 | else { |
| 10747 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10748 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10749 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10750 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10751 | /* Concat the two Unicode strings */ |
| 10752 | res = PyUnicode_New(new_len, maxchar); |
| 10753 | if (res == NULL) |
| 10754 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10755 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10756 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10757 | Py_DECREF(left); |
| 10758 | *p_left = res; |
| 10759 | } |
| 10760 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10761 | return; |
| 10762 | |
| 10763 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10764 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10765 | } |
| 10766 | |
| 10767 | void |
| 10768 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10769 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10770 | PyUnicode_Append(pleft, right); |
| 10771 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10772 | } |
| 10773 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10774 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10775 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10776 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10777 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10778 | 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] | 10779 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10780 | |
| 10781 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10782 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10783 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10784 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10785 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10786 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10788 | int kind1, kind2, kind; |
| 10789 | void *buf1, *buf2; |
| 10790 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10791 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10792 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10793 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10794 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10795 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10796 | kind1 = PyUnicode_KIND(self); |
| 10797 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10798 | if (kind2 > kind1) |
| 10799 | return PyLong_FromLong(0); |
| 10800 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10801 | buf1 = PyUnicode_DATA(self); |
| 10802 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10803 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10804 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10805 | if (!buf2) { |
| 10806 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10807 | return NULL; |
| 10808 | } |
| 10809 | len1 = PyUnicode_GET_LENGTH(self); |
| 10810 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10811 | |
| 10812 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10813 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10814 | case PyUnicode_1BYTE_KIND: |
| 10815 | iresult = ucs1lib_count( |
| 10816 | ((Py_UCS1*)buf1) + start, end - start, |
| 10817 | buf2, len2, PY_SSIZE_T_MAX |
| 10818 | ); |
| 10819 | break; |
| 10820 | case PyUnicode_2BYTE_KIND: |
| 10821 | iresult = ucs2lib_count( |
| 10822 | ((Py_UCS2*)buf1) + start, end - start, |
| 10823 | buf2, len2, PY_SSIZE_T_MAX |
| 10824 | ); |
| 10825 | break; |
| 10826 | case PyUnicode_4BYTE_KIND: |
| 10827 | iresult = ucs4lib_count( |
| 10828 | ((Py_UCS4*)buf1) + start, end - start, |
| 10829 | buf2, len2, PY_SSIZE_T_MAX |
| 10830 | ); |
| 10831 | break; |
| 10832 | default: |
| 10833 | assert(0); iresult = 0; |
| 10834 | } |
| 10835 | |
| 10836 | result = PyLong_FromSsize_t(iresult); |
| 10837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10838 | if (kind2 != kind) |
| 10839 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10840 | |
| 10841 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10842 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10843 | return result; |
| 10844 | } |
| 10845 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10846 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10847 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10848 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10849 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10850 | 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] | 10851 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10852 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10853 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10854 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10855 | |
| 10856 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10857 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10858 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10859 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10860 | char *encoding = NULL; |
| 10861 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10862 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10863 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10864 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10865 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10866 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10867 | } |
| 10868 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10869 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10870 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10871 | \n\ |
| 10872 | 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] | 10873 | 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] | 10874 | |
| 10875 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10876 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10877 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10878 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10879 | Py_UCS4 ch; |
| 10880 | PyObject *u; |
| 10881 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10882 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10883 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10884 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10885 | |
| 10886 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10887 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10888 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10889 | if (PyUnicode_READY(self) == -1) |
| 10890 | return NULL; |
| 10891 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10892 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10893 | src_len = PyUnicode_GET_LENGTH(self); |
| 10894 | i = j = line_pos = 0; |
| 10895 | kind = PyUnicode_KIND(self); |
| 10896 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10897 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10898 | for (; i < src_len; i++) { |
| 10899 | ch = PyUnicode_READ(kind, src_data, i); |
| 10900 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10901 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10902 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10903 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10904 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10905 | goto overflow; |
| 10906 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10907 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10908 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10909 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10910 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10911 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10912 | goto overflow; |
| 10913 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10914 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10915 | if (ch == '\n' || ch == '\r') |
| 10916 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10917 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10918 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10919 | if (!found) |
| 10920 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10921 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10922 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10923 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10924 | if (!u) |
| 10925 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10926 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10927 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10928 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10929 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10930 | for (; i < src_len; i++) { |
| 10931 | ch = PyUnicode_READ(kind, src_data, i); |
| 10932 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10933 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10934 | incr = tabsize - (line_pos % tabsize); |
| 10935 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10936 | FILL(kind, dest_data, ' ', j, incr); |
| 10937 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10938 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10939 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10940 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10941 | line_pos++; |
| 10942 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10943 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10944 | if (ch == '\n' || ch == '\r') |
| 10945 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10946 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10947 | } |
| 10948 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10949 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10950 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10951 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10952 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10953 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10954 | } |
| 10955 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10956 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10957 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10958 | \n\ |
| 10959 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10960 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10961 | arguments start and end are interpreted as in slice notation.\n\ |
| 10962 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10963 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10964 | |
| 10965 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10966 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10967 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10968 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10969 | Py_ssize_t start; |
| 10970 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10971 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10972 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10973 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10974 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10976 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10977 | if (PyUnicode_READY(self) == -1) |
| 10978 | return NULL; |
| 10979 | if (PyUnicode_READY(substring) == -1) |
| 10980 | return NULL; |
| 10981 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10982 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10983 | |
| 10984 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10985 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10986 | if (result == -2) |
| 10987 | return NULL; |
| 10988 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10989 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10990 | } |
| 10991 | |
| 10992 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10993 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10995 | void *data; |
| 10996 | enum PyUnicode_Kind kind; |
| 10997 | Py_UCS4 ch; |
| 10998 | PyObject *res; |
| 10999 | |
| 11000 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11001 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11002 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11003 | } |
| 11004 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11005 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11006 | return NULL; |
| 11007 | } |
| 11008 | kind = PyUnicode_KIND(self); |
| 11009 | data = PyUnicode_DATA(self); |
| 11010 | ch = PyUnicode_READ(kind, data, index); |
| 11011 | if (ch < 256) |
| 11012 | return get_latin1_char(ch); |
| 11013 | |
| 11014 | res = PyUnicode_New(1, ch); |
| 11015 | if (res == NULL) |
| 11016 | return NULL; |
| 11017 | kind = PyUnicode_KIND(res); |
| 11018 | data = PyUnicode_DATA(res); |
| 11019 | PyUnicode_WRITE(kind, data, 0, ch); |
| 11020 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 11021 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11022 | } |
| 11023 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11024 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11025 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11026 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11027 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11028 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11029 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11030 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11031 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11032 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11033 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11034 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11035 | if (_PyUnicode_HASH(self) != -1) |
| 11036 | return _PyUnicode_HASH(self); |
| 11037 | if (PyUnicode_READY(self) == -1) |
| 11038 | return -1; |
| 11039 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11040 | /* |
| 11041 | We make the hash of the empty string be 0, rather than using |
| 11042 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11043 | */ |
| 11044 | if (len == 0) { |
| 11045 | _PyUnicode_HASH(self) = 0; |
| 11046 | return 0; |
| 11047 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11048 | |
| 11049 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11050 | #define HASH(P) \ |
| 11051 | x ^= (Py_uhash_t) *P << 7; \ |
| 11052 | while (--len >= 0) \ |
| 11053 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11054 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11055 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11056 | switch (PyUnicode_KIND(self)) { |
| 11057 | case PyUnicode_1BYTE_KIND: { |
| 11058 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 11059 | HASH(c); |
| 11060 | break; |
| 11061 | } |
| 11062 | case PyUnicode_2BYTE_KIND: { |
| 11063 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 11064 | HASH(s); |
| 11065 | break; |
| 11066 | } |
| 11067 | default: { |
| 11068 | Py_UCS4 *l; |
| 11069 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 11070 | "Impossible switch case in unicode_hash"); |
| 11071 | l = PyUnicode_4BYTE_DATA(self); |
| 11072 | HASH(l); |
| 11073 | break; |
| 11074 | } |
| 11075 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 11076 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 11077 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11078 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11079 | if (x == -1) |
| 11080 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11081 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11082 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11083 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11084 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11086 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11087 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11088 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11089 | 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] | 11090 | |
| 11091 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11092 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11093 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11094 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11095 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11096 | Py_ssize_t start; |
| 11097 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11098 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11099 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11100 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11101 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11102 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11103 | if (PyUnicode_READY(self) == -1) |
| 11104 | return NULL; |
| 11105 | if (PyUnicode_READY(substring) == -1) |
| 11106 | return NULL; |
| 11107 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11108 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11109 | |
| 11110 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11111 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11112 | if (result == -2) |
| 11113 | return NULL; |
| 11114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | if (result < 0) { |
| 11116 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11117 | return NULL; |
| 11118 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11119 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11120 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11121 | } |
| 11122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11123 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11124 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11125 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11126 | 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] | 11127 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11128 | |
| 11129 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11130 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11131 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11132 | Py_ssize_t i, length; |
| 11133 | int kind; |
| 11134 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11135 | int cased; |
| 11136 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11137 | if (PyUnicode_READY(self) == -1) |
| 11138 | return NULL; |
| 11139 | length = PyUnicode_GET_LENGTH(self); |
| 11140 | kind = PyUnicode_KIND(self); |
| 11141 | data = PyUnicode_DATA(self); |
| 11142 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11143 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11144 | if (length == 1) |
| 11145 | return PyBool_FromLong( |
| 11146 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11147 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11148 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11149 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11150 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11152 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11153 | for (i = 0; i < length; i++) { |
| 11154 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11155 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11156 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11157 | return PyBool_FromLong(0); |
| 11158 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11159 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11160 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11161 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11162 | } |
| 11163 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11164 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11165 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11166 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11167 | 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] | 11168 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | |
| 11170 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11171 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11173 | Py_ssize_t i, length; |
| 11174 | int kind; |
| 11175 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | int cased; |
| 11177 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11178 | if (PyUnicode_READY(self) == -1) |
| 11179 | return NULL; |
| 11180 | length = PyUnicode_GET_LENGTH(self); |
| 11181 | kind = PyUnicode_KIND(self); |
| 11182 | data = PyUnicode_DATA(self); |
| 11183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11185 | if (length == 1) |
| 11186 | return PyBool_FromLong( |
| 11187 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11188 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11189 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11190 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11191 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11192 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11194 | for (i = 0; i < length; i++) { |
| 11195 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11196 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11197 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11198 | return PyBool_FromLong(0); |
| 11199 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11200 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11201 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11202 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11203 | } |
| 11204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11205 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11206 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11208 | Return True if S is a titlecased string and there is at least one\n\ |
| 11209 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11210 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11211 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11212 | |
| 11213 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11214 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11215 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11216 | Py_ssize_t i, length; |
| 11217 | int kind; |
| 11218 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11219 | int cased, previous_is_cased; |
| 11220 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11221 | if (PyUnicode_READY(self) == -1) |
| 11222 | return NULL; |
| 11223 | length = PyUnicode_GET_LENGTH(self); |
| 11224 | kind = PyUnicode_KIND(self); |
| 11225 | data = PyUnicode_DATA(self); |
| 11226 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11227 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11228 | if (length == 1) { |
| 11229 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11230 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11231 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11232 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11233 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11234 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11235 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11236 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 | cased = 0; |
| 11239 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11240 | for (i = 0; i < length; i++) { |
| 11241 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11242 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11243 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11244 | if (previous_is_cased) |
| 11245 | return PyBool_FromLong(0); |
| 11246 | previous_is_cased = 1; |
| 11247 | cased = 1; |
| 11248 | } |
| 11249 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11250 | if (!previous_is_cased) |
| 11251 | return PyBool_FromLong(0); |
| 11252 | previous_is_cased = 1; |
| 11253 | cased = 1; |
| 11254 | } |
| 11255 | else |
| 11256 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11258 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11259 | } |
| 11260 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11261 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11262 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11263 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11264 | Return True if all characters in S are whitespace\n\ |
| 11265 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11266 | |
| 11267 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11268 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11269 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11270 | Py_ssize_t i, length; |
| 11271 | int kind; |
| 11272 | void *data; |
| 11273 | |
| 11274 | if (PyUnicode_READY(self) == -1) |
| 11275 | return NULL; |
| 11276 | length = PyUnicode_GET_LENGTH(self); |
| 11277 | kind = PyUnicode_KIND(self); |
| 11278 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11280 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11281 | if (length == 1) |
| 11282 | return PyBool_FromLong( |
| 11283 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11284 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11285 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11286 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11287 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11288 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11289 | for (i = 0; i < length; i++) { |
| 11290 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11291 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11292 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11294 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11295 | } |
| 11296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11297 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11298 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11299 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11300 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11301 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11302 | |
| 11303 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11304 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11305 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11306 | Py_ssize_t i, length; |
| 11307 | int kind; |
| 11308 | void *data; |
| 11309 | |
| 11310 | if (PyUnicode_READY(self) == -1) |
| 11311 | return NULL; |
| 11312 | length = PyUnicode_GET_LENGTH(self); |
| 11313 | kind = PyUnicode_KIND(self); |
| 11314 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11315 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11316 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11317 | if (length == 1) |
| 11318 | return PyBool_FromLong( |
| 11319 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11320 | |
| 11321 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11322 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11323 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11324 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11325 | for (i = 0; i < length; i++) { |
| 11326 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11327 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11328 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11329 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11330 | } |
| 11331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11332 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11333 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11334 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11335 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11336 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11337 | |
| 11338 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11339 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11340 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11341 | int kind; |
| 11342 | void *data; |
| 11343 | Py_ssize_t len, i; |
| 11344 | |
| 11345 | if (PyUnicode_READY(self) == -1) |
| 11346 | return NULL; |
| 11347 | |
| 11348 | kind = PyUnicode_KIND(self); |
| 11349 | data = PyUnicode_DATA(self); |
| 11350 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11351 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11352 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11353 | if (len == 1) { |
| 11354 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11355 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11356 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11357 | |
| 11358 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11359 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11360 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11361 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | for (i = 0; i < len; i++) { |
| 11363 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11364 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11365 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11366 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11367 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11368 | } |
| 11369 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11370 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11371 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11372 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11373 | 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] | 11374 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | |
| 11376 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11377 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11378 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11379 | Py_ssize_t i, length; |
| 11380 | int kind; |
| 11381 | void *data; |
| 11382 | |
| 11383 | if (PyUnicode_READY(self) == -1) |
| 11384 | return NULL; |
| 11385 | length = PyUnicode_GET_LENGTH(self); |
| 11386 | kind = PyUnicode_KIND(self); |
| 11387 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11389 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11390 | if (length == 1) |
| 11391 | return PyBool_FromLong( |
| 11392 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11393 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11394 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11395 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11396 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11397 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11398 | for (i = 0; i < length; i++) { |
| 11399 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11400 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11401 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11402 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11403 | } |
| 11404 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11405 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11406 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11407 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11408 | Return True if all characters in S are digits\n\ |
| 11409 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11410 | |
| 11411 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11412 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11413 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11414 | Py_ssize_t i, length; |
| 11415 | int kind; |
| 11416 | void *data; |
| 11417 | |
| 11418 | if (PyUnicode_READY(self) == -1) |
| 11419 | return NULL; |
| 11420 | length = PyUnicode_GET_LENGTH(self); |
| 11421 | kind = PyUnicode_KIND(self); |
| 11422 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11424 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11425 | if (length == 1) { |
| 11426 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11427 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11428 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11430 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11431 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11432 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11433 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11434 | for (i = 0; i < length; i++) { |
| 11435 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11436 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11437 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11438 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | } |
| 11440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11441 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11442 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11444 | 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] | 11445 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11446 | |
| 11447 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11448 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11450 | Py_ssize_t i, length; |
| 11451 | int kind; |
| 11452 | void *data; |
| 11453 | |
| 11454 | if (PyUnicode_READY(self) == -1) |
| 11455 | return NULL; |
| 11456 | length = PyUnicode_GET_LENGTH(self); |
| 11457 | kind = PyUnicode_KIND(self); |
| 11458 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11461 | if (length == 1) |
| 11462 | return PyBool_FromLong( |
| 11463 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11464 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11465 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11466 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11467 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11469 | for (i = 0; i < length; i++) { |
| 11470 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11471 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11472 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11473 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11474 | } |
| 11475 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11476 | int |
| 11477 | PyUnicode_IsIdentifier(PyObject *self) |
| 11478 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11479 | int kind; |
| 11480 | void *data; |
| 11481 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11482 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11484 | if (PyUnicode_READY(self) == -1) { |
| 11485 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11486 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11487 | } |
| 11488 | |
| 11489 | /* Special case for empty strings */ |
| 11490 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11491 | return 0; |
| 11492 | kind = PyUnicode_KIND(self); |
| 11493 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11494 | |
| 11495 | /* PEP 3131 says that the first character must be in |
| 11496 | XID_Start and subsequent characters in XID_Continue, |
| 11497 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11498 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11499 | letters, digits, underscore). However, given the current |
| 11500 | definition of XID_Start and XID_Continue, it is sufficient |
| 11501 | to check just for these, except that _ must be allowed |
| 11502 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11503 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11504 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11505 | return 0; |
| 11506 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11507 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11508 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11509 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11510 | return 1; |
| 11511 | } |
| 11512 | |
| 11513 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11514 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11515 | \n\ |
| 11516 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11517 | to the language definition.\n\ |
| 11518 | \n\ |
| 11519 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11520 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11521 | |
| 11522 | static PyObject* |
| 11523 | unicode_isidentifier(PyObject *self) |
| 11524 | { |
| 11525 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11526 | } |
| 11527 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11528 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11529 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11530 | \n\ |
| 11531 | Return True if all characters in S are considered\n\ |
| 11532 | printable in repr() or S is empty, False otherwise."); |
| 11533 | |
| 11534 | static PyObject* |
| 11535 | unicode_isprintable(PyObject *self) |
| 11536 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11537 | Py_ssize_t i, length; |
| 11538 | int kind; |
| 11539 | void *data; |
| 11540 | |
| 11541 | if (PyUnicode_READY(self) == -1) |
| 11542 | return NULL; |
| 11543 | length = PyUnicode_GET_LENGTH(self); |
| 11544 | kind = PyUnicode_KIND(self); |
| 11545 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11546 | |
| 11547 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11548 | if (length == 1) |
| 11549 | return PyBool_FromLong( |
| 11550 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11552 | for (i = 0; i < length; i++) { |
| 11553 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11554 | Py_RETURN_FALSE; |
| 11555 | } |
| 11556 | } |
| 11557 | Py_RETURN_TRUE; |
| 11558 | } |
| 11559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11560 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11561 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | \n\ |
| 11563 | 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] | 11564 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | |
| 11566 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11567 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11568 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11569 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11570 | } |
| 11571 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11572 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11573 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11574 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11575 | if (PyUnicode_READY(self) == -1) |
| 11576 | return -1; |
| 11577 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11578 | } |
| 11579 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11580 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11581 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11582 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11583 | 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] | 11584 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11585 | |
| 11586 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11587 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11588 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11589 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11590 | Py_UCS4 fillchar = ' '; |
| 11591 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11592 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11593 | return NULL; |
| 11594 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11595 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11596 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11597 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11598 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11599 | return unicode_result_unchanged(self); |
| 11600 | |
| 11601 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11602 | } |
| 11603 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11604 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11605 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11606 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11607 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11608 | |
| 11609 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11610 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11612 | if (PyUnicode_READY(self) == -1) |
| 11613 | return NULL; |
| 11614 | if (PyUnicode_IS_ASCII(self)) |
| 11615 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11616 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11617 | } |
| 11618 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11619 | #define LEFTSTRIP 0 |
| 11620 | #define RIGHTSTRIP 1 |
| 11621 | #define BOTHSTRIP 2 |
| 11622 | |
| 11623 | /* Arrays indexed by above */ |
| 11624 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11625 | |
| 11626 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11627 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11628 | /* externally visible for str.strip(unicode) */ |
| 11629 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11630 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11631 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11632 | void *data; |
| 11633 | int kind; |
| 11634 | Py_ssize_t i, j, len; |
| 11635 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11637 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11638 | return NULL; |
| 11639 | |
| 11640 | kind = PyUnicode_KIND(self); |
| 11641 | data = PyUnicode_DATA(self); |
| 11642 | len = PyUnicode_GET_LENGTH(self); |
| 11643 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11644 | PyUnicode_DATA(sepobj), |
| 11645 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11646 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11647 | i = 0; |
| 11648 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11649 | while (i < len && |
| 11650 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11651 | i++; |
| 11652 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11653 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11654 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11655 | j = len; |
| 11656 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11657 | do { |
| 11658 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11659 | } while (j >= i && |
| 11660 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11661 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11662 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11663 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11664 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11665 | } |
| 11666 | |
| 11667 | PyObject* |
| 11668 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11669 | { |
| 11670 | unsigned char *data; |
| 11671 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11672 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11673 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11674 | if (PyUnicode_READY(self) == -1) |
| 11675 | return NULL; |
| 11676 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11677 | length = PyUnicode_GET_LENGTH(self); |
| 11678 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11679 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11680 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11681 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11682 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11683 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11684 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11685 | return NULL; |
| 11686 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11687 | if (start >= length || end < start) |
| 11688 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11689 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11690 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11691 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11692 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11693 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11694 | } |
| 11695 | else { |
| 11696 | kind = PyUnicode_KIND(self); |
| 11697 | data = PyUnicode_1BYTE_DATA(self); |
| 11698 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11699 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11700 | length); |
| 11701 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11702 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11703 | |
| 11704 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11705 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11706 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11707 | int kind; |
| 11708 | void *data; |
| 11709 | Py_ssize_t len, i, j; |
| 11710 | |
| 11711 | if (PyUnicode_READY(self) == -1) |
| 11712 | return NULL; |
| 11713 | |
| 11714 | kind = PyUnicode_KIND(self); |
| 11715 | data = PyUnicode_DATA(self); |
| 11716 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11717 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11718 | i = 0; |
| 11719 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11720 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11721 | i++; |
| 11722 | } |
| 11723 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11724 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11725 | j = len; |
| 11726 | if (striptype != LEFTSTRIP) { |
| 11727 | do { |
| 11728 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11729 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11730 | j++; |
| 11731 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11732 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11733 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11734 | } |
| 11735 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11736 | |
| 11737 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11738 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11739 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11740 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11741 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11742 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11743 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11744 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11745 | if (sep != NULL && sep != Py_None) { |
| 11746 | if (PyUnicode_Check(sep)) |
| 11747 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11748 | else { |
| 11749 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11750 | "%s arg must be None or str", |
| 11751 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11752 | return NULL; |
| 11753 | } |
| 11754 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11755 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11756 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11757 | } |
| 11758 | |
| 11759 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11760 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11761 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11762 | \n\ |
| 11763 | Return a copy of the string S with leading and trailing\n\ |
| 11764 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11765 | 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] | 11766 | |
| 11767 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11768 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11769 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11770 | if (PyTuple_GET_SIZE(args) == 0) |
| 11771 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11772 | else |
| 11773 | return do_argstrip(self, BOTHSTRIP, args); |
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(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11778 | "S.lstrip([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 whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11781 | 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] | 11782 | |
| 11783 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11784 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11785 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11786 | if (PyTuple_GET_SIZE(args) == 0) |
| 11787 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11788 | else |
| 11789 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11790 | } |
| 11791 | |
| 11792 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11793 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11794 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11795 | \n\ |
| 11796 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11797 | 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] | 11798 | |
| 11799 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11800 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11801 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11802 | if (PyTuple_GET_SIZE(args) == 0) |
| 11803 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11804 | else |
| 11805 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11806 | } |
| 11807 | |
| 11808 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11809 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11810 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11811 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11812 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11813 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11814 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 11815 | if (len < 1) |
| 11816 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11817 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11818 | /* no repeat, return original string */ |
| 11819 | if (len == 1) |
| 11820 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11821 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11822 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11823 | return NULL; |
| 11824 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11825 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11826 | PyErr_SetString(PyExc_OverflowError, |
| 11827 | "repeated string is too long"); |
| 11828 | return NULL; |
| 11829 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11830 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11831 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11832 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11833 | if (!u) |
| 11834 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11835 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11836 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11837 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11838 | const int kind = PyUnicode_KIND(str); |
| 11839 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11840 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11841 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11842 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11843 | } |
| 11844 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11845 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11846 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11847 | ucs2[n] = fill_char; |
| 11848 | } else { |
| 11849 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11850 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11851 | for (n = 0; n < len; ++n) |
| 11852 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11853 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11854 | } |
| 11855 | else { |
| 11856 | /* number of characters copied this far */ |
| 11857 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11858 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11859 | char *to = (char *) PyUnicode_DATA(u); |
| 11860 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11861 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11862 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11863 | n = (done <= nchars-done) ? done : nchars-done; |
| 11864 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11865 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11866 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11867 | } |
| 11868 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11869 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11870 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11871 | } |
| 11872 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11873 | PyObject * |
| 11874 | PyUnicode_Replace(PyObject *obj, |
| 11875 | PyObject *subobj, |
| 11876 | PyObject *replobj, |
| 11877 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11878 | { |
| 11879 | PyObject *self; |
| 11880 | PyObject *str1; |
| 11881 | PyObject *str2; |
| 11882 | PyObject *result; |
| 11883 | |
| 11884 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11885 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11886 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11887 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11888 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11889 | Py_DECREF(self); |
| 11890 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11891 | } |
| 11892 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11893 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11894 | Py_DECREF(self); |
| 11895 | Py_DECREF(str1); |
| 11896 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11898 | if (PyUnicode_READY(self) == -1 || |
| 11899 | PyUnicode_READY(str1) == -1 || |
| 11900 | PyUnicode_READY(str2) == -1) |
| 11901 | result = NULL; |
| 11902 | else |
| 11903 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | Py_DECREF(self); |
| 11905 | Py_DECREF(str1); |
| 11906 | Py_DECREF(str2); |
| 11907 | return result; |
| 11908 | } |
| 11909 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11910 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11911 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11912 | \n\ |
| 11913 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11914 | old replaced by new. If the optional argument count is\n\ |
| 11915 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11916 | |
| 11917 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11919 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11920 | PyObject *str1; |
| 11921 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11922 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11923 | PyObject *result; |
| 11924 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11925 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11926 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11927 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11928 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11929 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11930 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11931 | return NULL; |
| 11932 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11933 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11934 | Py_DECREF(str1); |
| 11935 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11936 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11937 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11938 | result = NULL; |
| 11939 | else |
| 11940 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11941 | |
| 11942 | Py_DECREF(str1); |
| 11943 | Py_DECREF(str2); |
| 11944 | return result; |
| 11945 | } |
| 11946 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11947 | static PyObject * |
| 11948 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11949 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11950 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11951 | Py_ssize_t isize; |
| 11952 | Py_ssize_t osize, squote, dquote, i, o; |
| 11953 | Py_UCS4 max, quote; |
| 11954 | int ikind, okind; |
| 11955 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11957 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11958 | return NULL; |
| 11959 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11960 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11961 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11962 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11963 | /* Compute length of output, quote characters, and |
| 11964 | maximum character */ |
| 11965 | osize = 2; /* quotes */ |
| 11966 | max = 127; |
| 11967 | squote = dquote = 0; |
| 11968 | ikind = PyUnicode_KIND(unicode); |
| 11969 | for (i = 0; i < isize; i++) { |
| 11970 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11971 | switch (ch) { |
| 11972 | case '\'': squote++; osize++; break; |
| 11973 | case '"': dquote++; osize++; break; |
| 11974 | case '\\': case '\t': case '\r': case '\n': |
| 11975 | osize += 2; break; |
| 11976 | default: |
| 11977 | /* Fast-path ASCII */ |
| 11978 | if (ch < ' ' || ch == 0x7f) |
| 11979 | osize += 4; /* \xHH */ |
| 11980 | else if (ch < 0x7f) |
| 11981 | osize++; |
| 11982 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11983 | osize++; |
| 11984 | max = ch > max ? ch : max; |
| 11985 | } |
| 11986 | else if (ch < 0x100) |
| 11987 | osize += 4; /* \xHH */ |
| 11988 | else if (ch < 0x10000) |
| 11989 | osize += 6; /* \uHHHH */ |
| 11990 | else |
| 11991 | osize += 10; /* \uHHHHHHHH */ |
| 11992 | } |
| 11993 | } |
| 11994 | |
| 11995 | quote = '\''; |
| 11996 | if (squote) { |
| 11997 | if (dquote) |
| 11998 | /* Both squote and dquote present. Use squote, |
| 11999 | and escape them */ |
| 12000 | osize += squote; |
| 12001 | else |
| 12002 | quote = '"'; |
| 12003 | } |
| 12004 | |
| 12005 | repr = PyUnicode_New(osize, max); |
| 12006 | if (repr == NULL) |
| 12007 | return NULL; |
| 12008 | okind = PyUnicode_KIND(repr); |
| 12009 | odata = PyUnicode_DATA(repr); |
| 12010 | |
| 12011 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12012 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 12013 | |
| 12014 | for (i = 0, o = 1; i < isize; i++) { |
| 12015 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12016 | |
| 12017 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12018 | if ((ch == quote) || (ch == '\\')) { |
| 12019 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12020 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12021 | continue; |
| 12022 | } |
| 12023 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12024 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12025 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12026 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12027 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12028 | } |
| 12029 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12030 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12031 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12032 | } |
| 12033 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12034 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12035 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12036 | } |
| 12037 | |
| 12038 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12039 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12040 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12041 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12042 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12043 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12044 | } |
| 12045 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12046 | /* Copy ASCII characters as-is */ |
| 12047 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12048 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12049 | } |
| 12050 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12051 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12052 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12053 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12054 | (categories Z* and C* except ASCII space) |
| 12055 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12056 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12057 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12058 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12059 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12060 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12061 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12062 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12063 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12064 | /* Map 16-bit characters to '\uxxxx' */ |
| 12065 | else if (ch <= 0xffff) { |
| 12066 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12067 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12068 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12069 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12070 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12071 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12072 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12073 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12074 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12075 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12076 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12077 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12078 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 12079 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12080 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12081 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12082 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12083 | } |
| 12084 | } |
| 12085 | /* Copy characters as-is */ |
| 12086 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12087 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12088 | } |
| 12089 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12090 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12091 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12092 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12093 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12094 | } |
| 12095 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12096 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12097 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12098 | \n\ |
| 12099 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12100 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12101 | arguments start and end are interpreted as in slice notation.\n\ |
| 12102 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12103 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12104 | |
| 12105 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12106 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12107 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12108 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12109 | Py_ssize_t start; |
| 12110 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12111 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12112 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12113 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12114 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12115 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12116 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12117 | if (PyUnicode_READY(self) == -1) |
| 12118 | return NULL; |
| 12119 | if (PyUnicode_READY(substring) == -1) |
| 12120 | return NULL; |
| 12121 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12122 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12123 | |
| 12124 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12125 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12126 | if (result == -2) |
| 12127 | return NULL; |
| 12128 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12129 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12130 | } |
| 12131 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12132 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12133 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12134 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12135 | 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] | 12136 | |
| 12137 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12138 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12139 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12140 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12141 | Py_ssize_t start; |
| 12142 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12143 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12144 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12145 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12146 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12147 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12148 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12149 | if (PyUnicode_READY(self) == -1) |
| 12150 | return NULL; |
| 12151 | if (PyUnicode_READY(substring) == -1) |
| 12152 | return NULL; |
| 12153 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12154 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12155 | |
| 12156 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12157 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12158 | if (result == -2) |
| 12159 | return NULL; |
| 12160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12161 | if (result < 0) { |
| 12162 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12163 | return NULL; |
| 12164 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12165 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12166 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12167 | } |
| 12168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12169 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12170 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12171 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12172 | 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] | 12173 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12174 | |
| 12175 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12176 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12177 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12178 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12179 | Py_UCS4 fillchar = ' '; |
| 12180 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12181 | 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] | 12182 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12183 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12184 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12185 | return NULL; |
| 12186 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12187 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12188 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12189 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12190 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | } |
| 12192 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12193 | PyObject * |
| 12194 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12195 | { |
| 12196 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12198 | s = PyUnicode_FromObject(s); |
| 12199 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12200 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12201 | if (sep != NULL) { |
| 12202 | sep = PyUnicode_FromObject(sep); |
| 12203 | if (sep == NULL) { |
| 12204 | Py_DECREF(s); |
| 12205 | return NULL; |
| 12206 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12207 | } |
| 12208 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12209 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12210 | |
| 12211 | Py_DECREF(s); |
| 12212 | Py_XDECREF(sep); |
| 12213 | return result; |
| 12214 | } |
| 12215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12216 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12217 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12218 | \n\ |
| 12219 | Return a list of the words in S, using sep as the\n\ |
| 12220 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12221 | 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] | 12222 | whitespace string is a separator and empty strings are\n\ |
| 12223 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12224 | |
| 12225 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12226 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12227 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12228 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12229 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12230 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12231 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12232 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12233 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12234 | return NULL; |
| 12235 | |
| 12236 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12237 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12238 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12239 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12240 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12241 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12242 | } |
| 12243 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12244 | PyObject * |
| 12245 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12246 | { |
| 12247 | PyObject* str_obj; |
| 12248 | PyObject* sep_obj; |
| 12249 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12250 | int kind1, kind2, kind; |
| 12251 | void *buf1 = NULL, *buf2 = NULL; |
| 12252 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12253 | |
| 12254 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12255 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12256 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12257 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12258 | if (!sep_obj) { |
| 12259 | Py_DECREF(str_obj); |
| 12260 | return NULL; |
| 12261 | } |
| 12262 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12263 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12264 | Py_DECREF(str_obj); |
| 12265 | return NULL; |
| 12266 | } |
| 12267 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12268 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12269 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12270 | kind = Py_MAX(kind1, kind2); |
| 12271 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12272 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12273 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12274 | if (!buf1) |
| 12275 | goto onError; |
| 12276 | buf2 = PyUnicode_DATA(sep_obj); |
| 12277 | if (kind2 != kind) |
| 12278 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12279 | if (!buf2) |
| 12280 | goto onError; |
| 12281 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12282 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12283 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12284 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12285 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12286 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12287 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12288 | else |
| 12289 | 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] | 12290 | break; |
| 12291 | case PyUnicode_2BYTE_KIND: |
| 12292 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12293 | break; |
| 12294 | case PyUnicode_4BYTE_KIND: |
| 12295 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12296 | break; |
| 12297 | default: |
| 12298 | assert(0); |
| 12299 | out = 0; |
| 12300 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12301 | |
| 12302 | Py_DECREF(sep_obj); |
| 12303 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12304 | if (kind1 != kind) |
| 12305 | PyMem_Free(buf1); |
| 12306 | if (kind2 != kind) |
| 12307 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12308 | |
| 12309 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12310 | onError: |
| 12311 | Py_DECREF(sep_obj); |
| 12312 | Py_DECREF(str_obj); |
| 12313 | if (kind1 != kind && buf1) |
| 12314 | PyMem_Free(buf1); |
| 12315 | if (kind2 != kind && buf2) |
| 12316 | PyMem_Free(buf2); |
| 12317 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12318 | } |
| 12319 | |
| 12320 | |
| 12321 | PyObject * |
| 12322 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12323 | { |
| 12324 | PyObject* str_obj; |
| 12325 | PyObject* sep_obj; |
| 12326 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12327 | int kind1, kind2, kind; |
| 12328 | void *buf1 = NULL, *buf2 = NULL; |
| 12329 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12330 | |
| 12331 | str_obj = PyUnicode_FromObject(str_in); |
| 12332 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12333 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12334 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12335 | if (!sep_obj) { |
| 12336 | Py_DECREF(str_obj); |
| 12337 | return NULL; |
| 12338 | } |
| 12339 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12340 | kind1 = PyUnicode_KIND(str_in); |
| 12341 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12342 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12343 | buf1 = PyUnicode_DATA(str_in); |
| 12344 | if (kind1 != kind) |
| 12345 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12346 | if (!buf1) |
| 12347 | goto onError; |
| 12348 | buf2 = PyUnicode_DATA(sep_obj); |
| 12349 | if (kind2 != kind) |
| 12350 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12351 | if (!buf2) |
| 12352 | goto onError; |
| 12353 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12354 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12355 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12356 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12357 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12358 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12359 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12360 | else |
| 12361 | 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] | 12362 | break; |
| 12363 | case PyUnicode_2BYTE_KIND: |
| 12364 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12365 | break; |
| 12366 | case PyUnicode_4BYTE_KIND: |
| 12367 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12368 | break; |
| 12369 | default: |
| 12370 | assert(0); |
| 12371 | out = 0; |
| 12372 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12373 | |
| 12374 | Py_DECREF(sep_obj); |
| 12375 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12376 | if (kind1 != kind) |
| 12377 | PyMem_Free(buf1); |
| 12378 | if (kind2 != kind) |
| 12379 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12380 | |
| 12381 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12382 | onError: |
| 12383 | Py_DECREF(sep_obj); |
| 12384 | Py_DECREF(str_obj); |
| 12385 | if (kind1 != kind && buf1) |
| 12386 | PyMem_Free(buf1); |
| 12387 | if (kind2 != kind && buf2) |
| 12388 | PyMem_Free(buf2); |
| 12389 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12390 | } |
| 12391 | |
| 12392 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12393 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12394 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12395 | 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] | 12396 | 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] | 12397 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12398 | |
| 12399 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12400 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12401 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12402 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12403 | } |
| 12404 | |
| 12405 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12406 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12407 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12408 | 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] | 12409 | 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] | 12410 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12411 | |
| 12412 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12413 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12414 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12415 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12416 | } |
| 12417 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12418 | PyObject * |
| 12419 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12420 | { |
| 12421 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12422 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12423 | s = PyUnicode_FromObject(s); |
| 12424 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12425 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12426 | if (sep != NULL) { |
| 12427 | sep = PyUnicode_FromObject(sep); |
| 12428 | if (sep == NULL) { |
| 12429 | Py_DECREF(s); |
| 12430 | return NULL; |
| 12431 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12432 | } |
| 12433 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12434 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12435 | |
| 12436 | Py_DECREF(s); |
| 12437 | Py_XDECREF(sep); |
| 12438 | return result; |
| 12439 | } |
| 12440 | |
| 12441 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12442 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12443 | \n\ |
| 12444 | Return a list of the words in S, using sep as the\n\ |
| 12445 | delimiter string, starting at the end of the string and\n\ |
| 12446 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12447 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12448 | is a separator."); |
| 12449 | |
| 12450 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12451 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12452 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12453 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12454 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12455 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12456 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12457 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12458 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12459 | return NULL; |
| 12460 | |
| 12461 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12462 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12463 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12464 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12465 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12466 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12467 | } |
| 12468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12469 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12470 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12471 | \n\ |
| 12472 | 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] | 12473 | 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] | 12474 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12475 | |
| 12476 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12477 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12478 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12479 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12480 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12481 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12482 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12483 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12484 | return NULL; |
| 12485 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12486 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12487 | } |
| 12488 | |
| 12489 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12490 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12491 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12492 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12493 | } |
| 12494 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12495 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12496 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12497 | \n\ |
| 12498 | 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] | 12499 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12500 | |
| 12501 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12502 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12503 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12504 | if (PyUnicode_READY(self) == -1) |
| 12505 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12506 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12507 | } |
| 12508 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12509 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12510 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12511 | \n\ |
| 12512 | Return a translation table usable for str.translate().\n\ |
| 12513 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12514 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12515 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12516 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12517 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12518 | character at the same position in y. If there is a third argument, it\n\ |
| 12519 | must be a string, whose characters will be mapped to None in the result."); |
| 12520 | |
| 12521 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12522 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12523 | { |
| 12524 | PyObject *x, *y = NULL, *z = NULL; |
| 12525 | PyObject *new = NULL, *key, *value; |
| 12526 | Py_ssize_t i = 0; |
| 12527 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12528 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12529 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12530 | return NULL; |
| 12531 | new = PyDict_New(); |
| 12532 | if (!new) |
| 12533 | return NULL; |
| 12534 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12535 | int x_kind, y_kind, z_kind; |
| 12536 | void *x_data, *y_data, *z_data; |
| 12537 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12538 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12539 | if (!PyUnicode_Check(x)) { |
| 12540 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12541 | "be a string if there is a second argument"); |
| 12542 | goto err; |
| 12543 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12544 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12545 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12546 | "arguments must have equal length"); |
| 12547 | goto err; |
| 12548 | } |
| 12549 | /* 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] | 12550 | x_kind = PyUnicode_KIND(x); |
| 12551 | y_kind = PyUnicode_KIND(y); |
| 12552 | x_data = PyUnicode_DATA(x); |
| 12553 | y_data = PyUnicode_DATA(y); |
| 12554 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12555 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12556 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12557 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12558 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12559 | if (!value) { |
| 12560 | Py_DECREF(key); |
| 12561 | goto err; |
| 12562 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12563 | res = PyDict_SetItem(new, key, value); |
| 12564 | Py_DECREF(key); |
| 12565 | Py_DECREF(value); |
| 12566 | if (res < 0) |
| 12567 | goto err; |
| 12568 | } |
| 12569 | /* create entries for deleting chars in z */ |
| 12570 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12571 | z_kind = PyUnicode_KIND(z); |
| 12572 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12573 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12574 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12575 | if (!key) |
| 12576 | goto err; |
| 12577 | res = PyDict_SetItem(new, key, Py_None); |
| 12578 | Py_DECREF(key); |
| 12579 | if (res < 0) |
| 12580 | goto err; |
| 12581 | } |
| 12582 | } |
| 12583 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12584 | int kind; |
| 12585 | void *data; |
| 12586 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12587 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12588 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12589 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12590 | "to maketrans it must be a dict"); |
| 12591 | goto err; |
| 12592 | } |
| 12593 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12594 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12595 | if (PyUnicode_Check(key)) { |
| 12596 | /* convert string keys to integer keys */ |
| 12597 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12598 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12599 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12600 | "table must be of length 1"); |
| 12601 | goto err; |
| 12602 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12603 | kind = PyUnicode_KIND(key); |
| 12604 | data = PyUnicode_DATA(key); |
| 12605 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12606 | if (!newkey) |
| 12607 | goto err; |
| 12608 | res = PyDict_SetItem(new, newkey, value); |
| 12609 | Py_DECREF(newkey); |
| 12610 | if (res < 0) |
| 12611 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12612 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12613 | /* just keep integer keys */ |
| 12614 | if (PyDict_SetItem(new, key, value) < 0) |
| 12615 | goto err; |
| 12616 | } else { |
| 12617 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12618 | "be strings or integers"); |
| 12619 | goto err; |
| 12620 | } |
| 12621 | } |
| 12622 | } |
| 12623 | return new; |
| 12624 | err: |
| 12625 | Py_DECREF(new); |
| 12626 | return NULL; |
| 12627 | } |
| 12628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12629 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12630 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12631 | \n\ |
| 12632 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12633 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12634 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12635 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12636 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12637 | |
| 12638 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12639 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12640 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12641 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12642 | } |
| 12643 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12644 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12645 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12646 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12647 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12648 | |
| 12649 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12650 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12651 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12652 | if (PyUnicode_READY(self) == -1) |
| 12653 | return NULL; |
| 12654 | if (PyUnicode_IS_ASCII(self)) |
| 12655 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12656 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | } |
| 12658 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12659 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12660 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12661 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12662 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12663 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12664 | |
| 12665 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12666 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12667 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12668 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12669 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12670 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12671 | int kind; |
| 12672 | void *data; |
| 12673 | Py_UCS4 chr; |
| 12674 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12675 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12676 | return NULL; |
| 12677 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12678 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12679 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12680 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12681 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12682 | return unicode_result_unchanged(self); |
| 12683 | |
| 12684 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | |
| 12686 | u = pad(self, fill, 0, '0'); |
| 12687 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12688 | if (u == NULL) |
| 12689 | return NULL; |
| 12690 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12691 | kind = PyUnicode_KIND(u); |
| 12692 | data = PyUnicode_DATA(u); |
| 12693 | chr = PyUnicode_READ(kind, data, fill); |
| 12694 | |
| 12695 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12696 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12697 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12698 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12699 | } |
| 12700 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12701 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12702 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12703 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12704 | |
| 12705 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12706 | static PyObject * |
| 12707 | unicode__decimal2ascii(PyObject *self) |
| 12708 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12709 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12710 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12711 | #endif |
| 12712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12713 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12714 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12715 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12716 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12717 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12718 | With optional end, stop comparing S at that position.\n\ |
| 12719 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12720 | |
| 12721 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12722 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12723 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12724 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12725 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12726 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12727 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12728 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12729 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12730 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12731 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12732 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12733 | if (PyTuple_Check(subobj)) { |
| 12734 | Py_ssize_t i; |
| 12735 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12736 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12737 | if (substring == NULL) |
| 12738 | return NULL; |
| 12739 | result = tailmatch(self, substring, start, end, -1); |
| 12740 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12741 | if (result == -1) |
| 12742 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12743 | if (result) { |
| 12744 | Py_RETURN_TRUE; |
| 12745 | } |
| 12746 | } |
| 12747 | /* nothing matched */ |
| 12748 | Py_RETURN_FALSE; |
| 12749 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12750 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12751 | if (substring == NULL) { |
| 12752 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12753 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12754 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12755 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12756 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12757 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12758 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12759 | if (result == -1) |
| 12760 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12761 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12762 | } |
| 12763 | |
| 12764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12765 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12766 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12767 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12768 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12769 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12770 | With optional end, stop comparing S at that position.\n\ |
| 12771 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12772 | |
| 12773 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12774 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12775 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12776 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12777 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12778 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12779 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12780 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12781 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12782 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12783 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12784 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12785 | if (PyTuple_Check(subobj)) { |
| 12786 | Py_ssize_t i; |
| 12787 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12788 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12789 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12790 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12791 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12792 | result = tailmatch(self, substring, start, end, +1); |
| 12793 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12794 | if (result == -1) |
| 12795 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12796 | if (result) { |
| 12797 | Py_RETURN_TRUE; |
| 12798 | } |
| 12799 | } |
| 12800 | Py_RETURN_FALSE; |
| 12801 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12802 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12803 | if (substring == NULL) { |
| 12804 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12805 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12806 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12807 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12808 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12809 | result = tailmatch(self, substring, start, end, +1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12810 | if (result == -1) |
| 12811 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12812 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12813 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12814 | } |
| 12815 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12816 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12817 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12818 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12819 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12820 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12821 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12822 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12823 | } |
| 12824 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12825 | void |
| 12826 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12827 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12828 | memset(writer, 0, sizeof(*writer)); |
| 12829 | #ifdef Py_DEBUG |
| 12830 | writer->kind = 5; /* invalid kind */ |
| 12831 | #endif |
| 12832 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12833 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12834 | } |
| 12835 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12836 | int |
| 12837 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12838 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12839 | { |
| 12840 | Py_ssize_t newlen; |
| 12841 | PyObject *newbuffer; |
| 12842 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12843 | assert(length > 0); |
| 12844 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12845 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12846 | PyErr_NoMemory(); |
| 12847 | return -1; |
| 12848 | } |
| 12849 | newlen = writer->pos + length; |
| 12850 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12851 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12852 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12853 | /* overallocate 25% to limit the number of resize */ |
| 12854 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12855 | newlen += newlen / 4; |
| 12856 | if (newlen < writer->min_length) |
| 12857 | newlen = writer->min_length; |
| 12858 | } |
| 12859 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12860 | if (writer->buffer == NULL) |
| 12861 | return -1; |
| 12862 | _PyUnicodeWriter_Update(writer); |
| 12863 | return 0; |
| 12864 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12865 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12866 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12867 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12868 | /* overallocate 25% to limit the number of resize */ |
| 12869 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12870 | newlen += newlen / 4; |
| 12871 | if (newlen < writer->min_length) |
| 12872 | newlen = writer->min_length; |
| 12873 | } |
| 12874 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12875 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12876 | /* resize + widen */ |
| 12877 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12878 | if (newbuffer == NULL) |
| 12879 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12880 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12881 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12882 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12883 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12884 | } |
| 12885 | else { |
| 12886 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12887 | if (newbuffer == NULL) |
| 12888 | return -1; |
| 12889 | } |
| 12890 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12891 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12892 | } |
| 12893 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12894 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12895 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12896 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12897 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12898 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12899 | writer->buffer, 0, writer->pos); |
| 12900 | Py_DECREF(writer->buffer); |
| 12901 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12902 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12903 | } |
| 12904 | return 0; |
| 12905 | } |
| 12906 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12907 | int |
| 12908 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12909 | { |
| 12910 | Py_UCS4 maxchar; |
| 12911 | Py_ssize_t len; |
| 12912 | |
| 12913 | if (PyUnicode_READY(str) == -1) |
| 12914 | return -1; |
| 12915 | len = PyUnicode_GET_LENGTH(str); |
| 12916 | if (len == 0) |
| 12917 | return 0; |
| 12918 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12919 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12920 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12921 | Py_INCREF(str); |
| 12922 | writer->buffer = str; |
| 12923 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12924 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12925 | writer->size = 0; |
| 12926 | writer->pos += len; |
| 12927 | return 0; |
| 12928 | } |
| 12929 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12930 | return -1; |
| 12931 | } |
| 12932 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12933 | str, 0, len); |
| 12934 | writer->pos += len; |
| 12935 | return 0; |
| 12936 | } |
| 12937 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 12938 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 12939 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 12940 | Py_ssize_t start, Py_ssize_t end) |
| 12941 | { |
| 12942 | Py_UCS4 maxchar; |
| 12943 | Py_ssize_t len; |
| 12944 | |
| 12945 | if (PyUnicode_READY(str) == -1) |
| 12946 | return -1; |
| 12947 | |
| 12948 | assert(0 <= start); |
| 12949 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 12950 | assert(start <= end); |
| 12951 | |
| 12952 | if (end == 0) |
| 12953 | return 0; |
| 12954 | |
| 12955 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 12956 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 12957 | |
| 12958 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 12959 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 12960 | else |
| 12961 | maxchar = writer->maxchar; |
| 12962 | len = end - start; |
| 12963 | |
| 12964 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 12965 | return -1; |
| 12966 | |
| 12967 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12968 | str, start, len); |
| 12969 | writer->pos += len; |
| 12970 | return 0; |
| 12971 | } |
| 12972 | |
| 12973 | int |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 12974 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) |
| 12975 | { |
| 12976 | Py_UCS4 maxchar; |
| 12977 | |
| 12978 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 12979 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 12980 | return -1; |
| 12981 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 12982 | writer->pos += len; |
| 12983 | return 0; |
| 12984 | } |
| 12985 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12986 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12987 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12988 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12989 | if (writer->pos == 0) { |
| 12990 | Py_XDECREF(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12991 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12992 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12993 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12994 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 12995 | return writer->buffer; |
| 12996 | } |
| 12997 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 12998 | PyObject *newbuffer; |
| 12999 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13000 | if (newbuffer == NULL) { |
| 13001 | Py_DECREF(writer->buffer); |
| 13002 | return NULL; |
| 13003 | } |
| 13004 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13005 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 13006 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 2cb16aa | 2013-03-06 19:28:37 +0100 | [diff] [blame] | 13007 | return unicode_result_ready(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13008 | } |
| 13009 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13010 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13011 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13012 | { |
| 13013 | Py_CLEAR(writer->buffer); |
| 13014 | } |
| 13015 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13016 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13017 | |
| 13018 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13019 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13020 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13021 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13022 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13023 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13024 | PyDoc_STRVAR(format_map__doc__, |
| 13025 | "S.format_map(mapping) -> str\n\ |
| 13026 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13027 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13028 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13029 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13030 | static PyObject * |
| 13031 | unicode__format__(PyObject* self, PyObject* args) |
| 13032 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13033 | PyObject *format_spec; |
| 13034 | _PyUnicodeWriter writer; |
| 13035 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13036 | |
| 13037 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13038 | return NULL; |
| 13039 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13040 | if (PyUnicode_READY(self) == -1) |
| 13041 | return NULL; |
| 13042 | _PyUnicodeWriter_Init(&writer, 0); |
| 13043 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13044 | self, format_spec, 0, |
| 13045 | PyUnicode_GET_LENGTH(format_spec)); |
| 13046 | if (ret == -1) { |
| 13047 | _PyUnicodeWriter_Dealloc(&writer); |
| 13048 | return NULL; |
| 13049 | } |
| 13050 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13051 | } |
| 13052 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13053 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13054 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13055 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13056 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13057 | |
| 13058 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13059 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13060 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13061 | Py_ssize_t size; |
| 13062 | |
| 13063 | /* If it's a compact object, account for base structure + |
| 13064 | character data. */ |
| 13065 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13066 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13067 | else if (PyUnicode_IS_COMPACT(v)) |
| 13068 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13069 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13070 | else { |
| 13071 | /* If it is a two-block object, account for base object, and |
| 13072 | for character block if present. */ |
| 13073 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13074 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13075 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13076 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13077 | } |
| 13078 | /* 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] | 13079 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13080 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13081 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13082 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13083 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13084 | |
| 13085 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13086 | } |
| 13087 | |
| 13088 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13089 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13090 | |
| 13091 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13092 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13093 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13094 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13095 | if (!copy) |
| 13096 | return NULL; |
| 13097 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13098 | } |
| 13099 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13100 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13101 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13102 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13103 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13104 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13105 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13106 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13107 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13108 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13109 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13110 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13111 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13112 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13113 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13114 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13115 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13116 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13117 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13118 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13119 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13120 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13121 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13122 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13123 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13124 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13125 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13126 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13127 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13128 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13129 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13130 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13131 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13132 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13133 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13134 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13135 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13136 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13137 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13138 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13139 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13140 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13141 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13142 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13143 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13144 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13145 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13146 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13147 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13148 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13149 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13150 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13151 | #endif |
| 13152 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13153 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13154 | {NULL, NULL} |
| 13155 | }; |
| 13156 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13157 | static PyObject * |
| 13158 | unicode_mod(PyObject *v, PyObject *w) |
| 13159 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13160 | if (!PyUnicode_Check(v)) |
| 13161 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13162 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13163 | } |
| 13164 | |
| 13165 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13166 | 0, /*nb_add*/ |
| 13167 | 0, /*nb_subtract*/ |
| 13168 | 0, /*nb_multiply*/ |
| 13169 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13170 | }; |
| 13171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13172 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13173 | (lenfunc) unicode_length, /* sq_length */ |
| 13174 | PyUnicode_Concat, /* sq_concat */ |
| 13175 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13176 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13177 | 0, /* sq_slice */ |
| 13178 | 0, /* sq_ass_item */ |
| 13179 | 0, /* sq_ass_slice */ |
| 13180 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13181 | }; |
| 13182 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13183 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13184 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13185 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13186 | if (PyUnicode_READY(self) == -1) |
| 13187 | return NULL; |
| 13188 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13189 | if (PyIndex_Check(item)) { |
| 13190 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13191 | if (i == -1 && PyErr_Occurred()) |
| 13192 | return NULL; |
| 13193 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13194 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13195 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13196 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13197 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13198 | PyObject *result; |
| 13199 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13200 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13201 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13202 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13203 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13204 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13205 | return NULL; |
| 13206 | } |
| 13207 | |
| 13208 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13209 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13210 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13211 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13212 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13213 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13214 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13215 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13216 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13217 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13218 | src_kind = PyUnicode_KIND(self); |
| 13219 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13220 | if (!PyUnicode_IS_ASCII(self)) { |
| 13221 | kind_limit = kind_maxchar_limit(src_kind); |
| 13222 | max_char = 0; |
| 13223 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13224 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13225 | if (ch > max_char) { |
| 13226 | max_char = ch; |
| 13227 | if (max_char >= kind_limit) |
| 13228 | break; |
| 13229 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13230 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13231 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13232 | else |
| 13233 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13234 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13235 | if (result == NULL) |
| 13236 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13237 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13238 | dest_data = PyUnicode_DATA(result); |
| 13239 | |
| 13240 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13241 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13242 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13243 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13244 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13245 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13246 | } else { |
| 13247 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13248 | return NULL; |
| 13249 | } |
| 13250 | } |
| 13251 | |
| 13252 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13253 | (lenfunc)unicode_length, /* mp_length */ |
| 13254 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13255 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13256 | }; |
| 13257 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13258 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13259 | /* Helpers for PyUnicode_Format() */ |
| 13260 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13261 | struct unicode_formatter_t { |
| 13262 | PyObject *args; |
| 13263 | int args_owned; |
| 13264 | Py_ssize_t arglen, argidx; |
| 13265 | PyObject *dict; |
| 13266 | |
| 13267 | enum PyUnicode_Kind fmtkind; |
| 13268 | Py_ssize_t fmtcnt, fmtpos; |
| 13269 | void *fmtdata; |
| 13270 | PyObject *fmtstr; |
| 13271 | |
| 13272 | _PyUnicodeWriter writer; |
| 13273 | }; |
| 13274 | |
| 13275 | struct unicode_format_arg_t { |
| 13276 | Py_UCS4 ch; |
| 13277 | int flags; |
| 13278 | Py_ssize_t width; |
| 13279 | int prec; |
| 13280 | int sign; |
| 13281 | }; |
| 13282 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13283 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13284 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13285 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13286 | Py_ssize_t argidx = ctx->argidx; |
| 13287 | |
| 13288 | if (argidx < ctx->arglen) { |
| 13289 | ctx->argidx++; |
| 13290 | if (ctx->arglen < 0) |
| 13291 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13292 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13293 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13294 | } |
| 13295 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13296 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13297 | return NULL; |
| 13298 | } |
| 13299 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13300 | /* 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] | 13301 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13302 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13303 | otherwise. |
| 13304 | |
| 13305 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13306 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13307 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13308 | PyObject **p_output, |
| 13309 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13310 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13311 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13312 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13313 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13314 | int prec; |
| 13315 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13317 | x = PyFloat_AsDouble(v); |
| 13318 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13319 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13320 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13321 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13322 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13323 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13324 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13325 | if (arg->flags & F_ALT) |
| 13326 | dtoa_flags = Py_DTSF_ALT; |
| 13327 | else |
| 13328 | dtoa_flags = 0; |
| 13329 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13330 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13331 | return -1; |
| 13332 | len = strlen(p); |
| 13333 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13334 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13335 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13336 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13337 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13338 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13339 | writer->pos += len; |
| 13340 | } |
| 13341 | else |
| 13342 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13343 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13344 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13345 | } |
| 13346 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13347 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13348 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13349 | * Python's regular ints. |
| 13350 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13351 | * The output string is of the form |
| 13352 | * "-"? ("0x" | "0X")? digit+ |
| 13353 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13354 | * set in flags. The case of hex digits will be correct, |
| 13355 | * There will be at least prec digits, zero-filled on the left if |
| 13356 | * necessary to get that many. |
| 13357 | * val object to be converted |
| 13358 | * flags bitmask of format flags; only F_ALT is looked at |
| 13359 | * prec minimum number of digits; 0-fill on left if needed |
| 13360 | * type a character in [duoxX]; u acts the same as d |
| 13361 | * |
| 13362 | * CAUTION: o, x and X conversions on regular ints can never |
| 13363 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13364 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13365 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13366 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13367 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13368 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13369 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13370 | Py_ssize_t i; |
| 13371 | int sign; /* 1 if '-', else 0 */ |
| 13372 | int len; /* number of characters */ |
| 13373 | Py_ssize_t llen; |
| 13374 | int numdigits; /* len == numnondigits + numdigits */ |
| 13375 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13376 | int prec = arg->prec; |
| 13377 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13378 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13379 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13380 | if (prec > INT_MAX-3) { |
| 13381 | PyErr_SetString(PyExc_OverflowError, |
| 13382 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13383 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13384 | } |
| 13385 | |
| 13386 | assert(PyLong_Check(val)); |
| 13387 | |
| 13388 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13389 | default: |
| 13390 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13391 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13392 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13393 | case 'u': |
| 13394 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13395 | if (PyBool_Check(val)) |
| 13396 | result = PyNumber_ToBase(val, 10); |
| 13397 | else |
| 13398 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13399 | break; |
| 13400 | case 'o': |
| 13401 | numnondigits = 2; |
| 13402 | result = PyNumber_ToBase(val, 8); |
| 13403 | break; |
| 13404 | case 'x': |
| 13405 | case 'X': |
| 13406 | numnondigits = 2; |
| 13407 | result = PyNumber_ToBase(val, 16); |
| 13408 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13409 | } |
| 13410 | if (!result) |
| 13411 | return NULL; |
| 13412 | |
| 13413 | assert(unicode_modifiable(result)); |
| 13414 | assert(PyUnicode_IS_READY(result)); |
| 13415 | assert(PyUnicode_IS_ASCII(result)); |
| 13416 | |
| 13417 | /* To modify the string in-place, there can only be one reference. */ |
| 13418 | if (Py_REFCNT(result) != 1) { |
| 13419 | PyErr_BadInternalCall(); |
| 13420 | return NULL; |
| 13421 | } |
| 13422 | buf = PyUnicode_DATA(result); |
| 13423 | llen = PyUnicode_GET_LENGTH(result); |
| 13424 | if (llen > INT_MAX) { |
| 13425 | PyErr_SetString(PyExc_ValueError, |
| 13426 | "string too large in _PyBytes_FormatLong"); |
| 13427 | return NULL; |
| 13428 | } |
| 13429 | len = (int)llen; |
| 13430 | sign = buf[0] == '-'; |
| 13431 | numnondigits += sign; |
| 13432 | numdigits = len - numnondigits; |
| 13433 | assert(numdigits > 0); |
| 13434 | |
| 13435 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13436 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13437 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13438 | assert(buf[sign] == '0'); |
| 13439 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13440 | buf[sign+1] == 'o'); |
| 13441 | numnondigits -= 2; |
| 13442 | buf += 2; |
| 13443 | len -= 2; |
| 13444 | if (sign) |
| 13445 | buf[0] = '-'; |
| 13446 | assert(len == numnondigits + numdigits); |
| 13447 | assert(numdigits > 0); |
| 13448 | } |
| 13449 | |
| 13450 | /* Fill with leading zeroes to meet minimum width. */ |
| 13451 | if (prec > numdigits) { |
| 13452 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13453 | numnondigits + prec); |
| 13454 | char *b1; |
| 13455 | if (!r1) { |
| 13456 | Py_DECREF(result); |
| 13457 | return NULL; |
| 13458 | } |
| 13459 | b1 = PyBytes_AS_STRING(r1); |
| 13460 | for (i = 0; i < numnondigits; ++i) |
| 13461 | *b1++ = *buf++; |
| 13462 | for (i = 0; i < prec - numdigits; i++) |
| 13463 | *b1++ = '0'; |
| 13464 | for (i = 0; i < numdigits; i++) |
| 13465 | *b1++ = *buf++; |
| 13466 | *b1 = '\0'; |
| 13467 | Py_DECREF(result); |
| 13468 | result = r1; |
| 13469 | buf = PyBytes_AS_STRING(result); |
| 13470 | len = numnondigits + prec; |
| 13471 | } |
| 13472 | |
| 13473 | /* Fix up case for hex conversions. */ |
| 13474 | if (type == 'X') { |
| 13475 | /* Need to convert all lower case letters to upper case. |
| 13476 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13477 | for (i = 0; i < len; i++) |
| 13478 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13479 | buf[i] -= 'a'-'A'; |
| 13480 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13481 | if (!PyUnicode_Check(result) |
| 13482 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13483 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13484 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13485 | Py_DECREF(result); |
| 13486 | result = unicode; |
| 13487 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13488 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13489 | if (PyUnicode_Resize(&result, len) < 0) |
| 13490 | Py_CLEAR(result); |
| 13491 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13492 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13493 | } |
| 13494 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13495 | /* Format an integer. |
| 13496 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13497 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13498 | * -1 and raise an exception on error */ |
| 13499 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13500 | mainformatlong(PyObject *v, |
| 13501 | struct unicode_format_arg_t *arg, |
| 13502 | PyObject **p_output, |
| 13503 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13504 | { |
| 13505 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13506 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13507 | |
| 13508 | if (!PyNumber_Check(v)) |
| 13509 | goto wrongtype; |
| 13510 | |
| 13511 | if (!PyLong_Check(v)) { |
| 13512 | iobj = PyNumber_Long(v); |
| 13513 | if (iobj == NULL) { |
| 13514 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13515 | goto wrongtype; |
| 13516 | return -1; |
| 13517 | } |
| 13518 | assert(PyLong_Check(iobj)); |
| 13519 | } |
| 13520 | else { |
| 13521 | iobj = v; |
| 13522 | Py_INCREF(iobj); |
| 13523 | } |
| 13524 | |
| 13525 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13526 | && arg->width == -1 && arg->prec == -1 |
| 13527 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 13528 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13529 | { |
| 13530 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13531 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13532 | int base; |
| 13533 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13534 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13535 | { |
| 13536 | default: |
| 13537 | assert(0 && "'type' not in [diuoxX]"); |
| 13538 | case 'd': |
| 13539 | case 'i': |
| 13540 | case 'u': |
| 13541 | base = 10; |
| 13542 | break; |
| 13543 | case 'o': |
| 13544 | base = 8; |
| 13545 | break; |
| 13546 | case 'x': |
| 13547 | case 'X': |
| 13548 | base = 16; |
| 13549 | break; |
| 13550 | } |
| 13551 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13552 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 13553 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13554 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13555 | } |
| 13556 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13557 | return 1; |
| 13558 | } |
| 13559 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13560 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13561 | Py_DECREF(iobj); |
| 13562 | if (res == NULL) |
| 13563 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13564 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13565 | return 0; |
| 13566 | |
| 13567 | wrongtype: |
| 13568 | PyErr_Format(PyExc_TypeError, |
| 13569 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13570 | "not %.200s", |
| 13571 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13572 | return -1; |
| 13573 | } |
| 13574 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13575 | static Py_UCS4 |
| 13576 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13577 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13578 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13579 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13580 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13581 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13583 | goto onError; |
| 13584 | } |
| 13585 | else { |
| 13586 | /* Integer input truncated to a character */ |
| 13587 | long x; |
| 13588 | x = PyLong_AsLong(v); |
| 13589 | if (x == -1 && PyErr_Occurred()) |
| 13590 | goto onError; |
| 13591 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13592 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13593 | PyErr_SetString(PyExc_OverflowError, |
| 13594 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13595 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13596 | } |
| 13597 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13598 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13599 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13600 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13601 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13602 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13603 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13604 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13605 | } |
| 13606 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13607 | /* Parse options of an argument: flags, width, precision. |
| 13608 | Handle also "%(name)" syntax. |
| 13609 | |
| 13610 | Return 0 if the argument has been formatted into arg->str. |
| 13611 | Return 1 if the argument has been written into ctx->writer, |
| 13612 | Raise an exception and return -1 on error. */ |
| 13613 | static int |
| 13614 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 13615 | struct unicode_format_arg_t *arg) |
| 13616 | { |
| 13617 | #define FORMAT_READ(ctx) \ |
| 13618 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 13619 | |
| 13620 | PyObject *v; |
| 13621 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13622 | if (arg->ch == '(') { |
| 13623 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 13624 | Py_ssize_t keystart; |
| 13625 | Py_ssize_t keylen; |
| 13626 | PyObject *key; |
| 13627 | int pcount = 1; |
| 13628 | |
| 13629 | if (ctx->dict == NULL) { |
| 13630 | PyErr_SetString(PyExc_TypeError, |
| 13631 | "format requires a mapping"); |
| 13632 | return -1; |
| 13633 | } |
| 13634 | ++ctx->fmtpos; |
| 13635 | --ctx->fmtcnt; |
| 13636 | keystart = ctx->fmtpos; |
| 13637 | /* Skip over balanced parentheses */ |
| 13638 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 13639 | arg->ch = FORMAT_READ(ctx); |
| 13640 | if (arg->ch == ')') |
| 13641 | --pcount; |
| 13642 | else if (arg->ch == '(') |
| 13643 | ++pcount; |
| 13644 | ctx->fmtpos++; |
| 13645 | } |
| 13646 | keylen = ctx->fmtpos - keystart - 1; |
| 13647 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 13648 | PyErr_SetString(PyExc_ValueError, |
| 13649 | "incomplete format key"); |
| 13650 | return -1; |
| 13651 | } |
| 13652 | key = PyUnicode_Substring(ctx->fmtstr, |
| 13653 | keystart, keystart + keylen); |
| 13654 | if (key == NULL) |
| 13655 | return -1; |
| 13656 | if (ctx->args_owned) { |
| 13657 | Py_DECREF(ctx->args); |
| 13658 | ctx->args_owned = 0; |
| 13659 | } |
| 13660 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 13661 | Py_DECREF(key); |
| 13662 | if (ctx->args == NULL) |
| 13663 | return -1; |
| 13664 | ctx->args_owned = 1; |
| 13665 | ctx->arglen = -1; |
| 13666 | ctx->argidx = -2; |
| 13667 | } |
| 13668 | |
| 13669 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13670 | while (--ctx->fmtcnt >= 0) { |
| 13671 | arg->ch = FORMAT_READ(ctx); |
| 13672 | ctx->fmtpos++; |
| 13673 | switch (arg->ch) { |
| 13674 | case '-': arg->flags |= F_LJUST; continue; |
| 13675 | case '+': arg->flags |= F_SIGN; continue; |
| 13676 | case ' ': arg->flags |= F_BLANK; continue; |
| 13677 | case '#': arg->flags |= F_ALT; continue; |
| 13678 | case '0': arg->flags |= F_ZERO; continue; |
| 13679 | } |
| 13680 | break; |
| 13681 | } |
| 13682 | |
| 13683 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13684 | if (arg->ch == '*') { |
| 13685 | v = unicode_format_getnextarg(ctx); |
| 13686 | if (v == NULL) |
| 13687 | return -1; |
| 13688 | if (!PyLong_Check(v)) { |
| 13689 | PyErr_SetString(PyExc_TypeError, |
| 13690 | "* wants int"); |
| 13691 | return -1; |
| 13692 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13693 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13694 | if (arg->width == -1 && PyErr_Occurred()) |
| 13695 | return -1; |
| 13696 | if (arg->width < 0) { |
| 13697 | arg->flags |= F_LJUST; |
| 13698 | arg->width = -arg->width; |
| 13699 | } |
| 13700 | if (--ctx->fmtcnt >= 0) { |
| 13701 | arg->ch = FORMAT_READ(ctx); |
| 13702 | ctx->fmtpos++; |
| 13703 | } |
| 13704 | } |
| 13705 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13706 | arg->width = arg->ch - '0'; |
| 13707 | while (--ctx->fmtcnt >= 0) { |
| 13708 | arg->ch = FORMAT_READ(ctx); |
| 13709 | ctx->fmtpos++; |
| 13710 | if (arg->ch < '0' || arg->ch > '9') |
| 13711 | break; |
| 13712 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 13713 | mixing signed and unsigned comparison. Since arg->ch is between |
| 13714 | '0' and '9', casting to int is safe. */ |
| 13715 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 13716 | PyErr_SetString(PyExc_ValueError, |
| 13717 | "width too big"); |
| 13718 | return -1; |
| 13719 | } |
| 13720 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 13721 | } |
| 13722 | } |
| 13723 | |
| 13724 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13725 | if (arg->ch == '.') { |
| 13726 | arg->prec = 0; |
| 13727 | if (--ctx->fmtcnt >= 0) { |
| 13728 | arg->ch = FORMAT_READ(ctx); |
| 13729 | ctx->fmtpos++; |
| 13730 | } |
| 13731 | if (arg->ch == '*') { |
| 13732 | v = unicode_format_getnextarg(ctx); |
| 13733 | if (v == NULL) |
| 13734 | return -1; |
| 13735 | if (!PyLong_Check(v)) { |
| 13736 | PyErr_SetString(PyExc_TypeError, |
| 13737 | "* wants int"); |
| 13738 | return -1; |
| 13739 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13740 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13741 | if (arg->prec == -1 && PyErr_Occurred()) |
| 13742 | return -1; |
| 13743 | if (arg->prec < 0) |
| 13744 | arg->prec = 0; |
| 13745 | if (--ctx->fmtcnt >= 0) { |
| 13746 | arg->ch = FORMAT_READ(ctx); |
| 13747 | ctx->fmtpos++; |
| 13748 | } |
| 13749 | } |
| 13750 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13751 | arg->prec = arg->ch - '0'; |
| 13752 | while (--ctx->fmtcnt >= 0) { |
| 13753 | arg->ch = FORMAT_READ(ctx); |
| 13754 | ctx->fmtpos++; |
| 13755 | if (arg->ch < '0' || arg->ch > '9') |
| 13756 | break; |
| 13757 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 13758 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 13759 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13760 | return -1; |
| 13761 | } |
| 13762 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 13763 | } |
| 13764 | } |
| 13765 | } |
| 13766 | |
| 13767 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 13768 | if (ctx->fmtcnt >= 0) { |
| 13769 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 13770 | if (--ctx->fmtcnt >= 0) { |
| 13771 | arg->ch = FORMAT_READ(ctx); |
| 13772 | ctx->fmtpos++; |
| 13773 | } |
| 13774 | } |
| 13775 | } |
| 13776 | if (ctx->fmtcnt < 0) { |
| 13777 | PyErr_SetString(PyExc_ValueError, |
| 13778 | "incomplete format"); |
| 13779 | return -1; |
| 13780 | } |
| 13781 | return 0; |
| 13782 | |
| 13783 | #undef FORMAT_READ |
| 13784 | } |
| 13785 | |
| 13786 | /* Format one argument. Supported conversion specifiers: |
| 13787 | |
| 13788 | - "s", "r", "a": any type |
| 13789 | - "i", "d", "u", "o", "x", "X": int |
| 13790 | - "e", "E", "f", "F", "g", "G": float |
| 13791 | - "c": int or str (1 character) |
| 13792 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13793 | When possible, the output is written directly into the Unicode writer |
| 13794 | (ctx->writer). A string is created when padding is required. |
| 13795 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13796 | Return 0 if the argument has been formatted into *p_str, |
| 13797 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13798 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13799 | static int |
| 13800 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 13801 | struct unicode_format_arg_t *arg, |
| 13802 | PyObject **p_str) |
| 13803 | { |
| 13804 | PyObject *v; |
| 13805 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13806 | |
| 13807 | if (ctx->fmtcnt == 0) |
| 13808 | ctx->writer.overallocate = 0; |
| 13809 | |
| 13810 | if (arg->ch == '%') { |
| 13811 | if (_PyUnicodeWriter_Prepare(writer, 1, '%') == -1) |
| 13812 | return -1; |
| 13813 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%'); |
| 13814 | writer->pos += 1; |
| 13815 | return 1; |
| 13816 | } |
| 13817 | |
| 13818 | v = unicode_format_getnextarg(ctx); |
| 13819 | if (v == NULL) |
| 13820 | return -1; |
| 13821 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13822 | |
| 13823 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13824 | case 's': |
| 13825 | case 'r': |
| 13826 | case 'a': |
| 13827 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 13828 | /* Fast path */ |
| 13829 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 13830 | return -1; |
| 13831 | return 1; |
| 13832 | } |
| 13833 | |
| 13834 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 13835 | *p_str = v; |
| 13836 | Py_INCREF(*p_str); |
| 13837 | } |
| 13838 | else { |
| 13839 | if (arg->ch == 's') |
| 13840 | *p_str = PyObject_Str(v); |
| 13841 | else if (arg->ch == 'r') |
| 13842 | *p_str = PyObject_Repr(v); |
| 13843 | else |
| 13844 | *p_str = PyObject_ASCII(v); |
| 13845 | } |
| 13846 | break; |
| 13847 | |
| 13848 | case 'i': |
| 13849 | case 'd': |
| 13850 | case 'u': |
| 13851 | case 'o': |
| 13852 | case 'x': |
| 13853 | case 'X': |
| 13854 | { |
| 13855 | int ret = mainformatlong(v, arg, p_str, writer); |
| 13856 | if (ret != 0) |
| 13857 | return ret; |
| 13858 | arg->sign = 1; |
| 13859 | break; |
| 13860 | } |
| 13861 | |
| 13862 | case 'e': |
| 13863 | case 'E': |
| 13864 | case 'f': |
| 13865 | case 'F': |
| 13866 | case 'g': |
| 13867 | case 'G': |
| 13868 | if (arg->width == -1 && arg->prec == -1 |
| 13869 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13870 | { |
| 13871 | /* Fast path */ |
| 13872 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 13873 | return -1; |
| 13874 | return 1; |
| 13875 | } |
| 13876 | |
| 13877 | arg->sign = 1; |
| 13878 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 13879 | return -1; |
| 13880 | break; |
| 13881 | |
| 13882 | case 'c': |
| 13883 | { |
| 13884 | Py_UCS4 ch = formatchar(v); |
| 13885 | if (ch == (Py_UCS4) -1) |
| 13886 | return -1; |
| 13887 | if (arg->width == -1 && arg->prec == -1) { |
| 13888 | /* Fast path */ |
| 13889 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) == -1) |
| 13890 | return -1; |
| 13891 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13892 | writer->pos += 1; |
| 13893 | return 1; |
| 13894 | } |
| 13895 | *p_str = PyUnicode_FromOrdinal(ch); |
| 13896 | break; |
| 13897 | } |
| 13898 | |
| 13899 | default: |
| 13900 | PyErr_Format(PyExc_ValueError, |
| 13901 | "unsupported format character '%c' (0x%x) " |
| 13902 | "at index %zd", |
| 13903 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 13904 | (int)arg->ch, |
| 13905 | ctx->fmtpos - 1); |
| 13906 | return -1; |
| 13907 | } |
| 13908 | if (*p_str == NULL) |
| 13909 | return -1; |
| 13910 | assert (PyUnicode_Check(*p_str)); |
| 13911 | return 0; |
| 13912 | } |
| 13913 | |
| 13914 | static int |
| 13915 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 13916 | struct unicode_format_arg_t *arg, |
| 13917 | PyObject *str) |
| 13918 | { |
| 13919 | Py_ssize_t len; |
| 13920 | enum PyUnicode_Kind kind; |
| 13921 | void *pbuf; |
| 13922 | Py_ssize_t pindex; |
| 13923 | Py_UCS4 signchar; |
| 13924 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13925 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13926 | Py_ssize_t sublen; |
| 13927 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13928 | Py_UCS4 fill; |
| 13929 | |
| 13930 | fill = ' '; |
| 13931 | if (arg->sign && arg->flags & F_ZERO) |
| 13932 | fill = '0'; |
| 13933 | |
| 13934 | if (PyUnicode_READY(str) == -1) |
| 13935 | return -1; |
| 13936 | |
| 13937 | len = PyUnicode_GET_LENGTH(str); |
| 13938 | if ((arg->width == -1 || arg->width <= len) |
| 13939 | && (arg->prec == -1 || arg->prec >= len) |
| 13940 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13941 | { |
| 13942 | /* Fast path */ |
| 13943 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 13944 | return -1; |
| 13945 | return 0; |
| 13946 | } |
| 13947 | |
| 13948 | /* Truncate the string for "s", "r" and "a" formats |
| 13949 | if the precision is set */ |
| 13950 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 13951 | if (arg->prec >= 0 && len > arg->prec) |
| 13952 | len = arg->prec; |
| 13953 | } |
| 13954 | |
| 13955 | /* Adjust sign and width */ |
| 13956 | kind = PyUnicode_KIND(str); |
| 13957 | pbuf = PyUnicode_DATA(str); |
| 13958 | pindex = 0; |
| 13959 | signchar = '\0'; |
| 13960 | if (arg->sign) { |
| 13961 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13962 | if (ch == '-' || ch == '+') { |
| 13963 | signchar = ch; |
| 13964 | len--; |
| 13965 | pindex++; |
| 13966 | } |
| 13967 | else if (arg->flags & F_SIGN) |
| 13968 | signchar = '+'; |
| 13969 | else if (arg->flags & F_BLANK) |
| 13970 | signchar = ' '; |
| 13971 | else |
| 13972 | arg->sign = 0; |
| 13973 | } |
| 13974 | if (arg->width < len) |
| 13975 | arg->width = len; |
| 13976 | |
| 13977 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13978 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13979 | if (!(arg->flags & F_LJUST)) { |
| 13980 | if (arg->sign) { |
| 13981 | if ((arg->width-1) > len) |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13982 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13983 | } |
| 13984 | else { |
| 13985 | if (arg->width > len) |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13986 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13987 | } |
| 13988 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13989 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 13990 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
| 13991 | maxchar = MAX_MAXCHAR(maxchar, strmaxchar); |
| 13992 | } |
| 13993 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13994 | buflen = arg->width; |
| 13995 | if (arg->sign && len == arg->width) |
| 13996 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13997 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13998 | return -1; |
| 13999 | |
| 14000 | /* Write the sign if needed */ |
| 14001 | if (arg->sign) { |
| 14002 | if (fill != ' ') { |
| 14003 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14004 | writer->pos += 1; |
| 14005 | } |
| 14006 | if (arg->width > len) |
| 14007 | arg->width--; |
| 14008 | } |
| 14009 | |
| 14010 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14011 | if the alternate form is used. |
| 14012 | For example, write "0x" for the "%#x" format. */ |
| 14013 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14014 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14015 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14016 | if (fill != ' ') { |
| 14017 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14018 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14019 | writer->pos += 2; |
| 14020 | pindex += 2; |
| 14021 | } |
| 14022 | arg->width -= 2; |
| 14023 | if (arg->width < 0) |
| 14024 | arg->width = 0; |
| 14025 | len -= 2; |
| 14026 | } |
| 14027 | |
| 14028 | /* Pad left with the fill character if needed */ |
| 14029 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14030 | sublen = arg->width - len; |
| 14031 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14032 | writer->pos += sublen; |
| 14033 | arg->width = len; |
| 14034 | } |
| 14035 | |
| 14036 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14037 | the alternate form is used */ |
| 14038 | if (fill == ' ') { |
| 14039 | if (arg->sign) { |
| 14040 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14041 | writer->pos += 1; |
| 14042 | } |
| 14043 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14044 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14045 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 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 | } |
| 14052 | |
| 14053 | /* Write characters */ |
| 14054 | if (len) { |
| 14055 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14056 | str, pindex, len); |
| 14057 | writer->pos += len; |
| 14058 | } |
| 14059 | |
| 14060 | /* Pad right with the fill character if needed */ |
| 14061 | if (arg->width > len) { |
| 14062 | sublen = arg->width - len; |
| 14063 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14064 | writer->pos += sublen; |
| 14065 | } |
| 14066 | return 0; |
| 14067 | } |
| 14068 | |
| 14069 | /* Helper of PyUnicode_Format(): format one arg. |
| 14070 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14071 | static int |
| 14072 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14073 | { |
| 14074 | struct unicode_format_arg_t arg; |
| 14075 | PyObject *str; |
| 14076 | int ret; |
| 14077 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14078 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14079 | arg.flags = 0; |
| 14080 | arg.width = -1; |
| 14081 | arg.prec = -1; |
| 14082 | arg.sign = 0; |
| 14083 | str = NULL; |
| 14084 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14085 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14086 | if (ret == -1) |
| 14087 | return -1; |
| 14088 | |
| 14089 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14090 | if (ret == -1) |
| 14091 | return -1; |
| 14092 | |
| 14093 | if (ret != 1) { |
| 14094 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14095 | Py_DECREF(str); |
| 14096 | if (ret == -1) |
| 14097 | return -1; |
| 14098 | } |
| 14099 | |
| 14100 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14101 | PyErr_SetString(PyExc_TypeError, |
| 14102 | "not all arguments converted during string formatting"); |
| 14103 | return -1; |
| 14104 | } |
| 14105 | return 0; |
| 14106 | } |
| 14107 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14108 | PyObject * |
| 14109 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14110 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14111 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14113 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14114 | PyErr_BadInternalCall(); |
| 14115 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14116 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14117 | |
| 14118 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14119 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14120 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14121 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14122 | Py_DECREF(ctx.fmtstr); |
| 14123 | return NULL; |
| 14124 | } |
| 14125 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14126 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14127 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14128 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14129 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14130 | _PyUnicodeWriter_Init(&ctx.writer, ctx.fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14131 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14132 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14133 | ctx.arglen = PyTuple_Size(args); |
| 14134 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14135 | } |
| 14136 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14137 | ctx.arglen = -1; |
| 14138 | ctx.argidx = -2; |
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 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14141 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14142 | ctx.dict = args; |
| 14143 | else |
| 14144 | ctx.dict = NULL; |
| 14145 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14146 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14147 | while (--ctx.fmtcnt >= 0) { |
| 14148 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14149 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14150 | |
| 14151 | nonfmtpos = ctx.fmtpos++; |
| 14152 | while (ctx.fmtcnt >= 0 && |
| 14153 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14154 | ctx.fmtpos++; |
| 14155 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14156 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14157 | if (ctx.fmtcnt < 0) { |
| 14158 | ctx.fmtpos--; |
| 14159 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14160 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14161 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14162 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14163 | nonfmtpos, ctx.fmtpos) < 0) |
| 14164 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14165 | } |
| 14166 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14167 | ctx.fmtpos++; |
| 14168 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14169 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14170 | } |
| 14171 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14172 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14173 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14174 | PyErr_SetString(PyExc_TypeError, |
| 14175 | "not all arguments converted during string formatting"); |
| 14176 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14177 | } |
| 14178 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14179 | if (ctx.args_owned) { |
| 14180 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14181 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14182 | Py_DECREF(ctx.fmtstr); |
| 14183 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14184 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14185 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14186 | Py_DECREF(ctx.fmtstr); |
| 14187 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14188 | if (ctx.args_owned) { |
| 14189 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14190 | } |
| 14191 | return NULL; |
| 14192 | } |
| 14193 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14194 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14195 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14196 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14197 | static PyObject * |
| 14198 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14199 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14200 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14201 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14202 | char *encoding = NULL; |
| 14203 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14204 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14205 | if (type != &PyUnicode_Type) |
| 14206 | return unicode_subtype_new(type, args, kwds); |
| 14207 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14208 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14209 | return NULL; |
| 14210 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14211 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14212 | if (encoding == NULL && errors == NULL) |
| 14213 | return PyObject_Str(x); |
| 14214 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14215 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14216 | } |
| 14217 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14218 | static PyObject * |
| 14219 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14220 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14221 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14222 | Py_ssize_t length, char_size; |
| 14223 | int share_wstr, share_utf8; |
| 14224 | unsigned int kind; |
| 14225 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14226 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14227 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14228 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14229 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14230 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14231 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14232 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14233 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14234 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14235 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14236 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14237 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14238 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14239 | if (self == NULL) { |
| 14240 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14241 | return NULL; |
| 14242 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14243 | kind = PyUnicode_KIND(unicode); |
| 14244 | length = PyUnicode_GET_LENGTH(unicode); |
| 14245 | |
| 14246 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14247 | #ifdef Py_DEBUG |
| 14248 | _PyUnicode_HASH(self) = -1; |
| 14249 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14250 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14251 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14252 | _PyUnicode_STATE(self).interned = 0; |
| 14253 | _PyUnicode_STATE(self).kind = kind; |
| 14254 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14255 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14256 | _PyUnicode_STATE(self).ready = 1; |
| 14257 | _PyUnicode_WSTR(self) = NULL; |
| 14258 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14259 | _PyUnicode_UTF8(self) = NULL; |
| 14260 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14261 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14262 | |
| 14263 | share_utf8 = 0; |
| 14264 | share_wstr = 0; |
| 14265 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14266 | char_size = 1; |
| 14267 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14268 | share_utf8 = 1; |
| 14269 | } |
| 14270 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14271 | char_size = 2; |
| 14272 | if (sizeof(wchar_t) == 2) |
| 14273 | share_wstr = 1; |
| 14274 | } |
| 14275 | else { |
| 14276 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14277 | char_size = 4; |
| 14278 | if (sizeof(wchar_t) == 4) |
| 14279 | share_wstr = 1; |
| 14280 | } |
| 14281 | |
| 14282 | /* Ensure we won't overflow the length. */ |
| 14283 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14284 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14285 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14286 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14287 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14288 | if (data == NULL) { |
| 14289 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14290 | goto onError; |
| 14291 | } |
| 14292 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14293 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14294 | if (share_utf8) { |
| 14295 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14296 | _PyUnicode_UTF8(self) = data; |
| 14297 | } |
| 14298 | if (share_wstr) { |
| 14299 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14300 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14301 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14302 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14303 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14304 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14305 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14306 | #ifdef Py_DEBUG |
| 14307 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14308 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14309 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14310 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14311 | |
| 14312 | onError: |
| 14313 | Py_DECREF(unicode); |
| 14314 | Py_DECREF(self); |
| 14315 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14316 | } |
| 14317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14318 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14319 | "str(object='') -> str\n\ |
| 14320 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14321 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14322 | Create a new string object from the given object. If encoding or\n\ |
| 14323 | errors is specified, then the object must expose a data buffer\n\ |
| 14324 | that will be decoded using the given encoding and error handler.\n\ |
| 14325 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14326 | or repr(object).\n\ |
| 14327 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14328 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14329 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14330 | static PyObject *unicode_iter(PyObject *seq); |
| 14331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14332 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14333 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14334 | "str", /* tp_name */ |
| 14335 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14336 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14337 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14338 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14339 | 0, /* tp_print */ |
| 14340 | 0, /* tp_getattr */ |
| 14341 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14342 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14343 | unicode_repr, /* tp_repr */ |
| 14344 | &unicode_as_number, /* tp_as_number */ |
| 14345 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14346 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14347 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14348 | 0, /* tp_call*/ |
| 14349 | (reprfunc) unicode_str, /* tp_str */ |
| 14350 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14351 | 0, /* tp_setattro */ |
| 14352 | 0, /* tp_as_buffer */ |
| 14353 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14354 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14355 | unicode_doc, /* tp_doc */ |
| 14356 | 0, /* tp_traverse */ |
| 14357 | 0, /* tp_clear */ |
| 14358 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14359 | 0, /* tp_weaklistoffset */ |
| 14360 | unicode_iter, /* tp_iter */ |
| 14361 | 0, /* tp_iternext */ |
| 14362 | unicode_methods, /* tp_methods */ |
| 14363 | 0, /* tp_members */ |
| 14364 | 0, /* tp_getset */ |
| 14365 | &PyBaseObject_Type, /* tp_base */ |
| 14366 | 0, /* tp_dict */ |
| 14367 | 0, /* tp_descr_get */ |
| 14368 | 0, /* tp_descr_set */ |
| 14369 | 0, /* tp_dictoffset */ |
| 14370 | 0, /* tp_init */ |
| 14371 | 0, /* tp_alloc */ |
| 14372 | unicode_new, /* tp_new */ |
| 14373 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14374 | }; |
| 14375 | |
| 14376 | /* Initialize the Unicode implementation */ |
| 14377 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14378 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14379 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14380 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14381 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14382 | 0x000A, /* LINE FEED */ |
| 14383 | 0x000D, /* CARRIAGE RETURN */ |
| 14384 | 0x001C, /* FILE SEPARATOR */ |
| 14385 | 0x001D, /* GROUP SEPARATOR */ |
| 14386 | 0x001E, /* RECORD SEPARATOR */ |
| 14387 | 0x0085, /* NEXT LINE */ |
| 14388 | 0x2028, /* LINE SEPARATOR */ |
| 14389 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14390 | }; |
| 14391 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14392 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14393 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14394 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14395 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14396 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14397 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14398 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14399 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14400 | |
| 14401 | /* initialize the linebreak bloom filter */ |
| 14402 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14403 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14404 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14405 | |
| 14406 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14407 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14408 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14409 | Py_FatalError("Can't initialize field name iterator type"); |
| 14410 | |
| 14411 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14412 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14413 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14414 | #ifdef HAVE_MBCS |
| 14415 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14416 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14417 | PyErr_SetFromWindowsErr(0); |
| 14418 | return -1; |
| 14419 | } |
| 14420 | #endif |
| 14421 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14422 | } |
| 14423 | |
| 14424 | /* Finalize the Unicode implementation */ |
| 14425 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14426 | int |
| 14427 | PyUnicode_ClearFreeList(void) |
| 14428 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14429 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14430 | } |
| 14431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14432 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14433 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14434 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14435 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14436 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14437 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14438 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14439 | for (i = 0; i < 256; i++) |
| 14440 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14441 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14442 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14443 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14444 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14445 | void |
| 14446 | PyUnicode_InternInPlace(PyObject **p) |
| 14447 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14448 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14449 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14450 | #ifdef Py_DEBUG |
| 14451 | assert(s != NULL); |
| 14452 | assert(_PyUnicode_CHECK(s)); |
| 14453 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14454 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14455 | return; |
| 14456 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14457 | /* If it's a subclass, we don't really know what putting |
| 14458 | it in the interned dict might do. */ |
| 14459 | if (!PyUnicode_CheckExact(s)) |
| 14460 | return; |
| 14461 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14462 | return; |
| 14463 | if (interned == NULL) { |
| 14464 | interned = PyDict_New(); |
| 14465 | if (interned == NULL) { |
| 14466 | PyErr_Clear(); /* Don't leave an exception */ |
| 14467 | return; |
| 14468 | } |
| 14469 | } |
| 14470 | /* It might be that the GetItem call fails even |
| 14471 | though the key is present in the dictionary, |
| 14472 | namely when this happens during a stack overflow. */ |
| 14473 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14474 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14475 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14476 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14477 | if (t) { |
| 14478 | Py_INCREF(t); |
| 14479 | Py_DECREF(*p); |
| 14480 | *p = t; |
| 14481 | return; |
| 14482 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14483 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14484 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14485 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14486 | PyErr_Clear(); |
| 14487 | PyThreadState_GET()->recursion_critical = 0; |
| 14488 | return; |
| 14489 | } |
| 14490 | PyThreadState_GET()->recursion_critical = 0; |
| 14491 | /* The two references in interned are not counted by refcnt. |
| 14492 | The deallocator will take care of this */ |
| 14493 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14494 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14495 | } |
| 14496 | |
| 14497 | void |
| 14498 | PyUnicode_InternImmortal(PyObject **p) |
| 14499 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14500 | PyUnicode_InternInPlace(p); |
| 14501 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14502 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14503 | Py_INCREF(*p); |
| 14504 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14505 | } |
| 14506 | |
| 14507 | PyObject * |
| 14508 | PyUnicode_InternFromString(const char *cp) |
| 14509 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14510 | PyObject *s = PyUnicode_FromString(cp); |
| 14511 | if (s == NULL) |
| 14512 | return NULL; |
| 14513 | PyUnicode_InternInPlace(&s); |
| 14514 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14515 | } |
| 14516 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14517 | void |
| 14518 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14519 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14520 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14521 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14522 | Py_ssize_t i, n; |
| 14523 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14524 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14525 | if (interned == NULL || !PyDict_Check(interned)) |
| 14526 | return; |
| 14527 | keys = PyDict_Keys(interned); |
| 14528 | if (keys == NULL || !PyList_Check(keys)) { |
| 14529 | PyErr_Clear(); |
| 14530 | return; |
| 14531 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14532 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14533 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14534 | detector, interned unicode strings are not forcibly deallocated; |
| 14535 | rather, we give them their stolen references back, and then clear |
| 14536 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14537 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14538 | n = PyList_GET_SIZE(keys); |
| 14539 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14540 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14541 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14542 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14543 | if (PyUnicode_READY(s) == -1) { |
| 14544 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14545 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14546 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14547 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14548 | case SSTATE_NOT_INTERNED: |
| 14549 | /* XXX Shouldn't happen */ |
| 14550 | break; |
| 14551 | case SSTATE_INTERNED_IMMORTAL: |
| 14552 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14553 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14554 | break; |
| 14555 | case SSTATE_INTERNED_MORTAL: |
| 14556 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14557 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14558 | break; |
| 14559 | default: |
| 14560 | Py_FatalError("Inconsistent interned string state."); |
| 14561 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14562 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14563 | } |
| 14564 | fprintf(stderr, "total size of all interned strings: " |
| 14565 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14566 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14567 | Py_DECREF(keys); |
| 14568 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14569 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14570 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14571 | |
| 14572 | |
| 14573 | /********************* Unicode Iterator **************************/ |
| 14574 | |
| 14575 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14576 | PyObject_HEAD |
| 14577 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14578 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14579 | } unicodeiterobject; |
| 14580 | |
| 14581 | static void |
| 14582 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14583 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14584 | _PyObject_GC_UNTRACK(it); |
| 14585 | Py_XDECREF(it->it_seq); |
| 14586 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14587 | } |
| 14588 | |
| 14589 | static int |
| 14590 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14591 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14592 | Py_VISIT(it->it_seq); |
| 14593 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14594 | } |
| 14595 | |
| 14596 | static PyObject * |
| 14597 | unicodeiter_next(unicodeiterobject *it) |
| 14598 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14599 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14600 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14601 | assert(it != NULL); |
| 14602 | seq = it->it_seq; |
| 14603 | if (seq == NULL) |
| 14604 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14605 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14606 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14607 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14608 | int kind = PyUnicode_KIND(seq); |
| 14609 | void *data = PyUnicode_DATA(seq); |
| 14610 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14611 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14612 | if (item != NULL) |
| 14613 | ++it->it_index; |
| 14614 | return item; |
| 14615 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14616 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14617 | Py_DECREF(seq); |
| 14618 | it->it_seq = NULL; |
| 14619 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14620 | } |
| 14621 | |
| 14622 | static PyObject * |
| 14623 | unicodeiter_len(unicodeiterobject *it) |
| 14624 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14625 | Py_ssize_t len = 0; |
| 14626 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14627 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14628 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14629 | } |
| 14630 | |
| 14631 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14632 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14633 | static PyObject * |
| 14634 | unicodeiter_reduce(unicodeiterobject *it) |
| 14635 | { |
| 14636 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14637 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14638 | it->it_seq, it->it_index); |
| 14639 | } else { |
| 14640 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14641 | if (u == NULL) |
| 14642 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14643 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14644 | } |
| 14645 | } |
| 14646 | |
| 14647 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14648 | |
| 14649 | static PyObject * |
| 14650 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14651 | { |
| 14652 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14653 | if (index == -1 && PyErr_Occurred()) |
| 14654 | return NULL; |
| 14655 | if (index < 0) |
| 14656 | index = 0; |
| 14657 | it->it_index = index; |
| 14658 | Py_RETURN_NONE; |
| 14659 | } |
| 14660 | |
| 14661 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14662 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14663 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14664 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14665 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14666 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14667 | reduce_doc}, |
| 14668 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14669 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14670 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14671 | }; |
| 14672 | |
| 14673 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14674 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14675 | "str_iterator", /* tp_name */ |
| 14676 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14677 | 0, /* tp_itemsize */ |
| 14678 | /* methods */ |
| 14679 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14680 | 0, /* tp_print */ |
| 14681 | 0, /* tp_getattr */ |
| 14682 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14683 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14684 | 0, /* tp_repr */ |
| 14685 | 0, /* tp_as_number */ |
| 14686 | 0, /* tp_as_sequence */ |
| 14687 | 0, /* tp_as_mapping */ |
| 14688 | 0, /* tp_hash */ |
| 14689 | 0, /* tp_call */ |
| 14690 | 0, /* tp_str */ |
| 14691 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14692 | 0, /* tp_setattro */ |
| 14693 | 0, /* tp_as_buffer */ |
| 14694 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14695 | 0, /* tp_doc */ |
| 14696 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14697 | 0, /* tp_clear */ |
| 14698 | 0, /* tp_richcompare */ |
| 14699 | 0, /* tp_weaklistoffset */ |
| 14700 | PyObject_SelfIter, /* tp_iter */ |
| 14701 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14702 | unicodeiter_methods, /* tp_methods */ |
| 14703 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14704 | }; |
| 14705 | |
| 14706 | static PyObject * |
| 14707 | unicode_iter(PyObject *seq) |
| 14708 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14709 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14710 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14711 | if (!PyUnicode_Check(seq)) { |
| 14712 | PyErr_BadInternalCall(); |
| 14713 | return NULL; |
| 14714 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14715 | if (PyUnicode_READY(seq) == -1) |
| 14716 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14717 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14718 | if (it == NULL) |
| 14719 | return NULL; |
| 14720 | it->it_index = 0; |
| 14721 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14722 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14723 | _PyObject_GC_TRACK(it); |
| 14724 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14725 | } |
| 14726 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14727 | |
| 14728 | size_t |
| 14729 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14730 | { |
| 14731 | int res = 0; |
| 14732 | while(*u++) |
| 14733 | res++; |
| 14734 | return res; |
| 14735 | } |
| 14736 | |
| 14737 | Py_UNICODE* |
| 14738 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14739 | { |
| 14740 | Py_UNICODE *u = s1; |
| 14741 | while ((*u++ = *s2++)); |
| 14742 | return s1; |
| 14743 | } |
| 14744 | |
| 14745 | Py_UNICODE* |
| 14746 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14747 | { |
| 14748 | Py_UNICODE *u = s1; |
| 14749 | while ((*u++ = *s2++)) |
| 14750 | if (n-- == 0) |
| 14751 | break; |
| 14752 | return s1; |
| 14753 | } |
| 14754 | |
| 14755 | Py_UNICODE* |
| 14756 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14757 | { |
| 14758 | Py_UNICODE *u1 = s1; |
| 14759 | u1 += Py_UNICODE_strlen(u1); |
| 14760 | Py_UNICODE_strcpy(u1, s2); |
| 14761 | return s1; |
| 14762 | } |
| 14763 | |
| 14764 | int |
| 14765 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14766 | { |
| 14767 | while (*s1 && *s2 && *s1 == *s2) |
| 14768 | s1++, s2++; |
| 14769 | if (*s1 && *s2) |
| 14770 | return (*s1 < *s2) ? -1 : +1; |
| 14771 | if (*s1) |
| 14772 | return 1; |
| 14773 | if (*s2) |
| 14774 | return -1; |
| 14775 | return 0; |
| 14776 | } |
| 14777 | |
| 14778 | int |
| 14779 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14780 | { |
| 14781 | register Py_UNICODE u1, u2; |
| 14782 | for (; n != 0; n--) { |
| 14783 | u1 = *s1; |
| 14784 | u2 = *s2; |
| 14785 | if (u1 != u2) |
| 14786 | return (u1 < u2) ? -1 : +1; |
| 14787 | if (u1 == '\0') |
| 14788 | return 0; |
| 14789 | s1++; |
| 14790 | s2++; |
| 14791 | } |
| 14792 | return 0; |
| 14793 | } |
| 14794 | |
| 14795 | Py_UNICODE* |
| 14796 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14797 | { |
| 14798 | const Py_UNICODE *p; |
| 14799 | for (p = s; *p; p++) |
| 14800 | if (*p == c) |
| 14801 | return (Py_UNICODE*)p; |
| 14802 | return NULL; |
| 14803 | } |
| 14804 | |
| 14805 | Py_UNICODE* |
| 14806 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14807 | { |
| 14808 | const Py_UNICODE *p; |
| 14809 | p = s + Py_UNICODE_strlen(s); |
| 14810 | while (p != s) { |
| 14811 | p--; |
| 14812 | if (*p == c) |
| 14813 | return (Py_UNICODE*)p; |
| 14814 | } |
| 14815 | return NULL; |
| 14816 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14817 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14818 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14819 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14820 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14821 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14822 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14823 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14824 | if (!PyUnicode_Check(unicode)) { |
| 14825 | PyErr_BadArgument(); |
| 14826 | return NULL; |
| 14827 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14828 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14829 | if (u == NULL) |
| 14830 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14831 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14832 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14833 | PyErr_NoMemory(); |
| 14834 | return NULL; |
| 14835 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14836 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14837 | size *= sizeof(Py_UNICODE); |
| 14838 | copy = PyMem_Malloc(size); |
| 14839 | if (copy == NULL) { |
| 14840 | PyErr_NoMemory(); |
| 14841 | return NULL; |
| 14842 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14843 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14844 | return copy; |
| 14845 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14846 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14847 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14848 | to the string.Formatter class implemented in Python. */ |
| 14849 | |
| 14850 | static PyMethodDef _string_methods[] = { |
| 14851 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14852 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14853 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14854 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14855 | {NULL, NULL} |
| 14856 | }; |
| 14857 | |
| 14858 | static struct PyModuleDef _string_module = { |
| 14859 | PyModuleDef_HEAD_INIT, |
| 14860 | "_string", |
| 14861 | PyDoc_STR("string helper module"), |
| 14862 | 0, |
| 14863 | _string_methods, |
| 14864 | NULL, |
| 14865 | NULL, |
| 14866 | NULL, |
| 14867 | NULL |
| 14868 | }; |
| 14869 | |
| 14870 | PyMODINIT_FUNC |
| 14871 | PyInit__string(void) |
| 14872 | { |
| 14873 | return PyModule_Create(&_string_module); |
| 14874 | } |
| 14875 | |
| 14876 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14877 | #ifdef __cplusplus |
| 14878 | } |
| 14879 | #endif |