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) { |
| 471 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 472 | if (ch < 256) { |
| 473 | PyObject *latin1_char = unicode_latin1[ch]; |
| 474 | if (latin1_char != NULL) { |
| 475 | if (unicode != latin1_char) { |
| 476 | Py_INCREF(latin1_char); |
| 477 | Py_DECREF(unicode); |
| 478 | } |
| 479 | return latin1_char; |
| 480 | } |
| 481 | else { |
| 482 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 483 | Py_INCREF(unicode); |
| 484 | unicode_latin1[ch] = unicode; |
| 485 | return unicode; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 491 | return unicode; |
| 492 | } |
| 493 | |
| 494 | static PyObject* |
| 495 | unicode_result(PyObject *unicode) |
| 496 | { |
| 497 | assert(_PyUnicode_CHECK(unicode)); |
| 498 | if (PyUnicode_IS_READY(unicode)) |
| 499 | return unicode_result_ready(unicode); |
| 500 | else |
| 501 | return unicode_result_wchar(unicode); |
| 502 | } |
| 503 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 504 | static PyObject* |
| 505 | unicode_result_unchanged(PyObject *unicode) |
| 506 | { |
| 507 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 508 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 509 | return NULL; |
| 510 | Py_INCREF(unicode); |
| 511 | return unicode; |
| 512 | } |
| 513 | else |
| 514 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 515 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 516 | } |
| 517 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 518 | #ifdef HAVE_MBCS |
| 519 | static OSVERSIONINFOEX winver; |
| 520 | #endif |
| 521 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 522 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 523 | |
| 524 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 525 | to keep things simple, we use a single bitmask, using the least 5 |
| 526 | bits from each unicode characters as the bit index. */ |
| 527 | |
| 528 | /* the linebreak mask is set up by Unicode_Init below */ |
| 529 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 530 | #if LONG_BIT >= 128 |
| 531 | #define BLOOM_WIDTH 128 |
| 532 | #elif LONG_BIT >= 64 |
| 533 | #define BLOOM_WIDTH 64 |
| 534 | #elif LONG_BIT >= 32 |
| 535 | #define BLOOM_WIDTH 32 |
| 536 | #else |
| 537 | #error "LONG_BIT is smaller than 32" |
| 538 | #endif |
| 539 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 540 | #define BLOOM_MASK unsigned long |
| 541 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 542 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 543 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 544 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 545 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 546 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 547 | #define BLOOM_LINEBREAK(ch) \ |
| 548 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 549 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 550 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 551 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 552 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 553 | { |
| 554 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 555 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 556 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 557 | Py_ssize_t i; |
| 558 | |
| 559 | mask = 0; |
| 560 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 561 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 562 | |
| 563 | return mask; |
| 564 | } |
| 565 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 566 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 567 | (BLOOM(mask, chr) \ |
| 568 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 569 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 570 | /* Compilation of templated routines */ |
| 571 | |
| 572 | #include "stringlib/asciilib.h" |
| 573 | #include "stringlib/fastsearch.h" |
| 574 | #include "stringlib/partition.h" |
| 575 | #include "stringlib/split.h" |
| 576 | #include "stringlib/count.h" |
| 577 | #include "stringlib/find.h" |
| 578 | #include "stringlib/find_max_char.h" |
| 579 | #include "stringlib/localeutil.h" |
| 580 | #include "stringlib/undef.h" |
| 581 | |
| 582 | #include "stringlib/ucs1lib.h" |
| 583 | #include "stringlib/fastsearch.h" |
| 584 | #include "stringlib/partition.h" |
| 585 | #include "stringlib/split.h" |
| 586 | #include "stringlib/count.h" |
| 587 | #include "stringlib/find.h" |
| 588 | #include "stringlib/find_max_char.h" |
| 589 | #include "stringlib/localeutil.h" |
| 590 | #include "stringlib/undef.h" |
| 591 | |
| 592 | #include "stringlib/ucs2lib.h" |
| 593 | #include "stringlib/fastsearch.h" |
| 594 | #include "stringlib/partition.h" |
| 595 | #include "stringlib/split.h" |
| 596 | #include "stringlib/count.h" |
| 597 | #include "stringlib/find.h" |
| 598 | #include "stringlib/find_max_char.h" |
| 599 | #include "stringlib/localeutil.h" |
| 600 | #include "stringlib/undef.h" |
| 601 | |
| 602 | #include "stringlib/ucs4lib.h" |
| 603 | #include "stringlib/fastsearch.h" |
| 604 | #include "stringlib/partition.h" |
| 605 | #include "stringlib/split.h" |
| 606 | #include "stringlib/count.h" |
| 607 | #include "stringlib/find.h" |
| 608 | #include "stringlib/find_max_char.h" |
| 609 | #include "stringlib/localeutil.h" |
| 610 | #include "stringlib/undef.h" |
| 611 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 612 | #include "stringlib/unicodedefs.h" |
| 613 | #include "stringlib/fastsearch.h" |
| 614 | #include "stringlib/count.h" |
| 615 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 616 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 617 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 618 | /* --- Unicode Object ----------------------------------------------------- */ |
| 619 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 620 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 621 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 622 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 623 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 624 | Py_ssize_t size, Py_UCS4 ch, |
| 625 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 626 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 627 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 628 | |
| 629 | switch (kind) { |
| 630 | case PyUnicode_1BYTE_KIND: |
| 631 | { |
| 632 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 633 | if (ch1 == ch) |
| 634 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 635 | else |
| 636 | return -1; |
| 637 | } |
| 638 | case PyUnicode_2BYTE_KIND: |
| 639 | { |
| 640 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 641 | if (ch2 == ch) |
| 642 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 643 | else |
| 644 | return -1; |
| 645 | } |
| 646 | case PyUnicode_4BYTE_KIND: |
| 647 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 648 | default: |
| 649 | assert(0); |
| 650 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 651 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 654 | #ifdef Py_DEBUG |
| 655 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 656 | earlier. |
| 657 | |
| 658 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 659 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 660 | invalid character in Unicode 6.0. */ |
| 661 | static void |
| 662 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 663 | { |
| 664 | int kind = PyUnicode_KIND(unicode); |
| 665 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 666 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 667 | if (length <= old_length) |
| 668 | return; |
| 669 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 670 | } |
| 671 | #endif |
| 672 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 673 | static PyObject* |
| 674 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 675 | { |
| 676 | Py_ssize_t char_size; |
| 677 | Py_ssize_t struct_size; |
| 678 | Py_ssize_t new_size; |
| 679 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 680 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 681 | #ifdef Py_DEBUG |
| 682 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 683 | #endif |
| 684 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 685 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 686 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 687 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 688 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 689 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 690 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 691 | struct_size = sizeof(PyASCIIObject); |
| 692 | else |
| 693 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 694 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 695 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 696 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 697 | PyErr_NoMemory(); |
| 698 | return NULL; |
| 699 | } |
| 700 | new_size = (struct_size + (length + 1) * char_size); |
| 701 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 702 | _Py_DEC_REFTOTAL; |
| 703 | _Py_ForgetReference(unicode); |
| 704 | |
| 705 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 706 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 707 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 708 | PyErr_NoMemory(); |
| 709 | return NULL; |
| 710 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 711 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 712 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 713 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 714 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 715 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 716 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 717 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 718 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 719 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 720 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 721 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 722 | _PyUnicode_WSTR(unicode) = NULL; |
| 723 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 724 | #ifdef Py_DEBUG |
| 725 | unicode_fill_invalid(unicode, old_length); |
| 726 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 727 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 728 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 729 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 730 | return unicode; |
| 731 | } |
| 732 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 733 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 734 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 735 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 736 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 737 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 738 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 739 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 740 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 741 | if (PyUnicode_IS_READY(unicode)) { |
| 742 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 743 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 744 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 745 | #ifdef Py_DEBUG |
| 746 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 747 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 748 | |
| 749 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 750 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 751 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 752 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 753 | |
| 754 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 755 | PyErr_NoMemory(); |
| 756 | return -1; |
| 757 | } |
| 758 | new_size = (length + 1) * char_size; |
| 759 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 760 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 761 | { |
| 762 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 763 | _PyUnicode_UTF8(unicode) = NULL; |
| 764 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 765 | } |
| 766 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 767 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 768 | if (data == NULL) { |
| 769 | PyErr_NoMemory(); |
| 770 | return -1; |
| 771 | } |
| 772 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 773 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 774 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 775 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 776 | } |
| 777 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 778 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 779 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 780 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 781 | _PyUnicode_LENGTH(unicode) = length; |
| 782 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 783 | #ifdef Py_DEBUG |
| 784 | unicode_fill_invalid(unicode, old_length); |
| 785 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 786 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 787 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 788 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 789 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 790 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 791 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 792 | |
| 793 | /* check for integer overflow */ |
| 794 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 795 | PyErr_NoMemory(); |
| 796 | return -1; |
| 797 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 798 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 799 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 800 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 801 | if (!wstr) { |
| 802 | PyErr_NoMemory(); |
| 803 | return -1; |
| 804 | } |
| 805 | _PyUnicode_WSTR(unicode) = wstr; |
| 806 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 807 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 808 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 812 | static PyObject* |
| 813 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 814 | { |
| 815 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 816 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 817 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 818 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 819 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 820 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 821 | |
| 822 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 823 | if (copy == NULL) |
| 824 | return NULL; |
| 825 | |
| 826 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 827 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 828 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 829 | } |
| 830 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 831 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 832 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 833 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 834 | if (w == NULL) |
| 835 | return NULL; |
| 836 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 837 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 838 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 839 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 840 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 844 | /* 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] | 845 | Ux0000 terminated; some code (e.g. new_identifier) |
| 846 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 847 | |
| 848 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 849 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 850 | |
| 851 | */ |
| 852 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 853 | static PyUnicodeObject * |
| 854 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 855 | { |
| 856 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 857 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 858 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 859 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 860 | if (length == 0 && unicode_empty != NULL) { |
| 861 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 862 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 865 | /* Ensure we won't overflow the size. */ |
| 866 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 867 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 868 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 869 | if (length < 0) { |
| 870 | PyErr_SetString(PyExc_SystemError, |
| 871 | "Negative size passed to _PyUnicode_New"); |
| 872 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 875 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 876 | if (unicode == NULL) |
| 877 | return NULL; |
| 878 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 879 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 880 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 881 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 882 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 883 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 884 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 885 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 886 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 887 | * the caller fails before initializing str -- unicode_resize() |
| 888 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 889 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 890 | * We don't want unicode_resize to read uninitialized memory in |
| 891 | * that case. |
| 892 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 893 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 894 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 895 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 896 | _PyUnicode_HASH(unicode) = -1; |
| 897 | _PyUnicode_STATE(unicode).interned = 0; |
| 898 | _PyUnicode_STATE(unicode).kind = 0; |
| 899 | _PyUnicode_STATE(unicode).compact = 0; |
| 900 | _PyUnicode_STATE(unicode).ready = 0; |
| 901 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 902 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 903 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 904 | _PyUnicode_UTF8(unicode) = NULL; |
| 905 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 906 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 907 | return unicode; |
| 908 | } |
| 909 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 910 | static const char* |
| 911 | unicode_kind_name(PyObject *unicode) |
| 912 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 913 | /* don't check consistency: unicode_kind_name() is called from |
| 914 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 915 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 916 | { |
| 917 | if (!PyUnicode_IS_READY(unicode)) |
| 918 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 919 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 920 | { |
| 921 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 922 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 923 | return "legacy ascii"; |
| 924 | else |
| 925 | return "legacy latin1"; |
| 926 | case PyUnicode_2BYTE_KIND: |
| 927 | return "legacy UCS2"; |
| 928 | case PyUnicode_4BYTE_KIND: |
| 929 | return "legacy UCS4"; |
| 930 | default: |
| 931 | return "<legacy invalid kind>"; |
| 932 | } |
| 933 | } |
| 934 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 935 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 936 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 937 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 938 | return "ascii"; |
| 939 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 940 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 941 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 942 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 943 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 944 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 945 | default: |
| 946 | return "<invalid compact kind>"; |
| 947 | } |
| 948 | } |
| 949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 950 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 951 | /* Functions wrapping macros for use in debugger */ |
| 952 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 953 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | void *_PyUnicode_compact_data(void *unicode) { |
| 957 | return _PyUnicode_COMPACT_DATA(unicode); |
| 958 | } |
| 959 | void *_PyUnicode_data(void *unicode){ |
| 960 | printf("obj %p\n", unicode); |
| 961 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 962 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 963 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 964 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 965 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 966 | return PyUnicode_DATA(unicode); |
| 967 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 968 | |
| 969 | void |
| 970 | _PyUnicode_Dump(PyObject *op) |
| 971 | { |
| 972 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 973 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 974 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 975 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 976 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 977 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 978 | { |
| 979 | if (ascii->state.ascii) |
| 980 | data = (ascii + 1); |
| 981 | else |
| 982 | data = (compact + 1); |
| 983 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 984 | else |
| 985 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 986 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 987 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 988 | if (ascii->wstr == data) |
| 989 | printf("shared "); |
| 990 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 991 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 992 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 993 | printf(" (%zu), ", compact->wstr_length); |
| 994 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 995 | printf("shared "); |
| 996 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 997 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 998 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 999 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1000 | #endif |
| 1001 | |
| 1002 | PyObject * |
| 1003 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1004 | { |
| 1005 | PyObject *obj; |
| 1006 | PyCompactUnicodeObject *unicode; |
| 1007 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1008 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1009 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1010 | Py_ssize_t char_size; |
| 1011 | Py_ssize_t struct_size; |
| 1012 | |
| 1013 | /* Optimization for empty strings */ |
| 1014 | if (size == 0 && unicode_empty != NULL) { |
| 1015 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1016 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1019 | is_ascii = 0; |
| 1020 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1021 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1022 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1023 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1024 | char_size = 1; |
| 1025 | is_ascii = 1; |
| 1026 | struct_size = sizeof(PyASCIIObject); |
| 1027 | } |
| 1028 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1029 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1030 | char_size = 1; |
| 1031 | } |
| 1032 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1033 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1034 | char_size = 2; |
| 1035 | if (sizeof(wchar_t) == 2) |
| 1036 | is_sharing = 1; |
| 1037 | } |
| 1038 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1039 | if (maxchar > MAX_UNICODE) { |
| 1040 | PyErr_SetString(PyExc_SystemError, |
| 1041 | "invalid maximum character passed to PyUnicode_New"); |
| 1042 | return NULL; |
| 1043 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1044 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1045 | char_size = 4; |
| 1046 | if (sizeof(wchar_t) == 4) |
| 1047 | is_sharing = 1; |
| 1048 | } |
| 1049 | |
| 1050 | /* Ensure we won't overflow the size. */ |
| 1051 | if (size < 0) { |
| 1052 | PyErr_SetString(PyExc_SystemError, |
| 1053 | "Negative size passed to PyUnicode_New"); |
| 1054 | return NULL; |
| 1055 | } |
| 1056 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1057 | return PyErr_NoMemory(); |
| 1058 | |
| 1059 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1060 | * PyObject_New() so we are able to allocate space for the object and |
| 1061 | * it's data buffer. |
| 1062 | */ |
| 1063 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1064 | if (obj == NULL) |
| 1065 | return PyErr_NoMemory(); |
| 1066 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1067 | if (obj == NULL) |
| 1068 | return NULL; |
| 1069 | |
| 1070 | unicode = (PyCompactUnicodeObject *)obj; |
| 1071 | if (is_ascii) |
| 1072 | data = ((PyASCIIObject*)obj) + 1; |
| 1073 | else |
| 1074 | data = unicode + 1; |
| 1075 | _PyUnicode_LENGTH(unicode) = size; |
| 1076 | _PyUnicode_HASH(unicode) = -1; |
| 1077 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1078 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1079 | _PyUnicode_STATE(unicode).compact = 1; |
| 1080 | _PyUnicode_STATE(unicode).ready = 1; |
| 1081 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1082 | if (is_ascii) { |
| 1083 | ((char*)data)[size] = 0; |
| 1084 | _PyUnicode_WSTR(unicode) = NULL; |
| 1085 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1086 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1087 | ((char*)data)[size] = 0; |
| 1088 | _PyUnicode_WSTR(unicode) = NULL; |
| 1089 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1090 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1091 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1092 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1093 | else { |
| 1094 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1095 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1096 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1097 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1098 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1099 | ((Py_UCS4*)data)[size] = 0; |
| 1100 | if (is_sharing) { |
| 1101 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1102 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1103 | } |
| 1104 | else { |
| 1105 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1106 | _PyUnicode_WSTR(unicode) = NULL; |
| 1107 | } |
| 1108 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1109 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1110 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1111 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1112 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1113 | return obj; |
| 1114 | } |
| 1115 | |
| 1116 | #if SIZEOF_WCHAR_T == 2 |
| 1117 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1118 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1119 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | |
| 1121 | This function assumes that unicode can hold one more code point than wstr |
| 1122 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1123 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1124 | 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] | 1125 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1126 | { |
| 1127 | const wchar_t *iter; |
| 1128 | Py_UCS4 *ucs4_out; |
| 1129 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1130 | assert(unicode != NULL); |
| 1131 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1132 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1133 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1134 | |
| 1135 | for (iter = begin; iter < end; ) { |
| 1136 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1137 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1138 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1139 | && (iter+1) < end |
| 1140 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1141 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1142 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1143 | iter += 2; |
| 1144 | } |
| 1145 | else { |
| 1146 | *ucs4_out++ = *iter; |
| 1147 | iter++; |
| 1148 | } |
| 1149 | } |
| 1150 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1151 | _PyUnicode_GET_LENGTH(unicode))); |
| 1152 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1153 | } |
| 1154 | #endif |
| 1155 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1156 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1157 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1158 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1159 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1160 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1161 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1162 | return -1; |
| 1163 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1164 | return 0; |
| 1165 | } |
| 1166 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1167 | static int |
| 1168 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1169 | PyObject *from, Py_ssize_t from_start, |
| 1170 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1171 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1172 | unsigned int from_kind, to_kind; |
| 1173 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1174 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1175 | assert(0 <= how_many); |
| 1176 | assert(0 <= from_start); |
| 1177 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1178 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1179 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1180 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1181 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1182 | assert(PyUnicode_Check(to)); |
| 1183 | assert(PyUnicode_IS_READY(to)); |
| 1184 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1185 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1186 | if (how_many == 0) |
| 1187 | return 0; |
| 1188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1189 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1190 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1191 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1192 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1193 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1194 | #ifdef Py_DEBUG |
| 1195 | if (!check_maxchar |
| 1196 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1197 | { |
| 1198 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1199 | Py_UCS4 ch; |
| 1200 | Py_ssize_t i; |
| 1201 | for (i=0; i < how_many; i++) { |
| 1202 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1203 | assert(ch <= to_maxchar); |
| 1204 | } |
| 1205 | } |
| 1206 | #endif |
| 1207 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1208 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1209 | if (check_maxchar |
| 1210 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1211 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1212 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1213 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1214 | Py_UCS4 max_char; |
| 1215 | max_char = ucs1lib_find_max_char(from_data, |
| 1216 | (Py_UCS1*)from_data + how_many); |
| 1217 | if (max_char >= 128) |
| 1218 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1219 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1220 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1221 | (char*)from_data + from_kind * from_start, |
| 1222 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1223 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1224 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1225 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1226 | { |
| 1227 | _PyUnicode_CONVERT_BYTES( |
| 1228 | Py_UCS1, Py_UCS2, |
| 1229 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1230 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1231 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1232 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1233 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1234 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1235 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1236 | { |
| 1237 | _PyUnicode_CONVERT_BYTES( |
| 1238 | Py_UCS1, Py_UCS4, |
| 1239 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1240 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1241 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1242 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1243 | } |
| 1244 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1245 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1246 | { |
| 1247 | _PyUnicode_CONVERT_BYTES( |
| 1248 | Py_UCS2, Py_UCS4, |
| 1249 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1250 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1251 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1252 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1253 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1254 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1255 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1256 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1257 | if (!check_maxchar) { |
| 1258 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1259 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1260 | { |
| 1261 | _PyUnicode_CONVERT_BYTES( |
| 1262 | Py_UCS2, Py_UCS1, |
| 1263 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1264 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1265 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1266 | ); |
| 1267 | } |
| 1268 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1269 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1270 | { |
| 1271 | _PyUnicode_CONVERT_BYTES( |
| 1272 | Py_UCS4, Py_UCS1, |
| 1273 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1274 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1275 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1276 | ); |
| 1277 | } |
| 1278 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1279 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1280 | { |
| 1281 | _PyUnicode_CONVERT_BYTES( |
| 1282 | Py_UCS4, Py_UCS2, |
| 1283 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1284 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1285 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1286 | ); |
| 1287 | } |
| 1288 | else { |
| 1289 | assert(0); |
| 1290 | return -1; |
| 1291 | } |
| 1292 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1293 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1294 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1295 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1296 | Py_ssize_t i; |
| 1297 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1298 | for (i=0; i < how_many; i++) { |
| 1299 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1300 | if (ch > to_maxchar) |
| 1301 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1302 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1303 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1304 | } |
| 1305 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1306 | return 0; |
| 1307 | } |
| 1308 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1309 | void |
| 1310 | _PyUnicode_FastCopyCharacters( |
| 1311 | PyObject *to, Py_ssize_t to_start, |
| 1312 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1313 | { |
| 1314 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1315 | } |
| 1316 | |
| 1317 | Py_ssize_t |
| 1318 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1319 | PyObject *from, Py_ssize_t from_start, |
| 1320 | Py_ssize_t how_many) |
| 1321 | { |
| 1322 | int err; |
| 1323 | |
| 1324 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1325 | PyErr_BadInternalCall(); |
| 1326 | return -1; |
| 1327 | } |
| 1328 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1329 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1330 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1331 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1332 | return -1; |
| 1333 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1334 | if (from_start < 0) { |
| 1335 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1336 | return -1; |
| 1337 | } |
| 1338 | if (to_start < 0) { |
| 1339 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1340 | return -1; |
| 1341 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1342 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1343 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1344 | PyErr_Format(PyExc_SystemError, |
| 1345 | "Cannot write %zi characters at %zi " |
| 1346 | "in a string of %zi characters", |
| 1347 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1348 | return -1; |
| 1349 | } |
| 1350 | |
| 1351 | if (how_many == 0) |
| 1352 | return 0; |
| 1353 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1354 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1355 | return -1; |
| 1356 | |
| 1357 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1358 | if (err) { |
| 1359 | PyErr_Format(PyExc_SystemError, |
| 1360 | "Cannot copy %s characters " |
| 1361 | "into a string of %s characters", |
| 1362 | unicode_kind_name(from), |
| 1363 | unicode_kind_name(to)); |
| 1364 | return -1; |
| 1365 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1366 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1367 | } |
| 1368 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1369 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1370 | correct string length can be computed before converting a string to UCS4. |
| 1371 | This function counts single surrogates as a character and not as a pair. |
| 1372 | |
| 1373 | Return 0 on success, or -1 on error. */ |
| 1374 | static int |
| 1375 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1376 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1377 | { |
| 1378 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1379 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1380 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1381 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1382 | *num_surrogates = 0; |
| 1383 | *maxchar = 0; |
| 1384 | |
| 1385 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1386 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1387 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1388 | && (iter+1) < end |
| 1389 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1390 | { |
| 1391 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1392 | ++(*num_surrogates); |
| 1393 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1394 | } |
| 1395 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1396 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1397 | { |
| 1398 | ch = *iter; |
| 1399 | iter++; |
| 1400 | } |
| 1401 | if (ch > *maxchar) { |
| 1402 | *maxchar = ch; |
| 1403 | if (*maxchar > MAX_UNICODE) { |
| 1404 | PyErr_Format(PyExc_ValueError, |
| 1405 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1406 | ch); |
| 1407 | return -1; |
| 1408 | } |
| 1409 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1410 | } |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1414 | int |
| 1415 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1416 | { |
| 1417 | wchar_t *end; |
| 1418 | Py_UCS4 maxchar = 0; |
| 1419 | Py_ssize_t num_surrogates; |
| 1420 | #if SIZEOF_WCHAR_T == 2 |
| 1421 | Py_ssize_t length_wo_surrogates; |
| 1422 | #endif |
| 1423 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1424 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1425 | strings were created using _PyObject_New() and where no canonical |
| 1426 | representation (the str field) has been set yet aka strings |
| 1427 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1428 | assert(_PyUnicode_CHECK(unicode)); |
| 1429 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1430 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1431 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1432 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1433 | /* Actually, it should neither be interned nor be anything else: */ |
| 1434 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1435 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1436 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1437 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1438 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1439 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1440 | |
| 1441 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1442 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1443 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1444 | PyErr_NoMemory(); |
| 1445 | return -1; |
| 1446 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1447 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1448 | _PyUnicode_WSTR(unicode), end, |
| 1449 | PyUnicode_1BYTE_DATA(unicode)); |
| 1450 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1451 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1452 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1453 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1454 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1455 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1456 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1457 | } |
| 1458 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1459 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1460 | _PyUnicode_UTF8(unicode) = NULL; |
| 1461 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1462 | } |
| 1463 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1464 | _PyUnicode_WSTR(unicode) = NULL; |
| 1465 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1466 | } |
| 1467 | /* In this case we might have to convert down from 4-byte native |
| 1468 | wchar_t to 2-byte unicode. */ |
| 1469 | else if (maxchar < 65536) { |
| 1470 | assert(num_surrogates == 0 && |
| 1471 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1472 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1473 | #if SIZEOF_WCHAR_T == 2 |
| 1474 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1475 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1476 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1477 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1478 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1479 | _PyUnicode_UTF8(unicode) = NULL; |
| 1480 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1481 | #else |
| 1482 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1483 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1484 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1485 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1486 | PyErr_NoMemory(); |
| 1487 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1488 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1489 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1490 | _PyUnicode_WSTR(unicode), end, |
| 1491 | PyUnicode_2BYTE_DATA(unicode)); |
| 1492 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1493 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1494 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1495 | _PyUnicode_UTF8(unicode) = NULL; |
| 1496 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1497 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1498 | _PyUnicode_WSTR(unicode) = NULL; |
| 1499 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1500 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1501 | } |
| 1502 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1503 | else { |
| 1504 | #if SIZEOF_WCHAR_T == 2 |
| 1505 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1506 | new normalized 4-byte version. */ |
| 1507 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1508 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1509 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1510 | PyErr_NoMemory(); |
| 1511 | return -1; |
| 1512 | } |
| 1513 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1514 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1515 | _PyUnicode_UTF8(unicode) = NULL; |
| 1516 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1517 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1518 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1519 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1520 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1521 | _PyUnicode_WSTR(unicode) = NULL; |
| 1522 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1523 | #else |
| 1524 | assert(num_surrogates == 0); |
| 1525 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1526 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1527 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1528 | _PyUnicode_UTF8(unicode) = NULL; |
| 1529 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1530 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1531 | #endif |
| 1532 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1533 | } |
| 1534 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1535 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1536 | return 0; |
| 1537 | } |
| 1538 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1539 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1540 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1541 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1542 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1543 | case SSTATE_NOT_INTERNED: |
| 1544 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1545 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1546 | case SSTATE_INTERNED_MORTAL: |
| 1547 | /* revive dead object temporarily for DelItem */ |
| 1548 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1549 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1550 | Py_FatalError( |
| 1551 | "deletion of interned string failed"); |
| 1552 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1553 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1554 | case SSTATE_INTERNED_IMMORTAL: |
| 1555 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1556 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1557 | default: |
| 1558 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1561 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1562 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1563 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1564 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1565 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1566 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1567 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1568 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1571 | #ifdef Py_DEBUG |
| 1572 | static int |
| 1573 | unicode_is_singleton(PyObject *unicode) |
| 1574 | { |
| 1575 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1576 | if (unicode == unicode_empty) |
| 1577 | return 1; |
| 1578 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1579 | { |
| 1580 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1581 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1582 | return 1; |
| 1583 | } |
| 1584 | return 0; |
| 1585 | } |
| 1586 | #endif |
| 1587 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1588 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1589 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1590 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1591 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1592 | if (Py_REFCNT(unicode) != 1) |
| 1593 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1594 | if (_PyUnicode_HASH(unicode) != -1) |
| 1595 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1596 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1597 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1598 | if (!PyUnicode_CheckExact(unicode)) |
| 1599 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1600 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1601 | /* singleton refcount is greater than 1 */ |
| 1602 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1603 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1604 | return 1; |
| 1605 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1606 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1607 | static int |
| 1608 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1609 | { |
| 1610 | PyObject *unicode; |
| 1611 | Py_ssize_t old_length; |
| 1612 | |
| 1613 | assert(p_unicode != NULL); |
| 1614 | unicode = *p_unicode; |
| 1615 | |
| 1616 | assert(unicode != NULL); |
| 1617 | assert(PyUnicode_Check(unicode)); |
| 1618 | assert(0 <= length); |
| 1619 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1620 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1621 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1622 | else |
| 1623 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1624 | if (old_length == length) |
| 1625 | return 0; |
| 1626 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1627 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1628 | _Py_INCREF_UNICODE_EMPTY(); |
| 1629 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1630 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1631 | Py_DECREF(*p_unicode); |
| 1632 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1633 | return 0; |
| 1634 | } |
| 1635 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1636 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1637 | PyObject *copy = resize_copy(unicode, length); |
| 1638 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1639 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1640 | Py_DECREF(*p_unicode); |
| 1641 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1642 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1645 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1646 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1647 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1648 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1649 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1650 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1651 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1652 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1655 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1656 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1657 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1658 | PyObject *unicode; |
| 1659 | if (p_unicode == NULL) { |
| 1660 | PyErr_BadInternalCall(); |
| 1661 | return -1; |
| 1662 | } |
| 1663 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1664 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1665 | { |
| 1666 | PyErr_BadInternalCall(); |
| 1667 | return -1; |
| 1668 | } |
| 1669 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1670 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1671 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1672 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1673 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1674 | WARNING: The function doesn't copy the terminating null character and |
| 1675 | doesn't check the maximum character (may write a latin1 character in an |
| 1676 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1677 | static void |
| 1678 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1679 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1680 | { |
| 1681 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1682 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1683 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1684 | |
| 1685 | switch (kind) { |
| 1686 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1687 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1688 | #ifdef Py_DEBUG |
| 1689 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1690 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1691 | (const Py_UCS1*)str, |
| 1692 | (const Py_UCS1*)str + len); |
| 1693 | assert(maxchar < 128); |
| 1694 | } |
| 1695 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1696 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1697 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1698 | } |
| 1699 | case PyUnicode_2BYTE_KIND: { |
| 1700 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1701 | Py_UCS2 *ucs2 = start; |
| 1702 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1703 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1704 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1705 | *ucs2 = (Py_UCS2)*str; |
| 1706 | |
| 1707 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1708 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1709 | } |
| 1710 | default: { |
| 1711 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1712 | Py_UCS4 *ucs4 = start; |
| 1713 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1714 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1715 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1716 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1717 | *ucs4 = (Py_UCS4)*str; |
| 1718 | |
| 1719 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1725 | static PyObject* |
| 1726 | get_latin1_char(unsigned char ch) |
| 1727 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1728 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1729 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1730 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1731 | if (!unicode) |
| 1732 | return NULL; |
| 1733 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1734 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1735 | unicode_latin1[ch] = unicode; |
| 1736 | } |
| 1737 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1738 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1739 | } |
| 1740 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1741 | PyObject * |
| 1742 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1743 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1744 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1745 | Py_UCS4 maxchar = 0; |
| 1746 | Py_ssize_t num_surrogates; |
| 1747 | |
| 1748 | if (u == NULL) |
| 1749 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1750 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1751 | /* If the Unicode data is known at construction time, we can apply |
| 1752 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1753 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1754 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1755 | if (size == 0) |
| 1756 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1757 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | /* Single character Unicode objects in the Latin-1 range are |
| 1759 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1760 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1761 | return get_latin1_char((unsigned char)*u); |
| 1762 | |
| 1763 | /* If not empty and not single character, copy the Unicode data |
| 1764 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1765 | if (find_maxchar_surrogates(u, u + size, |
| 1766 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1767 | return NULL; |
| 1768 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1769 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1770 | if (!unicode) |
| 1771 | return NULL; |
| 1772 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1773 | switch (PyUnicode_KIND(unicode)) { |
| 1774 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1775 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1776 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1777 | break; |
| 1778 | case PyUnicode_2BYTE_KIND: |
| 1779 | #if Py_UNICODE_SIZE == 2 |
| 1780 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1781 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1782 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1783 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1784 | #endif |
| 1785 | break; |
| 1786 | case PyUnicode_4BYTE_KIND: |
| 1787 | #if SIZEOF_WCHAR_T == 2 |
| 1788 | /* This is the only case which has to process surrogates, thus |
| 1789 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1790 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1791 | #else |
| 1792 | assert(num_surrogates == 0); |
| 1793 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1794 | #endif |
| 1795 | break; |
| 1796 | default: |
| 1797 | assert(0 && "Impossible state"); |
| 1798 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1799 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1800 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1803 | PyObject * |
| 1804 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1805 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1806 | if (size < 0) { |
| 1807 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1808 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1809 | return NULL; |
| 1810 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1811 | if (u != NULL) |
| 1812 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1813 | else |
| 1814 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1817 | PyObject * |
| 1818 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1819 | { |
| 1820 | size_t size = strlen(u); |
| 1821 | if (size > PY_SSIZE_T_MAX) { |
| 1822 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1823 | return NULL; |
| 1824 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1825 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1828 | PyObject * |
| 1829 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1830 | { |
| 1831 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1832 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1833 | strlen(id->string), |
| 1834 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1835 | if (!id->object) |
| 1836 | return NULL; |
| 1837 | PyUnicode_InternInPlace(&id->object); |
| 1838 | assert(!id->next); |
| 1839 | id->next = static_strings; |
| 1840 | static_strings = id; |
| 1841 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1842 | return id->object; |
| 1843 | } |
| 1844 | |
| 1845 | void |
| 1846 | _PyUnicode_ClearStaticStrings() |
| 1847 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1848 | _Py_Identifier *tmp, *s = static_strings; |
| 1849 | while (s) { |
| 1850 | Py_DECREF(s->object); |
| 1851 | s->object = NULL; |
| 1852 | tmp = s->next; |
| 1853 | s->next = NULL; |
| 1854 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1855 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1856 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1857 | } |
| 1858 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1859 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1860 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1861 | PyObject* |
| 1862 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1863 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1864 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1865 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1866 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1867 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1868 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1869 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1870 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1871 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1872 | unicode = PyUnicode_New(size, 127); |
| 1873 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1874 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1875 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1876 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1877 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1880 | static Py_UCS4 |
| 1881 | kind_maxchar_limit(unsigned int kind) |
| 1882 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1883 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1884 | case PyUnicode_1BYTE_KIND: |
| 1885 | return 0x80; |
| 1886 | case PyUnicode_2BYTE_KIND: |
| 1887 | return 0x100; |
| 1888 | case PyUnicode_4BYTE_KIND: |
| 1889 | return 0x10000; |
| 1890 | default: |
| 1891 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1892 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1896 | Py_LOCAL_INLINE(Py_UCS4) |
| 1897 | align_maxchar(Py_UCS4 maxchar) |
| 1898 | { |
| 1899 | if (maxchar <= 127) |
| 1900 | return 127; |
| 1901 | else if (maxchar <= 255) |
| 1902 | return 255; |
| 1903 | else if (maxchar <= 65535) |
| 1904 | return 65535; |
| 1905 | else |
| 1906 | return MAX_UNICODE; |
| 1907 | } |
| 1908 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1909 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1910 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1911 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1912 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1913 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1914 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1915 | if (size == 0) |
| 1916 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1917 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1918 | if (size == 1) |
| 1919 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1920 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1921 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1922 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1923 | if (!res) |
| 1924 | return NULL; |
| 1925 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1926 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1927 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1930 | static PyObject* |
| 1931 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1932 | { |
| 1933 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1934 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1935 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1936 | if (size == 0) |
| 1937 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1938 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1939 | if (size == 1) { |
| 1940 | Py_UCS4 ch = u[0]; |
| 1941 | if (ch < 256) |
| 1942 | return get_latin1_char((unsigned char)ch); |
| 1943 | |
| 1944 | res = PyUnicode_New(1, ch); |
| 1945 | if (res == NULL) |
| 1946 | return NULL; |
| 1947 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1948 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1949 | return res; |
| 1950 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1951 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1952 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1953 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1954 | if (!res) |
| 1955 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1956 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1957 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1958 | else { |
| 1959 | _PyUnicode_CONVERT_BYTES( |
| 1960 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1961 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1962 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1963 | return res; |
| 1964 | } |
| 1965 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1966 | static PyObject* |
| 1967 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1968 | { |
| 1969 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1970 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1971 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1972 | if (size == 0) |
| 1973 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1974 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1975 | if (size == 1) { |
| 1976 | Py_UCS4 ch = u[0]; |
| 1977 | if (ch < 256) |
| 1978 | return get_latin1_char((unsigned char)ch); |
| 1979 | |
| 1980 | res = PyUnicode_New(1, ch); |
| 1981 | if (res == NULL) |
| 1982 | return NULL; |
| 1983 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1984 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1985 | return res; |
| 1986 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1987 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1988 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1989 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1990 | if (!res) |
| 1991 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1992 | if (max_char < 256) |
| 1993 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1994 | PyUnicode_1BYTE_DATA(res)); |
| 1995 | else if (max_char < 0x10000) |
| 1996 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1997 | PyUnicode_2BYTE_DATA(res)); |
| 1998 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2000 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2001 | return res; |
| 2002 | } |
| 2003 | |
| 2004 | PyObject* |
| 2005 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2006 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2007 | if (size < 0) { |
| 2008 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2009 | return NULL; |
| 2010 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2011 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2012 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2013 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2014 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2015 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2016 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2017 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2018 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2019 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2020 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2021 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2022 | } |
| 2023 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2024 | Py_UCS4 |
| 2025 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2026 | { |
| 2027 | enum PyUnicode_Kind kind; |
| 2028 | void *startptr, *endptr; |
| 2029 | |
| 2030 | assert(PyUnicode_IS_READY(unicode)); |
| 2031 | assert(0 <= start); |
| 2032 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2033 | assert(start <= end); |
| 2034 | |
| 2035 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2036 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2037 | |
| 2038 | if (start == end) |
| 2039 | return 127; |
| 2040 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2041 | if (PyUnicode_IS_ASCII(unicode)) |
| 2042 | return 127; |
| 2043 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2044 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2045 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2046 | endptr = (char *)startptr + end * kind; |
| 2047 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2048 | switch(kind) { |
| 2049 | case PyUnicode_1BYTE_KIND: |
| 2050 | return ucs1lib_find_max_char(startptr, endptr); |
| 2051 | case PyUnicode_2BYTE_KIND: |
| 2052 | return ucs2lib_find_max_char(startptr, endptr); |
| 2053 | case PyUnicode_4BYTE_KIND: |
| 2054 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2055 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2056 | assert(0); |
| 2057 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2061 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2062 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2063 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2064 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2065 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2066 | { |
| 2067 | PyObject *unicode, *copy; |
| 2068 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2069 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2070 | unsigned int kind; |
| 2071 | |
| 2072 | assert(p_unicode != NULL); |
| 2073 | unicode = *p_unicode; |
| 2074 | assert(PyUnicode_IS_READY(unicode)); |
| 2075 | if (PyUnicode_IS_ASCII(unicode)) |
| 2076 | return; |
| 2077 | |
| 2078 | len = PyUnicode_GET_LENGTH(unicode); |
| 2079 | kind = PyUnicode_KIND(unicode); |
| 2080 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2081 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2082 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2083 | if (max_char >= 128) |
| 2084 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2085 | } |
| 2086 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2087 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2088 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2089 | if (max_char >= 256) |
| 2090 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2091 | } |
| 2092 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2093 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2094 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2095 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2096 | if (max_char >= 0x10000) |
| 2097 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2098 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2099 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2100 | if (copy != NULL) |
| 2101 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2102 | Py_DECREF(unicode); |
| 2103 | *p_unicode = copy; |
| 2104 | } |
| 2105 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2106 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2107 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2108 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2109 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2110 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2111 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2112 | if (!PyUnicode_Check(unicode)) { |
| 2113 | PyErr_BadInternalCall(); |
| 2114 | return NULL; |
| 2115 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2116 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2117 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2118 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2119 | length = PyUnicode_GET_LENGTH(unicode); |
| 2120 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2121 | if (!copy) |
| 2122 | return NULL; |
| 2123 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2124 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2125 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2126 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2127 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2128 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2129 | } |
| 2130 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2131 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2132 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2133 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2134 | |
| 2135 | void* |
| 2136 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2137 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2138 | Py_ssize_t len; |
| 2139 | void *result; |
| 2140 | unsigned int skind; |
| 2141 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2142 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2143 | return NULL; |
| 2144 | |
| 2145 | len = PyUnicode_GET_LENGTH(s); |
| 2146 | skind = PyUnicode_KIND(s); |
| 2147 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2148 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2149 | return NULL; |
| 2150 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2151 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2152 | case PyUnicode_2BYTE_KIND: |
| 2153 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2154 | if (!result) |
| 2155 | return PyErr_NoMemory(); |
| 2156 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2157 | _PyUnicode_CONVERT_BYTES( |
| 2158 | Py_UCS1, Py_UCS2, |
| 2159 | PyUnicode_1BYTE_DATA(s), |
| 2160 | PyUnicode_1BYTE_DATA(s) + len, |
| 2161 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2163 | case PyUnicode_4BYTE_KIND: |
| 2164 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2165 | if (!result) |
| 2166 | return PyErr_NoMemory(); |
| 2167 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2168 | _PyUnicode_CONVERT_BYTES( |
| 2169 | Py_UCS2, Py_UCS4, |
| 2170 | PyUnicode_2BYTE_DATA(s), |
| 2171 | PyUnicode_2BYTE_DATA(s) + len, |
| 2172 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2173 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2174 | else { |
| 2175 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2176 | _PyUnicode_CONVERT_BYTES( |
| 2177 | Py_UCS1, Py_UCS4, |
| 2178 | PyUnicode_1BYTE_DATA(s), |
| 2179 | PyUnicode_1BYTE_DATA(s) + len, |
| 2180 | result); |
| 2181 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2182 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2183 | default: |
| 2184 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2185 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2186 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2187 | return NULL; |
| 2188 | } |
| 2189 | |
| 2190 | static Py_UCS4* |
| 2191 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2192 | int copy_null) |
| 2193 | { |
| 2194 | int kind; |
| 2195 | void *data; |
| 2196 | Py_ssize_t len, targetlen; |
| 2197 | if (PyUnicode_READY(string) == -1) |
| 2198 | return NULL; |
| 2199 | kind = PyUnicode_KIND(string); |
| 2200 | data = PyUnicode_DATA(string); |
| 2201 | len = PyUnicode_GET_LENGTH(string); |
| 2202 | targetlen = len; |
| 2203 | if (copy_null) |
| 2204 | targetlen++; |
| 2205 | if (!target) { |
| 2206 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2207 | PyErr_NoMemory(); |
| 2208 | return NULL; |
| 2209 | } |
| 2210 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2211 | if (!target) { |
| 2212 | PyErr_NoMemory(); |
| 2213 | return NULL; |
| 2214 | } |
| 2215 | } |
| 2216 | else { |
| 2217 | if (targetsize < targetlen) { |
| 2218 | PyErr_Format(PyExc_SystemError, |
| 2219 | "string is longer than the buffer"); |
| 2220 | if (copy_null && 0 < targetsize) |
| 2221 | target[0] = 0; |
| 2222 | return NULL; |
| 2223 | } |
| 2224 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2225 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2226 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2227 | _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] | 2228 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2229 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2230 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2231 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2232 | } |
| 2233 | else { |
| 2234 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2235 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2236 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2237 | if (copy_null) |
| 2238 | target[len] = 0; |
| 2239 | return target; |
| 2240 | } |
| 2241 | |
| 2242 | Py_UCS4* |
| 2243 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2244 | int copy_null) |
| 2245 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2246 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2247 | PyErr_BadInternalCall(); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | return as_ucs4(string, target, targetsize, copy_null); |
| 2251 | } |
| 2252 | |
| 2253 | Py_UCS4* |
| 2254 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2255 | { |
| 2256 | return as_ucs4(string, NULL, 0, 1); |
| 2257 | } |
| 2258 | |
| 2259 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2260 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2261 | PyObject * |
| 2262 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2263 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2264 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2265 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2266 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2267 | PyErr_BadInternalCall(); |
| 2268 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2271 | if (size == -1) { |
| 2272 | size = wcslen(w); |
| 2273 | } |
| 2274 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2275 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2278 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2279 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2280 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2281 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2282 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2283 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2284 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2285 | if (longflag) |
| 2286 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2287 | else if (longlongflag) { |
| 2288 | /* longlongflag should only ever be nonzero on machines with |
| 2289 | HAVE_LONG_LONG defined */ |
| 2290 | #ifdef HAVE_LONG_LONG |
| 2291 | char *f = PY_FORMAT_LONG_LONG; |
| 2292 | while (*f) |
| 2293 | *fmt++ = *f++; |
| 2294 | #else |
| 2295 | /* we shouldn't ever get here */ |
| 2296 | assert(0); |
| 2297 | *fmt++ = 'l'; |
| 2298 | #endif |
| 2299 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2300 | else if (size_tflag) { |
| 2301 | char *f = PY_FORMAT_SIZE_T; |
| 2302 | while (*f) |
| 2303 | *fmt++ = *f++; |
| 2304 | } |
| 2305 | *fmt++ = c; |
| 2306 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2309 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2310 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2311 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2312 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2313 | |
| 2314 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2315 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2316 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2317 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2318 | const char *p; |
| 2319 | Py_ssize_t len; |
| 2320 | int zeropad; |
| 2321 | int width; |
| 2322 | int precision; |
| 2323 | int longflag; |
| 2324 | int longlongflag; |
| 2325 | int size_tflag; |
| 2326 | int fill; |
| 2327 | |
| 2328 | p = f; |
| 2329 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2330 | zeropad = 0; |
| 2331 | if (*f == '0') { |
| 2332 | zeropad = 1; |
| 2333 | f++; |
| 2334 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2335 | |
| 2336 | /* 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] | 2337 | width = 0; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2338 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2339 | if (width > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2340 | PyErr_SetString(PyExc_ValueError, |
| 2341 | "width too big"); |
| 2342 | return NULL; |
| 2343 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2344 | width = (width*10) + (*f - '0'); |
| 2345 | f++; |
| 2346 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2347 | precision = 0; |
| 2348 | if (*f == '.') { |
| 2349 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2350 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2351 | if (precision > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2352 | PyErr_SetString(PyExc_ValueError, |
| 2353 | "precision too big"); |
| 2354 | return NULL; |
| 2355 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2356 | precision = (precision*10) + (*f - '0'); |
| 2357 | f++; |
| 2358 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2359 | if (*f == '%') { |
| 2360 | /* "%.3%s" => f points to "3" */ |
| 2361 | f--; |
| 2362 | } |
| 2363 | } |
| 2364 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2365 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2366 | f--; |
| 2367 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2368 | |
| 2369 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2370 | longflag = 0; |
| 2371 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2372 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2373 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2374 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2375 | longflag = 1; |
| 2376 | ++f; |
| 2377 | } |
| 2378 | #ifdef HAVE_LONG_LONG |
| 2379 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2380 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2381 | longlongflag = 1; |
| 2382 | f += 2; |
| 2383 | } |
| 2384 | #endif |
| 2385 | } |
| 2386 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2387 | 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] | 2388 | size_tflag = 1; |
| 2389 | ++f; |
| 2390 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2391 | |
| 2392 | if (f[1] == '\0') |
| 2393 | writer->overallocate = 0; |
| 2394 | |
| 2395 | switch (*f) { |
| 2396 | case 'c': |
| 2397 | { |
| 2398 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2399 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
| 2400 | PyErr_SetString(PyExc_ValueError, |
| 2401 | "character argument not in range(0x110000)"); |
| 2402 | return NULL; |
| 2403 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2404 | if (_PyUnicodeWriter_Prepare(writer, 1, ordinal) == -1) |
| 2405 | return NULL; |
| 2406 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ordinal); |
| 2407 | writer->pos++; |
| 2408 | break; |
| 2409 | } |
| 2410 | |
| 2411 | case 'i': |
| 2412 | case 'd': |
| 2413 | case 'u': |
| 2414 | case 'x': |
| 2415 | { |
| 2416 | /* used by sprintf */ |
| 2417 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2418 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2419 | |
| 2420 | if (*f == 'u') { |
| 2421 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2422 | |
| 2423 | if (longflag) |
| 2424 | len = sprintf(buffer, fmt, |
| 2425 | va_arg(*vargs, unsigned long)); |
| 2426 | #ifdef HAVE_LONG_LONG |
| 2427 | else if (longlongflag) |
| 2428 | len = sprintf(buffer, fmt, |
| 2429 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2430 | #endif |
| 2431 | else if (size_tflag) |
| 2432 | len = sprintf(buffer, fmt, |
| 2433 | va_arg(*vargs, size_t)); |
| 2434 | else |
| 2435 | len = sprintf(buffer, fmt, |
| 2436 | va_arg(*vargs, unsigned int)); |
| 2437 | } |
| 2438 | else if (*f == 'x') { |
| 2439 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2440 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2441 | } |
| 2442 | else { |
| 2443 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2444 | |
| 2445 | if (longflag) |
| 2446 | len = sprintf(buffer, fmt, |
| 2447 | va_arg(*vargs, long)); |
| 2448 | #ifdef HAVE_LONG_LONG |
| 2449 | else if (longlongflag) |
| 2450 | len = sprintf(buffer, fmt, |
| 2451 | va_arg(*vargs, PY_LONG_LONG)); |
| 2452 | #endif |
| 2453 | else if (size_tflag) |
| 2454 | len = sprintf(buffer, fmt, |
| 2455 | va_arg(*vargs, Py_ssize_t)); |
| 2456 | else |
| 2457 | len = sprintf(buffer, fmt, |
| 2458 | va_arg(*vargs, int)); |
| 2459 | } |
| 2460 | assert(len >= 0); |
| 2461 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2462 | if (precision < len) |
| 2463 | precision = len; |
| 2464 | if (width > precision) { |
| 2465 | Py_UCS4 fillchar; |
| 2466 | fill = width - precision; |
| 2467 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2468 | if (_PyUnicodeWriter_Prepare(writer, fill, fillchar) == -1) |
| 2469 | return NULL; |
| 2470 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2471 | return NULL; |
| 2472 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2473 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2474 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2475 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2476 | if (_PyUnicodeWriter_Prepare(writer, fill, '0') == -1) |
| 2477 | return NULL; |
| 2478 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2479 | return NULL; |
| 2480 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2481 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2482 | if (_PyUnicodeWriter_WriteCstr(writer, buffer, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2483 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2484 | break; |
| 2485 | } |
| 2486 | |
| 2487 | case 'p': |
| 2488 | { |
| 2489 | char number[MAX_LONG_LONG_CHARS]; |
| 2490 | |
| 2491 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2492 | assert(len >= 0); |
| 2493 | |
| 2494 | /* %p is ill-defined: ensure leading 0x. */ |
| 2495 | if (number[1] == 'X') |
| 2496 | number[1] = 'x'; |
| 2497 | else if (number[1] != 'x') { |
| 2498 | memmove(number + 2, number, |
| 2499 | strlen(number) + 1); |
| 2500 | number[0] = '0'; |
| 2501 | number[1] = 'x'; |
| 2502 | len += 2; |
| 2503 | } |
| 2504 | |
| 2505 | if (_PyUnicodeWriter_WriteCstr(writer, number, len) == -1) |
| 2506 | return NULL; |
| 2507 | break; |
| 2508 | } |
| 2509 | |
| 2510 | case 's': |
| 2511 | { |
| 2512 | /* UTF-8 */ |
| 2513 | const char *s = va_arg(*vargs, const char*); |
| 2514 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
| 2515 | if (!str) |
| 2516 | return NULL; |
| 2517 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2518 | Py_DECREF(str); |
| 2519 | return NULL; |
| 2520 | } |
| 2521 | Py_DECREF(str); |
| 2522 | break; |
| 2523 | } |
| 2524 | |
| 2525 | case 'U': |
| 2526 | { |
| 2527 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2528 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2529 | |
| 2530 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2531 | return NULL; |
| 2532 | break; |
| 2533 | } |
| 2534 | |
| 2535 | case 'V': |
| 2536 | { |
| 2537 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2538 | const char *str = va_arg(*vargs, const char *); |
| 2539 | PyObject *str_obj; |
| 2540 | assert(obj || str); |
| 2541 | if (obj) { |
| 2542 | assert(_PyUnicode_CHECK(obj)); |
| 2543 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2544 | return NULL; |
| 2545 | } |
| 2546 | else { |
| 2547 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
| 2548 | if (!str_obj) |
| 2549 | return NULL; |
| 2550 | if (_PyUnicodeWriter_WriteStr(writer, str_obj) == -1) { |
| 2551 | Py_DECREF(str_obj); |
| 2552 | return NULL; |
| 2553 | } |
| 2554 | Py_DECREF(str_obj); |
| 2555 | } |
| 2556 | break; |
| 2557 | } |
| 2558 | |
| 2559 | case 'S': |
| 2560 | { |
| 2561 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2562 | PyObject *str; |
| 2563 | assert(obj); |
| 2564 | str = PyObject_Str(obj); |
| 2565 | if (!str) |
| 2566 | return NULL; |
| 2567 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2568 | Py_DECREF(str); |
| 2569 | return NULL; |
| 2570 | } |
| 2571 | Py_DECREF(str); |
| 2572 | break; |
| 2573 | } |
| 2574 | |
| 2575 | case 'R': |
| 2576 | { |
| 2577 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2578 | PyObject *repr; |
| 2579 | assert(obj); |
| 2580 | repr = PyObject_Repr(obj); |
| 2581 | if (!repr) |
| 2582 | return NULL; |
| 2583 | if (_PyUnicodeWriter_WriteStr(writer, repr) == -1) { |
| 2584 | Py_DECREF(repr); |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | Py_DECREF(repr); |
| 2588 | break; |
| 2589 | } |
| 2590 | |
| 2591 | case 'A': |
| 2592 | { |
| 2593 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2594 | PyObject *ascii; |
| 2595 | assert(obj); |
| 2596 | ascii = PyObject_ASCII(obj); |
| 2597 | if (!ascii) |
| 2598 | return NULL; |
| 2599 | if (_PyUnicodeWriter_WriteStr(writer, ascii) == -1) { |
| 2600 | Py_DECREF(ascii); |
| 2601 | return NULL; |
| 2602 | } |
| 2603 | Py_DECREF(ascii); |
| 2604 | break; |
| 2605 | } |
| 2606 | |
| 2607 | case '%': |
| 2608 | if (_PyUnicodeWriter_Prepare(writer, 1, '%') == 1) |
| 2609 | return NULL; |
| 2610 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%'); |
| 2611 | writer->pos++; |
| 2612 | break; |
| 2613 | |
| 2614 | default: |
| 2615 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2616 | of the format string to the output string. (we cannot just |
| 2617 | skip the code, since there's no way to know what's in the |
| 2618 | argument list) */ |
| 2619 | len = strlen(p); |
| 2620 | if (_PyUnicodeWriter_WriteCstr(writer, p, len) == -1) |
| 2621 | return NULL; |
| 2622 | f = p+len; |
| 2623 | return f; |
| 2624 | } |
| 2625 | |
| 2626 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2627 | return f; |
| 2628 | } |
| 2629 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2630 | PyObject * |
| 2631 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2632 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2633 | va_list vargs2; |
| 2634 | const char *f; |
| 2635 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2636 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2637 | _PyUnicodeWriter_Init(&writer, strlen(format) + 100); |
| 2638 | |
| 2639 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2640 | Copy it to be able to pass a reference to a subfunction. */ |
| 2641 | Py_VA_COPY(vargs2, vargs); |
| 2642 | |
| 2643 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2644 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2645 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2646 | if (f == NULL) |
| 2647 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2648 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2649 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2650 | const char *p; |
| 2651 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2652 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2653 | p = f; |
| 2654 | do |
| 2655 | { |
| 2656 | if ((unsigned char)*p > 127) { |
| 2657 | PyErr_Format(PyExc_ValueError, |
| 2658 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2659 | "string, got a non-ASCII byte: 0x%02x", |
| 2660 | (unsigned char)*p); |
| 2661 | return NULL; |
| 2662 | } |
| 2663 | p++; |
| 2664 | } |
| 2665 | while (*p != '\0' && *p != '%'); |
| 2666 | len = p - f; |
| 2667 | |
| 2668 | if (*p == '\0') |
| 2669 | writer.overallocate = 0; |
| 2670 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
| 2671 | goto fail; |
| 2672 | unicode_write_cstr(writer.buffer, writer.pos, f, len); |
| 2673 | writer.pos += len; |
| 2674 | |
| 2675 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2676 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2677 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2678 | return _PyUnicodeWriter_Finish(&writer); |
| 2679 | |
| 2680 | fail: |
| 2681 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2682 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2685 | PyObject * |
| 2686 | PyUnicode_FromFormat(const char *format, ...) |
| 2687 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2688 | PyObject* ret; |
| 2689 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2690 | |
| 2691 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2692 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2693 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2694 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2695 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2696 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2697 | va_end(vargs); |
| 2698 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2701 | #ifdef HAVE_WCHAR_H |
| 2702 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2703 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2704 | convert a Unicode object to a wide character string. |
| 2705 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2706 | - 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] | 2707 | character) required to convert the unicode object. Ignore size argument. |
| 2708 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2709 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2710 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2711 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2712 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2713 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2714 | wchar_t *w, |
| 2715 | Py_ssize_t size) |
| 2716 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2717 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2718 | const wchar_t *wstr; |
| 2719 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2720 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2721 | if (wstr == NULL) |
| 2722 | return -1; |
| 2723 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2724 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2725 | if (size > res) |
| 2726 | size = res + 1; |
| 2727 | else |
| 2728 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2729 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2730 | return res; |
| 2731 | } |
| 2732 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2733 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2737 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2738 | wchar_t *w, |
| 2739 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2740 | { |
| 2741 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2742 | PyErr_BadInternalCall(); |
| 2743 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2744 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2745 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2748 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2749 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2750 | Py_ssize_t *size) |
| 2751 | { |
| 2752 | wchar_t* buffer; |
| 2753 | Py_ssize_t buflen; |
| 2754 | |
| 2755 | if (unicode == NULL) { |
| 2756 | PyErr_BadInternalCall(); |
| 2757 | return NULL; |
| 2758 | } |
| 2759 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2760 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2761 | if (buflen == -1) |
| 2762 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2763 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2764 | PyErr_NoMemory(); |
| 2765 | return NULL; |
| 2766 | } |
| 2767 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2768 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2769 | if (buffer == NULL) { |
| 2770 | PyErr_NoMemory(); |
| 2771 | return NULL; |
| 2772 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2773 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2774 | if (buflen == -1) { |
| 2775 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2776 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2777 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2778 | if (size != NULL) |
| 2779 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2780 | return buffer; |
| 2781 | } |
| 2782 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2783 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2784 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2785 | PyObject * |
| 2786 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2787 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2788 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2789 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2790 | PyErr_SetString(PyExc_ValueError, |
| 2791 | "chr() arg not in range(0x110000)"); |
| 2792 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2793 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2794 | |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2795 | if ((Py_UCS4)ordinal < 256) |
| 2796 | return get_latin1_char((unsigned char)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2797 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2798 | v = PyUnicode_New(1, ordinal); |
| 2799 | if (v == NULL) |
| 2800 | return NULL; |
| 2801 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2802 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2803 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2806 | PyObject * |
| 2807 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2808 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2809 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2810 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2811 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2812 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2813 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2814 | Py_INCREF(obj); |
| 2815 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2816 | } |
| 2817 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2818 | /* For a Unicode subtype that's not a Unicode object, |
| 2819 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2820 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2821 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2822 | PyErr_Format(PyExc_TypeError, |
| 2823 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2824 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2825 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2828 | PyObject * |
| 2829 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2830 | const char *encoding, |
| 2831 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2832 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2833 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2834 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2835 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2836 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2837 | PyErr_BadInternalCall(); |
| 2838 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2840 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2841 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2842 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2843 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2844 | _Py_RETURN_UNICODE_EMPTY(); |
| 2845 | v = PyUnicode_Decode( |
| 2846 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2847 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2848 | return v; |
| 2849 | } |
| 2850 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2851 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2852 | PyErr_SetString(PyExc_TypeError, |
| 2853 | "decoding str is not supported"); |
| 2854 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2855 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2857 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2858 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2859 | PyErr_Format(PyExc_TypeError, |
| 2860 | "coercing to str: need bytes, bytearray " |
| 2861 | "or buffer-like object, %.80s found", |
| 2862 | Py_TYPE(obj)->tp_name); |
| 2863 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2864 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2865 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2866 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2867 | PyBuffer_Release(&buffer); |
| 2868 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2869 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2870 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2871 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2872 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2873 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2876 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2877 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2878 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2879 | int |
| 2880 | _Py_normalize_encoding(const char *encoding, |
| 2881 | char *lower, |
| 2882 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2883 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2884 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2885 | char *l; |
| 2886 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2887 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2888 | if (encoding == NULL) { |
| 2889 | strcpy(lower, "utf-8"); |
| 2890 | return 1; |
| 2891 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2892 | e = encoding; |
| 2893 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2894 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2895 | while (*e) { |
| 2896 | if (l == l_end) |
| 2897 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2898 | if (Py_ISUPPER(*e)) { |
| 2899 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2900 | } |
| 2901 | else if (*e == '_') { |
| 2902 | *l++ = '-'; |
| 2903 | e++; |
| 2904 | } |
| 2905 | else { |
| 2906 | *l++ = *e++; |
| 2907 | } |
| 2908 | } |
| 2909 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2910 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2913 | PyObject * |
| 2914 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2915 | Py_ssize_t size, |
| 2916 | const char *encoding, |
| 2917 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2918 | { |
| 2919 | PyObject *buffer = NULL, *unicode; |
| 2920 | Py_buffer info; |
| 2921 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2922 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2923 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2924 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2925 | if ((strcmp(lower, "utf-8") == 0) || |
| 2926 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2927 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2928 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2929 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2930 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2931 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2932 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2933 | else if (strcmp(lower, "mbcs") == 0) |
| 2934 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2935 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2936 | else if (strcmp(lower, "ascii") == 0) |
| 2937 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2938 | else if (strcmp(lower, "utf-16") == 0) |
| 2939 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2940 | else if (strcmp(lower, "utf-32") == 0) |
| 2941 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2942 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2943 | |
| 2944 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2945 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2946 | 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] | 2947 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2948 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2949 | if (buffer == NULL) |
| 2950 | goto onError; |
| 2951 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2952 | if (unicode == NULL) |
| 2953 | goto onError; |
| 2954 | if (!PyUnicode_Check(unicode)) { |
| 2955 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2956 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2957 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2958 | Py_DECREF(unicode); |
| 2959 | goto onError; |
| 2960 | } |
| 2961 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2962 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2963 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2964 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | Py_XDECREF(buffer); |
| 2966 | return NULL; |
| 2967 | } |
| 2968 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2969 | PyObject * |
| 2970 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2971 | const char *encoding, |
| 2972 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2973 | { |
| 2974 | PyObject *v; |
| 2975 | |
| 2976 | if (!PyUnicode_Check(unicode)) { |
| 2977 | PyErr_BadArgument(); |
| 2978 | goto onError; |
| 2979 | } |
| 2980 | |
| 2981 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2982 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2983 | |
| 2984 | /* Decode via the codec registry */ |
| 2985 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2986 | if (v == NULL) |
| 2987 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2988 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2989 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2990 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2991 | return NULL; |
| 2992 | } |
| 2993 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2994 | PyObject * |
| 2995 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2996 | const char *encoding, |
| 2997 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2998 | { |
| 2999 | PyObject *v; |
| 3000 | |
| 3001 | if (!PyUnicode_Check(unicode)) { |
| 3002 | PyErr_BadArgument(); |
| 3003 | goto onError; |
| 3004 | } |
| 3005 | |
| 3006 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3007 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3008 | |
| 3009 | /* Decode via the codec registry */ |
| 3010 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3011 | if (v == NULL) |
| 3012 | goto onError; |
| 3013 | if (!PyUnicode_Check(v)) { |
| 3014 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3015 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3016 | Py_TYPE(v)->tp_name); |
| 3017 | Py_DECREF(v); |
| 3018 | goto onError; |
| 3019 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3020 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3021 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3022 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3023 | return NULL; |
| 3024 | } |
| 3025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3026 | PyObject * |
| 3027 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3028 | Py_ssize_t size, |
| 3029 | const char *encoding, |
| 3030 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3031 | { |
| 3032 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3033 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3034 | unicode = PyUnicode_FromUnicode(s, size); |
| 3035 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3036 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3038 | Py_DECREF(unicode); |
| 3039 | return v; |
| 3040 | } |
| 3041 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3042 | PyObject * |
| 3043 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3044 | const char *encoding, |
| 3045 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3046 | { |
| 3047 | PyObject *v; |
| 3048 | |
| 3049 | if (!PyUnicode_Check(unicode)) { |
| 3050 | PyErr_BadArgument(); |
| 3051 | goto onError; |
| 3052 | } |
| 3053 | |
| 3054 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3055 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3056 | |
| 3057 | /* Encode via the codec registry */ |
| 3058 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3059 | if (v == NULL) |
| 3060 | goto onError; |
| 3061 | return v; |
| 3062 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3063 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3064 | return NULL; |
| 3065 | } |
| 3066 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3067 | static size_t |
| 3068 | wcstombs_errorpos(const wchar_t *wstr) |
| 3069 | { |
| 3070 | size_t len; |
| 3071 | #if SIZEOF_WCHAR_T == 2 |
| 3072 | wchar_t buf[3]; |
| 3073 | #else |
| 3074 | wchar_t buf[2]; |
| 3075 | #endif |
| 3076 | char outbuf[MB_LEN_MAX]; |
| 3077 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3078 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3079 | #if SIZEOF_WCHAR_T == 2 |
| 3080 | buf[2] = 0; |
| 3081 | #else |
| 3082 | buf[1] = 0; |
| 3083 | #endif |
| 3084 | start = wstr; |
| 3085 | while (*wstr != L'\0') |
| 3086 | { |
| 3087 | previous = wstr; |
| 3088 | #if SIZEOF_WCHAR_T == 2 |
| 3089 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3090 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3091 | { |
| 3092 | buf[0] = wstr[0]; |
| 3093 | buf[1] = wstr[1]; |
| 3094 | wstr += 2; |
| 3095 | } |
| 3096 | else { |
| 3097 | buf[0] = *wstr; |
| 3098 | buf[1] = 0; |
| 3099 | wstr++; |
| 3100 | } |
| 3101 | #else |
| 3102 | buf[0] = *wstr; |
| 3103 | wstr++; |
| 3104 | #endif |
| 3105 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3106 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3107 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3111 | return 0; |
| 3112 | } |
| 3113 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3114 | static int |
| 3115 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3116 | { |
| 3117 | if (errors == NULL) { |
| 3118 | *surrogateescape = 0; |
| 3119 | return 0; |
| 3120 | } |
| 3121 | |
| 3122 | if (strcmp(errors, "strict") == 0) { |
| 3123 | *surrogateescape = 0; |
| 3124 | return 0; |
| 3125 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3126 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3127 | *surrogateescape = 1; |
| 3128 | return 0; |
| 3129 | } |
| 3130 | PyErr_Format(PyExc_ValueError, |
| 3131 | "only 'strict' and 'surrogateescape' error handlers " |
| 3132 | "are supported, not '%s'", |
| 3133 | errors); |
| 3134 | return -1; |
| 3135 | } |
| 3136 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3137 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3138 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3139 | { |
| 3140 | Py_ssize_t wlen, wlen2; |
| 3141 | wchar_t *wstr; |
| 3142 | PyObject *bytes = NULL; |
| 3143 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3144 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3145 | PyObject *exc; |
| 3146 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3147 | int surrogateescape; |
| 3148 | |
| 3149 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3150 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3151 | |
| 3152 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3153 | if (wstr == NULL) |
| 3154 | return NULL; |
| 3155 | |
| 3156 | wlen2 = wcslen(wstr); |
| 3157 | if (wlen2 != wlen) { |
| 3158 | PyMem_Free(wstr); |
| 3159 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3160 | return NULL; |
| 3161 | } |
| 3162 | |
| 3163 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3164 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3165 | char *str; |
| 3166 | |
| 3167 | str = _Py_wchar2char(wstr, &error_pos); |
| 3168 | if (str == NULL) { |
| 3169 | if (error_pos == (size_t)-1) { |
| 3170 | PyErr_NoMemory(); |
| 3171 | PyMem_Free(wstr); |
| 3172 | return NULL; |
| 3173 | } |
| 3174 | else { |
| 3175 | goto encode_error; |
| 3176 | } |
| 3177 | } |
| 3178 | PyMem_Free(wstr); |
| 3179 | |
| 3180 | bytes = PyBytes_FromString(str); |
| 3181 | PyMem_Free(str); |
| 3182 | } |
| 3183 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3184 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3185 | size_t len, len2; |
| 3186 | |
| 3187 | len = wcstombs(NULL, wstr, 0); |
| 3188 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3189 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3190 | goto encode_error; |
| 3191 | } |
| 3192 | |
| 3193 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3194 | if (bytes == NULL) { |
| 3195 | PyMem_Free(wstr); |
| 3196 | return NULL; |
| 3197 | } |
| 3198 | |
| 3199 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3200 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3201 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3202 | goto encode_error; |
| 3203 | } |
| 3204 | PyMem_Free(wstr); |
| 3205 | } |
| 3206 | return bytes; |
| 3207 | |
| 3208 | encode_error: |
| 3209 | errmsg = strerror(errno); |
| 3210 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3211 | |
| 3212 | if (error_pos == (size_t)-1) |
| 3213 | error_pos = wcstombs_errorpos(wstr); |
| 3214 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3215 | PyMem_Free(wstr); |
| 3216 | Py_XDECREF(bytes); |
| 3217 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3218 | if (errmsg != NULL) { |
| 3219 | size_t errlen; |
| 3220 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3221 | if (wstr != NULL) { |
| 3222 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3223 | PyMem_Free(wstr); |
| 3224 | } else |
| 3225 | errmsg = NULL; |
| 3226 | } |
| 3227 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3228 | reason = PyUnicode_FromString( |
| 3229 | "wcstombs() encountered an unencodable " |
| 3230 | "wide character"); |
| 3231 | if (reason == NULL) |
| 3232 | return NULL; |
| 3233 | |
| 3234 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3235 | "locale", unicode, |
| 3236 | (Py_ssize_t)error_pos, |
| 3237 | (Py_ssize_t)(error_pos+1), |
| 3238 | reason); |
| 3239 | Py_DECREF(reason); |
| 3240 | if (exc != NULL) { |
| 3241 | PyCodec_StrictErrors(exc); |
| 3242 | Py_XDECREF(exc); |
| 3243 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3244 | return NULL; |
| 3245 | } |
| 3246 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3247 | PyObject * |
| 3248 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3249 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3250 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3251 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3252 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3253 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3254 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3255 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3256 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3257 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3258 | the Python codec requires to encode at least its own filename. Use the C |
| 3259 | version of the locale codec until the codec registry is initialized and |
| 3260 | the Python codec is loaded. |
| 3261 | |
| 3262 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3263 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3264 | subinterpreters. */ |
| 3265 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3266 | return PyUnicode_AsEncodedString(unicode, |
| 3267 | Py_FileSystemDefaultEncoding, |
| 3268 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3269 | } |
| 3270 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3271 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3272 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3273 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3276 | PyObject * |
| 3277 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3278 | const char *encoding, |
| 3279 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3280 | { |
| 3281 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3282 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3283 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3284 | if (!PyUnicode_Check(unicode)) { |
| 3285 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3286 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3287 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3288 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3289 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3290 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3291 | if ((strcmp(lower, "utf-8") == 0) || |
| 3292 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3293 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3294 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3295 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3296 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3297 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3298 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3299 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3300 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3301 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3302 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3303 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3304 | else if (strcmp(lower, "mbcs") == 0) |
| 3305 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3306 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3307 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3308 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3309 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3310 | |
| 3311 | /* Encode via the codec registry */ |
| 3312 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3313 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3314 | return NULL; |
| 3315 | |
| 3316 | /* The normal path */ |
| 3317 | if (PyBytes_Check(v)) |
| 3318 | return v; |
| 3319 | |
| 3320 | /* 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] | 3321 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3322 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3323 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3324 | |
| 3325 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3326 | "encoder %s returned bytearray instead of bytes", |
| 3327 | encoding); |
| 3328 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3329 | Py_DECREF(v); |
| 3330 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3331 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3332 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3333 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3334 | Py_DECREF(v); |
| 3335 | return b; |
| 3336 | } |
| 3337 | |
| 3338 | PyErr_Format(PyExc_TypeError, |
| 3339 | "encoder did not return a bytes object (type=%.400s)", |
| 3340 | Py_TYPE(v)->tp_name); |
| 3341 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3342 | return NULL; |
| 3343 | } |
| 3344 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3345 | PyObject * |
| 3346 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3347 | const char *encoding, |
| 3348 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3349 | { |
| 3350 | PyObject *v; |
| 3351 | |
| 3352 | if (!PyUnicode_Check(unicode)) { |
| 3353 | PyErr_BadArgument(); |
| 3354 | goto onError; |
| 3355 | } |
| 3356 | |
| 3357 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3358 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3359 | |
| 3360 | /* Encode via the codec registry */ |
| 3361 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3362 | if (v == NULL) |
| 3363 | goto onError; |
| 3364 | if (!PyUnicode_Check(v)) { |
| 3365 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3366 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3367 | Py_TYPE(v)->tp_name); |
| 3368 | Py_DECREF(v); |
| 3369 | goto onError; |
| 3370 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3371 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3372 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3373 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3374 | return NULL; |
| 3375 | } |
| 3376 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3377 | static size_t |
| 3378 | mbstowcs_errorpos(const char *str, size_t len) |
| 3379 | { |
| 3380 | #ifdef HAVE_MBRTOWC |
| 3381 | const char *start = str; |
| 3382 | mbstate_t mbs; |
| 3383 | size_t converted; |
| 3384 | wchar_t ch; |
| 3385 | |
| 3386 | memset(&mbs, 0, sizeof mbs); |
| 3387 | while (len) |
| 3388 | { |
| 3389 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3390 | if (converted == 0) |
| 3391 | /* Reached end of string */ |
| 3392 | break; |
| 3393 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3394 | /* Conversion error or incomplete character */ |
| 3395 | return str - start; |
| 3396 | } |
| 3397 | else { |
| 3398 | str += converted; |
| 3399 | len -= converted; |
| 3400 | } |
| 3401 | } |
| 3402 | /* failed to find the undecodable byte sequence */ |
| 3403 | return 0; |
| 3404 | #endif |
| 3405 | return 0; |
| 3406 | } |
| 3407 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3408 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3409 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3410 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3411 | { |
| 3412 | wchar_t smallbuf[256]; |
| 3413 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3414 | wchar_t *wstr; |
| 3415 | size_t wlen, wlen2; |
| 3416 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3417 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3418 | size_t error_pos; |
| 3419 | char *errmsg; |
| 3420 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3421 | |
| 3422 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3423 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3424 | |
| 3425 | if (str[len] != '\0' || len != strlen(str)) { |
| 3426 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3427 | return NULL; |
| 3428 | } |
| 3429 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3430 | if (surrogateescape) { |
| 3431 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3432 | wstr = _Py_char2wchar(str, &wlen); |
| 3433 | if (wstr == NULL) { |
| 3434 | if (wlen == (size_t)-1) |
| 3435 | PyErr_NoMemory(); |
| 3436 | else |
| 3437 | PyErr_SetFromErrno(PyExc_OSError); |
| 3438 | return NULL; |
| 3439 | } |
| 3440 | |
| 3441 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3442 | PyMem_Free(wstr); |
| 3443 | } |
| 3444 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3445 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3446 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3447 | wlen = mbstowcs(NULL, str, 0); |
| 3448 | #else |
| 3449 | wlen = len; |
| 3450 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3451 | if (wlen == (size_t)-1) |
| 3452 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3453 | if (wlen+1 <= smallbuf_len) { |
| 3454 | wstr = smallbuf; |
| 3455 | } |
| 3456 | else { |
| 3457 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3458 | return PyErr_NoMemory(); |
| 3459 | |
| 3460 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3461 | if (!wstr) |
| 3462 | return PyErr_NoMemory(); |
| 3463 | } |
| 3464 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3465 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3466 | if (wlen2 == (size_t)-1) { |
| 3467 | if (wstr != smallbuf) |
| 3468 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3469 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3470 | } |
| 3471 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3472 | assert(wlen2 == wlen); |
| 3473 | #endif |
| 3474 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3475 | if (wstr != smallbuf) |
| 3476 | PyMem_Free(wstr); |
| 3477 | } |
| 3478 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3479 | |
| 3480 | decode_error: |
| 3481 | errmsg = strerror(errno); |
| 3482 | assert(errmsg != NULL); |
| 3483 | |
| 3484 | error_pos = mbstowcs_errorpos(str, len); |
| 3485 | if (errmsg != NULL) { |
| 3486 | size_t errlen; |
| 3487 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3488 | if (wstr != NULL) { |
| 3489 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3490 | PyMem_Free(wstr); |
| 3491 | } else |
| 3492 | errmsg = NULL; |
| 3493 | } |
| 3494 | if (errmsg == NULL) |
| 3495 | reason = PyUnicode_FromString( |
| 3496 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3497 | if (reason == NULL) |
| 3498 | return NULL; |
| 3499 | |
| 3500 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3501 | "locale", str, len, |
| 3502 | (Py_ssize_t)error_pos, |
| 3503 | (Py_ssize_t)(error_pos+1), |
| 3504 | reason); |
| 3505 | Py_DECREF(reason); |
| 3506 | if (exc != NULL) { |
| 3507 | PyCodec_StrictErrors(exc); |
| 3508 | Py_XDECREF(exc); |
| 3509 | } |
| 3510 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3514 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3515 | { |
| 3516 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3517 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | |
| 3521 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3522 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3523 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3524 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3525 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3526 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3527 | PyObject* |
| 3528 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3529 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3530 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3531 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3532 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3533 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3534 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3535 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3536 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3537 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3538 | the Python codec requires to encode at least its own filename. Use the C |
| 3539 | version of the locale codec until the codec registry is initialized and |
| 3540 | the Python codec is loaded. |
| 3541 | |
| 3542 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3543 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3544 | subinterpreters. */ |
| 3545 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3546 | return PyUnicode_Decode(s, size, |
| 3547 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3548 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3549 | } |
| 3550 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3551 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3552 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3553 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3556 | |
| 3557 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3558 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3559 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3560 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3561 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3562 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3563 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3564 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3565 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3566 | if (pos == -1) |
| 3567 | return 0; |
| 3568 | else |
| 3569 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3570 | } |
| 3571 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3572 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3573 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3574 | { |
| 3575 | PyObject *output = NULL; |
| 3576 | Py_ssize_t size; |
| 3577 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3578 | if (arg == NULL) { |
| 3579 | Py_DECREF(*(PyObject**)addr); |
| 3580 | return 1; |
| 3581 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3582 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3583 | output = arg; |
| 3584 | Py_INCREF(output); |
| 3585 | } |
| 3586 | else { |
| 3587 | arg = PyUnicode_FromObject(arg); |
| 3588 | if (!arg) |
| 3589 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3590 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3591 | Py_DECREF(arg); |
| 3592 | if (!output) |
| 3593 | return 0; |
| 3594 | if (!PyBytes_Check(output)) { |
| 3595 | Py_DECREF(output); |
| 3596 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3597 | return 0; |
| 3598 | } |
| 3599 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3600 | size = PyBytes_GET_SIZE(output); |
| 3601 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3602 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3603 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3604 | Py_DECREF(output); |
| 3605 | return 0; |
| 3606 | } |
| 3607 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3608 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3612 | int |
| 3613 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3614 | { |
| 3615 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3616 | if (arg == NULL) { |
| 3617 | Py_DECREF(*(PyObject**)addr); |
| 3618 | return 1; |
| 3619 | } |
| 3620 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3621 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3622 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3623 | output = arg; |
| 3624 | Py_INCREF(output); |
| 3625 | } |
| 3626 | else { |
| 3627 | arg = PyBytes_FromObject(arg); |
| 3628 | if (!arg) |
| 3629 | return 0; |
| 3630 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3631 | PyBytes_GET_SIZE(arg)); |
| 3632 | Py_DECREF(arg); |
| 3633 | if (!output) |
| 3634 | return 0; |
| 3635 | if (!PyUnicode_Check(output)) { |
| 3636 | Py_DECREF(output); |
| 3637 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3638 | return 0; |
| 3639 | } |
| 3640 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3641 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3642 | Py_DECREF(output); |
| 3643 | return 0; |
| 3644 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3645 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3646 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3647 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3648 | Py_DECREF(output); |
| 3649 | return 0; |
| 3650 | } |
| 3651 | *(PyObject**)addr = output; |
| 3652 | return Py_CLEANUP_SUPPORTED; |
| 3653 | } |
| 3654 | |
| 3655 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3656 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3657 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3658 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3659 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3660 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3661 | if (!PyUnicode_Check(unicode)) { |
| 3662 | PyErr_BadArgument(); |
| 3663 | return NULL; |
| 3664 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3665 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3666 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3667 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3668 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3669 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3670 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3671 | if (bytes == NULL) |
| 3672 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3673 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3674 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3675 | Py_DECREF(bytes); |
| 3676 | return NULL; |
| 3677 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3678 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3679 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3680 | PyBytes_AS_STRING(bytes), |
| 3681 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3682 | Py_DECREF(bytes); |
| 3683 | } |
| 3684 | |
| 3685 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3686 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3687 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3691 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3692 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3693 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3694 | } |
| 3695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3696 | Py_UNICODE * |
| 3697 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3698 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3699 | const unsigned char *one_byte; |
| 3700 | #if SIZEOF_WCHAR_T == 4 |
| 3701 | const Py_UCS2 *two_bytes; |
| 3702 | #else |
| 3703 | const Py_UCS4 *four_bytes; |
| 3704 | const Py_UCS4 *ucs4_end; |
| 3705 | Py_ssize_t num_surrogates; |
| 3706 | #endif |
| 3707 | wchar_t *w; |
| 3708 | wchar_t *wchar_end; |
| 3709 | |
| 3710 | if (!PyUnicode_Check(unicode)) { |
| 3711 | PyErr_BadArgument(); |
| 3712 | return NULL; |
| 3713 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3714 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3715 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3716 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3717 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3718 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3719 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3720 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3721 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3722 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3723 | num_surrogates = 0; |
| 3724 | |
| 3725 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3726 | if (*four_bytes > 0xFFFF) |
| 3727 | ++num_surrogates; |
| 3728 | } |
| 3729 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3730 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3731 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3732 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3733 | PyErr_NoMemory(); |
| 3734 | return NULL; |
| 3735 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3736 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3737 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3738 | w = _PyUnicode_WSTR(unicode); |
| 3739 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3740 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3741 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3742 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3743 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3744 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3745 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3746 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3747 | } |
| 3748 | else |
| 3749 | *w = *four_bytes; |
| 3750 | |
| 3751 | if (w > wchar_end) { |
| 3752 | assert(0 && "Miscalculated string end"); |
| 3753 | } |
| 3754 | } |
| 3755 | *w = 0; |
| 3756 | #else |
| 3757 | /* sizeof(wchar_t) == 4 */ |
| 3758 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3759 | "should share memory already."); |
| 3760 | return NULL; |
| 3761 | #endif |
| 3762 | } |
| 3763 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3764 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3765 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3766 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3767 | PyErr_NoMemory(); |
| 3768 | return NULL; |
| 3769 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3770 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3771 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3772 | w = _PyUnicode_WSTR(unicode); |
| 3773 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3774 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3775 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3776 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3777 | for (; w < wchar_end; ++one_byte, ++w) |
| 3778 | *w = *one_byte; |
| 3779 | /* null-terminate the wstr */ |
| 3780 | *w = 0; |
| 3781 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3782 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3783 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3784 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3785 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3786 | *w = *two_bytes; |
| 3787 | /* null-terminate the wstr */ |
| 3788 | *w = 0; |
| 3789 | #else |
| 3790 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3791 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3792 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3793 | Py_FatalError("Impossible unicode object state, wstr " |
| 3794 | "and str should share memory already."); |
| 3795 | return NULL; |
| 3796 | #endif |
| 3797 | } |
| 3798 | else { |
| 3799 | assert(0 && "This should never happen."); |
| 3800 | } |
| 3801 | } |
| 3802 | } |
| 3803 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3804 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3805 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3808 | Py_UNICODE * |
| 3809 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3810 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3811 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3814 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3815 | Py_ssize_t |
| 3816 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3817 | { |
| 3818 | if (!PyUnicode_Check(unicode)) { |
| 3819 | PyErr_BadArgument(); |
| 3820 | goto onError; |
| 3821 | } |
| 3822 | return PyUnicode_GET_SIZE(unicode); |
| 3823 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3824 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3825 | return -1; |
| 3826 | } |
| 3827 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3828 | Py_ssize_t |
| 3829 | PyUnicode_GetLength(PyObject *unicode) |
| 3830 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3831 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3832 | PyErr_BadArgument(); |
| 3833 | return -1; |
| 3834 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3835 | if (PyUnicode_READY(unicode) == -1) |
| 3836 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3837 | return PyUnicode_GET_LENGTH(unicode); |
| 3838 | } |
| 3839 | |
| 3840 | Py_UCS4 |
| 3841 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3842 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3843 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3844 | PyErr_BadArgument(); |
| 3845 | return (Py_UCS4)-1; |
| 3846 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3847 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3848 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3849 | return (Py_UCS4)-1; |
| 3850 | } |
| 3851 | return PyUnicode_READ_CHAR(unicode, index); |
| 3852 | } |
| 3853 | |
| 3854 | int |
| 3855 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3856 | { |
| 3857 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3858 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | return -1; |
| 3860 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3861 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3862 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3863 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3864 | return -1; |
| 3865 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3866 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3867 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3868 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3869 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3870 | return -1; |
| 3871 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3872 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3873 | index, ch); |
| 3874 | return 0; |
| 3875 | } |
| 3876 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3877 | const char * |
| 3878 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3879 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3880 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3883 | /* create or adjust a UnicodeDecodeError */ |
| 3884 | static void |
| 3885 | make_decode_exception(PyObject **exceptionObject, |
| 3886 | const char *encoding, |
| 3887 | const char *input, Py_ssize_t length, |
| 3888 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3889 | const char *reason) |
| 3890 | { |
| 3891 | if (*exceptionObject == NULL) { |
| 3892 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3893 | encoding, input, length, startpos, endpos, reason); |
| 3894 | } |
| 3895 | else { |
| 3896 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3897 | goto onError; |
| 3898 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3899 | goto onError; |
| 3900 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3901 | goto onError; |
| 3902 | } |
| 3903 | return; |
| 3904 | |
| 3905 | onError: |
| 3906 | Py_DECREF(*exceptionObject); |
| 3907 | *exceptionObject = NULL; |
| 3908 | } |
| 3909 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3910 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3911 | /* error handling callback helper: |
| 3912 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3913 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3914 | and adjust various state variables. |
| 3915 | return 0 on success, -1 on error |
| 3916 | */ |
| 3917 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3918 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3919 | unicode_decode_call_errorhandler_wchar( |
| 3920 | const char *errors, PyObject **errorHandler, |
| 3921 | const char *encoding, const char *reason, |
| 3922 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3923 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3924 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3925 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3926 | 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] | 3927 | |
| 3928 | PyObject *restuple = NULL; |
| 3929 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3930 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3931 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3932 | Py_ssize_t requiredsize; |
| 3933 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3934 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3935 | wchar_t *repwstr; |
| 3936 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3937 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3938 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 3939 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3940 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3941 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3942 | *errorHandler = PyCodec_LookupError(errors); |
| 3943 | if (*errorHandler == NULL) |
| 3944 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3947 | make_decode_exception(exceptionObject, |
| 3948 | encoding, |
| 3949 | *input, *inend - *input, |
| 3950 | *startinpos, *endinpos, |
| 3951 | reason); |
| 3952 | if (*exceptionObject == NULL) |
| 3953 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3954 | |
| 3955 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3956 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3957 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3958 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3959 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3960 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3961 | } |
| 3962 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3964 | |
| 3965 | /* Copy back the bytes variables, which might have been modified by the |
| 3966 | callback */ |
| 3967 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3968 | if (!inputobj) |
| 3969 | goto onError; |
| 3970 | if (!PyBytes_Check(inputobj)) { |
| 3971 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 3972 | } |
| 3973 | *input = PyBytes_AS_STRING(inputobj); |
| 3974 | insize = PyBytes_GET_SIZE(inputobj); |
| 3975 | *inend = *input + insize; |
| 3976 | /* we can DECREF safely, as the exception has another reference, |
| 3977 | so the object won't go away. */ |
| 3978 | Py_DECREF(inputobj); |
| 3979 | |
| 3980 | if (newpos<0) |
| 3981 | newpos = insize+newpos; |
| 3982 | if (newpos<0 || newpos>insize) { |
| 3983 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3984 | goto onError; |
| 3985 | } |
| 3986 | |
| 3987 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 3988 | if (repwstr == NULL) |
| 3989 | goto onError; |
| 3990 | /* need more space? (at least enough for what we |
| 3991 | have+the replacement+the rest of the string (starting |
| 3992 | at the new input position), so we won't have to check space |
| 3993 | when there are no errors in the rest of the string) */ |
| 3994 | requiredsize = *outpos + repwlen + insize-newpos; |
| 3995 | if (requiredsize > outsize) { |
| 3996 | if (requiredsize < 2*outsize) |
| 3997 | requiredsize = 2*outsize; |
| 3998 | if (unicode_resize(output, requiredsize) < 0) |
| 3999 | goto onError; |
| 4000 | } |
| 4001 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4002 | *outpos += repwlen; |
| 4003 | |
| 4004 | *endinpos = newpos; |
| 4005 | *inptr = *input + newpos; |
| 4006 | |
| 4007 | /* we made it! */ |
| 4008 | Py_XDECREF(restuple); |
| 4009 | return 0; |
| 4010 | |
| 4011 | onError: |
| 4012 | Py_XDECREF(restuple); |
| 4013 | return -1; |
| 4014 | } |
| 4015 | #endif /* HAVE_MBCS */ |
| 4016 | |
| 4017 | static int |
| 4018 | unicode_decode_call_errorhandler_writer( |
| 4019 | const char *errors, PyObject **errorHandler, |
| 4020 | const char *encoding, const char *reason, |
| 4021 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4022 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4023 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4024 | { |
| 4025 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4026 | |
| 4027 | PyObject *restuple = NULL; |
| 4028 | PyObject *repunicode = NULL; |
| 4029 | Py_ssize_t insize; |
| 4030 | Py_ssize_t newpos; |
| 4031 | PyObject *inputobj = NULL; |
| 4032 | |
| 4033 | if (*errorHandler == NULL) { |
| 4034 | *errorHandler = PyCodec_LookupError(errors); |
| 4035 | if (*errorHandler == NULL) |
| 4036 | goto onError; |
| 4037 | } |
| 4038 | |
| 4039 | make_decode_exception(exceptionObject, |
| 4040 | encoding, |
| 4041 | *input, *inend - *input, |
| 4042 | *startinpos, *endinpos, |
| 4043 | reason); |
| 4044 | if (*exceptionObject == NULL) |
| 4045 | goto onError; |
| 4046 | |
| 4047 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4048 | if (restuple == NULL) |
| 4049 | goto onError; |
| 4050 | if (!PyTuple_Check(restuple)) { |
| 4051 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4052 | goto onError; |
| 4053 | } |
| 4054 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4055 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4056 | |
| 4057 | /* Copy back the bytes variables, which might have been modified by the |
| 4058 | callback */ |
| 4059 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4060 | if (!inputobj) |
| 4061 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4062 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4063 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4064 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4065 | *input = PyBytes_AS_STRING(inputobj); |
| 4066 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4067 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4068 | /* we can DECREF safely, as the exception has another reference, |
| 4069 | so the object won't go away. */ |
| 4070 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4071 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4072 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4073 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4074 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4075 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4076 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4077 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4078 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4079 | writer->overallocate = 1; |
| 4080 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
| 4081 | return |
| 4082 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4083 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4084 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4085 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4086 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4087 | Py_XDECREF(restuple); |
| 4088 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4089 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4090 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4091 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4092 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4095 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4096 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4097 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4098 | |
| 4099 | /* Three simple macros defining base-64. */ |
| 4100 | |
| 4101 | /* Is c a base-64 character? */ |
| 4102 | |
| 4103 | #define IS_BASE64(c) \ |
| 4104 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4105 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4106 | ((c) >= '0' && (c) <= '9') || \ |
| 4107 | (c) == '+' || (c) == '/') |
| 4108 | |
| 4109 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4110 | |
| 4111 | #define FROM_BASE64(c) \ |
| 4112 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4113 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4114 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4115 | (c) == '+' ? 62 : 63) |
| 4116 | |
| 4117 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4118 | |
| 4119 | #define TO_BASE64(n) \ |
| 4120 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4121 | |
| 4122 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4123 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4124 | * byte not decoding to itself is the + which begins a base64 |
| 4125 | * string. */ |
| 4126 | |
| 4127 | #define DECODE_DIRECT(c) \ |
| 4128 | ((c) <= 127 && (c) != '+') |
| 4129 | |
| 4130 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4131 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4132 | * the above). See RFC2152. This array identifies these different |
| 4133 | * sets: |
| 4134 | * 0 : "Set D" |
| 4135 | * alphanumeric and '(),-./:? |
| 4136 | * 1 : "Set O" |
| 4137 | * !"#$%&*;<=>@[]^_`{|} |
| 4138 | * 2 : "whitespace" |
| 4139 | * ht nl cr sp |
| 4140 | * 3 : special (must be base64 encoded) |
| 4141 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4142 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4143 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4144 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4145 | char utf7_category[128] = { |
| 4146 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4147 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4148 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4149 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4150 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4151 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4152 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4153 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4154 | /* @ A B C D E F G H I J K L M N O */ |
| 4155 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4156 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4157 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4158 | /* ` a b c d e f g h i j k l m n o */ |
| 4159 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4160 | /* p q r s t u v w x y z { | } ~ del */ |
| 4161 | 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] | 4162 | }; |
| 4163 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4164 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4165 | * answer depends on whether we are encoding set O as itself, and also |
| 4166 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4167 | * clear that the answers to these questions vary between |
| 4168 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4169 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4170 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4171 | ((c) < 128 && (c) > 0 && \ |
| 4172 | ((utf7_category[(c)] == 0) || \ |
| 4173 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4174 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4175 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4176 | PyObject * |
| 4177 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4178 | Py_ssize_t size, |
| 4179 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4180 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4181 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4182 | } |
| 4183 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4184 | /* The decoder. The only state we preserve is our read position, |
| 4185 | * i.e. how many characters we have consumed. So if we end in the |
| 4186 | * middle of a shift sequence we have to back off the read position |
| 4187 | * and the output to the beginning of the sequence, otherwise we lose |
| 4188 | * all the shift state (seen bits, number of bits seen, high |
| 4189 | * surrogate). */ |
| 4190 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4191 | PyObject * |
| 4192 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4193 | Py_ssize_t size, |
| 4194 | const char *errors, |
| 4195 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4196 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4197 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4198 | Py_ssize_t startinpos; |
| 4199 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4200 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4201 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4202 | const char *errmsg = ""; |
| 4203 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4204 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4205 | unsigned int base64bits = 0; |
| 4206 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4207 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4208 | PyObject *errorHandler = NULL; |
| 4209 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4210 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4211 | if (size == 0) { |
| 4212 | if (consumed) |
| 4213 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4214 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4215 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4216 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4217 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4218 | _PyUnicodeWriter_Init(&writer, 0); |
| 4219 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4220 | goto onError; |
| 4221 | |
| 4222 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4223 | e = s + size; |
| 4224 | |
| 4225 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4226 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4227 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4228 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4229 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4230 | if (inShift) { /* in a base-64 section */ |
| 4231 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4232 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4233 | base64bits += 6; |
| 4234 | s++; |
| 4235 | if (base64bits >= 16) { |
| 4236 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4237 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4238 | base64bits -= 16; |
| 4239 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4240 | if (surrogate) { |
| 4241 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4242 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4243 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4244 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch2) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4245 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4246 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch2); |
| 4247 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4248 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4249 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4250 | } |
| 4251 | else { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4252 | if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4253 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4254 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate); |
| 4255 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4256 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4257 | } |
| 4258 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4259 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4260 | /* first surrogate */ |
| 4261 | surrogate = outCh; |
| 4262 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4263 | else { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4264 | if (_PyUnicodeWriter_Prepare(&writer, 1, outCh) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4265 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4266 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, outCh); |
| 4267 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4268 | } |
| 4269 | } |
| 4270 | } |
| 4271 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4272 | inShift = 0; |
| 4273 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4274 | if (surrogate) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4275 | if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4276 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4277 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate); |
| 4278 | writer.pos++; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4279 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4280 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4281 | if (base64bits > 0) { /* left-over bits */ |
| 4282 | if (base64bits >= 6) { |
| 4283 | /* We've seen at least one base-64 character */ |
| 4284 | errmsg = "partial character in shift sequence"; |
| 4285 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4286 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4287 | else { |
| 4288 | /* Some bits remain; they should be zero */ |
| 4289 | if (base64buffer != 0) { |
| 4290 | errmsg = "non-zero padding bits in shift sequence"; |
| 4291 | goto utf7Error; |
| 4292 | } |
| 4293 | } |
| 4294 | } |
| 4295 | if (ch != '-') { |
| 4296 | /* '-' is absorbed; other terminating |
| 4297 | characters are preserved */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4298 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -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, ch); |
| 4301 | writer.pos++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4302 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4303 | } |
| 4304 | } |
| 4305 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4306 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4307 | s++; /* consume '+' */ |
| 4308 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4309 | s++; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4310 | if (_PyUnicodeWriter_Prepare(&writer, 1, '+') == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4311 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4312 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '+'); |
| 4313 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4314 | } |
| 4315 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4316 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4317 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4318 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4319 | } |
| 4320 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4321 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4322 | s++; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4323 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
| 4324 | goto onError; |
| 4325 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4326 | writer.pos++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4327 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4328 | else { |
| 4329 | startinpos = s-starts; |
| 4330 | s++; |
| 4331 | errmsg = "unexpected special character"; |
| 4332 | goto utf7Error; |
| 4333 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4334 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4335 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4336 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4337 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4338 | errors, &errorHandler, |
| 4339 | "utf7", errmsg, |
| 4340 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4341 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4342 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4345 | /* end of string */ |
| 4346 | |
| 4347 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4348 | /* if we're in an inconsistent state, that's an error */ |
| 4349 | if (surrogate || |
| 4350 | (base64bits >= 6) || |
| 4351 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4352 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4353 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 | errors, &errorHandler, |
| 4355 | "utf7", "unterminated shift sequence", |
| 4356 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4357 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4358 | goto onError; |
| 4359 | if (s < e) |
| 4360 | goto restart; |
| 4361 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4362 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4363 | |
| 4364 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4365 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4366 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4367 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4368 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | } |
| 4370 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4371 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4372 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4373 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4374 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4375 | Py_XDECREF(errorHandler); |
| 4376 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4377 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4378 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4379 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4380 | Py_XDECREF(errorHandler); |
| 4381 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4382 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4383 | return NULL; |
| 4384 | } |
| 4385 | |
| 4386 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4387 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4388 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4389 | int base64SetO, |
| 4390 | int base64WhiteSpace, |
| 4391 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4392 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4393 | int kind; |
| 4394 | void *data; |
| 4395 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4396 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4397 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4398 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4399 | unsigned int base64bits = 0; |
| 4400 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4401 | char * out; |
| 4402 | char * start; |
| 4403 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4404 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4405 | return NULL; |
| 4406 | kind = PyUnicode_KIND(str); |
| 4407 | data = PyUnicode_DATA(str); |
| 4408 | len = PyUnicode_GET_LENGTH(str); |
| 4409 | |
| 4410 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4411 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4412 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4413 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4414 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4415 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4416 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4417 | if (v == NULL) |
| 4418 | return NULL; |
| 4419 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4420 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4421 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4422 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4423 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4424 | if (inShift) { |
| 4425 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4426 | /* shifting out */ |
| 4427 | if (base64bits) { /* output remaining bits */ |
| 4428 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4429 | base64buffer = 0; |
| 4430 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4431 | } |
| 4432 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4434 | so no '-' is required, except if the character is itself a '-' */ |
| 4435 | if (IS_BASE64(ch) || ch == '-') { |
| 4436 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4437 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4438 | *out++ = (char) ch; |
| 4439 | } |
| 4440 | else { |
| 4441 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4442 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4443 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4444 | else { /* not in a shift sequence */ |
| 4445 | if (ch == '+') { |
| 4446 | *out++ = '+'; |
| 4447 | *out++ = '-'; |
| 4448 | } |
| 4449 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4450 | *out++ = (char) ch; |
| 4451 | } |
| 4452 | else { |
| 4453 | *out++ = '+'; |
| 4454 | inShift = 1; |
| 4455 | goto encode_char; |
| 4456 | } |
| 4457 | } |
| 4458 | continue; |
| 4459 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4460 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4461 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4462 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4463 | /* code first surrogate */ |
| 4464 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4465 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4466 | while (base64bits >= 6) { |
| 4467 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4468 | base64bits -= 6; |
| 4469 | } |
| 4470 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4471 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4472 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4473 | base64bits += 16; |
| 4474 | base64buffer = (base64buffer << 16) | ch; |
| 4475 | while (base64bits >= 6) { |
| 4476 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4477 | base64bits -= 6; |
| 4478 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4479 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4480 | if (base64bits) |
| 4481 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4482 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4483 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4484 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4485 | return NULL; |
| 4486 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4487 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4488 | PyObject * |
| 4489 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4490 | Py_ssize_t size, |
| 4491 | int base64SetO, |
| 4492 | int base64WhiteSpace, |
| 4493 | const char *errors) |
| 4494 | { |
| 4495 | PyObject *result; |
| 4496 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4497 | if (tmp == NULL) |
| 4498 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4499 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4500 | base64WhiteSpace, errors); |
| 4501 | Py_DECREF(tmp); |
| 4502 | return result; |
| 4503 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4504 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4505 | #undef IS_BASE64 |
| 4506 | #undef FROM_BASE64 |
| 4507 | #undef TO_BASE64 |
| 4508 | #undef DECODE_DIRECT |
| 4509 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4510 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4511 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4512 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4513 | PyObject * |
| 4514 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4515 | Py_ssize_t size, |
| 4516 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4517 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4518 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4519 | } |
| 4520 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4521 | #include "stringlib/asciilib.h" |
| 4522 | #include "stringlib/codecs.h" |
| 4523 | #include "stringlib/undef.h" |
| 4524 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4525 | #include "stringlib/ucs1lib.h" |
| 4526 | #include "stringlib/codecs.h" |
| 4527 | #include "stringlib/undef.h" |
| 4528 | |
| 4529 | #include "stringlib/ucs2lib.h" |
| 4530 | #include "stringlib/codecs.h" |
| 4531 | #include "stringlib/undef.h" |
| 4532 | |
| 4533 | #include "stringlib/ucs4lib.h" |
| 4534 | #include "stringlib/codecs.h" |
| 4535 | #include "stringlib/undef.h" |
| 4536 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4537 | /* Mask to quickly check whether a C 'long' contains a |
| 4538 | non-ASCII, UTF8-encoded char. */ |
| 4539 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4540 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4541 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4542 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4543 | #else |
| 4544 | # error C 'long' size should be either 4 or 8! |
| 4545 | #endif |
| 4546 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4547 | static Py_ssize_t |
| 4548 | 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] | 4549 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4550 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4551 | 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] | 4552 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4553 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4554 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4555 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4556 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4557 | an explanation. */ |
| 4558 | /* Help register allocation */ |
| 4559 | register const char *_p = p; |
| 4560 | register Py_UCS1 * q = dest; |
| 4561 | while (_p < aligned_end) { |
| 4562 | unsigned long value = *(const unsigned long *) _p; |
| 4563 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4564 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4565 | *((unsigned long *)q) = value; |
| 4566 | _p += SIZEOF_LONG; |
| 4567 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4568 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4569 | p = _p; |
| 4570 | while (p < end) { |
| 4571 | if ((unsigned char)*p & 0x80) |
| 4572 | break; |
| 4573 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4574 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4575 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4576 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4577 | #endif |
| 4578 | while (p < end) { |
| 4579 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4580 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4581 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4582 | /* Help register allocation */ |
| 4583 | register const char *_p = p; |
| 4584 | while (_p < aligned_end) { |
| 4585 | unsigned long value = *(unsigned long *) _p; |
| 4586 | if (value & ASCII_CHAR_MASK) |
| 4587 | break; |
| 4588 | _p += SIZEOF_LONG; |
| 4589 | } |
| 4590 | p = _p; |
| 4591 | if (_p == end) |
| 4592 | break; |
| 4593 | } |
| 4594 | if ((unsigned char)*p & 0x80) |
| 4595 | break; |
| 4596 | ++p; |
| 4597 | } |
| 4598 | memcpy(dest, start, p - start); |
| 4599 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4600 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4601 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4602 | PyObject * |
| 4603 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4604 | Py_ssize_t size, |
| 4605 | const char *errors, |
| 4606 | Py_ssize_t *consumed) |
| 4607 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4608 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4609 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4610 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4611 | |
| 4612 | Py_ssize_t startinpos; |
| 4613 | Py_ssize_t endinpos; |
| 4614 | const char *errmsg = ""; |
| 4615 | PyObject *errorHandler = NULL; |
| 4616 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4617 | |
| 4618 | if (size == 0) { |
| 4619 | if (consumed) |
| 4620 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4621 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4622 | } |
| 4623 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4624 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4625 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4626 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4627 | *consumed = 1; |
| 4628 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4629 | } |
| 4630 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4631 | _PyUnicodeWriter_Init(&writer, 0); |
| 4632 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4633 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4634 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4635 | writer.pos = ascii_decode(s, end, writer.data); |
| 4636 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4637 | while (s < end) { |
| 4638 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4639 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4640 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4641 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4642 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4643 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4644 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4645 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4646 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4647 | } else { |
| 4648 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4649 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | switch (ch) { |
| 4653 | case 0: |
| 4654 | if (s == end || consumed) |
| 4655 | goto End; |
| 4656 | errmsg = "unexpected end of data"; |
| 4657 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4658 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4659 | break; |
| 4660 | case 1: |
| 4661 | errmsg = "invalid start byte"; |
| 4662 | startinpos = s - starts; |
| 4663 | endinpos = startinpos + 1; |
| 4664 | break; |
| 4665 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4666 | case 3: |
| 4667 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4668 | errmsg = "invalid continuation byte"; |
| 4669 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4670 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4671 | break; |
| 4672 | default: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4673 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4674 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4675 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4676 | writer.pos++; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4677 | continue; |
| 4678 | } |
| 4679 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4680 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4681 | errors, &errorHandler, |
| 4682 | "utf-8", errmsg, |
| 4683 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4684 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4685 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4686 | } |
| 4687 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4688 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4689 | if (consumed) |
| 4690 | *consumed = s - starts; |
| 4691 | |
| 4692 | Py_XDECREF(errorHandler); |
| 4693 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4694 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4695 | |
| 4696 | onError: |
| 4697 | Py_XDECREF(errorHandler); |
| 4698 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4699 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4700 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4701 | } |
| 4702 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4703 | #ifdef __APPLE__ |
| 4704 | |
| 4705 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4706 | used to decode the command line arguments on Mac OS X. |
| 4707 | |
| 4708 | Return a pointer to a newly allocated wide character string (use |
| 4709 | PyMem_Free() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4710 | |
| 4711 | wchar_t* |
| 4712 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4713 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4714 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4715 | wchar_t *unicode; |
| 4716 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4717 | |
| 4718 | /* Note: size will always be longer than the resulting Unicode |
| 4719 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4720 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4721 | return NULL; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4722 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4723 | if (!unicode) |
| 4724 | return NULL; |
| 4725 | |
| 4726 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4727 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4728 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4729 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4730 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4731 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4732 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4733 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4734 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4735 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4736 | if (ch > 0xFF) { |
| 4737 | #if SIZEOF_WCHAR_T == 4 |
| 4738 | assert(0); |
| 4739 | #else |
| 4740 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4741 | /* compute and append the two surrogates: */ |
| 4742 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4743 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4744 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4745 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4746 | else { |
| 4747 | if (!ch && s == e) |
| 4748 | break; |
| 4749 | /* surrogateescape */ |
| 4750 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4751 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4752 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4753 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4754 | return unicode; |
| 4755 | } |
| 4756 | |
| 4757 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4758 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4759 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4760 | |
| 4761 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4762 | and allocate exactly as much space needed at the end. Else allocate the |
| 4763 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4764 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4765 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4766 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4767 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4768 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4769 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4770 | void *data; |
| 4771 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4772 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4773 | if (!PyUnicode_Check(unicode)) { |
| 4774 | PyErr_BadArgument(); |
| 4775 | return NULL; |
| 4776 | } |
| 4777 | |
| 4778 | if (PyUnicode_READY(unicode) == -1) |
| 4779 | return NULL; |
| 4780 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4781 | if (PyUnicode_UTF8(unicode)) |
| 4782 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4783 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4784 | |
| 4785 | kind = PyUnicode_KIND(unicode); |
| 4786 | data = PyUnicode_DATA(unicode); |
| 4787 | size = PyUnicode_GET_LENGTH(unicode); |
| 4788 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4789 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4790 | default: |
| 4791 | assert(0); |
| 4792 | case PyUnicode_1BYTE_KIND: |
| 4793 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4794 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4795 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4796 | case PyUnicode_2BYTE_KIND: |
| 4797 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4798 | case PyUnicode_4BYTE_KIND: |
| 4799 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4800 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4803 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4804 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4805 | Py_ssize_t size, |
| 4806 | const char *errors) |
| 4807 | { |
| 4808 | PyObject *v, *unicode; |
| 4809 | |
| 4810 | unicode = PyUnicode_FromUnicode(s, size); |
| 4811 | if (unicode == NULL) |
| 4812 | return NULL; |
| 4813 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4814 | Py_DECREF(unicode); |
| 4815 | return v; |
| 4816 | } |
| 4817 | |
| 4818 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4819 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4820 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4821 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4824 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4825 | |
| 4826 | PyObject * |
| 4827 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4828 | Py_ssize_t size, |
| 4829 | const char *errors, |
| 4830 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4831 | { |
| 4832 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4833 | } |
| 4834 | |
| 4835 | PyObject * |
| 4836 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4837 | Py_ssize_t size, |
| 4838 | const char *errors, |
| 4839 | int *byteorder, |
| 4840 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4841 | { |
| 4842 | const char *starts = s; |
| 4843 | Py_ssize_t startinpos; |
| 4844 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4845 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4846 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4847 | int le, bo = 0; /* assume native ordering by default */ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4848 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4849 | PyObject *errorHandler = NULL; |
| 4850 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4851 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4852 | q = (unsigned char *)s; |
| 4853 | e = q + size; |
| 4854 | |
| 4855 | if (byteorder) |
| 4856 | bo = *byteorder; |
| 4857 | |
| 4858 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4859 | byte order setting accordingly. In native mode, the leading BOM |
| 4860 | mark is skipped, in all other modes, it is copied to the output |
| 4861 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4862 | if (bo == 0 && size >= 4) { |
| 4863 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4864 | if (bom == 0x0000FEFF) { |
| 4865 | bo = -1; |
| 4866 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4867 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4868 | else if (bom == 0xFFFE0000) { |
| 4869 | bo = 1; |
| 4870 | q += 4; |
| 4871 | } |
| 4872 | if (byteorder) |
| 4873 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4876 | if (q == e) { |
| 4877 | if (consumed) |
| 4878 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4879 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4882 | #ifdef WORDS_BIGENDIAN |
| 4883 | le = bo < 0; |
| 4884 | #else |
| 4885 | le = bo <= 0; |
| 4886 | #endif |
| 4887 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4888 | _PyUnicodeWriter_Init(&writer, 0); |
| 4889 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 3) / 4, 127) == -1) |
| 4890 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4891 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4892 | while (1) { |
| 4893 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4894 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4895 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4896 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4897 | enum PyUnicode_Kind kind = writer.kind; |
| 4898 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4899 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4900 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4901 | if (le) { |
| 4902 | do { |
| 4903 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4904 | if (ch > maxch) |
| 4905 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4906 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4907 | q += 4; |
| 4908 | } while (q <= last); |
| 4909 | } |
| 4910 | else { |
| 4911 | do { |
| 4912 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 4913 | if (ch > maxch) |
| 4914 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4915 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4916 | q += 4; |
| 4917 | } while (q <= last); |
| 4918 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4919 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4920 | } |
| 4921 | |
| 4922 | if (ch <= maxch) { |
| 4923 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4924 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4925 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4926 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4927 | startinpos = ((const char *)q) - starts; |
| 4928 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4929 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4930 | else { |
| 4931 | if (ch < 0x110000) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4932 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4933 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4934 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4935 | writer.pos++; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4936 | q += 4; |
| 4937 | continue; |
| 4938 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4939 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4940 | startinpos = ((const char *)q) - starts; |
| 4941 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4942 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4943 | |
| 4944 | /* The remaining input chars are ignored if the callback |
| 4945 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4946 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4947 | errors, &errorHandler, |
| 4948 | "utf32", errmsg, |
| 4949 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4950 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4951 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4954 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4955 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4956 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4957 | Py_XDECREF(errorHandler); |
| 4958 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4959 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4960 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4961 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4962 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4963 | Py_XDECREF(errorHandler); |
| 4964 | Py_XDECREF(exc); |
| 4965 | return NULL; |
| 4966 | } |
| 4967 | |
| 4968 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4969 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 4970 | const char *errors, |
| 4971 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4972 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4973 | int kind; |
| 4974 | void *data; |
| 4975 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4976 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4977 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4978 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4979 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 4980 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4981 | int iorder[] = {0, 1, 2, 3}; |
| 4982 | #else |
| 4983 | int iorder[] = {3, 2, 1, 0}; |
| 4984 | #endif |
| 4985 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4986 | #define STORECHAR(CH) \ |
| 4987 | do { \ |
| 4988 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4989 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4990 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4991 | p[iorder[0]] = (CH) & 0xff; \ |
| 4992 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4993 | } while(0) |
| 4994 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4995 | if (!PyUnicode_Check(str)) { |
| 4996 | PyErr_BadArgument(); |
| 4997 | return NULL; |
| 4998 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4999 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5000 | return NULL; |
| 5001 | kind = PyUnicode_KIND(str); |
| 5002 | data = PyUnicode_DATA(str); |
| 5003 | len = PyUnicode_GET_LENGTH(str); |
| 5004 | |
| 5005 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5006 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5007 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5008 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5009 | if (v == NULL) |
| 5010 | return NULL; |
| 5011 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5012 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5013 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5014 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5015 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5016 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5017 | |
| 5018 | if (byteorder == -1) { |
| 5019 | /* force LE */ |
| 5020 | iorder[0] = 0; |
| 5021 | iorder[1] = 1; |
| 5022 | iorder[2] = 2; |
| 5023 | iorder[3] = 3; |
| 5024 | } |
| 5025 | else if (byteorder == 1) { |
| 5026 | /* force BE */ |
| 5027 | iorder[0] = 3; |
| 5028 | iorder[1] = 2; |
| 5029 | iorder[2] = 1; |
| 5030 | iorder[3] = 0; |
| 5031 | } |
| 5032 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5033 | for (i = 0; i < len; i++) |
| 5034 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5035 | |
| 5036 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5037 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5038 | #undef STORECHAR |
| 5039 | } |
| 5040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5041 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5042 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5043 | Py_ssize_t size, |
| 5044 | const char *errors, |
| 5045 | int byteorder) |
| 5046 | { |
| 5047 | PyObject *result; |
| 5048 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5049 | if (tmp == NULL) |
| 5050 | return NULL; |
| 5051 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5052 | Py_DECREF(tmp); |
| 5053 | return result; |
| 5054 | } |
| 5055 | |
| 5056 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5057 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5058 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5059 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5062 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5063 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5064 | PyObject * |
| 5065 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5066 | Py_ssize_t size, |
| 5067 | const char *errors, |
| 5068 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5069 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5070 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5071 | } |
| 5072 | |
| 5073 | PyObject * |
| 5074 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5075 | Py_ssize_t size, |
| 5076 | const char *errors, |
| 5077 | int *byteorder, |
| 5078 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5079 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5080 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5081 | Py_ssize_t startinpos; |
| 5082 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5083 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5084 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5085 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5086 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5087 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5088 | PyObject *errorHandler = NULL; |
| 5089 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5090 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5091 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5092 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5093 | |
| 5094 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5095 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5097 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5098 | byte order setting accordingly. In native mode, the leading BOM |
| 5099 | mark is skipped, in all other modes, it is copied to the output |
| 5100 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5101 | if (bo == 0 && size >= 2) { |
| 5102 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5103 | if (bom == 0xFEFF) { |
| 5104 | q += 2; |
| 5105 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5106 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5107 | else if (bom == 0xFFFE) { |
| 5108 | q += 2; |
| 5109 | bo = 1; |
| 5110 | } |
| 5111 | if (byteorder) |
| 5112 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5113 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5114 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5115 | if (q == e) { |
| 5116 | if (consumed) |
| 5117 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5118 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5119 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5120 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5121 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5122 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5123 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5124 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5125 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5126 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5127 | /* Note: size will always be longer than the resulting Unicode |
| 5128 | character count */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5129 | _PyUnicodeWriter_Init(&writer, 0); |
| 5130 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 1) / 2, 127) == -1) |
| 5131 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5132 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5133 | while (1) { |
| 5134 | Py_UCS4 ch = 0; |
| 5135 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5136 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5137 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5138 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5139 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5140 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5141 | native_ordering); |
| 5142 | else |
| 5143 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5144 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5145 | native_ordering); |
| 5146 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5147 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5148 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5149 | native_ordering); |
| 5150 | } else { |
| 5151 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5152 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5153 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5154 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5155 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5156 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5157 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5158 | switch (ch) |
| 5159 | { |
| 5160 | case 0: |
| 5161 | /* remaining byte at the end? (size should be even) */ |
| 5162 | if (q == e || consumed) |
| 5163 | goto End; |
| 5164 | errmsg = "truncated data"; |
| 5165 | startinpos = ((const char *)q) - starts; |
| 5166 | endinpos = ((const char *)e) - starts; |
| 5167 | break; |
| 5168 | /* The remaining input chars are ignored if the callback |
| 5169 | chooses to skip the input */ |
| 5170 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5171 | q -= 2; |
| 5172 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5173 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5174 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5175 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5176 | endinpos = ((const char *)e) - starts; |
| 5177 | break; |
| 5178 | case 2: |
| 5179 | errmsg = "illegal encoding"; |
| 5180 | startinpos = ((const char *)q) - 2 - starts; |
| 5181 | endinpos = startinpos + 2; |
| 5182 | break; |
| 5183 | case 3: |
| 5184 | errmsg = "illegal UTF-16 surrogate"; |
| 5185 | startinpos = ((const char *)q) - 4 - starts; |
| 5186 | endinpos = startinpos + 2; |
| 5187 | break; |
| 5188 | default: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5189 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5190 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5191 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 5192 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5193 | continue; |
| 5194 | } |
| 5195 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5196 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5197 | errors, |
| 5198 | &errorHandler, |
| 5199 | "utf16", errmsg, |
| 5200 | &starts, |
| 5201 | (const char **)&e, |
| 5202 | &startinpos, |
| 5203 | &endinpos, |
| 5204 | &exc, |
| 5205 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5206 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5207 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5208 | } |
| 5209 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5210 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5211 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5212 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5213 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5214 | Py_XDECREF(errorHandler); |
| 5215 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5216 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5217 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5218 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5219 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5220 | Py_XDECREF(errorHandler); |
| 5221 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5222 | return NULL; |
| 5223 | } |
| 5224 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5225 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5226 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5227 | const char *errors, |
| 5228 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5229 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5230 | enum PyUnicode_Kind kind; |
| 5231 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5232 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5233 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5234 | unsigned short *out; |
| 5235 | Py_ssize_t bytesize; |
| 5236 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5237 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5238 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5239 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5240 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5241 | #endif |
| 5242 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5243 | if (!PyUnicode_Check(str)) { |
| 5244 | PyErr_BadArgument(); |
| 5245 | return NULL; |
| 5246 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5247 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5248 | return NULL; |
| 5249 | kind = PyUnicode_KIND(str); |
| 5250 | data = PyUnicode_DATA(str); |
| 5251 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5252 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5253 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5254 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5255 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5256 | const Py_UCS4 *end = in + len; |
| 5257 | while (in < end) |
| 5258 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5259 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5260 | } |
| 5261 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5262 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5263 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5264 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | if (v == NULL) |
| 5266 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5267 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5268 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5269 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5270 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5272 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5273 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5274 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5275 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5276 | switch (kind) { |
| 5277 | case PyUnicode_1BYTE_KIND: { |
| 5278 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5279 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5280 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5281 | case PyUnicode_2BYTE_KIND: { |
| 5282 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5283 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5284 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5285 | case PyUnicode_4BYTE_KIND: { |
| 5286 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5287 | break; |
| 5288 | } |
| 5289 | default: |
| 5290 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5291 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5292 | |
| 5293 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5294 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5297 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5298 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5299 | Py_ssize_t size, |
| 5300 | const char *errors, |
| 5301 | int byteorder) |
| 5302 | { |
| 5303 | PyObject *result; |
| 5304 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5305 | if (tmp == NULL) |
| 5306 | return NULL; |
| 5307 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5308 | Py_DECREF(tmp); |
| 5309 | return result; |
| 5310 | } |
| 5311 | |
| 5312 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5313 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5314 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5315 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5319 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5320 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5321 | if all the escapes in the string make it still a valid ASCII string. |
| 5322 | Returns -1 if any escapes were found which cause the string to |
| 5323 | pop out of ASCII range. Otherwise returns the length of the |
| 5324 | required buffer to hold the string. |
| 5325 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5326 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5327 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5328 | { |
| 5329 | const unsigned char *p = (const unsigned char *)s; |
| 5330 | const unsigned char *end = p + size; |
| 5331 | Py_ssize_t length = 0; |
| 5332 | |
| 5333 | if (size < 0) |
| 5334 | return -1; |
| 5335 | |
| 5336 | for (; p < end; ++p) { |
| 5337 | if (*p > 127) { |
| 5338 | /* Non-ASCII */ |
| 5339 | return -1; |
| 5340 | } |
| 5341 | else if (*p != '\\') { |
| 5342 | /* Normal character */ |
| 5343 | ++length; |
| 5344 | } |
| 5345 | else { |
| 5346 | /* Backslash-escape, check next char */ |
| 5347 | ++p; |
| 5348 | /* Escape sequence reaches till end of string or |
| 5349 | non-ASCII follow-up. */ |
| 5350 | if (p >= end || *p > 127) |
| 5351 | return -1; |
| 5352 | switch (*p) { |
| 5353 | case '\n': |
| 5354 | /* backslash + \n result in zero characters */ |
| 5355 | break; |
| 5356 | case '\\': case '\'': case '\"': |
| 5357 | case 'b': case 'f': case 't': |
| 5358 | case 'n': case 'r': case 'v': case 'a': |
| 5359 | ++length; |
| 5360 | break; |
| 5361 | case '0': case '1': case '2': case '3': |
| 5362 | case '4': case '5': case '6': case '7': |
| 5363 | case 'x': case 'u': case 'U': case 'N': |
| 5364 | /* these do not guarantee ASCII characters */ |
| 5365 | return -1; |
| 5366 | default: |
| 5367 | /* count the backslash + the other character */ |
| 5368 | length += 2; |
| 5369 | } |
| 5370 | } |
| 5371 | } |
| 5372 | return length; |
| 5373 | } |
| 5374 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5375 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5376 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5377 | PyObject * |
| 5378 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5379 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5380 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5382 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5383 | Py_ssize_t startinpos; |
| 5384 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5385 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5387 | char* message; |
| 5388 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5389 | PyObject *errorHandler = NULL; |
| 5390 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5391 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5392 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5393 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5394 | if (len == 0) |
| 5395 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5396 | |
| 5397 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5398 | either the string is pure ASCII with named escapes like \n, etc. |
| 5399 | and we determined it's exact size (common case) |
| 5400 | or it contains \x, \u, ... escape sequences. then we create a |
| 5401 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5402 | _PyUnicodeWriter_Init(&writer, 0); |
| 5403 | if (len > 0) { |
| 5404 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5405 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5406 | assert(writer.kind == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5407 | } |
| 5408 | else { |
| 5409 | /* Escaped strings will always be longer than the resulting |
| 5410 | Unicode string, so we start with size here and then reduce the |
| 5411 | length after conversion to the true value. |
| 5412 | (but if the error callback returns a long replacement string |
| 5413 | we'll have to allocate more space) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5414 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5415 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5416 | } |
| 5417 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5419 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5421 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | while (s < end) { |
| 5423 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5424 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5425 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | |
| 5427 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5428 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5429 | x = (unsigned char)*s; |
| 5430 | s++; |
| 5431 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5432 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5433 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5434 | writer.pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | continue; |
| 5436 | } |
| 5437 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5438 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | /* \ - Escapes */ |
| 5440 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5441 | c = *s++; |
| 5442 | if (s > end) |
| 5443 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5444 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5445 | /* The only case in which i == ascii_length is a backslash |
| 5446 | followed by a newline. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5447 | 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] | 5448 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5449 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5450 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5451 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5452 | #define WRITECHAR(ch) \ |
| 5453 | do { \ |
| 5454 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) \ |
| 5455 | goto onError; \ |
| 5456 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); \ |
| 5457 | writer.pos++; \ |
| 5458 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5459 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5461 | case '\\': WRITECHAR('\\'); break; |
| 5462 | case '\'': WRITECHAR('\''); break; |
| 5463 | case '\"': WRITECHAR('\"'); break; |
| 5464 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5465 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5466 | case 'f': WRITECHAR('\014'); break; |
| 5467 | case 't': WRITECHAR('\t'); break; |
| 5468 | case 'n': WRITECHAR('\n'); break; |
| 5469 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5470 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5471 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5472 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5473 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5474 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5475 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | case '0': case '1': case '2': case '3': |
| 5477 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5478 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5479 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5480 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5481 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5482 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5483 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5484 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5485 | break; |
| 5486 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5487 | /* hex escapes */ |
| 5488 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5490 | digits = 2; |
| 5491 | message = "truncated \\xXX escape"; |
| 5492 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5494 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5495 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5496 | digits = 4; |
| 5497 | message = "truncated \\uXXXX escape"; |
| 5498 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5499 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5500 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5501 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5502 | digits = 8; |
| 5503 | message = "truncated \\UXXXXXXXX escape"; |
| 5504 | hexescape: |
| 5505 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5506 | if (end - s < digits) { |
| 5507 | /* count only hex digits */ |
| 5508 | for (; s < end; ++s) { |
| 5509 | c = (unsigned char)*s; |
| 5510 | if (!Py_ISXDIGIT(c)) |
| 5511 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5512 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5513 | goto error; |
| 5514 | } |
| 5515 | for (; digits--; ++s) { |
| 5516 | c = (unsigned char)*s; |
| 5517 | if (!Py_ISXDIGIT(c)) |
| 5518 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5519 | chr = (chr<<4) & ~0xF; |
| 5520 | if (c >= '0' && c <= '9') |
| 5521 | chr += c - '0'; |
| 5522 | else if (c >= 'a' && c <= 'f') |
| 5523 | chr += 10 + c - 'a'; |
| 5524 | else |
| 5525 | chr += 10 + c - 'A'; |
| 5526 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5527 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5528 | /* _decoding_error will have already written into the |
| 5529 | target buffer. */ |
| 5530 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5531 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5532 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5533 | message = "illegal Unicode character"; |
| 5534 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5535 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5536 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5537 | break; |
| 5538 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5539 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5540 | case 'N': |
| 5541 | message = "malformed \\N character escape"; |
| 5542 | if (ucnhash_CAPI == NULL) { |
| 5543 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5544 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5545 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5546 | if (ucnhash_CAPI == NULL) |
| 5547 | goto ucnhashError; |
| 5548 | } |
| 5549 | if (*s == '{') { |
| 5550 | const char *start = s+1; |
| 5551 | /* look for the closing brace */ |
| 5552 | while (*s != '}' && s < end) |
| 5553 | s++; |
| 5554 | if (s > start && s < end && *s == '}') { |
| 5555 | /* found a name. look it up in the unicode database */ |
| 5556 | message = "unknown Unicode character name"; |
| 5557 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5558 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5559 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5560 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5561 | goto store; |
| 5562 | } |
| 5563 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5564 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5565 | |
| 5566 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5567 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5568 | message = "\\ at end of string"; |
| 5569 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5570 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5571 | } |
| 5572 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5573 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5574 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5575 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5576 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5577 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5578 | continue; |
| 5579 | |
| 5580 | error: |
| 5581 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5582 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5583 | errors, &errorHandler, |
| 5584 | "unicodeescape", message, |
| 5585 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5586 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5587 | goto onError; |
| 5588 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5589 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5590 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5591 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5592 | Py_XDECREF(errorHandler); |
| 5593 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5594 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5595 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5596 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5597 | PyErr_SetString( |
| 5598 | PyExc_UnicodeError, |
| 5599 | "\\N escapes not supported (can't load unicodedata module)" |
| 5600 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5601 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5602 | Py_XDECREF(errorHandler); |
| 5603 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5604 | return NULL; |
| 5605 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5606 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5607 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5608 | Py_XDECREF(errorHandler); |
| 5609 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | return NULL; |
| 5611 | } |
| 5612 | |
| 5613 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5614 | |
| 5615 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5616 | appropriate. |
| 5617 | |
| 5618 | */ |
| 5619 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5620 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5621 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5623 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5624 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5626 | int kind; |
| 5627 | void *data; |
| 5628 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5629 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5630 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5631 | escape. |
| 5632 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5633 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5634 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5635 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5636 | */ |
| 5637 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5638 | if (!PyUnicode_Check(unicode)) { |
| 5639 | PyErr_BadArgument(); |
| 5640 | return NULL; |
| 5641 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5642 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5643 | return NULL; |
| 5644 | len = PyUnicode_GET_LENGTH(unicode); |
| 5645 | kind = PyUnicode_KIND(unicode); |
| 5646 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5647 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5648 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5649 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5650 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5651 | } |
| 5652 | |
| 5653 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5654 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5655 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5656 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5657 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5658 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5659 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5660 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5661 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5662 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | if (repr == NULL) |
| 5664 | return NULL; |
| 5665 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5666 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5668 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5669 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5670 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5671 | /* Escape backslashes */ |
| 5672 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | *p++ = '\\'; |
| 5674 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5675 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5676 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5677 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5678 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5679 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5680 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5681 | *p++ = '\\'; |
| 5682 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5683 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5684 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5685 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5686 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5687 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5688 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5689 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5690 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5691 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5692 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5695 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5696 | *p++ = '\\'; |
| 5697 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5698 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5699 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5700 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5701 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5703 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5704 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5705 | else if (ch == '\t') { |
| 5706 | *p++ = '\\'; |
| 5707 | *p++ = 't'; |
| 5708 | } |
| 5709 | else if (ch == '\n') { |
| 5710 | *p++ = '\\'; |
| 5711 | *p++ = 'n'; |
| 5712 | } |
| 5713 | else if (ch == '\r') { |
| 5714 | *p++ = '\\'; |
| 5715 | *p++ = 'r'; |
| 5716 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5717 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5718 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5719 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5720 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5721 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5722 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5723 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5724 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5725 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5726 | /* Copy everything else as-is */ |
| 5727 | else |
| 5728 | *p++ = (char) ch; |
| 5729 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5730 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5731 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5732 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5733 | return NULL; |
| 5734 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | } |
| 5736 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5737 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5738 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5739 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5741 | PyObject *result; |
| 5742 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5743 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5745 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5746 | Py_DECREF(tmp); |
| 5747 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5748 | } |
| 5749 | |
| 5750 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5751 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5752 | PyObject * |
| 5753 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5754 | Py_ssize_t size, |
| 5755 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5756 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5757 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5758 | Py_ssize_t startinpos; |
| 5759 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5760 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | const char *end; |
| 5762 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5763 | PyObject *errorHandler = NULL; |
| 5764 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5765 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5766 | if (size == 0) |
| 5767 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5768 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | /* Escaped strings will always be longer than the resulting |
| 5770 | 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] | 5771 | length after conversion to the true value. (But decoding error |
| 5772 | handler might have to resize the string) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5773 | _PyUnicodeWriter_Init(&writer, 1); |
| 5774 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5775 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5776 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5777 | end = s + size; |
| 5778 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5779 | unsigned char c; |
| 5780 | Py_UCS4 x; |
| 5781 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5782 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5783 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5784 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5785 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5786 | x = (unsigned char)*s++; |
| 5787 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5788 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5789 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5790 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5791 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5792 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5793 | startinpos = s-starts; |
| 5794 | |
| 5795 | /* \u-escapes are only interpreted iff the number of leading |
| 5796 | backslashes if odd */ |
| 5797 | bs = s; |
| 5798 | for (;s < end;) { |
| 5799 | if (*s != '\\') |
| 5800 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5801 | x = (unsigned char)*s++; |
| 5802 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5803 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5804 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5805 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5806 | } |
| 5807 | if (((s - bs) & 1) == 0 || |
| 5808 | s >= end || |
| 5809 | (*s != 'u' && *s != 'U')) { |
| 5810 | continue; |
| 5811 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5812 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5813 | count = *s=='u' ? 4 : 8; |
| 5814 | s++; |
| 5815 | |
| 5816 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5817 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5818 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5819 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5820 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5821 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5822 | errors, &errorHandler, |
| 5823 | "rawunicodeescape", "truncated \\uXXXX", |
| 5824 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5825 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5826 | goto onError; |
| 5827 | goto nextByte; |
| 5828 | } |
| 5829 | x = (x<<4) & ~0xF; |
| 5830 | if (c >= '0' && c <= '9') |
| 5831 | x += c - '0'; |
| 5832 | else if (c >= 'a' && c <= 'f') |
| 5833 | x += 10 + c - 'a'; |
| 5834 | else |
| 5835 | x += 10 + c - 'A'; |
| 5836 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5837 | if (x <= MAX_UNICODE) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5838 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5839 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5840 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5841 | writer.pos++; |
| 5842 | } |
| 5843 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5844 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5845 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5846 | errors, &errorHandler, |
| 5847 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5848 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5849 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5850 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5851 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5852 | nextByte: |
| 5853 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5855 | Py_XDECREF(errorHandler); |
| 5856 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5857 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5858 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5859 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5860 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5861 | Py_XDECREF(errorHandler); |
| 5862 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5863 | return NULL; |
| 5864 | } |
| 5865 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5866 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5867 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5868 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5870 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | char *p; |
| 5872 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5873 | Py_ssize_t expandsize, pos; |
| 5874 | int kind; |
| 5875 | void *data; |
| 5876 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5877 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5878 | if (!PyUnicode_Check(unicode)) { |
| 5879 | PyErr_BadArgument(); |
| 5880 | return NULL; |
| 5881 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5882 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5883 | return NULL; |
| 5884 | kind = PyUnicode_KIND(unicode); |
| 5885 | data = PyUnicode_DATA(unicode); |
| 5886 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5887 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 5888 | bytes, and 1 byte characters 4. */ |
| 5889 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5890 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5891 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5892 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5893 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5894 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5895 | if (repr == NULL) |
| 5896 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5897 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5898 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5899 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5900 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5901 | for (pos = 0; pos < len; pos++) { |
| 5902 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5903 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5904 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5905 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5906 | *p++ = '\\'; |
| 5907 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5908 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 5909 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 5910 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 5911 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 5912 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5913 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5914 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5915 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5916 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5917 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5918 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5919 | *p++ = '\\'; |
| 5920 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5921 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5922 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5923 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5924 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5926 | /* Copy everything else as-is */ |
| 5927 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | *p++ = (char) ch; |
| 5929 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5930 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5931 | assert(p > q); |
| 5932 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5933 | return NULL; |
| 5934 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5937 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5938 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 5939 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5940 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5941 | PyObject *result; |
| 5942 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5943 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5944 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5945 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 5946 | Py_DECREF(tmp); |
| 5947 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5950 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5951 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5952 | PyObject * |
| 5953 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5954 | Py_ssize_t size, |
| 5955 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5956 | { |
| 5957 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5958 | Py_ssize_t startinpos; |
| 5959 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5960 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5961 | const char *end; |
| 5962 | const char *reason; |
| 5963 | PyObject *errorHandler = NULL; |
| 5964 | PyObject *exc = NULL; |
| 5965 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5966 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 5967 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5968 | 1)) |
| 5969 | return NULL; |
| 5970 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5971 | if (size == 0) |
| 5972 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5973 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5974 | /* XXX overflow detection missing */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5975 | _PyUnicodeWriter_Init(&writer, 0); |
| 5976 | 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] | 5977 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5978 | end = s + size; |
| 5979 | |
| 5980 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 5981 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 5982 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 5983 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 5984 | endinpos = end-starts; |
| 5985 | reason = "truncated input"; |
| 5986 | goto error; |
| 5987 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 5988 | /* We copy the raw representation one byte at a time because the |
| 5989 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 5990 | ((char *) &uch)[0] = s[0]; |
| 5991 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 5992 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 5993 | ((char *) &uch)[2] = s[2]; |
| 5994 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 5995 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 5996 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 5997 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5998 | /* We have to sanity check the raw data, otherwise doom looms for |
| 5999 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6000 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6001 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6002 | reason = "illegal code point (> 0x10FFFF)"; |
| 6003 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6004 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6005 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6006 | s += Py_UNICODE_SIZE; |
| 6007 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6008 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6009 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6010 | Py_UNICODE uch2; |
| 6011 | ((char *) &uch2)[0] = s[0]; |
| 6012 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6013 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6014 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6015 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6016 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6017 | } |
| 6018 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6019 | #endif |
| 6020 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6021 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6022 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6023 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 6024 | writer.pos++; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6025 | continue; |
| 6026 | |
| 6027 | error: |
| 6028 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6029 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6030 | errors, &errorHandler, |
| 6031 | "unicode_internal", reason, |
| 6032 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6033 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6034 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6037 | Py_XDECREF(errorHandler); |
| 6038 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6039 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6040 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6041 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6042 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6043 | Py_XDECREF(errorHandler); |
| 6044 | Py_XDECREF(exc); |
| 6045 | return NULL; |
| 6046 | } |
| 6047 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6049 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6050 | PyObject * |
| 6051 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6052 | Py_ssize_t size, |
| 6053 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6056 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6059 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6060 | static void |
| 6061 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6062 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6063 | PyObject *unicode, |
| 6064 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6065 | const char *reason) |
| 6066 | { |
| 6067 | if (*exceptionObject == NULL) { |
| 6068 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6069 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6070 | encoding, unicode, startpos, endpos, reason); |
| 6071 | } |
| 6072 | else { |
| 6073 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6074 | goto onError; |
| 6075 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6076 | goto onError; |
| 6077 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6078 | goto onError; |
| 6079 | return; |
| 6080 | onError: |
| 6081 | Py_DECREF(*exceptionObject); |
| 6082 | *exceptionObject = NULL; |
| 6083 | } |
| 6084 | } |
| 6085 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6086 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6087 | static void |
| 6088 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6089 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6090 | PyObject *unicode, |
| 6091 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6092 | const char *reason) |
| 6093 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6094 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6095 | encoding, unicode, startpos, endpos, reason); |
| 6096 | if (*exceptionObject != NULL) |
| 6097 | PyCodec_StrictErrors(*exceptionObject); |
| 6098 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6099 | |
| 6100 | /* error handling callback helper: |
| 6101 | build arguments, call the callback and check the arguments, |
| 6102 | put the result into newpos and return the replacement string, which |
| 6103 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6104 | static PyObject * |
| 6105 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6106 | PyObject **errorHandler, |
| 6107 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6108 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6109 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6110 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6111 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6112 | 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] | 6113 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6114 | PyObject *restuple; |
| 6115 | PyObject *resunicode; |
| 6116 | |
| 6117 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6119 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6120 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6123 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6124 | return NULL; |
| 6125 | len = PyUnicode_GET_LENGTH(unicode); |
| 6126 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6127 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6128 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6129 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6130 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6131 | |
| 6132 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6133 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6134 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6135 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6136 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6137 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6138 | Py_DECREF(restuple); |
| 6139 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6140 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6141 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6142 | &resunicode, newpos)) { |
| 6143 | Py_DECREF(restuple); |
| 6144 | return NULL; |
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 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6147 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6148 | Py_DECREF(restuple); |
| 6149 | return NULL; |
| 6150 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6151 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6152 | *newpos = len + *newpos; |
| 6153 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6154 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6155 | Py_DECREF(restuple); |
| 6156 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6157 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6158 | Py_INCREF(resunicode); |
| 6159 | Py_DECREF(restuple); |
| 6160 | return resunicode; |
| 6161 | } |
| 6162 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6163 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6164 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6165 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6166 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6167 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6168 | /* input state */ |
| 6169 | Py_ssize_t pos=0, size; |
| 6170 | int kind; |
| 6171 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6172 | /* output object */ |
| 6173 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6174 | /* pointer into the output */ |
| 6175 | char *str; |
| 6176 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6177 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6178 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6179 | 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] | 6180 | PyObject *errorHandler = NULL; |
| 6181 | PyObject *exc = NULL; |
| 6182 | /* the following variable is used for caching string comparisons |
| 6183 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6184 | int known_errorHandler = -1; |
| 6185 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6186 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6187 | return NULL; |
| 6188 | size = PyUnicode_GET_LENGTH(unicode); |
| 6189 | kind = PyUnicode_KIND(unicode); |
| 6190 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6191 | /* allocate enough for a simple encoding without |
| 6192 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6193 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6194 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6195 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6196 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6197 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6198 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6199 | ressize = size; |
| 6200 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6201 | while (pos < size) { |
| 6202 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6203 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6204 | /* can we encode this? */ |
| 6205 | if (c<limit) { |
| 6206 | /* no overflow check, because we know that the space is enough */ |
| 6207 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6208 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6209 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6210 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6211 | Py_ssize_t requiredsize; |
| 6212 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6213 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6215 | Py_ssize_t collstart = pos; |
| 6216 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6217 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6218 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6219 | ++collend; |
| 6220 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6221 | if (known_errorHandler==-1) { |
| 6222 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6223 | known_errorHandler = 1; |
| 6224 | else if (!strcmp(errors, "replace")) |
| 6225 | known_errorHandler = 2; |
| 6226 | else if (!strcmp(errors, "ignore")) |
| 6227 | known_errorHandler = 3; |
| 6228 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6229 | known_errorHandler = 4; |
| 6230 | else |
| 6231 | known_errorHandler = 0; |
| 6232 | } |
| 6233 | switch (known_errorHandler) { |
| 6234 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6235 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6236 | goto onError; |
| 6237 | case 2: /* replace */ |
| 6238 | while (collstart++<collend) |
| 6239 | *str++ = '?'; /* fall through */ |
| 6240 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6241 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6242 | break; |
| 6243 | case 4: /* xmlcharrefreplace */ |
| 6244 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6245 | /* determine replacement size */ |
| 6246 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6247 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6248 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6249 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6250 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6251 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6252 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6253 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6254 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6255 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6256 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6258 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6259 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6260 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6261 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6263 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6264 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6265 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6266 | if (requiredsize > ressize) { |
| 6267 | if (requiredsize<2*ressize) |
| 6268 | requiredsize = 2*ressize; |
| 6269 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6270 | goto onError; |
| 6271 | str = PyBytes_AS_STRING(res) + respos; |
| 6272 | ressize = requiredsize; |
| 6273 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6274 | /* generate replacement */ |
| 6275 | for (i = collstart; i < collend; ++i) { |
| 6276 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6278 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6279 | break; |
| 6280 | default: |
| 6281 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6282 | encoding, reason, unicode, &exc, |
| 6283 | collstart, collend, &newpos); |
| 6284 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6285 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6286 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6287 | if (PyBytes_Check(repunicode)) { |
| 6288 | /* Directly copy bytes result to output. */ |
| 6289 | repsize = PyBytes_Size(repunicode); |
| 6290 | if (repsize > 1) { |
| 6291 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6292 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6293 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6294 | Py_DECREF(repunicode); |
| 6295 | goto onError; |
| 6296 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6297 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6298 | ressize += repsize-1; |
| 6299 | } |
| 6300 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6301 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6302 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6303 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6304 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6305 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6306 | /* need more space? (at least enough for what we |
| 6307 | have+the replacement+the rest of the string, so |
| 6308 | we won't have to check space for encodable characters) */ |
| 6309 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6310 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6311 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6312 | if (requiredsize > ressize) { |
| 6313 | if (requiredsize<2*ressize) |
| 6314 | requiredsize = 2*ressize; |
| 6315 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6316 | Py_DECREF(repunicode); |
| 6317 | goto onError; |
| 6318 | } |
| 6319 | str = PyBytes_AS_STRING(res) + respos; |
| 6320 | ressize = requiredsize; |
| 6321 | } |
| 6322 | /* check if there is anything unencodable in the replacement |
| 6323 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6324 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6325 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6326 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6327 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6328 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6329 | Py_DECREF(repunicode); |
| 6330 | goto onError; |
| 6331 | } |
| 6332 | *str = (char)c; |
| 6333 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6334 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6335 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6336 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6337 | } |
| 6338 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6339 | /* Resize if we allocated to much */ |
| 6340 | size = str - PyBytes_AS_STRING(res); |
| 6341 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6342 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6343 | if (_PyBytes_Resize(&res, size) < 0) |
| 6344 | goto onError; |
| 6345 | } |
| 6346 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6347 | Py_XDECREF(errorHandler); |
| 6348 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6349 | return res; |
| 6350 | |
| 6351 | onError: |
| 6352 | Py_XDECREF(res); |
| 6353 | Py_XDECREF(errorHandler); |
| 6354 | Py_XDECREF(exc); |
| 6355 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6356 | } |
| 6357 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6358 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6359 | PyObject * |
| 6360 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6361 | Py_ssize_t size, |
| 6362 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6363 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6364 | PyObject *result; |
| 6365 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6366 | if (unicode == NULL) |
| 6367 | return NULL; |
| 6368 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6369 | Py_DECREF(unicode); |
| 6370 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6373 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6374 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6375 | { |
| 6376 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6377 | PyErr_BadArgument(); |
| 6378 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6380 | if (PyUnicode_READY(unicode) == -1) |
| 6381 | return NULL; |
| 6382 | /* Fast path: if it is a one-byte string, construct |
| 6383 | bytes object directly. */ |
| 6384 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6385 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6386 | PyUnicode_GET_LENGTH(unicode)); |
| 6387 | /* Non-Latin-1 characters present. Defer to above function to |
| 6388 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6389 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | PyObject* |
| 6393 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6394 | { |
| 6395 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6399 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6400 | PyObject * |
| 6401 | PyUnicode_DecodeASCII(const char *s, |
| 6402 | Py_ssize_t size, |
| 6403 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6405 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6406 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6407 | int kind; |
| 6408 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6409 | Py_ssize_t startinpos; |
| 6410 | Py_ssize_t endinpos; |
| 6411 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6412 | const char *e; |
| 6413 | PyObject *errorHandler = NULL; |
| 6414 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6417 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6418 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6419 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6420 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6421 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6422 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6423 | _PyUnicodeWriter_Init(&writer, 0); |
| 6424 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6426 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6427 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6428 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6429 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6430 | writer.pos = outpos; |
| 6431 | if (writer.pos == size) |
| 6432 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6433 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6434 | s += writer.pos; |
| 6435 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6436 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6437 | register unsigned char c = (unsigned char)*s; |
| 6438 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6439 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6440 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6441 | ++s; |
| 6442 | } |
| 6443 | else { |
| 6444 | startinpos = s-starts; |
| 6445 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6446 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6447 | errors, &errorHandler, |
| 6448 | "ascii", "ordinal not in range(128)", |
| 6449 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6450 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6451 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6452 | kind = writer.kind; |
| 6453 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6454 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6455 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6456 | Py_XDECREF(errorHandler); |
| 6457 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6458 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6459 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6460 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6461 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6462 | Py_XDECREF(errorHandler); |
| 6463 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6464 | return NULL; |
| 6465 | } |
| 6466 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6467 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6468 | PyObject * |
| 6469 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6470 | Py_ssize_t size, |
| 6471 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6472 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6473 | PyObject *result; |
| 6474 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6475 | if (unicode == NULL) |
| 6476 | return NULL; |
| 6477 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6478 | Py_DECREF(unicode); |
| 6479 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6480 | } |
| 6481 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6482 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6483 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | { |
| 6485 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6486 | PyErr_BadArgument(); |
| 6487 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6488 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6489 | if (PyUnicode_READY(unicode) == -1) |
| 6490 | return NULL; |
| 6491 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6492 | directly. Else defer to above function to raise the exception. */ |
| 6493 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6494 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6495 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6496 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6497 | } |
| 6498 | |
| 6499 | PyObject * |
| 6500 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6501 | { |
| 6502 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | } |
| 6504 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6505 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6506 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6507 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6508 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6509 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6510 | #define NEED_RETRY |
| 6511 | #endif |
| 6512 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6513 | #ifndef WC_ERR_INVALID_CHARS |
| 6514 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6515 | #endif |
| 6516 | |
| 6517 | static char* |
| 6518 | code_page_name(UINT code_page, PyObject **obj) |
| 6519 | { |
| 6520 | *obj = NULL; |
| 6521 | if (code_page == CP_ACP) |
| 6522 | return "mbcs"; |
| 6523 | if (code_page == CP_UTF7) |
| 6524 | return "CP_UTF7"; |
| 6525 | if (code_page == CP_UTF8) |
| 6526 | return "CP_UTF8"; |
| 6527 | |
| 6528 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6529 | if (*obj == NULL) |
| 6530 | return NULL; |
| 6531 | return PyBytes_AS_STRING(*obj); |
| 6532 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6533 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6534 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6535 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6536 | { |
| 6537 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6538 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6539 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6540 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6541 | return 0; |
| 6542 | |
| 6543 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6544 | if (prev == curr) |
| 6545 | return 1; |
| 6546 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6547 | as it assumes an incomplete character consists of a single |
| 6548 | byte. */ |
| 6549 | if (curr - prev == 2) |
| 6550 | return 1; |
| 6551 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6552 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6553 | return 0; |
| 6554 | } |
| 6555 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6556 | static DWORD |
| 6557 | decode_code_page_flags(UINT code_page) |
| 6558 | { |
| 6559 | if (code_page == CP_UTF7) { |
| 6560 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6561 | return 0; |
| 6562 | } |
| 6563 | else |
| 6564 | return MB_ERR_INVALID_CHARS; |
| 6565 | } |
| 6566 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6567 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6568 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6569 | * mode. |
| 6570 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6571 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6572 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6573 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6574 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6575 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6576 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6577 | const char *in, |
| 6578 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6579 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6580 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6581 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6582 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6583 | |
| 6584 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6585 | assert(insize > 0); |
| 6586 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6587 | if (outsize <= 0) |
| 6588 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6589 | |
| 6590 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6592 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6593 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6594 | if (*v == NULL) |
| 6595 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6596 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6597 | } |
| 6598 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6599 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6600 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6601 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6602 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6603 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
| 6606 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6607 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6608 | if (outsize <= 0) |
| 6609 | goto error; |
| 6610 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6611 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6612 | error: |
| 6613 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6614 | return -2; |
| 6615 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6616 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6617 | } |
| 6618 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6619 | /* |
| 6620 | * Decode a byte string from a code page into unicode object with an error |
| 6621 | * handler. |
| 6622 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6623 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6624 | * UnicodeDecodeError exception and returns -1 on error. |
| 6625 | */ |
| 6626 | static int |
| 6627 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6628 | PyObject **v, |
| 6629 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6630 | const char *errors) |
| 6631 | { |
| 6632 | const char *startin = in; |
| 6633 | const char *endin = in + size; |
| 6634 | const DWORD flags = decode_code_page_flags(code_page); |
| 6635 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6636 | 2000 English version of the message. */ |
| 6637 | const char *reason = "No mapping for the Unicode character exists " |
| 6638 | "in the target code page."; |
| 6639 | /* each step cannot decode more than 1 character, but a character can be |
| 6640 | represented as a surrogate pair */ |
| 6641 | wchar_t buffer[2], *startout, *out; |
| 6642 | int insize, outsize; |
| 6643 | PyObject *errorHandler = NULL; |
| 6644 | PyObject *exc = NULL; |
| 6645 | PyObject *encoding_obj = NULL; |
| 6646 | char *encoding; |
| 6647 | DWORD err; |
| 6648 | int ret = -1; |
| 6649 | |
| 6650 | assert(size > 0); |
| 6651 | |
| 6652 | encoding = code_page_name(code_page, &encoding_obj); |
| 6653 | if (encoding == NULL) |
| 6654 | return -1; |
| 6655 | |
| 6656 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6657 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6658 | UnicodeDecodeError. */ |
| 6659 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6660 | if (exc != NULL) { |
| 6661 | PyCodec_StrictErrors(exc); |
| 6662 | Py_CLEAR(exc); |
| 6663 | } |
| 6664 | goto error; |
| 6665 | } |
| 6666 | |
| 6667 | if (*v == NULL) { |
| 6668 | /* Create unicode object */ |
| 6669 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6670 | PyErr_NoMemory(); |
| 6671 | goto error; |
| 6672 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6673 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6674 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6675 | if (*v == NULL) |
| 6676 | goto error; |
| 6677 | startout = PyUnicode_AS_UNICODE(*v); |
| 6678 | } |
| 6679 | else { |
| 6680 | /* Extend unicode object */ |
| 6681 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6682 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6683 | PyErr_NoMemory(); |
| 6684 | goto error; |
| 6685 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6686 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6687 | goto error; |
| 6688 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6689 | } |
| 6690 | |
| 6691 | /* Decode the byte string character per character */ |
| 6692 | out = startout; |
| 6693 | while (in < endin) |
| 6694 | { |
| 6695 | /* Decode a character */ |
| 6696 | insize = 1; |
| 6697 | do |
| 6698 | { |
| 6699 | outsize = MultiByteToWideChar(code_page, flags, |
| 6700 | in, insize, |
| 6701 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6702 | if (outsize > 0) |
| 6703 | break; |
| 6704 | err = GetLastError(); |
| 6705 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6706 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6707 | { |
| 6708 | PyErr_SetFromWindowsErr(0); |
| 6709 | goto error; |
| 6710 | } |
| 6711 | insize++; |
| 6712 | } |
| 6713 | /* 4=maximum length of a UTF-8 sequence */ |
| 6714 | while (insize <= 4 && (in + insize) <= endin); |
| 6715 | |
| 6716 | if (outsize <= 0) { |
| 6717 | Py_ssize_t startinpos, endinpos, outpos; |
| 6718 | |
| 6719 | startinpos = in - startin; |
| 6720 | endinpos = startinpos + 1; |
| 6721 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6722 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6723 | errors, &errorHandler, |
| 6724 | encoding, reason, |
| 6725 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6726 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6727 | { |
| 6728 | goto error; |
| 6729 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6730 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6731 | } |
| 6732 | else { |
| 6733 | in += insize; |
| 6734 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6735 | out += outsize; |
| 6736 | } |
| 6737 | } |
| 6738 | |
| 6739 | /* write a NUL character at the end */ |
| 6740 | *out = 0; |
| 6741 | |
| 6742 | /* Extend unicode object */ |
| 6743 | outsize = out - startout; |
| 6744 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6745 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6746 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6747 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6748 | |
| 6749 | error: |
| 6750 | Py_XDECREF(encoding_obj); |
| 6751 | Py_XDECREF(errorHandler); |
| 6752 | Py_XDECREF(exc); |
| 6753 | return ret; |
| 6754 | } |
| 6755 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6756 | static PyObject * |
| 6757 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6758 | const char *s, Py_ssize_t size, |
| 6759 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6760 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6761 | PyObject *v = NULL; |
| 6762 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6763 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6764 | if (code_page < 0) { |
| 6765 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6766 | return NULL; |
| 6767 | } |
| 6768 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6769 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6770 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6771 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6772 | do |
| 6773 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6774 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6775 | if (size > INT_MAX) { |
| 6776 | chunk_size = INT_MAX; |
| 6777 | final = 0; |
| 6778 | done = 0; |
| 6779 | } |
| 6780 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6781 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6782 | { |
| 6783 | chunk_size = (int)size; |
| 6784 | final = (consumed == NULL); |
| 6785 | done = 1; |
| 6786 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6787 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6788 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6789 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6790 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6791 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6792 | if (chunk_size == 0 && done) { |
| 6793 | if (v != NULL) |
| 6794 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6795 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6796 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6797 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6798 | |
| 6799 | converted = decode_code_page_strict(code_page, &v, |
| 6800 | s, chunk_size); |
| 6801 | if (converted == -2) |
| 6802 | converted = decode_code_page_errors(code_page, &v, |
| 6803 | s, chunk_size, |
| 6804 | errors); |
| 6805 | assert(converted != 0); |
| 6806 | |
| 6807 | if (converted < 0) { |
| 6808 | Py_XDECREF(v); |
| 6809 | return NULL; |
| 6810 | } |
| 6811 | |
| 6812 | if (consumed) |
| 6813 | *consumed += converted; |
| 6814 | |
| 6815 | s += converted; |
| 6816 | size -= converted; |
| 6817 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6818 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6819 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6822 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6823 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6824 | const char *s, |
| 6825 | Py_ssize_t size, |
| 6826 | const char *errors, |
| 6827 | Py_ssize_t *consumed) |
| 6828 | { |
| 6829 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6830 | } |
| 6831 | |
| 6832 | PyObject * |
| 6833 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6834 | Py_ssize_t size, |
| 6835 | const char *errors, |
| 6836 | Py_ssize_t *consumed) |
| 6837 | { |
| 6838 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6839 | } |
| 6840 | |
| 6841 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6842 | PyUnicode_DecodeMBCS(const char *s, |
| 6843 | Py_ssize_t size, |
| 6844 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6845 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6846 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6847 | } |
| 6848 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6849 | static DWORD |
| 6850 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6851 | { |
| 6852 | if (code_page == CP_UTF8) { |
| 6853 | if (winver.dwMajorVersion >= 6) |
| 6854 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6855 | and later */ |
| 6856 | return WC_ERR_INVALID_CHARS; |
| 6857 | else |
| 6858 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 6859 | return 0; |
| 6860 | } |
| 6861 | else if (code_page == CP_UTF7) { |
| 6862 | /* CP_UTF7 only supports flags=0 */ |
| 6863 | return 0; |
| 6864 | } |
| 6865 | else { |
| 6866 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 6867 | return 0; |
| 6868 | else |
| 6869 | return WC_NO_BEST_FIT_CHARS; |
| 6870 | } |
| 6871 | } |
| 6872 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6873 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6874 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 6875 | * mode. |
| 6876 | * |
| 6877 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6878 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6879 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6880 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6881 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6882 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6883 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6884 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6885 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6886 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 6887 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6888 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6889 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6890 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6891 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 6892 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6893 | /* Create a substring so that we can get the UTF-16 representation |
| 6894 | of just the slice under consideration. */ |
| 6895 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6896 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6897 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6898 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6899 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6900 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6901 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6902 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6903 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6904 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 6905 | if (substring == NULL) |
| 6906 | return -1; |
| 6907 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 6908 | if (p == NULL) { |
| 6909 | Py_DECREF(substring); |
| 6910 | return -1; |
| 6911 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6912 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6913 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6914 | outsize = WideCharToMultiByte(code_page, flags, |
| 6915 | p, size, |
| 6916 | NULL, 0, |
| 6917 | NULL, pusedDefaultChar); |
| 6918 | if (outsize <= 0) |
| 6919 | goto error; |
| 6920 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6921 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 6922 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6923 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6924 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6925 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6926 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6927 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6928 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6929 | if (*outbytes == NULL) { |
| 6930 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6931 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6932 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6933 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6934 | } |
| 6935 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6936 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6937 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 6938 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 6939 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6940 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6941 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6942 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6943 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 6944 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6945 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6946 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6947 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6948 | } |
| 6949 | |
| 6950 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6951 | outsize = WideCharToMultiByte(code_page, flags, |
| 6952 | p, size, |
| 6953 | out, outsize, |
| 6954 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6955 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6956 | if (outsize <= 0) |
| 6957 | goto error; |
| 6958 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6959 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6960 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6961 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6962 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6963 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6964 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6965 | return -2; |
| 6966 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6967 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6968 | } |
| 6969 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6970 | /* |
| 6971 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 6972 | * error handler. |
| 6973 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6974 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6975 | * -1 on other error. |
| 6976 | */ |
| 6977 | static int |
| 6978 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 6979 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6980 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6981 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6982 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6983 | Py_ssize_t pos = unicode_offset; |
| 6984 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6985 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6986 | 2000 English version of the message. */ |
| 6987 | const char *reason = "invalid character"; |
| 6988 | /* 4=maximum length of a UTF-8 sequence */ |
| 6989 | char buffer[4]; |
| 6990 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 6991 | Py_ssize_t outsize; |
| 6992 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6993 | PyObject *errorHandler = NULL; |
| 6994 | PyObject *exc = NULL; |
| 6995 | PyObject *encoding_obj = NULL; |
| 6996 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6997 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6998 | PyObject *rep; |
| 6999 | int ret = -1; |
| 7000 | |
| 7001 | assert(insize > 0); |
| 7002 | |
| 7003 | encoding = code_page_name(code_page, &encoding_obj); |
| 7004 | if (encoding == NULL) |
| 7005 | return -1; |
| 7006 | |
| 7007 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7008 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7009 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7010 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7011 | if (exc != NULL) { |
| 7012 | PyCodec_StrictErrors(exc); |
| 7013 | Py_DECREF(exc); |
| 7014 | } |
| 7015 | Py_XDECREF(encoding_obj); |
| 7016 | return -1; |
| 7017 | } |
| 7018 | |
| 7019 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7020 | pusedDefaultChar = &usedDefaultChar; |
| 7021 | else |
| 7022 | pusedDefaultChar = NULL; |
| 7023 | |
| 7024 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7025 | PyErr_NoMemory(); |
| 7026 | goto error; |
| 7027 | } |
| 7028 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7029 | |
| 7030 | if (*outbytes == NULL) { |
| 7031 | /* Create string object */ |
| 7032 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7033 | if (*outbytes == NULL) |
| 7034 | goto error; |
| 7035 | out = PyBytes_AS_STRING(*outbytes); |
| 7036 | } |
| 7037 | else { |
| 7038 | /* Extend string object */ |
| 7039 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7040 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7041 | PyErr_NoMemory(); |
| 7042 | goto error; |
| 7043 | } |
| 7044 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7045 | goto error; |
| 7046 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7047 | } |
| 7048 | |
| 7049 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7050 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7051 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7052 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7053 | wchar_t chars[2]; |
| 7054 | int charsize; |
| 7055 | if (ch < 0x10000) { |
| 7056 | chars[0] = (wchar_t)ch; |
| 7057 | charsize = 1; |
| 7058 | } |
| 7059 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7060 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7061 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7062 | charsize = 2; |
| 7063 | } |
| 7064 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7065 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7066 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7067 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7068 | NULL, pusedDefaultChar); |
| 7069 | if (outsize > 0) { |
| 7070 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7071 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7072 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7073 | memcpy(out, buffer, outsize); |
| 7074 | out += outsize; |
| 7075 | continue; |
| 7076 | } |
| 7077 | } |
| 7078 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7079 | PyErr_SetFromWindowsErr(0); |
| 7080 | goto error; |
| 7081 | } |
| 7082 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7083 | rep = unicode_encode_call_errorhandler( |
| 7084 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7085 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7086 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7087 | if (rep == NULL) |
| 7088 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7089 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7090 | |
| 7091 | if (PyBytes_Check(rep)) { |
| 7092 | outsize = PyBytes_GET_SIZE(rep); |
| 7093 | if (outsize != 1) { |
| 7094 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7095 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7096 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7097 | Py_DECREF(rep); |
| 7098 | goto error; |
| 7099 | } |
| 7100 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7101 | } |
| 7102 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7103 | out += outsize; |
| 7104 | } |
| 7105 | else { |
| 7106 | Py_ssize_t i; |
| 7107 | enum PyUnicode_Kind kind; |
| 7108 | void *data; |
| 7109 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7110 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7111 | Py_DECREF(rep); |
| 7112 | goto error; |
| 7113 | } |
| 7114 | |
| 7115 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7116 | if (outsize != 1) { |
| 7117 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7118 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7119 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7120 | Py_DECREF(rep); |
| 7121 | goto error; |
| 7122 | } |
| 7123 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7124 | } |
| 7125 | kind = PyUnicode_KIND(rep); |
| 7126 | data = PyUnicode_DATA(rep); |
| 7127 | for (i=0; i < outsize; i++) { |
| 7128 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7129 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7130 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7131 | encoding, unicode, |
| 7132 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7133 | "unable to encode error handler result to ASCII"); |
| 7134 | Py_DECREF(rep); |
| 7135 | goto error; |
| 7136 | } |
| 7137 | *out = (unsigned char)ch; |
| 7138 | out++; |
| 7139 | } |
| 7140 | } |
| 7141 | Py_DECREF(rep); |
| 7142 | } |
| 7143 | /* write a NUL byte */ |
| 7144 | *out = 0; |
| 7145 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7146 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7147 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7148 | goto error; |
| 7149 | ret = 0; |
| 7150 | |
| 7151 | error: |
| 7152 | Py_XDECREF(encoding_obj); |
| 7153 | Py_XDECREF(errorHandler); |
| 7154 | Py_XDECREF(exc); |
| 7155 | return ret; |
| 7156 | } |
| 7157 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7158 | static PyObject * |
| 7159 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7160 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7161 | const char *errors) |
| 7162 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7163 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7164 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7165 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7166 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7167 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7168 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7169 | return NULL; |
| 7170 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7171 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | if (code_page < 0) { |
| 7173 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7174 | return NULL; |
| 7175 | } |
| 7176 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7177 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7178 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7179 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7180 | offset = 0; |
| 7181 | do |
| 7182 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7183 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7184 | /* 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] | 7185 | chunks. */ |
| 7186 | if (len > INT_MAX/2) { |
| 7187 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7188 | done = 0; |
| 7189 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7190 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7191 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7192 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7193 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7194 | done = 1; |
| 7195 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7196 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7197 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7198 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7199 | errors); |
| 7200 | if (ret == -2) |
| 7201 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7202 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7203 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7204 | if (ret < 0) { |
| 7205 | Py_XDECREF(outbytes); |
| 7206 | return NULL; |
| 7207 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7208 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7209 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7210 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7211 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7212 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7213 | return outbytes; |
| 7214 | } |
| 7215 | |
| 7216 | PyObject * |
| 7217 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7218 | Py_ssize_t size, |
| 7219 | const char *errors) |
| 7220 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7221 | PyObject *unicode, *res; |
| 7222 | unicode = PyUnicode_FromUnicode(p, size); |
| 7223 | if (unicode == NULL) |
| 7224 | return NULL; |
| 7225 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7226 | Py_DECREF(unicode); |
| 7227 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7228 | } |
| 7229 | |
| 7230 | PyObject * |
| 7231 | PyUnicode_EncodeCodePage(int code_page, |
| 7232 | PyObject *unicode, |
| 7233 | const char *errors) |
| 7234 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7235 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7236 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7237 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7238 | PyObject * |
| 7239 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7240 | { |
| 7241 | if (!PyUnicode_Check(unicode)) { |
| 7242 | PyErr_BadArgument(); |
| 7243 | return NULL; |
| 7244 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7245 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7246 | } |
| 7247 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7248 | #undef NEED_RETRY |
| 7249 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7250 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7252 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7253 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7254 | PyObject * |
| 7255 | PyUnicode_DecodeCharmap(const char *s, |
| 7256 | Py_ssize_t size, |
| 7257 | PyObject *mapping, |
| 7258 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7259 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7260 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7261 | Py_ssize_t startinpos; |
| 7262 | Py_ssize_t endinpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7263 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7264 | _PyUnicodeWriter writer; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7265 | PyObject *errorHandler = NULL; |
| 7266 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7267 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7268 | /* Default to Latin-1 */ |
| 7269 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7270 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7272 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7273 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7274 | _PyUnicodeWriter_Init(&writer, 0); |
| 7275 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7276 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7277 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7278 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7279 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7280 | Py_ssize_t maplen; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7281 | enum PyUnicode_Kind mapkind; |
| 7282 | void *mapdata; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7283 | Py_UCS4 x; |
| 7284 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7285 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7286 | return NULL; |
| 7287 | |
| 7288 | maplen = PyUnicode_GET_LENGTH(mapping); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7289 | mapdata = PyUnicode_DATA(mapping); |
| 7290 | mapkind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7291 | while (s < e) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7292 | unsigned char ch; |
| 7293 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7294 | enum PyUnicode_Kind outkind = writer.kind; |
| 7295 | void *outdata = writer.data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7296 | if (outkind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7297 | Py_UCS4 maxchar = writer.maxchar; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7298 | while (s < e) { |
| 7299 | unsigned char ch = *s; |
| 7300 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7301 | if (x > maxchar) |
| 7302 | goto Error; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7303 | PyUnicode_WRITE(PyUnicode_1BYTE_KIND, outdata, writer.pos, x); |
| 7304 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7305 | ++s; |
| 7306 | } |
| 7307 | break; |
| 7308 | } |
| 7309 | else if (outkind == PyUnicode_2BYTE_KIND) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7310 | while (s < e) { |
| 7311 | unsigned char ch = *s; |
| 7312 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7313 | if (x == 0xFFFE) |
| 7314 | goto Error; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7315 | PyUnicode_WRITE(PyUnicode_2BYTE_KIND, outdata, writer.pos, x); |
| 7316 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7317 | ++s; |
| 7318 | } |
| 7319 | break; |
| 7320 | } |
| 7321 | } |
| 7322 | ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7323 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7324 | if (ch < maplen) |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7325 | x = PyUnicode_READ(mapkind, mapdata, ch); |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7326 | else |
| 7327 | x = 0xfffe; /* invalid value */ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7328 | Error: |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7329 | if (x == 0xfffe) |
| 7330 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7331 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7332 | startinpos = s-starts; |
| 7333 | endinpos = startinpos+1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7334 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7335 | errors, &errorHandler, |
| 7336 | "charmap", "character maps to <undefined>", |
| 7337 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7338 | &writer)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7339 | goto onError; |
| 7340 | } |
| 7341 | continue; |
| 7342 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7343 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7344 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7345 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7346 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 7347 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7348 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7349 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7350 | } |
| 7351 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7352 | while (s < e) { |
| 7353 | unsigned char ch = *s; |
| 7354 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7355 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7356 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7357 | w = PyLong_FromLong((long)ch); |
| 7358 | if (w == NULL) |
| 7359 | goto onError; |
| 7360 | x = PyObject_GetItem(mapping, w); |
| 7361 | Py_DECREF(w); |
| 7362 | if (x == NULL) { |
| 7363 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7364 | /* No mapping found means: mapping is undefined. */ |
| 7365 | PyErr_Clear(); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7366 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7367 | } else |
| 7368 | goto onError; |
| 7369 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7370 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7371 | /* Apply mapping */ |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7372 | if (x == Py_None) |
| 7373 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7374 | if (PyLong_Check(x)) { |
| 7375 | long value = PyLong_AS_LONG(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7376 | if (value == 0xFFFE) |
| 7377 | goto Undefined; |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 7378 | if (value < 0 || value > MAX_UNICODE) { |
| 7379 | PyErr_Format(PyExc_TypeError, |
| 7380 | "character mapping must be in range(0x%lx)", |
| 7381 | (unsigned long)MAX_UNICODE + 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7382 | Py_DECREF(x); |
| 7383 | goto onError; |
| 7384 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7385 | |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7386 | if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7387 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7388 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7389 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7390 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value); |
| 7391 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7392 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7393 | else if (PyUnicode_Check(x)) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7394 | if (PyUnicode_READY(x) == -1) { |
| 7395 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7396 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7397 | } |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7398 | if (PyUnicode_GET_LENGTH(x) == 1) { |
Serhiy Storchaka | 45d16d9 | 2013-01-15 15:01:20 +0200 | [diff] [blame] | 7399 | Py_UCS4 value = PyUnicode_READ_CHAR(x, 0); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7400 | if (value == 0xFFFE) |
| 7401 | goto Undefined; |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7402 | if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) { |
| 7403 | Py_DECREF(x); |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7404 | goto onError; |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7405 | } |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7406 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value); |
| 7407 | writer.pos++; |
| 7408 | } |
| 7409 | else { |
| 7410 | writer.overallocate = 1; |
Serhiy Storchaka | 2aee6a6 | 2013-01-29 12:16:57 +0200 | [diff] [blame] | 7411 | if (_PyUnicodeWriter_WriteStr(&writer, x) == -1) { |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7412 | Py_DECREF(x); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7413 | goto onError; |
Serhiy Storchaka | afb1cb5 | 2013-01-29 12:13:22 +0200 | [diff] [blame] | 7414 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7415 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7416 | } |
| 7417 | else { |
| 7418 | /* wrong return value */ |
| 7419 | PyErr_SetString(PyExc_TypeError, |
| 7420 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7421 | Py_DECREF(x); |
| 7422 | goto onError; |
| 7423 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7424 | Py_DECREF(x); |
| 7425 | ++s; |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7426 | continue; |
| 7427 | Undefined: |
| 7428 | /* undefined mapping */ |
| 7429 | Py_XDECREF(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7430 | startinpos = s-starts; |
| 7431 | endinpos = startinpos+1; |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7432 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7433 | errors, &errorHandler, |
| 7434 | "charmap", "character maps to <undefined>", |
| 7435 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame] | 7436 | &writer)) { |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7437 | goto onError; |
| 7438 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7439 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7441 | Py_XDECREF(errorHandler); |
| 7442 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7443 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7444 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7445 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7446 | Py_XDECREF(errorHandler); |
| 7447 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7448 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7449 | return NULL; |
| 7450 | } |
| 7451 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7452 | /* Charmap encoding: the lookup table */ |
| 7453 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7454 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7455 | PyObject_HEAD |
| 7456 | unsigned char level1[32]; |
| 7457 | int count2, count3; |
| 7458 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7459 | }; |
| 7460 | |
| 7461 | static PyObject* |
| 7462 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7463 | { |
| 7464 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7465 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7466 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7467 | } |
| 7468 | |
| 7469 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7470 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7471 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7472 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7473 | }; |
| 7474 | |
| 7475 | static void |
| 7476 | encoding_map_dealloc(PyObject* o) |
| 7477 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7478 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7479 | } |
| 7480 | |
| 7481 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7482 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7483 | "EncodingMap", /*tp_name*/ |
| 7484 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7485 | 0, /*tp_itemsize*/ |
| 7486 | /* methods */ |
| 7487 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7488 | 0, /*tp_print*/ |
| 7489 | 0, /*tp_getattr*/ |
| 7490 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7491 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7492 | 0, /*tp_repr*/ |
| 7493 | 0, /*tp_as_number*/ |
| 7494 | 0, /*tp_as_sequence*/ |
| 7495 | 0, /*tp_as_mapping*/ |
| 7496 | 0, /*tp_hash*/ |
| 7497 | 0, /*tp_call*/ |
| 7498 | 0, /*tp_str*/ |
| 7499 | 0, /*tp_getattro*/ |
| 7500 | 0, /*tp_setattro*/ |
| 7501 | 0, /*tp_as_buffer*/ |
| 7502 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7503 | 0, /*tp_doc*/ |
| 7504 | 0, /*tp_traverse*/ |
| 7505 | 0, /*tp_clear*/ |
| 7506 | 0, /*tp_richcompare*/ |
| 7507 | 0, /*tp_weaklistoffset*/ |
| 7508 | 0, /*tp_iter*/ |
| 7509 | 0, /*tp_iternext*/ |
| 7510 | encoding_map_methods, /*tp_methods*/ |
| 7511 | 0, /*tp_members*/ |
| 7512 | 0, /*tp_getset*/ |
| 7513 | 0, /*tp_base*/ |
| 7514 | 0, /*tp_dict*/ |
| 7515 | 0, /*tp_descr_get*/ |
| 7516 | 0, /*tp_descr_set*/ |
| 7517 | 0, /*tp_dictoffset*/ |
| 7518 | 0, /*tp_init*/ |
| 7519 | 0, /*tp_alloc*/ |
| 7520 | 0, /*tp_new*/ |
| 7521 | 0, /*tp_free*/ |
| 7522 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7523 | }; |
| 7524 | |
| 7525 | PyObject* |
| 7526 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7527 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7528 | PyObject *result; |
| 7529 | struct encoding_map *mresult; |
| 7530 | int i; |
| 7531 | int need_dict = 0; |
| 7532 | unsigned char level1[32]; |
| 7533 | unsigned char level2[512]; |
| 7534 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7535 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7536 | int kind; |
| 7537 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7538 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7539 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7540 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7541 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7542 | PyErr_BadArgument(); |
| 7543 | return NULL; |
| 7544 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7545 | kind = PyUnicode_KIND(string); |
| 7546 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7547 | length = PyUnicode_GET_LENGTH(string); |
| 7548 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7549 | memset(level1, 0xFF, sizeof level1); |
| 7550 | memset(level2, 0xFF, sizeof level2); |
| 7551 | |
| 7552 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7553 | or if there are non-BMP characters, we need to use |
| 7554 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7555 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7556 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7557 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7558 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7559 | ch = PyUnicode_READ(kind, data, i); |
| 7560 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7561 | need_dict = 1; |
| 7562 | break; |
| 7563 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7564 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7565 | /* unmapped character */ |
| 7566 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7567 | l1 = ch >> 11; |
| 7568 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7569 | if (level1[l1] == 0xFF) |
| 7570 | level1[l1] = count2++; |
| 7571 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7572 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7573 | } |
| 7574 | |
| 7575 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7576 | need_dict = 1; |
| 7577 | |
| 7578 | if (need_dict) { |
| 7579 | PyObject *result = PyDict_New(); |
| 7580 | PyObject *key, *value; |
| 7581 | if (!result) |
| 7582 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7583 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7584 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7585 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7586 | if (!key || !value) |
| 7587 | goto failed1; |
| 7588 | if (PyDict_SetItem(result, key, value) == -1) |
| 7589 | goto failed1; |
| 7590 | Py_DECREF(key); |
| 7591 | Py_DECREF(value); |
| 7592 | } |
| 7593 | return result; |
| 7594 | failed1: |
| 7595 | Py_XDECREF(key); |
| 7596 | Py_XDECREF(value); |
| 7597 | Py_DECREF(result); |
| 7598 | return NULL; |
| 7599 | } |
| 7600 | |
| 7601 | /* Create a three-level trie */ |
| 7602 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7603 | 16*count2 + 128*count3 - 1); |
| 7604 | if (!result) |
| 7605 | return PyErr_NoMemory(); |
| 7606 | PyObject_Init(result, &EncodingMapType); |
| 7607 | mresult = (struct encoding_map*)result; |
| 7608 | mresult->count2 = count2; |
| 7609 | mresult->count3 = count3; |
| 7610 | mlevel1 = mresult->level1; |
| 7611 | mlevel2 = mresult->level23; |
| 7612 | mlevel3 = mresult->level23 + 16*count2; |
| 7613 | memcpy(mlevel1, level1, 32); |
| 7614 | memset(mlevel2, 0xFF, 16*count2); |
| 7615 | memset(mlevel3, 0, 128*count3); |
| 7616 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7617 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7618 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7619 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7620 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7621 | /* unmapped character */ |
| 7622 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7623 | o1 = ch>>11; |
| 7624 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7625 | i2 = 16*mlevel1[o1] + o2; |
| 7626 | if (mlevel2[i2] == 0xFF) |
| 7627 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7628 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7629 | i3 = 128*mlevel2[i2] + o3; |
| 7630 | mlevel3[i3] = i; |
| 7631 | } |
| 7632 | return result; |
| 7633 | } |
| 7634 | |
| 7635 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7636 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7637 | { |
| 7638 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7639 | int l1 = c>>11; |
| 7640 | int l2 = (c>>7) & 0xF; |
| 7641 | int l3 = c & 0x7F; |
| 7642 | int i; |
| 7643 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7644 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7645 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7646 | if (c == 0) |
| 7647 | return 0; |
| 7648 | /* level 1*/ |
| 7649 | i = map->level1[l1]; |
| 7650 | if (i == 0xFF) { |
| 7651 | return -1; |
| 7652 | } |
| 7653 | /* level 2*/ |
| 7654 | i = map->level23[16*i+l2]; |
| 7655 | if (i == 0xFF) { |
| 7656 | return -1; |
| 7657 | } |
| 7658 | /* level 3 */ |
| 7659 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7660 | if (i == 0) { |
| 7661 | return -1; |
| 7662 | } |
| 7663 | return i; |
| 7664 | } |
| 7665 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7666 | /* Lookup the character ch in the mapping. If the character |
| 7667 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7668 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7669 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7670 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7671 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7672 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7673 | PyObject *x; |
| 7674 | |
| 7675 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7676 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7677 | x = PyObject_GetItem(mapping, w); |
| 7678 | Py_DECREF(w); |
| 7679 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7680 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7681 | /* No mapping found means: mapping is undefined. */ |
| 7682 | PyErr_Clear(); |
| 7683 | x = Py_None; |
| 7684 | Py_INCREF(x); |
| 7685 | return x; |
| 7686 | } else |
| 7687 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7688 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7689 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7690 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7691 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7692 | long value = PyLong_AS_LONG(x); |
| 7693 | if (value < 0 || value > 255) { |
| 7694 | PyErr_SetString(PyExc_TypeError, |
| 7695 | "character mapping must be in range(256)"); |
| 7696 | Py_DECREF(x); |
| 7697 | return NULL; |
| 7698 | } |
| 7699 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7700 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7701 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7702 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7703 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7704 | /* wrong return value */ |
| 7705 | PyErr_Format(PyExc_TypeError, |
| 7706 | "character mapping must return integer, bytes or None, not %.400s", |
| 7707 | x->ob_type->tp_name); |
| 7708 | Py_DECREF(x); |
| 7709 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7710 | } |
| 7711 | } |
| 7712 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7713 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7714 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7715 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7716 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7717 | /* exponentially overallocate to minimize reallocations */ |
| 7718 | if (requiredsize < 2*outsize) |
| 7719 | requiredsize = 2*outsize; |
| 7720 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7721 | return -1; |
| 7722 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7723 | } |
| 7724 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7725 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7726 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7727 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7728 | /* 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] | 7729 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7730 | space is available. Return a new reference to the object that |
| 7731 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7732 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7733 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7734 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7735 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7736 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7737 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7738 | PyObject *rep; |
| 7739 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7740 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7741 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7742 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7743 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7744 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7745 | if (res == -1) |
| 7746 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7747 | if (outsize<requiredsize) |
| 7748 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7749 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7750 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7751 | outstart[(*outpos)++] = (char)res; |
| 7752 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7753 | } |
| 7754 | |
| 7755 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7756 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7757 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7758 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7759 | Py_DECREF(rep); |
| 7760 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7761 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7762 | if (PyLong_Check(rep)) { |
| 7763 | Py_ssize_t requiredsize = *outpos+1; |
| 7764 | if (outsize<requiredsize) |
| 7765 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7766 | Py_DECREF(rep); |
| 7767 | return enc_EXCEPTION; |
| 7768 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7769 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7770 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7771 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7772 | else { |
| 7773 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7774 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7775 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7776 | if (outsize<requiredsize) |
| 7777 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7778 | Py_DECREF(rep); |
| 7779 | return enc_EXCEPTION; |
| 7780 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7781 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7782 | memcpy(outstart + *outpos, repchars, repsize); |
| 7783 | *outpos += repsize; |
| 7784 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7785 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7786 | Py_DECREF(rep); |
| 7787 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7788 | } |
| 7789 | |
| 7790 | /* handle an error in PyUnicode_EncodeCharmap |
| 7791 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7792 | static int |
| 7793 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7794 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7795 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7796 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7797 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7798 | { |
| 7799 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7800 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7801 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7802 | enum PyUnicode_Kind kind; |
| 7803 | void *data; |
| 7804 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7805 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7806 | Py_ssize_t collstartpos = *inpos; |
| 7807 | Py_ssize_t collendpos = *inpos+1; |
| 7808 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7809 | char *encoding = "charmap"; |
| 7810 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7811 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7812 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7813 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7814 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7815 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7816 | return -1; |
| 7817 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7818 | /* find all unencodable characters */ |
| 7819 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7820 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7821 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7822 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7823 | val = encoding_map_lookup(ch, mapping); |
| 7824 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7825 | break; |
| 7826 | ++collendpos; |
| 7827 | continue; |
| 7828 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7829 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7830 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7831 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7832 | if (rep==NULL) |
| 7833 | return -1; |
| 7834 | else if (rep!=Py_None) { |
| 7835 | Py_DECREF(rep); |
| 7836 | break; |
| 7837 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7838 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7839 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7840 | } |
| 7841 | /* cache callback name lookup |
| 7842 | * (if not done yet, i.e. it's the first error) */ |
| 7843 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7844 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7845 | *known_errorHandler = 1; |
| 7846 | else if (!strcmp(errors, "replace")) |
| 7847 | *known_errorHandler = 2; |
| 7848 | else if (!strcmp(errors, "ignore")) |
| 7849 | *known_errorHandler = 3; |
| 7850 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7851 | *known_errorHandler = 4; |
| 7852 | else |
| 7853 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7854 | } |
| 7855 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7856 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7857 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7858 | return -1; |
| 7859 | case 2: /* replace */ |
| 7860 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7861 | x = charmapencode_output('?', mapping, res, respos); |
| 7862 | if (x==enc_EXCEPTION) { |
| 7863 | return -1; |
| 7864 | } |
| 7865 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7866 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7867 | return -1; |
| 7868 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7869 | } |
| 7870 | /* fall through */ |
| 7871 | case 3: /* ignore */ |
| 7872 | *inpos = collendpos; |
| 7873 | break; |
| 7874 | case 4: /* xmlcharrefreplace */ |
| 7875 | /* generate replacement (temporarily (mis)uses p) */ |
| 7876 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7877 | char buffer[2+29+1+1]; |
| 7878 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7879 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7880 | for (cp = buffer; *cp; ++cp) { |
| 7881 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7882 | if (x==enc_EXCEPTION) |
| 7883 | return -1; |
| 7884 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7885 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | return -1; |
| 7887 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7888 | } |
| 7889 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7890 | *inpos = collendpos; |
| 7891 | break; |
| 7892 | default: |
| 7893 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7894 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7895 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7896 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7897 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7898 | if (PyBytes_Check(repunicode)) { |
| 7899 | /* Directly copy bytes result to output. */ |
| 7900 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7901 | Py_ssize_t requiredsize; |
| 7902 | repsize = PyBytes_Size(repunicode); |
| 7903 | requiredsize = *respos + repsize; |
| 7904 | if (requiredsize > outsize) |
| 7905 | /* Make room for all additional bytes. */ |
| 7906 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7907 | Py_DECREF(repunicode); |
| 7908 | return -1; |
| 7909 | } |
| 7910 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7911 | PyBytes_AsString(repunicode), repsize); |
| 7912 | *respos += repsize; |
| 7913 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7914 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7915 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7916 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7917 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7918 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7919 | Py_DECREF(repunicode); |
| 7920 | return -1; |
| 7921 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 7922 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7923 | data = PyUnicode_DATA(repunicode); |
| 7924 | kind = PyUnicode_KIND(repunicode); |
| 7925 | for (index = 0; index < repsize; index++) { |
| 7926 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 7927 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7928 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7929 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7930 | return -1; |
| 7931 | } |
| 7932 | else if (x==enc_FAILED) { |
| 7933 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7934 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7935 | return -1; |
| 7936 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7937 | } |
| 7938 | *inpos = newpos; |
| 7939 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7940 | } |
| 7941 | return 0; |
| 7942 | } |
| 7943 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7944 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7945 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 7946 | PyObject *mapping, |
| 7947 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7948 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7949 | /* output object */ |
| 7950 | PyObject *res = NULL; |
| 7951 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7952 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7953 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7954 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7955 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7956 | PyObject *errorHandler = NULL; |
| 7957 | PyObject *exc = NULL; |
| 7958 | /* the following variable is used for caching string comparisons |
| 7959 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7960 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7961 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7962 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7963 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7964 | return NULL; |
| 7965 | size = PyUnicode_GET_LENGTH(unicode); |
| 7966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7967 | /* Default to Latin-1 */ |
| 7968 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7969 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7970 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7971 | /* allocate enough for a simple encoding without |
| 7972 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7973 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7974 | if (res == NULL) |
| 7975 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7976 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7977 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7978 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7979 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7980 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7981 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7982 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | if (x==enc_EXCEPTION) /* error */ |
| 7984 | goto onError; |
| 7985 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7986 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7987 | &exc, |
| 7988 | &known_errorHandler, &errorHandler, errors, |
| 7989 | &res, &respos)) { |
| 7990 | goto onError; |
| 7991 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7992 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7993 | else |
| 7994 | /* done with this character => adjust input position */ |
| 7995 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7996 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7997 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7998 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7999 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8000 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8001 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8002 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8003 | Py_XDECREF(exc); |
| 8004 | Py_XDECREF(errorHandler); |
| 8005 | return res; |
| 8006 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8007 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8008 | Py_XDECREF(res); |
| 8009 | Py_XDECREF(exc); |
| 8010 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8011 | return NULL; |
| 8012 | } |
| 8013 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8014 | /* Deprecated */ |
| 8015 | PyObject * |
| 8016 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8017 | Py_ssize_t size, |
| 8018 | PyObject *mapping, |
| 8019 | const char *errors) |
| 8020 | { |
| 8021 | PyObject *result; |
| 8022 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8023 | if (unicode == NULL) |
| 8024 | return NULL; |
| 8025 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8026 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8027 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8028 | } |
| 8029 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8030 | PyObject * |
| 8031 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8032 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | { |
| 8034 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8035 | PyErr_BadArgument(); |
| 8036 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8037 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8038 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8039 | } |
| 8040 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8041 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8042 | static void |
| 8043 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8044 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8045 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8046 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8048 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8049 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8050 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8051 | } |
| 8052 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8053 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8054 | goto onError; |
| 8055 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8056 | goto onError; |
| 8057 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8058 | goto onError; |
| 8059 | return; |
| 8060 | onError: |
| 8061 | Py_DECREF(*exceptionObject); |
| 8062 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8063 | } |
| 8064 | } |
| 8065 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8066 | /* error handling callback helper: |
| 8067 | build arguments, call the callback and check the arguments, |
| 8068 | put the result into newpos and return the replacement string, which |
| 8069 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8070 | static PyObject * |
| 8071 | unicode_translate_call_errorhandler(const char *errors, |
| 8072 | PyObject **errorHandler, |
| 8073 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8074 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8075 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8076 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8077 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8078 | 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] | 8079 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8080 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8081 | PyObject *restuple; |
| 8082 | PyObject *resunicode; |
| 8083 | |
| 8084 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8085 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8086 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8091 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8092 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8093 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8094 | |
| 8095 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8096 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8097 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8098 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8099 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8100 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8101 | Py_DECREF(restuple); |
| 8102 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8103 | } |
| 8104 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8105 | &resunicode, &i_newpos)) { |
| 8106 | Py_DECREF(restuple); |
| 8107 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8108 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8109 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8110 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8111 | else |
| 8112 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8113 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8114 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8115 | Py_DECREF(restuple); |
| 8116 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8117 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8118 | Py_INCREF(resunicode); |
| 8119 | Py_DECREF(restuple); |
| 8120 | return resunicode; |
| 8121 | } |
| 8122 | |
| 8123 | /* Lookup the character ch in the mapping and put the result in result, |
| 8124 | which must be decrefed by the caller. |
| 8125 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8126 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8127 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8128 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8129 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8130 | PyObject *x; |
| 8131 | |
| 8132 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8133 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8134 | x = PyObject_GetItem(mapping, w); |
| 8135 | Py_DECREF(w); |
| 8136 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8137 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8138 | /* No mapping found means: use 1:1 mapping. */ |
| 8139 | PyErr_Clear(); |
| 8140 | *result = NULL; |
| 8141 | return 0; |
| 8142 | } else |
| 8143 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8144 | } |
| 8145 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8146 | *result = x; |
| 8147 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8148 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8149 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8150 | long value = PyLong_AS_LONG(x); |
| 8151 | long max = PyUnicode_GetMax(); |
| 8152 | if (value < 0 || value > max) { |
| 8153 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8154 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8155 | Py_DECREF(x); |
| 8156 | return -1; |
| 8157 | } |
| 8158 | *result = x; |
| 8159 | return 0; |
| 8160 | } |
| 8161 | else if (PyUnicode_Check(x)) { |
| 8162 | *result = x; |
| 8163 | return 0; |
| 8164 | } |
| 8165 | else { |
| 8166 | /* wrong return value */ |
| 8167 | PyErr_SetString(PyExc_TypeError, |
| 8168 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8169 | Py_DECREF(x); |
| 8170 | return -1; |
| 8171 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8172 | } |
| 8173 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | if not reallocate and adjust various state variables. |
| 8175 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8176 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8177 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8178 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8179 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8180 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8181 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8182 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8183 | /* exponentially overallocate to minimize reallocations */ |
| 8184 | if (requiredsize < 2 * oldsize) |
| 8185 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8186 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8187 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8188 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8189 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8190 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8191 | } |
| 8192 | return 0; |
| 8193 | } |
| 8194 | /* lookup the character, put the result in the output string and adjust |
| 8195 | various state variables. Return a new reference to the object that |
| 8196 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8197 | undefined (in which case no character was written). |
| 8198 | The called must decref result. |
| 8199 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8200 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8201 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8202 | PyObject *mapping, Py_UCS4 **output, |
| 8203 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8204 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8205 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8206 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8207 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8208 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8209 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8210 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8211 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8212 | } |
| 8213 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8214 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8215 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8216 | /* 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] | 8217 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8218 | } |
| 8219 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8220 | Py_ssize_t repsize; |
| 8221 | if (PyUnicode_READY(*res) == -1) |
| 8222 | return -1; |
| 8223 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8224 | if (repsize==1) { |
| 8225 | /* 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] | 8226 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8227 | } |
| 8228 | else if (repsize!=0) { |
| 8229 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8230 | Py_ssize_t requiredsize = *opos + |
| 8231 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8232 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8233 | Py_ssize_t i; |
| 8234 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8235 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8236 | for(i = 0; i < repsize; i++) |
| 8237 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8238 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8239 | } |
| 8240 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8241 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8242 | return 0; |
| 8243 | } |
| 8244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8245 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8246 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8247 | PyObject *mapping, |
| 8248 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8249 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8250 | /* input object */ |
| 8251 | char *idata; |
| 8252 | Py_ssize_t size, i; |
| 8253 | int kind; |
| 8254 | /* output buffer */ |
| 8255 | Py_UCS4 *output = NULL; |
| 8256 | Py_ssize_t osize; |
| 8257 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8258 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8259 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8260 | char *reason = "character maps to <undefined>"; |
| 8261 | PyObject *errorHandler = NULL; |
| 8262 | PyObject *exc = NULL; |
| 8263 | /* the following variable is used for caching string comparisons |
| 8264 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8265 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8266 | int known_errorHandler = -1; |
| 8267 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8268 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8269 | PyErr_BadArgument(); |
| 8270 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8271 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8272 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8273 | if (PyUnicode_READY(input) == -1) |
| 8274 | return NULL; |
| 8275 | idata = (char*)PyUnicode_DATA(input); |
| 8276 | kind = PyUnicode_KIND(input); |
| 8277 | size = PyUnicode_GET_LENGTH(input); |
| 8278 | i = 0; |
| 8279 | |
| 8280 | if (size == 0) { |
| 8281 | Py_INCREF(input); |
| 8282 | return input; |
| 8283 | } |
| 8284 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8285 | /* allocate enough for a simple 1:1 translation without |
| 8286 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8287 | osize = size; |
| 8288 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8289 | opos = 0; |
| 8290 | if (output == NULL) { |
| 8291 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8292 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8293 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8294 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8295 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8296 | /* try to encode it */ |
| 8297 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8298 | if (charmaptranslate_output(input, i, mapping, |
| 8299 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8300 | Py_XDECREF(x); |
| 8301 | goto onError; |
| 8302 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8303 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8305 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8306 | else { /* untranslatable character */ |
| 8307 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8308 | Py_ssize_t repsize; |
| 8309 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8310 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8311 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8312 | Py_ssize_t collstart = i; |
| 8313 | Py_ssize_t collend = i+1; |
| 8314 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8315 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8316 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8317 | while (collend < size) { |
| 8318 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8319 | goto onError; |
| 8320 | Py_XDECREF(x); |
| 8321 | if (x!=Py_None) |
| 8322 | break; |
| 8323 | ++collend; |
| 8324 | } |
| 8325 | /* cache callback name lookup |
| 8326 | * (if not done yet, i.e. it's the first error) */ |
| 8327 | if (known_errorHandler==-1) { |
| 8328 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8329 | known_errorHandler = 1; |
| 8330 | else if (!strcmp(errors, "replace")) |
| 8331 | known_errorHandler = 2; |
| 8332 | else if (!strcmp(errors, "ignore")) |
| 8333 | known_errorHandler = 3; |
| 8334 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8335 | known_errorHandler = 4; |
| 8336 | else |
| 8337 | known_errorHandler = 0; |
| 8338 | } |
| 8339 | switch (known_errorHandler) { |
| 8340 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8341 | make_translate_exception(&exc, |
| 8342 | input, collstart, collend, reason); |
| 8343 | if (exc != NULL) |
| 8344 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8345 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8346 | case 2: /* replace */ |
| 8347 | /* 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] | 8348 | for (coll = collstart; coll<collend; coll++) |
| 8349 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8350 | /* fall through */ |
| 8351 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8352 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8353 | break; |
| 8354 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8355 | /* generate replacement (temporarily (mis)uses i) */ |
| 8356 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8357 | char buffer[2+29+1+1]; |
| 8358 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8359 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8360 | if (charmaptranslate_makespace(&output, &osize, |
| 8361 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8362 | goto onError; |
| 8363 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8364 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8365 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8366 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8367 | break; |
| 8368 | default: |
| 8369 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8370 | reason, input, &exc, |
| 8371 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8372 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8373 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8374 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8375 | Py_DECREF(repunicode); |
| 8376 | goto onError; |
| 8377 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8378 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8379 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8380 | if (charmaptranslate_makespace(&output, &osize, |
| 8381 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8382 | Py_DECREF(repunicode); |
| 8383 | goto onError; |
| 8384 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8385 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8386 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8387 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8389 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8390 | } |
| 8391 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8392 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8393 | if (!res) |
| 8394 | goto onError; |
| 8395 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8396 | Py_XDECREF(exc); |
| 8397 | Py_XDECREF(errorHandler); |
| 8398 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8399 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8400 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8401 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8402 | Py_XDECREF(exc); |
| 8403 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8404 | return NULL; |
| 8405 | } |
| 8406 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8407 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8408 | PyObject * |
| 8409 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8410 | Py_ssize_t size, |
| 8411 | PyObject *mapping, |
| 8412 | const char *errors) |
| 8413 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8414 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8415 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8416 | if (!unicode) |
| 8417 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8418 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8419 | Py_DECREF(unicode); |
| 8420 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8421 | } |
| 8422 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8423 | PyObject * |
| 8424 | PyUnicode_Translate(PyObject *str, |
| 8425 | PyObject *mapping, |
| 8426 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8427 | { |
| 8428 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | str = PyUnicode_FromObject(str); |
| 8431 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8432 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8433 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | Py_DECREF(str); |
| 8435 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8436 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8437 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8438 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8439 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8440 | { |
| 8441 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8442 | called as a callback from fixup() which does it already. */ |
| 8443 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8444 | const int kind = PyUnicode_KIND(self); |
| 8445 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8446 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8447 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8448 | Py_ssize_t i; |
| 8449 | |
| 8450 | for (i = 0; i < len; ++i) { |
| 8451 | ch = PyUnicode_READ(kind, data, i); |
| 8452 | fixed = 0; |
| 8453 | if (ch > 127) { |
| 8454 | if (Py_UNICODE_ISSPACE(ch)) |
| 8455 | fixed = ' '; |
| 8456 | else { |
| 8457 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8458 | if (decimal >= 0) |
| 8459 | fixed = '0' + decimal; |
| 8460 | } |
| 8461 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8462 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8463 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8464 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8465 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8466 | else |
| 8467 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8468 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8469 | } |
| 8470 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8471 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8472 | } |
| 8473 | |
| 8474 | PyObject * |
| 8475 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8476 | { |
| 8477 | if (!PyUnicode_Check(unicode)) { |
| 8478 | PyErr_BadInternalCall(); |
| 8479 | return NULL; |
| 8480 | } |
| 8481 | if (PyUnicode_READY(unicode) == -1) |
| 8482 | return NULL; |
| 8483 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8484 | /* If the string is already ASCII, just return the same string */ |
| 8485 | Py_INCREF(unicode); |
| 8486 | return unicode; |
| 8487 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8488 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8489 | } |
| 8490 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8491 | PyObject * |
| 8492 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8493 | Py_ssize_t length) |
| 8494 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8495 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8496 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8497 | Py_UCS4 maxchar; |
| 8498 | enum PyUnicode_Kind kind; |
| 8499 | void *data; |
| 8500 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8501 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8502 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8503 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8504 | if (ch > 127) { |
| 8505 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8506 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8507 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8508 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8509 | } |
| 8510 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8511 | |
| 8512 | /* Copy to a new string */ |
| 8513 | decimal = PyUnicode_New(length, maxchar); |
| 8514 | if (decimal == NULL) |
| 8515 | return decimal; |
| 8516 | kind = PyUnicode_KIND(decimal); |
| 8517 | data = PyUnicode_DATA(decimal); |
| 8518 | /* Iterate over code points */ |
| 8519 | for (i = 0; i < length; i++) { |
| 8520 | Py_UNICODE ch = s[i]; |
| 8521 | if (ch > 127) { |
| 8522 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8523 | if (decimal >= 0) |
| 8524 | ch = '0' + decimal; |
| 8525 | } |
| 8526 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8527 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8528 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8529 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8530 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8531 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8532 | int |
| 8533 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8534 | Py_ssize_t length, |
| 8535 | char *output, |
| 8536 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8537 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8538 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8539 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8540 | enum PyUnicode_Kind kind; |
| 8541 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8542 | |
| 8543 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8544 | PyErr_BadArgument(); |
| 8545 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8546 | } |
| 8547 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8548 | unicode = PyUnicode_FromUnicode(s, length); |
| 8549 | if (unicode == NULL) |
| 8550 | return -1; |
| 8551 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8552 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8553 | Py_DECREF(unicode); |
| 8554 | return -1; |
| 8555 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8556 | kind = PyUnicode_KIND(unicode); |
| 8557 | data = PyUnicode_DATA(unicode); |
| 8558 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8559 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8560 | PyObject *exc; |
| 8561 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8563 | Py_ssize_t startpos; |
| 8564 | |
| 8565 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8566 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8567 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8568 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8569 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8570 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8571 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8572 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8573 | if (decimal >= 0) { |
| 8574 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8575 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8576 | continue; |
| 8577 | } |
| 8578 | if (0 < ch && ch < 256) { |
| 8579 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8580 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8581 | continue; |
| 8582 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8583 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8584 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8585 | exc = NULL; |
| 8586 | raise_encode_exception(&exc, "decimal", unicode, |
| 8587 | startpos, startpos+1, |
| 8588 | "invalid decimal Unicode string"); |
| 8589 | Py_XDECREF(exc); |
| 8590 | Py_DECREF(unicode); |
| 8591 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8592 | } |
| 8593 | /* 0-terminate the output string */ |
| 8594 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8595 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8596 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8597 | } |
| 8598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8599 | /* --- Helpers ------------------------------------------------------------ */ |
| 8600 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8601 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8602 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8603 | Py_ssize_t start, |
| 8604 | Py_ssize_t end) |
| 8605 | { |
| 8606 | int kind1, kind2, kind; |
| 8607 | void *buf1, *buf2; |
| 8608 | Py_ssize_t len1, len2, result; |
| 8609 | |
| 8610 | kind1 = PyUnicode_KIND(s1); |
| 8611 | kind2 = PyUnicode_KIND(s2); |
| 8612 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8613 | buf1 = PyUnicode_DATA(s1); |
| 8614 | buf2 = PyUnicode_DATA(s2); |
| 8615 | if (kind1 != kind) |
| 8616 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8617 | if (!buf1) |
| 8618 | return -2; |
| 8619 | if (kind2 != kind) |
| 8620 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8621 | if (!buf2) { |
| 8622 | if (kind1 != kind) PyMem_Free(buf1); |
| 8623 | return -2; |
| 8624 | } |
| 8625 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8626 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8627 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8628 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8629 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8630 | case PyUnicode_1BYTE_KIND: |
| 8631 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8632 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8633 | else |
| 8634 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8635 | break; |
| 8636 | case PyUnicode_2BYTE_KIND: |
| 8637 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8638 | break; |
| 8639 | case PyUnicode_4BYTE_KIND: |
| 8640 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8641 | break; |
| 8642 | default: |
| 8643 | assert(0); result = -2; |
| 8644 | } |
| 8645 | } |
| 8646 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8647 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8648 | case PyUnicode_1BYTE_KIND: |
| 8649 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8650 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8651 | else |
| 8652 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8653 | break; |
| 8654 | case PyUnicode_2BYTE_KIND: |
| 8655 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8656 | break; |
| 8657 | case PyUnicode_4BYTE_KIND: |
| 8658 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8659 | break; |
| 8660 | default: |
| 8661 | assert(0); result = -2; |
| 8662 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8663 | } |
| 8664 | |
| 8665 | if (kind1 != kind) |
| 8666 | PyMem_Free(buf1); |
| 8667 | if (kind2 != kind) |
| 8668 | PyMem_Free(buf2); |
| 8669 | |
| 8670 | return result; |
| 8671 | } |
| 8672 | |
| 8673 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8674 | _PyUnicode_InsertThousandsGrouping( |
| 8675 | PyObject *unicode, Py_ssize_t index, |
| 8676 | Py_ssize_t n_buffer, |
| 8677 | void *digits, Py_ssize_t n_digits, |
| 8678 | Py_ssize_t min_width, |
| 8679 | const char *grouping, PyObject *thousands_sep, |
| 8680 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8681 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8682 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8683 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8684 | Py_ssize_t thousands_sep_len; |
| 8685 | Py_ssize_t len; |
| 8686 | |
| 8687 | if (unicode != NULL) { |
| 8688 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8689 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8690 | } |
| 8691 | else { |
| 8692 | kind = PyUnicode_1BYTE_KIND; |
| 8693 | data = NULL; |
| 8694 | } |
| 8695 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8696 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8697 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8698 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8699 | if (thousands_sep_kind < kind) { |
| 8700 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8701 | if (!thousands_sep_data) |
| 8702 | return -1; |
| 8703 | } |
| 8704 | else { |
| 8705 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8706 | if (!data) |
| 8707 | return -1; |
| 8708 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8709 | } |
| 8710 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8711 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8712 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8713 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8714 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8715 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8716 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8717 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8718 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8719 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8720 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8721 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8722 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8723 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8724 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8725 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8726 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8727 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8728 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8729 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8730 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8731 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8732 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8733 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8734 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8735 | break; |
| 8736 | default: |
| 8737 | assert(0); |
| 8738 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8740 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8741 | if (thousands_sep_kind < kind) |
| 8742 | PyMem_Free(thousands_sep_data); |
| 8743 | else |
| 8744 | PyMem_Free(data); |
| 8745 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8746 | if (unicode == NULL) { |
| 8747 | *maxchar = 127; |
| 8748 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8749 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8750 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8751 | } |
| 8752 | } |
| 8753 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8754 | } |
| 8755 | |
| 8756 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8757 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8758 | #define ADJUST_INDICES(start, end, len) \ |
| 8759 | if (end > len) \ |
| 8760 | end = len; \ |
| 8761 | else if (end < 0) { \ |
| 8762 | end += len; \ |
| 8763 | if (end < 0) \ |
| 8764 | end = 0; \ |
| 8765 | } \ |
| 8766 | if (start < 0) { \ |
| 8767 | start += len; \ |
| 8768 | if (start < 0) \ |
| 8769 | start = 0; \ |
| 8770 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8771 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8772 | Py_ssize_t |
| 8773 | PyUnicode_Count(PyObject *str, |
| 8774 | PyObject *substr, |
| 8775 | Py_ssize_t start, |
| 8776 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8777 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8778 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8779 | PyObject* str_obj; |
| 8780 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8781 | int kind1, kind2, kind; |
| 8782 | void *buf1 = NULL, *buf2 = NULL; |
| 8783 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8784 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8785 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8786 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8787 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8788 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8789 | if (!sub_obj) { |
| 8790 | Py_DECREF(str_obj); |
| 8791 | return -1; |
| 8792 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8793 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8794 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8795 | Py_DECREF(str_obj); |
| 8796 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8797 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8798 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8799 | kind1 = PyUnicode_KIND(str_obj); |
| 8800 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8801 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8802 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8803 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8804 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8805 | if (kind2 > kind) { |
| 8806 | Py_DECREF(sub_obj); |
| 8807 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8808 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8809 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8810 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8811 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8812 | if (!buf2) |
| 8813 | goto onError; |
| 8814 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8815 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8816 | |
| 8817 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8818 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8819 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8820 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8821 | result = asciilib_count( |
| 8822 | ((Py_UCS1*)buf1) + start, end - start, |
| 8823 | buf2, len2, PY_SSIZE_T_MAX |
| 8824 | ); |
| 8825 | else |
| 8826 | result = ucs1lib_count( |
| 8827 | ((Py_UCS1*)buf1) + start, end - start, |
| 8828 | buf2, len2, PY_SSIZE_T_MAX |
| 8829 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8830 | break; |
| 8831 | case PyUnicode_2BYTE_KIND: |
| 8832 | result = ucs2lib_count( |
| 8833 | ((Py_UCS2*)buf1) + start, end - start, |
| 8834 | buf2, len2, PY_SSIZE_T_MAX |
| 8835 | ); |
| 8836 | break; |
| 8837 | case PyUnicode_4BYTE_KIND: |
| 8838 | result = ucs4lib_count( |
| 8839 | ((Py_UCS4*)buf1) + start, end - start, |
| 8840 | buf2, len2, PY_SSIZE_T_MAX |
| 8841 | ); |
| 8842 | break; |
| 8843 | default: |
| 8844 | assert(0); result = 0; |
| 8845 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8846 | |
| 8847 | Py_DECREF(sub_obj); |
| 8848 | Py_DECREF(str_obj); |
| 8849 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8850 | if (kind2 != kind) |
| 8851 | PyMem_Free(buf2); |
| 8852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8853 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8854 | onError: |
| 8855 | Py_DECREF(sub_obj); |
| 8856 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8857 | if (kind2 != kind && buf2) |
| 8858 | PyMem_Free(buf2); |
| 8859 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8860 | } |
| 8861 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8862 | Py_ssize_t |
| 8863 | PyUnicode_Find(PyObject *str, |
| 8864 | PyObject *sub, |
| 8865 | Py_ssize_t start, |
| 8866 | Py_ssize_t end, |
| 8867 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8868 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8869 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8870 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8871 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8872 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8873 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8874 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8875 | if (!sub) { |
| 8876 | Py_DECREF(str); |
| 8877 | return -2; |
| 8878 | } |
| 8879 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 8880 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8881 | Py_DECREF(str); |
| 8882 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8883 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8884 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8885 | result = any_find_slice(direction, |
| 8886 | str, sub, start, end |
| 8887 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8888 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8889 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8890 | Py_DECREF(sub); |
| 8891 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8892 | return result; |
| 8893 | } |
| 8894 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8895 | Py_ssize_t |
| 8896 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8897 | Py_ssize_t start, Py_ssize_t end, |
| 8898 | int direction) |
| 8899 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8900 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8901 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8902 | if (PyUnicode_READY(str) == -1) |
| 8903 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8904 | if (start < 0 || end < 0) { |
| 8905 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8906 | return -2; |
| 8907 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8908 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8909 | end = PyUnicode_GET_LENGTH(str); |
| 8910 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8911 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 8912 | kind, end-start, ch, direction); |
| 8913 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8914 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8915 | else |
| 8916 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8917 | } |
| 8918 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8919 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8920 | tailmatch(PyObject *self, |
| 8921 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8922 | Py_ssize_t start, |
| 8923 | Py_ssize_t end, |
| 8924 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8925 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8926 | int kind_self; |
| 8927 | int kind_sub; |
| 8928 | void *data_self; |
| 8929 | void *data_sub; |
| 8930 | Py_ssize_t offset; |
| 8931 | Py_ssize_t i; |
| 8932 | Py_ssize_t end_sub; |
| 8933 | |
| 8934 | if (PyUnicode_READY(self) == -1 || |
| 8935 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 8936 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8937 | |
| 8938 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8939 | return 1; |
| 8940 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8941 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8942 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8943 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8944 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8945 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8946 | kind_self = PyUnicode_KIND(self); |
| 8947 | data_self = PyUnicode_DATA(self); |
| 8948 | kind_sub = PyUnicode_KIND(substring); |
| 8949 | data_sub = PyUnicode_DATA(substring); |
| 8950 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8951 | |
| 8952 | if (direction > 0) |
| 8953 | offset = end; |
| 8954 | else |
| 8955 | offset = start; |
| 8956 | |
| 8957 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8958 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8959 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8960 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8961 | /* If both are of the same kind, memcmp is sufficient */ |
| 8962 | if (kind_self == kind_sub) { |
| 8963 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8964 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8965 | data_sub, |
| 8966 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8967 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8968 | } |
| 8969 | /* otherwise we have to compare each character by first accesing it */ |
| 8970 | else { |
| 8971 | /* We do not need to compare 0 and len(substring)-1 because |
| 8972 | the if statement above ensured already that they are equal |
| 8973 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8974 | for (i = 1; i < end_sub; ++i) { |
| 8975 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8976 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8977 | return 0; |
| 8978 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8979 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8980 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8981 | } |
| 8982 | |
| 8983 | return 0; |
| 8984 | } |
| 8985 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8986 | Py_ssize_t |
| 8987 | PyUnicode_Tailmatch(PyObject *str, |
| 8988 | PyObject *substr, |
| 8989 | Py_ssize_t start, |
| 8990 | Py_ssize_t end, |
| 8991 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8992 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8993 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8994 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8995 | str = PyUnicode_FromObject(str); |
| 8996 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8997 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8998 | substr = PyUnicode_FromObject(substr); |
| 8999 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9000 | Py_DECREF(str); |
| 9001 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9003 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9004 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9005 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9006 | Py_DECREF(str); |
| 9007 | Py_DECREF(substr); |
| 9008 | return result; |
| 9009 | } |
| 9010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9011 | /* Apply fixfct filter to the Unicode object self and return a |
| 9012 | reference to the modified object */ |
| 9013 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9014 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9015 | fixup(PyObject *self, |
| 9016 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9017 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9018 | PyObject *u; |
| 9019 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9020 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9021 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9022 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9023 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9024 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9025 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9027 | /* fix functions return the new maximum character in a string, |
| 9028 | if the kind of the resulting unicode object does not change, |
| 9029 | everything is fine. Otherwise we need to change the string kind |
| 9030 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9031 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9032 | |
| 9033 | if (maxchar_new == 0) { |
| 9034 | /* no changes */; |
| 9035 | if (PyUnicode_CheckExact(self)) { |
| 9036 | Py_DECREF(u); |
| 9037 | Py_INCREF(self); |
| 9038 | return self; |
| 9039 | } |
| 9040 | else |
| 9041 | return u; |
| 9042 | } |
| 9043 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9044 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9045 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9046 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9047 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9048 | |
| 9049 | /* In case the maximum character changed, we need to |
| 9050 | convert the string to the new category. */ |
| 9051 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9052 | if (v == NULL) { |
| 9053 | Py_DECREF(u); |
| 9054 | return NULL; |
| 9055 | } |
| 9056 | if (maxchar_new > maxchar_old) { |
| 9057 | /* If the maxchar increased so that the kind changed, not all |
| 9058 | characters are representable anymore and we need to fix the |
| 9059 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9060 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9061 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9062 | maxchar_old = fixfct(v); |
| 9063 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9064 | } |
| 9065 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9066 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9067 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9068 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9069 | Py_DECREF(u); |
| 9070 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9071 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9072 | } |
| 9073 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9074 | static PyObject * |
| 9075 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9076 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9077 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9078 | char *resdata, *data = PyUnicode_DATA(self); |
| 9079 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9080 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9081 | res = PyUnicode_New(len, 127); |
| 9082 | if (res == NULL) |
| 9083 | return NULL; |
| 9084 | resdata = PyUnicode_DATA(res); |
| 9085 | if (lower) |
| 9086 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9087 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9088 | _Py_bytes_upper(resdata, data, len); |
| 9089 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | } |
| 9091 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9092 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9093 | 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] | 9094 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9095 | Py_ssize_t j; |
| 9096 | int final_sigma; |
| 9097 | Py_UCS4 c; |
| 9098 | /* 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] | 9099 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9100 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9101 | |
| 9102 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9103 | */ |
| 9104 | for (j = i - 1; j >= 0; j--) { |
| 9105 | c = PyUnicode_READ(kind, data, j); |
| 9106 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9107 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9108 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9109 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9110 | if (final_sigma) { |
| 9111 | for (j = i + 1; j < length; j++) { |
| 9112 | c = PyUnicode_READ(kind, data, j); |
| 9113 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9114 | break; |
| 9115 | } |
| 9116 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9117 | } |
| 9118 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9119 | } |
| 9120 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9121 | static int |
| 9122 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9123 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9124 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9125 | /* Obscure special case. */ |
| 9126 | if (c == 0x3A3) { |
| 9127 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9128 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9129 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9130 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9131 | } |
| 9132 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9133 | static Py_ssize_t |
| 9134 | 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] | 9135 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9136 | Py_ssize_t i, k = 0; |
| 9137 | int n_res, j; |
| 9138 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9139 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9140 | c = PyUnicode_READ(kind, data, 0); |
| 9141 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9142 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9143 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9144 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9145 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9146 | for (i = 1; i < length; i++) { |
| 9147 | c = PyUnicode_READ(kind, data, i); |
| 9148 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9149 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9150 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9151 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9152 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9153 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9154 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9155 | } |
| 9156 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9157 | static Py_ssize_t |
| 9158 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9159 | Py_ssize_t i, k = 0; |
| 9160 | |
| 9161 | for (i = 0; i < length; i++) { |
| 9162 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9163 | int n_res, j; |
| 9164 | if (Py_UNICODE_ISUPPER(c)) { |
| 9165 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9166 | } |
| 9167 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9168 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9169 | } |
| 9170 | else { |
| 9171 | n_res = 1; |
| 9172 | mapped[0] = c; |
| 9173 | } |
| 9174 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9175 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9176 | res[k++] = mapped[j]; |
| 9177 | } |
| 9178 | } |
| 9179 | return k; |
| 9180 | } |
| 9181 | |
| 9182 | static Py_ssize_t |
| 9183 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9184 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9185 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9186 | Py_ssize_t i, k = 0; |
| 9187 | |
| 9188 | for (i = 0; i < length; i++) { |
| 9189 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9190 | int n_res, j; |
| 9191 | if (lower) |
| 9192 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9193 | else |
| 9194 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9195 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9196 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9197 | res[k++] = mapped[j]; |
| 9198 | } |
| 9199 | } |
| 9200 | return k; |
| 9201 | } |
| 9202 | |
| 9203 | static Py_ssize_t |
| 9204 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9205 | { |
| 9206 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9207 | } |
| 9208 | |
| 9209 | static Py_ssize_t |
| 9210 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9211 | { |
| 9212 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9213 | } |
| 9214 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9215 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9216 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9217 | { |
| 9218 | Py_ssize_t i, k = 0; |
| 9219 | |
| 9220 | for (i = 0; i < length; i++) { |
| 9221 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9222 | Py_UCS4 mapped[3]; |
| 9223 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9224 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9225 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9226 | res[k++] = mapped[j]; |
| 9227 | } |
| 9228 | } |
| 9229 | return k; |
| 9230 | } |
| 9231 | |
| 9232 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9233 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9234 | { |
| 9235 | Py_ssize_t i, k = 0; |
| 9236 | int previous_is_cased; |
| 9237 | |
| 9238 | previous_is_cased = 0; |
| 9239 | for (i = 0; i < length; i++) { |
| 9240 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9241 | Py_UCS4 mapped[3]; |
| 9242 | int n_res, j; |
| 9243 | |
| 9244 | if (previous_is_cased) |
| 9245 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9246 | else |
| 9247 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9248 | |
| 9249 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9250 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9251 | res[k++] = mapped[j]; |
| 9252 | } |
| 9253 | |
| 9254 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9255 | } |
| 9256 | return k; |
| 9257 | } |
| 9258 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9259 | static PyObject * |
| 9260 | case_operation(PyObject *self, |
| 9261 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9262 | { |
| 9263 | PyObject *res = NULL; |
| 9264 | Py_ssize_t length, newlength = 0; |
| 9265 | int kind, outkind; |
| 9266 | void *data, *outdata; |
| 9267 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9268 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9269 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9270 | |
| 9271 | kind = PyUnicode_KIND(self); |
| 9272 | data = PyUnicode_DATA(self); |
| 9273 | length = PyUnicode_GET_LENGTH(self); |
| 9274 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9275 | if (tmp == NULL) |
| 9276 | return PyErr_NoMemory(); |
| 9277 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9278 | res = PyUnicode_New(newlength, maxchar); |
| 9279 | if (res == NULL) |
| 9280 | goto leave; |
| 9281 | tmpend = tmp + newlength; |
| 9282 | outdata = PyUnicode_DATA(res); |
| 9283 | outkind = PyUnicode_KIND(res); |
| 9284 | switch (outkind) { |
| 9285 | case PyUnicode_1BYTE_KIND: |
| 9286 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9287 | break; |
| 9288 | case PyUnicode_2BYTE_KIND: |
| 9289 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9290 | break; |
| 9291 | case PyUnicode_4BYTE_KIND: |
| 9292 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9293 | break; |
| 9294 | default: |
| 9295 | assert(0); |
| 9296 | break; |
| 9297 | } |
| 9298 | leave: |
| 9299 | PyMem_FREE(tmp); |
| 9300 | return res; |
| 9301 | } |
| 9302 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9303 | PyObject * |
| 9304 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9305 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9306 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9307 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9308 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9309 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9310 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9311 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9312 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9313 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9314 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9315 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9316 | int use_memcpy; |
| 9317 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9318 | PyObject *last_obj; |
| 9319 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9320 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9321 | fseq = PySequence_Fast(seq, ""); |
| 9322 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9323 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9324 | } |
| 9325 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9326 | /* NOTE: the following code can't call back into Python code, |
| 9327 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9328 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9329 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9330 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9331 | /* If empty sequence, return u"". */ |
| 9332 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9333 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9334 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9335 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9336 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9337 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9338 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9339 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9340 | if (seqlen == 1) { |
| 9341 | if (PyUnicode_CheckExact(items[0])) { |
| 9342 | res = items[0]; |
| 9343 | Py_INCREF(res); |
| 9344 | Py_DECREF(fseq); |
| 9345 | return res; |
| 9346 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9347 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9348 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9349 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9350 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9351 | /* Set up sep and seplen */ |
| 9352 | if (separator == NULL) { |
| 9353 | /* fall back to a blank space separator */ |
| 9354 | sep = PyUnicode_FromOrdinal(' '); |
| 9355 | if (!sep) |
| 9356 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9357 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9358 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9359 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9360 | else { |
| 9361 | if (!PyUnicode_Check(separator)) { |
| 9362 | PyErr_Format(PyExc_TypeError, |
| 9363 | "separator: expected str instance," |
| 9364 | " %.80s found", |
| 9365 | Py_TYPE(separator)->tp_name); |
| 9366 | goto onError; |
| 9367 | } |
| 9368 | if (PyUnicode_READY(separator)) |
| 9369 | goto onError; |
| 9370 | sep = separator; |
| 9371 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9372 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9373 | /* inc refcount to keep this code path symmetric with the |
| 9374 | above case of a blank separator */ |
| 9375 | Py_INCREF(sep); |
| 9376 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9377 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9378 | } |
| 9379 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9380 | /* There are at least two things to join, or else we have a subclass |
| 9381 | * of str in the sequence. |
| 9382 | * Do a pre-pass to figure out the total amount of space we'll |
| 9383 | * need (sz), and see whether all argument are strings. |
| 9384 | */ |
| 9385 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9386 | #ifdef Py_DEBUG |
| 9387 | use_memcpy = 0; |
| 9388 | #else |
| 9389 | use_memcpy = 1; |
| 9390 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9391 | for (i = 0; i < seqlen; i++) { |
| 9392 | const Py_ssize_t old_sz = sz; |
| 9393 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9394 | if (!PyUnicode_Check(item)) { |
| 9395 | PyErr_Format(PyExc_TypeError, |
| 9396 | "sequence item %zd: expected str instance," |
| 9397 | " %.80s found", |
| 9398 | i, Py_TYPE(item)->tp_name); |
| 9399 | goto onError; |
| 9400 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9401 | if (PyUnicode_READY(item) == -1) |
| 9402 | goto onError; |
| 9403 | sz += PyUnicode_GET_LENGTH(item); |
| 9404 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9405 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9406 | if (i != 0) |
| 9407 | sz += seplen; |
| 9408 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9409 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9410 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9411 | goto onError; |
| 9412 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9413 | if (use_memcpy && last_obj != NULL) { |
| 9414 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9415 | use_memcpy = 0; |
| 9416 | } |
| 9417 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9418 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9419 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9420 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9421 | if (res == NULL) |
| 9422 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9423 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9424 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9425 | #ifdef Py_DEBUG |
| 9426 | use_memcpy = 0; |
| 9427 | #else |
| 9428 | if (use_memcpy) { |
| 9429 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9430 | kind = PyUnicode_KIND(res); |
| 9431 | if (seplen != 0) |
| 9432 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9433 | } |
| 9434 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9435 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9436 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9437 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9438 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9439 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9440 | if (use_memcpy) { |
| 9441 | Py_MEMCPY(res_data, |
| 9442 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9443 | kind * seplen); |
| 9444 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9445 | } |
| 9446 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9447 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9448 | res_offset += seplen; |
| 9449 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9450 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9451 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9452 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9453 | if (use_memcpy) { |
| 9454 | Py_MEMCPY(res_data, |
| 9455 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9456 | kind * itemlen); |
| 9457 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9458 | } |
| 9459 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9460 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9461 | res_offset += itemlen; |
| 9462 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9463 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9464 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9465 | if (use_memcpy) |
| 9466 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9467 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9468 | else |
| 9469 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9470 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9471 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9472 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9473 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9474 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9475 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9476 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9477 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9478 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9479 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9480 | return NULL; |
| 9481 | } |
| 9482 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9483 | #define FILL(kind, data, value, start, length) \ |
| 9484 | do { \ |
| 9485 | Py_ssize_t i_ = 0; \ |
| 9486 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9487 | switch ((kind)) { \ |
| 9488 | case PyUnicode_1BYTE_KIND: { \ |
| 9489 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9490 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9491 | break; \ |
| 9492 | } \ |
| 9493 | case PyUnicode_2BYTE_KIND: { \ |
| 9494 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9495 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9496 | break; \ |
| 9497 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9498 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9499 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9500 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9501 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9502 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9503 | } \ |
| 9504 | } \ |
| 9505 | } while (0) |
| 9506 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9507 | void |
| 9508 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9509 | Py_UCS4 fill_char) |
| 9510 | { |
| 9511 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9512 | const void *data = PyUnicode_DATA(unicode); |
| 9513 | assert(PyUnicode_IS_READY(unicode)); |
| 9514 | assert(unicode_modifiable(unicode)); |
| 9515 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9516 | assert(start >= 0); |
| 9517 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9518 | FILL(kind, data, fill_char, start, length); |
| 9519 | } |
| 9520 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9521 | Py_ssize_t |
| 9522 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9523 | Py_UCS4 fill_char) |
| 9524 | { |
| 9525 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9526 | |
| 9527 | if (!PyUnicode_Check(unicode)) { |
| 9528 | PyErr_BadInternalCall(); |
| 9529 | return -1; |
| 9530 | } |
| 9531 | if (PyUnicode_READY(unicode) == -1) |
| 9532 | return -1; |
| 9533 | if (unicode_check_modifiable(unicode)) |
| 9534 | return -1; |
| 9535 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9536 | if (start < 0) { |
| 9537 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9538 | return -1; |
| 9539 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9540 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9541 | PyErr_SetString(PyExc_ValueError, |
| 9542 | "fill character is bigger than " |
| 9543 | "the string maximum character"); |
| 9544 | return -1; |
| 9545 | } |
| 9546 | |
| 9547 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9548 | length = Py_MIN(maxlen, length); |
| 9549 | if (length <= 0) |
| 9550 | return 0; |
| 9551 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9552 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9553 | return length; |
| 9554 | } |
| 9555 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9556 | static PyObject * |
| 9557 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9558 | Py_ssize_t left, |
| 9559 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9560 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9561 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9562 | PyObject *u; |
| 9563 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9564 | int kind; |
| 9565 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9566 | |
| 9567 | if (left < 0) |
| 9568 | left = 0; |
| 9569 | if (right < 0) |
| 9570 | right = 0; |
| 9571 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9572 | if (left == 0 && right == 0) |
| 9573 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9574 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9575 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9576 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9577 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9578 | return NULL; |
| 9579 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9580 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9581 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9582 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9583 | if (!u) |
| 9584 | return NULL; |
| 9585 | |
| 9586 | kind = PyUnicode_KIND(u); |
| 9587 | data = PyUnicode_DATA(u); |
| 9588 | if (left) |
| 9589 | FILL(kind, data, fill, 0, left); |
| 9590 | if (right) |
| 9591 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9592 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9593 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9594 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9595 | } |
| 9596 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9597 | PyObject * |
| 9598 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9599 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9600 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9601 | |
| 9602 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9603 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9604 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9605 | if (PyUnicode_READY(string) == -1) { |
| 9606 | Py_DECREF(string); |
| 9607 | return NULL; |
| 9608 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9609 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9610 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9611 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9612 | if (PyUnicode_IS_ASCII(string)) |
| 9613 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9614 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9615 | PyUnicode_GET_LENGTH(string), keepends); |
| 9616 | else |
| 9617 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9618 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9619 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9620 | break; |
| 9621 | case PyUnicode_2BYTE_KIND: |
| 9622 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9623 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9624 | PyUnicode_GET_LENGTH(string), keepends); |
| 9625 | break; |
| 9626 | case PyUnicode_4BYTE_KIND: |
| 9627 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9628 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9629 | PyUnicode_GET_LENGTH(string), keepends); |
| 9630 | break; |
| 9631 | default: |
| 9632 | assert(0); |
| 9633 | list = 0; |
| 9634 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9635 | Py_DECREF(string); |
| 9636 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9637 | } |
| 9638 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9639 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9640 | split(PyObject *self, |
| 9641 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9642 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9643 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9644 | int kind1, kind2, kind; |
| 9645 | void *buf1, *buf2; |
| 9646 | Py_ssize_t len1, len2; |
| 9647 | PyObject* out; |
| 9648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9649 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9650 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9651 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9652 | if (PyUnicode_READY(self) == -1) |
| 9653 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9654 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9655 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9656 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9657 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9658 | if (PyUnicode_IS_ASCII(self)) |
| 9659 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9660 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9661 | PyUnicode_GET_LENGTH(self), maxcount |
| 9662 | ); |
| 9663 | else |
| 9664 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9665 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9666 | PyUnicode_GET_LENGTH(self), maxcount |
| 9667 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9668 | case PyUnicode_2BYTE_KIND: |
| 9669 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9670 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9671 | PyUnicode_GET_LENGTH(self), maxcount |
| 9672 | ); |
| 9673 | case PyUnicode_4BYTE_KIND: |
| 9674 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9675 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9676 | PyUnicode_GET_LENGTH(self), maxcount |
| 9677 | ); |
| 9678 | default: |
| 9679 | assert(0); |
| 9680 | return NULL; |
| 9681 | } |
| 9682 | |
| 9683 | if (PyUnicode_READY(substring) == -1) |
| 9684 | return NULL; |
| 9685 | |
| 9686 | kind1 = PyUnicode_KIND(self); |
| 9687 | kind2 = PyUnicode_KIND(substring); |
| 9688 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9689 | buf1 = PyUnicode_DATA(self); |
| 9690 | buf2 = PyUnicode_DATA(substring); |
| 9691 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9692 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9693 | if (!buf1) |
| 9694 | return NULL; |
| 9695 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9696 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9697 | if (!buf2) { |
| 9698 | if (kind1 != kind) PyMem_Free(buf1); |
| 9699 | return NULL; |
| 9700 | } |
| 9701 | len1 = PyUnicode_GET_LENGTH(self); |
| 9702 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9703 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9704 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9705 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9706 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9707 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9708 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9709 | else |
| 9710 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9711 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9712 | break; |
| 9713 | case PyUnicode_2BYTE_KIND: |
| 9714 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9715 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9716 | break; |
| 9717 | case PyUnicode_4BYTE_KIND: |
| 9718 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9719 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9720 | break; |
| 9721 | default: |
| 9722 | out = NULL; |
| 9723 | } |
| 9724 | if (kind1 != kind) |
| 9725 | PyMem_Free(buf1); |
| 9726 | if (kind2 != kind) |
| 9727 | PyMem_Free(buf2); |
| 9728 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9729 | } |
| 9730 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9731 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9732 | rsplit(PyObject *self, |
| 9733 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9734 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9735 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9736 | int kind1, kind2, kind; |
| 9737 | void *buf1, *buf2; |
| 9738 | Py_ssize_t len1, len2; |
| 9739 | PyObject* out; |
| 9740 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9741 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9742 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9743 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9744 | if (PyUnicode_READY(self) == -1) |
| 9745 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9746 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9747 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9748 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9749 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9750 | if (PyUnicode_IS_ASCII(self)) |
| 9751 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9752 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9753 | PyUnicode_GET_LENGTH(self), maxcount |
| 9754 | ); |
| 9755 | else |
| 9756 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9757 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9758 | PyUnicode_GET_LENGTH(self), maxcount |
| 9759 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9760 | case PyUnicode_2BYTE_KIND: |
| 9761 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9762 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9763 | PyUnicode_GET_LENGTH(self), maxcount |
| 9764 | ); |
| 9765 | case PyUnicode_4BYTE_KIND: |
| 9766 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9767 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9768 | PyUnicode_GET_LENGTH(self), maxcount |
| 9769 | ); |
| 9770 | default: |
| 9771 | assert(0); |
| 9772 | return NULL; |
| 9773 | } |
| 9774 | |
| 9775 | if (PyUnicode_READY(substring) == -1) |
| 9776 | return NULL; |
| 9777 | |
| 9778 | kind1 = PyUnicode_KIND(self); |
| 9779 | kind2 = PyUnicode_KIND(substring); |
| 9780 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9781 | buf1 = PyUnicode_DATA(self); |
| 9782 | buf2 = PyUnicode_DATA(substring); |
| 9783 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9784 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9785 | if (!buf1) |
| 9786 | return NULL; |
| 9787 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9788 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9789 | if (!buf2) { |
| 9790 | if (kind1 != kind) PyMem_Free(buf1); |
| 9791 | return NULL; |
| 9792 | } |
| 9793 | len1 = PyUnicode_GET_LENGTH(self); |
| 9794 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9795 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9796 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9797 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9798 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9799 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9800 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9801 | else |
| 9802 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9803 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9804 | break; |
| 9805 | case PyUnicode_2BYTE_KIND: |
| 9806 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9807 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9808 | break; |
| 9809 | case PyUnicode_4BYTE_KIND: |
| 9810 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9811 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9812 | break; |
| 9813 | default: |
| 9814 | out = NULL; |
| 9815 | } |
| 9816 | if (kind1 != kind) |
| 9817 | PyMem_Free(buf1); |
| 9818 | if (kind2 != kind) |
| 9819 | PyMem_Free(buf2); |
| 9820 | return out; |
| 9821 | } |
| 9822 | |
| 9823 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9824 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9825 | 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] | 9826 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9827 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9828 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9829 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9830 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9831 | else |
| 9832 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | case PyUnicode_2BYTE_KIND: |
| 9834 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9835 | case PyUnicode_4BYTE_KIND: |
| 9836 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9837 | } |
| 9838 | assert(0); |
| 9839 | return -1; |
| 9840 | } |
| 9841 | |
| 9842 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9843 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9844 | 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] | 9845 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 9846 | switch (kind) { |
| 9847 | case PyUnicode_1BYTE_KIND: |
| 9848 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9849 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9850 | else |
| 9851 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9852 | case PyUnicode_2BYTE_KIND: |
| 9853 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9854 | case PyUnicode_4BYTE_KIND: |
| 9855 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9856 | } |
| 9857 | assert(0); |
| 9858 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9859 | } |
| 9860 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9861 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9862 | replace(PyObject *self, PyObject *str1, |
| 9863 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9864 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 | PyObject *u; |
| 9866 | char *sbuf = PyUnicode_DATA(self); |
| 9867 | char *buf1 = PyUnicode_DATA(str1); |
| 9868 | char *buf2 = PyUnicode_DATA(str2); |
| 9869 | int srelease = 0, release1 = 0, release2 = 0; |
| 9870 | int skind = PyUnicode_KIND(self); |
| 9871 | int kind1 = PyUnicode_KIND(str1); |
| 9872 | int kind2 = PyUnicode_KIND(str2); |
| 9873 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9874 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9875 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9876 | int mayshrink; |
| 9877 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9878 | |
| 9879 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9880 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9881 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9882 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9883 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9884 | if (str1 == str2) |
| 9885 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9886 | if (skind < kind1) |
| 9887 | /* substring too wide to be present */ |
| 9888 | goto nothing; |
| 9889 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9890 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9891 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 9892 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 9893 | result string. */ |
| 9894 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9895 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9896 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9897 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9898 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9899 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9900 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9901 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9902 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9903 | Py_UCS4 u1, u2; |
| 9904 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9905 | Py_ssize_t index, pos; |
| 9906 | char *src; |
| 9907 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9908 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9909 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 9910 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9911 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9913 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9914 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9915 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9916 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9918 | |
| 9919 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 9920 | index = 0; |
| 9921 | src = sbuf; |
| 9922 | while (--maxcount) |
| 9923 | { |
| 9924 | pos++; |
| 9925 | src += pos * PyUnicode_KIND(self); |
| 9926 | slen -= pos; |
| 9927 | index += pos; |
| 9928 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 9929 | if (pos < 0) |
| 9930 | break; |
| 9931 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 9932 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9933 | } |
| 9934 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9935 | int rkind = skind; |
| 9936 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9937 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9938 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9939 | if (kind1 < rkind) { |
| 9940 | /* widen substring */ |
| 9941 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9942 | if (!buf1) goto error; |
| 9943 | release1 = 1; |
| 9944 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9945 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9946 | if (i < 0) |
| 9947 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9948 | if (rkind > kind2) { |
| 9949 | /* widen replacement */ |
| 9950 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9951 | if (!buf2) goto error; |
| 9952 | release2 = 1; |
| 9953 | } |
| 9954 | else if (rkind < kind2) { |
| 9955 | /* widen self and buf1 */ |
| 9956 | rkind = kind2; |
| 9957 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 9958 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9959 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9960 | if (!sbuf) goto error; |
| 9961 | srelease = 1; |
| 9962 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9963 | if (!buf1) goto error; |
| 9964 | release1 = 1; |
| 9965 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9966 | u = PyUnicode_New(slen, maxchar); |
| 9967 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9968 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9969 | assert(PyUnicode_KIND(u) == rkind); |
| 9970 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9971 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9972 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9973 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9974 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9975 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9976 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9977 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9978 | |
| 9979 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9980 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9981 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9982 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9983 | if (i == -1) |
| 9984 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9985 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9986 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9987 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9988 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9989 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9990 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9991 | } |
| 9992 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9993 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 9994 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9995 | int rkind = skind; |
| 9996 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9997 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9999 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10000 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10001 | if (!buf1) goto error; |
| 10002 | release1 = 1; |
| 10003 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10004 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10005 | if (n == 0) |
| 10006 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10007 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10008 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10010 | if (!buf2) goto error; |
| 10011 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10012 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10013 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10014 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10015 | rkind = kind2; |
| 10016 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10017 | if (!sbuf) goto error; |
| 10018 | srelease = 1; |
| 10019 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10020 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10021 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10022 | if (!buf1) goto error; |
| 10023 | release1 = 1; |
| 10024 | } |
| 10025 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10026 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10027 | 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] | 10028 | PyErr_SetString(PyExc_OverflowError, |
| 10029 | "replace string is too long"); |
| 10030 | goto error; |
| 10031 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10032 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10033 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10034 | _Py_INCREF_UNICODE_EMPTY(); |
| 10035 | if (!unicode_empty) |
| 10036 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10037 | u = unicode_empty; |
| 10038 | goto done; |
| 10039 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10040 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10041 | PyErr_SetString(PyExc_OverflowError, |
| 10042 | "replace string is too long"); |
| 10043 | goto error; |
| 10044 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10045 | u = PyUnicode_New(new_size, maxchar); |
| 10046 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10047 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10048 | assert(PyUnicode_KIND(u) == rkind); |
| 10049 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10050 | ires = i = 0; |
| 10051 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10052 | while (n-- > 0) { |
| 10053 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10054 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10055 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10056 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10057 | if (j == -1) |
| 10058 | break; |
| 10059 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10060 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10061 | memcpy(res + rkind * ires, |
| 10062 | sbuf + rkind * i, |
| 10063 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10064 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10065 | } |
| 10066 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10067 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10068 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10070 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10072 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10073 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10074 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10075 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10076 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10077 | memcpy(res + rkind * ires, |
| 10078 | sbuf + rkind * i, |
| 10079 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10080 | } |
| 10081 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10082 | /* interleave */ |
| 10083 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10084 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10086 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10087 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10088 | if (--n <= 0) |
| 10089 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10090 | memcpy(res + rkind * ires, |
| 10091 | sbuf + rkind * i, |
| 10092 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | ires++; |
| 10094 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10095 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10096 | memcpy(res + rkind * ires, |
| 10097 | sbuf + rkind * i, |
| 10098 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10099 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10100 | } |
| 10101 | |
| 10102 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10103 | unicode_adjust_maxchar(&u); |
| 10104 | if (u == NULL) |
| 10105 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10106 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10107 | |
| 10108 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | if (srelease) |
| 10110 | PyMem_FREE(sbuf); |
| 10111 | if (release1) |
| 10112 | PyMem_FREE(buf1); |
| 10113 | if (release2) |
| 10114 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10115 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10116 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10117 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10118 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10119 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10120 | if (srelease) |
| 10121 | PyMem_FREE(sbuf); |
| 10122 | if (release1) |
| 10123 | PyMem_FREE(buf1); |
| 10124 | if (release2) |
| 10125 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10126 | return unicode_result_unchanged(self); |
| 10127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10128 | error: |
| 10129 | if (srelease && sbuf) |
| 10130 | PyMem_FREE(sbuf); |
| 10131 | if (release1 && buf1) |
| 10132 | PyMem_FREE(buf1); |
| 10133 | if (release2 && buf2) |
| 10134 | PyMem_FREE(buf2); |
| 10135 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10136 | } |
| 10137 | |
| 10138 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10139 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10140 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10141 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10142 | \n\ |
| 10143 | 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] | 10144 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10145 | |
| 10146 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10147 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10148 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10149 | if (PyUnicode_READY(self) == -1) |
| 10150 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10151 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10152 | } |
| 10153 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10154 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10155 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10156 | \n\ |
| 10157 | 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] | 10158 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10159 | |
| 10160 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10161 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10162 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10163 | if (PyUnicode_READY(self) == -1) |
| 10164 | return NULL; |
| 10165 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10166 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10167 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10168 | } |
| 10169 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10170 | PyDoc_STRVAR(casefold__doc__, |
| 10171 | "S.casefold() -> str\n\ |
| 10172 | \n\ |
| 10173 | Return a version of S suitable for caseless comparisons."); |
| 10174 | |
| 10175 | static PyObject * |
| 10176 | unicode_casefold(PyObject *self) |
| 10177 | { |
| 10178 | if (PyUnicode_READY(self) == -1) |
| 10179 | return NULL; |
| 10180 | if (PyUnicode_IS_ASCII(self)) |
| 10181 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10182 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10183 | } |
| 10184 | |
| 10185 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10186 | /* Argument converter. Coerces to a single unicode character */ |
| 10187 | |
| 10188 | static int |
| 10189 | convert_uc(PyObject *obj, void *addr) |
| 10190 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10192 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10193 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10194 | uniobj = PyUnicode_FromObject(obj); |
| 10195 | if (uniobj == NULL) { |
| 10196 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10197 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10198 | return 0; |
| 10199 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10200 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10201 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10202 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10203 | Py_DECREF(uniobj); |
| 10204 | return 0; |
| 10205 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10206 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10207 | Py_DECREF(uniobj); |
| 10208 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10209 | } |
| 10210 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10211 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10212 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10213 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10214 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10215 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10216 | |
| 10217 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10218 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10219 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10220 | Py_ssize_t marg, left; |
| 10221 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10222 | Py_UCS4 fillchar = ' '; |
| 10223 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10224 | 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] | 10225 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10226 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10227 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10228 | return NULL; |
| 10229 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10230 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10231 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10232 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10233 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10234 | left = marg / 2 + (marg & width & 1); |
| 10235 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10236 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10237 | } |
| 10238 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10239 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10240 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10241 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10242 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10243 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame^] | 10244 | #define COMPARE(TYPE1, TYPE2) \ |
| 10245 | do { \ |
| 10246 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10247 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10248 | TYPE1* end = p1 + len; \ |
| 10249 | Py_UCS4 c1, c2; \ |
| 10250 | for (; p1 != end; p1++, p2++) { \ |
| 10251 | c1 = *p1; \ |
| 10252 | c2 = *p2; \ |
| 10253 | if (c1 != c2) \ |
| 10254 | return (c1 < c2) ? -1 : 1; \ |
| 10255 | } \ |
| 10256 | } \ |
| 10257 | while (0) |
| 10258 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10259 | int kind1, kind2; |
| 10260 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame^] | 10261 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10262 | |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10263 | /* a string is equal to itself */ |
| 10264 | if (str1 == str2) |
| 10265 | return 0; |
| 10266 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | kind1 = PyUnicode_KIND(str1); |
| 10268 | kind2 = PyUnicode_KIND(str2); |
| 10269 | data1 = PyUnicode_DATA(str1); |
| 10270 | data2 = PyUnicode_DATA(str2); |
| 10271 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10272 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10273 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10274 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame^] | 10275 | switch(kind1) { |
| 10276 | case PyUnicode_1BYTE_KIND: |
| 10277 | { |
| 10278 | switch(kind2) { |
| 10279 | case PyUnicode_1BYTE_KIND: |
| 10280 | { |
| 10281 | int cmp = memcmp(data1, data2, len); |
| 10282 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10283 | if (cmp < 0) |
| 10284 | return -1; |
| 10285 | if (cmp > 0) |
| 10286 | return 1; |
| 10287 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10288 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame^] | 10289 | case PyUnicode_2BYTE_KIND: |
| 10290 | COMPARE(Py_UCS1, Py_UCS2); |
| 10291 | break; |
| 10292 | case PyUnicode_4BYTE_KIND: |
| 10293 | COMPARE(Py_UCS1, Py_UCS4); |
| 10294 | break; |
| 10295 | default: |
| 10296 | assert(0); |
| 10297 | } |
| 10298 | break; |
| 10299 | } |
| 10300 | case PyUnicode_2BYTE_KIND: |
| 10301 | { |
| 10302 | switch(kind2) { |
| 10303 | case PyUnicode_1BYTE_KIND: |
| 10304 | COMPARE(Py_UCS2, Py_UCS1); |
| 10305 | break; |
| 10306 | case PyUnicode_2BYTE_KIND: |
| 10307 | COMPARE(Py_UCS2, Py_UCS2); |
| 10308 | break; |
| 10309 | case PyUnicode_4BYTE_KIND: |
| 10310 | COMPARE(Py_UCS2, Py_UCS4); |
| 10311 | break; |
| 10312 | default: |
| 10313 | assert(0); |
| 10314 | } |
| 10315 | break; |
| 10316 | } |
| 10317 | case PyUnicode_4BYTE_KIND: |
| 10318 | { |
| 10319 | switch(kind2) { |
| 10320 | case PyUnicode_1BYTE_KIND: |
| 10321 | COMPARE(Py_UCS4, Py_UCS1); |
| 10322 | break; |
| 10323 | case PyUnicode_2BYTE_KIND: |
| 10324 | COMPARE(Py_UCS4, Py_UCS2); |
| 10325 | break; |
| 10326 | case PyUnicode_4BYTE_KIND: |
| 10327 | COMPARE(Py_UCS4, Py_UCS4); |
| 10328 | break; |
| 10329 | default: |
| 10330 | assert(0); |
| 10331 | } |
| 10332 | break; |
| 10333 | } |
| 10334 | default: |
| 10335 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10336 | } |
| 10337 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10338 | if (len1 == len2) |
| 10339 | return 0; |
| 10340 | if (len1 < len2) |
| 10341 | return -1; |
| 10342 | else |
| 10343 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame^] | 10344 | |
| 10345 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10346 | } |
| 10347 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10348 | static int |
| 10349 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10350 | { |
| 10351 | int kind; |
| 10352 | void *data1, *data2; |
| 10353 | Py_ssize_t len; |
| 10354 | int cmp; |
| 10355 | |
| 10356 | /* a string is equal to itself */ |
| 10357 | if (str1 == str2) |
| 10358 | return 1; |
| 10359 | |
| 10360 | len = PyUnicode_GET_LENGTH(str1); |
| 10361 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10362 | return 0; |
| 10363 | kind = PyUnicode_KIND(str1); |
| 10364 | if (PyUnicode_KIND(str2) != kind) |
| 10365 | return 0; |
| 10366 | data1 = PyUnicode_DATA(str1); |
| 10367 | data2 = PyUnicode_DATA(str2); |
| 10368 | |
| 10369 | cmp = memcmp(data1, data2, len * kind); |
| 10370 | return (cmp == 0); |
| 10371 | } |
| 10372 | |
| 10373 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10374 | int |
| 10375 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10376 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10377 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10378 | if (PyUnicode_READY(left) == -1 || |
| 10379 | PyUnicode_READY(right) == -1) |
| 10380 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10381 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10382 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10383 | PyErr_Format(PyExc_TypeError, |
| 10384 | "Can't compare %.100s and %.100s", |
| 10385 | left->ob_type->tp_name, |
| 10386 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10387 | return -1; |
| 10388 | } |
| 10389 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10390 | int |
| 10391 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10392 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10393 | Py_ssize_t i; |
| 10394 | int kind; |
| 10395 | void *data; |
| 10396 | Py_UCS4 chr; |
| 10397 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10398 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10399 | if (PyUnicode_READY(uni) == -1) |
| 10400 | return -1; |
| 10401 | kind = PyUnicode_KIND(uni); |
| 10402 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10403 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10404 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10405 | if (chr != str[i]) |
| 10406 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10407 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10408 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10409 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10410 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10411 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10412 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10413 | return 0; |
| 10414 | } |
| 10415 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10416 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10417 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10418 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10419 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10420 | PyObject * |
| 10421 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10422 | { |
| 10423 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10424 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10425 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10426 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10427 | Py_RETURN_NOTIMPLEMENTED; |
| 10428 | |
| 10429 | if (PyUnicode_READY(left) == -1 || |
| 10430 | PyUnicode_READY(right) == -1) |
| 10431 | return NULL; |
| 10432 | |
| 10433 | if (op == Py_EQ || op == Py_NE) { |
| 10434 | result = unicode_compare_eq(left, right); |
| 10435 | if (op == Py_EQ) |
| 10436 | v = TEST_COND(result); |
| 10437 | else |
| 10438 | v = TEST_COND(!result); |
| 10439 | } |
| 10440 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10441 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10442 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10443 | /* Convert the return value to a Boolean */ |
| 10444 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10445 | case Py_LE: |
| 10446 | v = TEST_COND(result <= 0); |
| 10447 | break; |
| 10448 | case Py_GE: |
| 10449 | v = TEST_COND(result >= 0); |
| 10450 | break; |
| 10451 | case Py_LT: |
| 10452 | v = TEST_COND(result == -1); |
| 10453 | break; |
| 10454 | case Py_GT: |
| 10455 | v = TEST_COND(result == 1); |
| 10456 | break; |
| 10457 | default: |
| 10458 | PyErr_BadArgument(); |
| 10459 | return NULL; |
| 10460 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10461 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10462 | Py_INCREF(v); |
| 10463 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10464 | } |
| 10465 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10466 | int |
| 10467 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10468 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10469 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10470 | int kind1, kind2, kind; |
| 10471 | void *buf1, *buf2; |
| 10472 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10473 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10474 | |
| 10475 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10476 | sub = PyUnicode_FromObject(element); |
| 10477 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10478 | PyErr_Format(PyExc_TypeError, |
| 10479 | "'in <string>' requires string as left operand, not %s", |
| 10480 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10481 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10482 | } |
| 10483 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10484 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10485 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10486 | Py_DECREF(sub); |
| 10487 | return -1; |
| 10488 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10489 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10490 | Py_DECREF(sub); |
| 10491 | Py_DECREF(str); |
| 10492 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | kind1 = PyUnicode_KIND(str); |
| 10495 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10496 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10497 | buf1 = PyUnicode_DATA(str); |
| 10498 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10499 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10500 | if (kind2 > kind) { |
| 10501 | Py_DECREF(sub); |
| 10502 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10503 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10504 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10505 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10506 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10507 | if (!buf2) { |
| 10508 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10509 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10510 | return -1; |
| 10511 | } |
| 10512 | len1 = PyUnicode_GET_LENGTH(str); |
| 10513 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10514 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10515 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10516 | case PyUnicode_1BYTE_KIND: |
| 10517 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10518 | break; |
| 10519 | case PyUnicode_2BYTE_KIND: |
| 10520 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10521 | break; |
| 10522 | case PyUnicode_4BYTE_KIND: |
| 10523 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10524 | break; |
| 10525 | default: |
| 10526 | result = -1; |
| 10527 | assert(0); |
| 10528 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10529 | |
| 10530 | Py_DECREF(str); |
| 10531 | Py_DECREF(sub); |
| 10532 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10533 | if (kind2 != kind) |
| 10534 | PyMem_Free(buf2); |
| 10535 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10536 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10537 | } |
| 10538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10539 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10540 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10541 | PyObject * |
| 10542 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10543 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10544 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10545 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10546 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10547 | |
| 10548 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10549 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10550 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10551 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10552 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10553 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10554 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10555 | |
| 10556 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10557 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10558 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10559 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10560 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10561 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10562 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10563 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10564 | } |
| 10565 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10566 | u_len = PyUnicode_GET_LENGTH(u); |
| 10567 | v_len = PyUnicode_GET_LENGTH(v); |
| 10568 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10569 | PyErr_SetString(PyExc_OverflowError, |
| 10570 | "strings are too large to concat"); |
| 10571 | goto onError; |
| 10572 | } |
| 10573 | new_len = u_len + v_len; |
| 10574 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10575 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10576 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10577 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10579 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10580 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10581 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10582 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10583 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10584 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10585 | Py_DECREF(u); |
| 10586 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10587 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10588 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10590 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10591 | Py_XDECREF(u); |
| 10592 | Py_XDECREF(v); |
| 10593 | return NULL; |
| 10594 | } |
| 10595 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10596 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10597 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10598 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10599 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10600 | Py_UCS4 maxchar, maxchar2; |
| 10601 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10602 | |
| 10603 | if (p_left == NULL) { |
| 10604 | if (!PyErr_Occurred()) |
| 10605 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10606 | return; |
| 10607 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10608 | left = *p_left; |
Serhiy Storchaka | 6c83e73 | 2013-01-04 12:39:34 +0200 | [diff] [blame] | 10609 | if (right == NULL || left == NULL || !PyUnicode_Check(left)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10610 | if (!PyErr_Occurred()) |
| 10611 | PyErr_BadInternalCall(); |
| 10612 | goto error; |
| 10613 | } |
| 10614 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10615 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10616 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10617 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10618 | goto error; |
| 10619 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10620 | /* Shortcuts */ |
| 10621 | if (left == unicode_empty) { |
| 10622 | Py_DECREF(left); |
| 10623 | Py_INCREF(right); |
| 10624 | *p_left = right; |
| 10625 | return; |
| 10626 | } |
| 10627 | if (right == unicode_empty) |
| 10628 | return; |
| 10629 | |
| 10630 | left_len = PyUnicode_GET_LENGTH(left); |
| 10631 | right_len = PyUnicode_GET_LENGTH(right); |
| 10632 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10633 | PyErr_SetString(PyExc_OverflowError, |
| 10634 | "strings are too large to concat"); |
| 10635 | goto error; |
| 10636 | } |
| 10637 | new_len = left_len + right_len; |
| 10638 | |
| 10639 | if (unicode_modifiable(left) |
| 10640 | && PyUnicode_CheckExact(right) |
| 10641 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10642 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10643 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10644 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10645 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10646 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10647 | { |
| 10648 | /* append inplace */ |
| 10649 | if (unicode_resize(p_left, new_len) != 0) { |
| 10650 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10651 | * deallocated so it cannot be put back into |
| 10652 | * 'variable'. The MemoryError is raised when there |
| 10653 | * is no value in 'variable', which might (very |
| 10654 | * remotely) be a cause of incompatibilities. |
| 10655 | */ |
| 10656 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10657 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10658 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10659 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10660 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10661 | else { |
| 10662 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10663 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10664 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10665 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10666 | /* Concat the two Unicode strings */ |
| 10667 | res = PyUnicode_New(new_len, maxchar); |
| 10668 | if (res == NULL) |
| 10669 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10670 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10671 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10672 | Py_DECREF(left); |
| 10673 | *p_left = res; |
| 10674 | } |
| 10675 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10676 | return; |
| 10677 | |
| 10678 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10679 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10680 | } |
| 10681 | |
| 10682 | void |
| 10683 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10684 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10685 | PyUnicode_Append(pleft, right); |
| 10686 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10687 | } |
| 10688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10689 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10690 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10691 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10692 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10693 | 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] | 10694 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10695 | |
| 10696 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10697 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10698 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10699 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10700 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10701 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10702 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10703 | int kind1, kind2, kind; |
| 10704 | void *buf1, *buf2; |
| 10705 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10707 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10708 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10709 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10710 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10711 | kind1 = PyUnicode_KIND(self); |
| 10712 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10713 | if (kind2 > kind1) |
| 10714 | return PyLong_FromLong(0); |
| 10715 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10716 | buf1 = PyUnicode_DATA(self); |
| 10717 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10718 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10719 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10720 | if (!buf2) { |
| 10721 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10722 | return NULL; |
| 10723 | } |
| 10724 | len1 = PyUnicode_GET_LENGTH(self); |
| 10725 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10726 | |
| 10727 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10728 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10729 | case PyUnicode_1BYTE_KIND: |
| 10730 | iresult = ucs1lib_count( |
| 10731 | ((Py_UCS1*)buf1) + start, end - start, |
| 10732 | buf2, len2, PY_SSIZE_T_MAX |
| 10733 | ); |
| 10734 | break; |
| 10735 | case PyUnicode_2BYTE_KIND: |
| 10736 | iresult = ucs2lib_count( |
| 10737 | ((Py_UCS2*)buf1) + start, end - start, |
| 10738 | buf2, len2, PY_SSIZE_T_MAX |
| 10739 | ); |
| 10740 | break; |
| 10741 | case PyUnicode_4BYTE_KIND: |
| 10742 | iresult = ucs4lib_count( |
| 10743 | ((Py_UCS4*)buf1) + start, end - start, |
| 10744 | buf2, len2, PY_SSIZE_T_MAX |
| 10745 | ); |
| 10746 | break; |
| 10747 | default: |
| 10748 | assert(0); iresult = 0; |
| 10749 | } |
| 10750 | |
| 10751 | result = PyLong_FromSsize_t(iresult); |
| 10752 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10753 | if (kind2 != kind) |
| 10754 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10755 | |
| 10756 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10757 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10758 | return result; |
| 10759 | } |
| 10760 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10761 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10762 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10764 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10765 | 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] | 10766 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10767 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10768 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10769 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10770 | |
| 10771 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10772 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10773 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10774 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10775 | char *encoding = NULL; |
| 10776 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10777 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10778 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10779 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10780 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10781 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10782 | } |
| 10783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10784 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10785 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10786 | \n\ |
| 10787 | 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] | 10788 | 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] | 10789 | |
| 10790 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10791 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10792 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10793 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10794 | Py_UCS4 ch; |
| 10795 | PyObject *u; |
| 10796 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10797 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10798 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10799 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10800 | |
| 10801 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10802 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10803 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10804 | if (PyUnicode_READY(self) == -1) |
| 10805 | return NULL; |
| 10806 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10807 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10808 | src_len = PyUnicode_GET_LENGTH(self); |
| 10809 | i = j = line_pos = 0; |
| 10810 | kind = PyUnicode_KIND(self); |
| 10811 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10812 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10813 | for (; i < src_len; i++) { |
| 10814 | ch = PyUnicode_READ(kind, src_data, i); |
| 10815 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10816 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10817 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10818 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10819 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10820 | goto overflow; |
| 10821 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10822 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10823 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10824 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10825 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10826 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10827 | goto overflow; |
| 10828 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10829 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10830 | if (ch == '\n' || ch == '\r') |
| 10831 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10832 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10833 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10834 | if (!found) |
| 10835 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10836 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10837 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10838 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10839 | if (!u) |
| 10840 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10841 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10842 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10843 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10844 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10845 | for (; i < src_len; i++) { |
| 10846 | ch = PyUnicode_READ(kind, src_data, i); |
| 10847 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10848 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10849 | incr = tabsize - (line_pos % tabsize); |
| 10850 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10851 | FILL(kind, dest_data, ' ', j, incr); |
| 10852 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10853 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10854 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10855 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10856 | line_pos++; |
| 10857 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10858 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10859 | if (ch == '\n' || ch == '\r') |
| 10860 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10861 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10862 | } |
| 10863 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10864 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10865 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10866 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10867 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10868 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10869 | } |
| 10870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10871 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10872 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10873 | \n\ |
| 10874 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10875 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10876 | arguments start and end are interpreted as in slice notation.\n\ |
| 10877 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10878 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10879 | |
| 10880 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10881 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10882 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10883 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10884 | Py_ssize_t start; |
| 10885 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10886 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10887 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10888 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10889 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10890 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10891 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10892 | if (PyUnicode_READY(self) == -1) |
| 10893 | return NULL; |
| 10894 | if (PyUnicode_READY(substring) == -1) |
| 10895 | return NULL; |
| 10896 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10897 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10898 | |
| 10899 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10900 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10901 | if (result == -2) |
| 10902 | return NULL; |
| 10903 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10904 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10905 | } |
| 10906 | |
| 10907 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10908 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10909 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10910 | void *data; |
| 10911 | enum PyUnicode_Kind kind; |
| 10912 | Py_UCS4 ch; |
| 10913 | PyObject *res; |
| 10914 | |
| 10915 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 10916 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10917 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10918 | } |
| 10919 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 10920 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10921 | return NULL; |
| 10922 | } |
| 10923 | kind = PyUnicode_KIND(self); |
| 10924 | data = PyUnicode_DATA(self); |
| 10925 | ch = PyUnicode_READ(kind, data, index); |
| 10926 | if (ch < 256) |
| 10927 | return get_latin1_char(ch); |
| 10928 | |
| 10929 | res = PyUnicode_New(1, ch); |
| 10930 | if (res == NULL) |
| 10931 | return NULL; |
| 10932 | kind = PyUnicode_KIND(res); |
| 10933 | data = PyUnicode_DATA(res); |
| 10934 | PyUnicode_WRITE(kind, data, 0, ch); |
| 10935 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 10936 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10937 | } |
| 10938 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10939 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10940 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10941 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10942 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10943 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10944 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 10945 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10946 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 10947 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 10948 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 10949 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10950 | if (_PyUnicode_HASH(self) != -1) |
| 10951 | return _PyUnicode_HASH(self); |
| 10952 | if (PyUnicode_READY(self) == -1) |
| 10953 | return -1; |
| 10954 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 10955 | /* |
| 10956 | We make the hash of the empty string be 0, rather than using |
| 10957 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 10958 | */ |
| 10959 | if (len == 0) { |
| 10960 | _PyUnicode_HASH(self) = 0; |
| 10961 | return 0; |
| 10962 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10963 | |
| 10964 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10965 | #define HASH(P) \ |
| 10966 | x ^= (Py_uhash_t) *P << 7; \ |
| 10967 | while (--len >= 0) \ |
| 10968 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10969 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10970 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10971 | switch (PyUnicode_KIND(self)) { |
| 10972 | case PyUnicode_1BYTE_KIND: { |
| 10973 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10974 | HASH(c); |
| 10975 | break; |
| 10976 | } |
| 10977 | case PyUnicode_2BYTE_KIND: { |
| 10978 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10979 | HASH(s); |
| 10980 | break; |
| 10981 | } |
| 10982 | default: { |
| 10983 | Py_UCS4 *l; |
| 10984 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10985 | "Impossible switch case in unicode_hash"); |
| 10986 | l = PyUnicode_4BYTE_DATA(self); |
| 10987 | HASH(l); |
| 10988 | break; |
| 10989 | } |
| 10990 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10991 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 10992 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10993 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10994 | if (x == -1) |
| 10995 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10996 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10997 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10999 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11001 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11002 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11004 | 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] | 11005 | |
| 11006 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11007 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11009 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11010 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11011 | Py_ssize_t start; |
| 11012 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11013 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11014 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11015 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11016 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11018 | if (PyUnicode_READY(self) == -1) |
| 11019 | return NULL; |
| 11020 | if (PyUnicode_READY(substring) == -1) |
| 11021 | return NULL; |
| 11022 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11023 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11024 | |
| 11025 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11027 | if (result == -2) |
| 11028 | return NULL; |
| 11029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11030 | if (result < 0) { |
| 11031 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11032 | return NULL; |
| 11033 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11034 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11035 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11036 | } |
| 11037 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11038 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11039 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11040 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11041 | 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] | 11042 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | |
| 11044 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11045 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11046 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11047 | Py_ssize_t i, length; |
| 11048 | int kind; |
| 11049 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11050 | int cased; |
| 11051 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11052 | if (PyUnicode_READY(self) == -1) |
| 11053 | return NULL; |
| 11054 | length = PyUnicode_GET_LENGTH(self); |
| 11055 | kind = PyUnicode_KIND(self); |
| 11056 | data = PyUnicode_DATA(self); |
| 11057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11058 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11059 | if (length == 1) |
| 11060 | return PyBool_FromLong( |
| 11061 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11062 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11063 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11064 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11065 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11067 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11068 | for (i = 0; i < length; i++) { |
| 11069 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11070 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11071 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11072 | return PyBool_FromLong(0); |
| 11073 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11074 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11075 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11076 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11077 | } |
| 11078 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11079 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11080 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11081 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11082 | 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] | 11083 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11084 | |
| 11085 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11086 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11087 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11088 | Py_ssize_t i, length; |
| 11089 | int kind; |
| 11090 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11091 | int cased; |
| 11092 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11093 | if (PyUnicode_READY(self) == -1) |
| 11094 | return NULL; |
| 11095 | length = PyUnicode_GET_LENGTH(self); |
| 11096 | kind = PyUnicode_KIND(self); |
| 11097 | data = PyUnicode_DATA(self); |
| 11098 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11099 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11100 | if (length == 1) |
| 11101 | return PyBool_FromLong( |
| 11102 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11103 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11104 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11105 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11106 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11107 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11108 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11109 | for (i = 0; i < length; i++) { |
| 11110 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11111 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11112 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11113 | return PyBool_FromLong(0); |
| 11114 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11115 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11116 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11117 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11118 | } |
| 11119 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11120 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11121 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11122 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11123 | Return True if S is a titlecased string and there is at least one\n\ |
| 11124 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11125 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11126 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11127 | |
| 11128 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11129 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11130 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11131 | Py_ssize_t i, length; |
| 11132 | int kind; |
| 11133 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11134 | int cased, previous_is_cased; |
| 11135 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11136 | if (PyUnicode_READY(self) == -1) |
| 11137 | return NULL; |
| 11138 | length = PyUnicode_GET_LENGTH(self); |
| 11139 | kind = PyUnicode_KIND(self); |
| 11140 | data = PyUnicode_DATA(self); |
| 11141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | if (length == 1) { |
| 11144 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11145 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11146 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11147 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11148 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11149 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11150 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11151 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11152 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11153 | cased = 0; |
| 11154 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11155 | for (i = 0; i < length; i++) { |
| 11156 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11157 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11158 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11159 | if (previous_is_cased) |
| 11160 | return PyBool_FromLong(0); |
| 11161 | previous_is_cased = 1; |
| 11162 | cased = 1; |
| 11163 | } |
| 11164 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11165 | if (!previous_is_cased) |
| 11166 | return PyBool_FromLong(0); |
| 11167 | previous_is_cased = 1; |
| 11168 | cased = 1; |
| 11169 | } |
| 11170 | else |
| 11171 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11173 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11174 | } |
| 11175 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11176 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11177 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11178 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11179 | Return True if all characters in S are whitespace\n\ |
| 11180 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11181 | |
| 11182 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11183 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11185 | Py_ssize_t i, length; |
| 11186 | int kind; |
| 11187 | void *data; |
| 11188 | |
| 11189 | if (PyUnicode_READY(self) == -1) |
| 11190 | return NULL; |
| 11191 | length = PyUnicode_GET_LENGTH(self); |
| 11192 | kind = PyUnicode_KIND(self); |
| 11193 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11194 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11195 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11196 | if (length == 1) |
| 11197 | return PyBool_FromLong( |
| 11198 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11200 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11201 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11202 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11203 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11204 | for (i = 0; i < length; i++) { |
| 11205 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11206 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11207 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11208 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11209 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | } |
| 11211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11212 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11213 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11214 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11215 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11216 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11217 | |
| 11218 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11219 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11220 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11221 | Py_ssize_t i, length; |
| 11222 | int kind; |
| 11223 | void *data; |
| 11224 | |
| 11225 | if (PyUnicode_READY(self) == -1) |
| 11226 | return NULL; |
| 11227 | length = PyUnicode_GET_LENGTH(self); |
| 11228 | kind = PyUnicode_KIND(self); |
| 11229 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11230 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11231 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11232 | if (length == 1) |
| 11233 | return PyBool_FromLong( |
| 11234 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11235 | |
| 11236 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11237 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11238 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11239 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11240 | for (i = 0; i < length; i++) { |
| 11241 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11242 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11243 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11244 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11245 | } |
| 11246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11247 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11248 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11249 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11250 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11251 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11252 | |
| 11253 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11254 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11255 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11256 | int kind; |
| 11257 | void *data; |
| 11258 | Py_ssize_t len, i; |
| 11259 | |
| 11260 | if (PyUnicode_READY(self) == -1) |
| 11261 | return NULL; |
| 11262 | |
| 11263 | kind = PyUnicode_KIND(self); |
| 11264 | data = PyUnicode_DATA(self); |
| 11265 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11266 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11267 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11268 | if (len == 1) { |
| 11269 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11270 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11271 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11272 | |
| 11273 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11274 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11275 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11276 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11277 | for (i = 0; i < len; i++) { |
| 11278 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11279 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11280 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11281 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11282 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11283 | } |
| 11284 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11285 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11286 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11287 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11288 | 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] | 11289 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11290 | |
| 11291 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11292 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11294 | Py_ssize_t i, length; |
| 11295 | int kind; |
| 11296 | void *data; |
| 11297 | |
| 11298 | if (PyUnicode_READY(self) == -1) |
| 11299 | return NULL; |
| 11300 | length = PyUnicode_GET_LENGTH(self); |
| 11301 | kind = PyUnicode_KIND(self); |
| 11302 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11304 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11305 | if (length == 1) |
| 11306 | return PyBool_FromLong( |
| 11307 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11308 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11309 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11310 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11311 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11313 | for (i = 0; i < length; i++) { |
| 11314 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11315 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11316 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11317 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11318 | } |
| 11319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11320 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11321 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11322 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11323 | Return True if all characters in S are digits\n\ |
| 11324 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11325 | |
| 11326 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11327 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11328 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11329 | Py_ssize_t i, length; |
| 11330 | int kind; |
| 11331 | void *data; |
| 11332 | |
| 11333 | if (PyUnicode_READY(self) == -1) |
| 11334 | return NULL; |
| 11335 | length = PyUnicode_GET_LENGTH(self); |
| 11336 | kind = PyUnicode_KIND(self); |
| 11337 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11340 | if (length == 1) { |
| 11341 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11342 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11343 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11344 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11345 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11346 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11347 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11348 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11349 | for (i = 0; i < length; i++) { |
| 11350 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11351 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11352 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11353 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11354 | } |
| 11355 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11356 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11357 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11358 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11359 | 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] | 11360 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11361 | |
| 11362 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11363 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11364 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11365 | Py_ssize_t i, length; |
| 11366 | int kind; |
| 11367 | void *data; |
| 11368 | |
| 11369 | if (PyUnicode_READY(self) == -1) |
| 11370 | return NULL; |
| 11371 | length = PyUnicode_GET_LENGTH(self); |
| 11372 | kind = PyUnicode_KIND(self); |
| 11373 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11376 | if (length == 1) |
| 11377 | return PyBool_FromLong( |
| 11378 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11379 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11380 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11381 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11382 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11383 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11384 | for (i = 0; i < length; i++) { |
| 11385 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11386 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11387 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11388 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11389 | } |
| 11390 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11391 | int |
| 11392 | PyUnicode_IsIdentifier(PyObject *self) |
| 11393 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11394 | int kind; |
| 11395 | void *data; |
| 11396 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11397 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11399 | if (PyUnicode_READY(self) == -1) { |
| 11400 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11401 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11402 | } |
| 11403 | |
| 11404 | /* Special case for empty strings */ |
| 11405 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11406 | return 0; |
| 11407 | kind = PyUnicode_KIND(self); |
| 11408 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11409 | |
| 11410 | /* PEP 3131 says that the first character must be in |
| 11411 | XID_Start and subsequent characters in XID_Continue, |
| 11412 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11413 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11414 | letters, digits, underscore). However, given the current |
| 11415 | definition of XID_Start and XID_Continue, it is sufficient |
| 11416 | to check just for these, except that _ must be allowed |
| 11417 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11418 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11419 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11420 | return 0; |
| 11421 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11422 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11423 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11424 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11425 | return 1; |
| 11426 | } |
| 11427 | |
| 11428 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11429 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11430 | \n\ |
| 11431 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11432 | to the language definition.\n\ |
| 11433 | \n\ |
| 11434 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11435 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11436 | |
| 11437 | static PyObject* |
| 11438 | unicode_isidentifier(PyObject *self) |
| 11439 | { |
| 11440 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11441 | } |
| 11442 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11443 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11444 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11445 | \n\ |
| 11446 | Return True if all characters in S are considered\n\ |
| 11447 | printable in repr() or S is empty, False otherwise."); |
| 11448 | |
| 11449 | static PyObject* |
| 11450 | unicode_isprintable(PyObject *self) |
| 11451 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11452 | Py_ssize_t i, length; |
| 11453 | int kind; |
| 11454 | void *data; |
| 11455 | |
| 11456 | if (PyUnicode_READY(self) == -1) |
| 11457 | return NULL; |
| 11458 | length = PyUnicode_GET_LENGTH(self); |
| 11459 | kind = PyUnicode_KIND(self); |
| 11460 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11461 | |
| 11462 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11463 | if (length == 1) |
| 11464 | return PyBool_FromLong( |
| 11465 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11466 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11467 | for (i = 0; i < length; i++) { |
| 11468 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11469 | Py_RETURN_FALSE; |
| 11470 | } |
| 11471 | } |
| 11472 | Py_RETURN_TRUE; |
| 11473 | } |
| 11474 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11475 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11476 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11477 | \n\ |
| 11478 | 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] | 11479 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | |
| 11481 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11482 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11484 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 | } |
| 11486 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11487 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11488 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11490 | if (PyUnicode_READY(self) == -1) |
| 11491 | return -1; |
| 11492 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | } |
| 11494 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11495 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11496 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11497 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11498 | 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] | 11499 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11500 | |
| 11501 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11502 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11503 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11504 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 | Py_UCS4 fillchar = ' '; |
| 11506 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11507 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11508 | return NULL; |
| 11509 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11510 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11511 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11513 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11514 | return unicode_result_unchanged(self); |
| 11515 | |
| 11516 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11517 | } |
| 11518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11519 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11520 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11521 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11522 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11523 | |
| 11524 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11525 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11526 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11527 | if (PyUnicode_READY(self) == -1) |
| 11528 | return NULL; |
| 11529 | if (PyUnicode_IS_ASCII(self)) |
| 11530 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11531 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11532 | } |
| 11533 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11534 | #define LEFTSTRIP 0 |
| 11535 | #define RIGHTSTRIP 1 |
| 11536 | #define BOTHSTRIP 2 |
| 11537 | |
| 11538 | /* Arrays indexed by above */ |
| 11539 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11540 | |
| 11541 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11542 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11543 | /* externally visible for str.strip(unicode) */ |
| 11544 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11545 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11546 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11547 | void *data; |
| 11548 | int kind; |
| 11549 | Py_ssize_t i, j, len; |
| 11550 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11552 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11553 | return NULL; |
| 11554 | |
| 11555 | kind = PyUnicode_KIND(self); |
| 11556 | data = PyUnicode_DATA(self); |
| 11557 | len = PyUnicode_GET_LENGTH(self); |
| 11558 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11559 | PyUnicode_DATA(sepobj), |
| 11560 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11561 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11562 | i = 0; |
| 11563 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11564 | while (i < len && |
| 11565 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11566 | i++; |
| 11567 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11568 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11569 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11570 | j = len; |
| 11571 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11572 | do { |
| 11573 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11574 | } while (j >= i && |
| 11575 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11576 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11577 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11578 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11579 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11580 | } |
| 11581 | |
| 11582 | PyObject* |
| 11583 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11584 | { |
| 11585 | unsigned char *data; |
| 11586 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11587 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11588 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11589 | if (PyUnicode_READY(self) == -1) |
| 11590 | return NULL; |
| 11591 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11592 | length = PyUnicode_GET_LENGTH(self); |
| 11593 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11594 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11595 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11596 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11597 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11598 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11599 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11600 | return NULL; |
| 11601 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 11602 | if (start >= length || end < start) |
| 11603 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11604 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11605 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11606 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11607 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11608 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11609 | } |
| 11610 | else { |
| 11611 | kind = PyUnicode_KIND(self); |
| 11612 | data = PyUnicode_1BYTE_DATA(self); |
| 11613 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11614 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11615 | length); |
| 11616 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11617 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11618 | |
| 11619 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11620 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11621 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11622 | int kind; |
| 11623 | void *data; |
| 11624 | Py_ssize_t len, i, j; |
| 11625 | |
| 11626 | if (PyUnicode_READY(self) == -1) |
| 11627 | return NULL; |
| 11628 | |
| 11629 | kind = PyUnicode_KIND(self); |
| 11630 | data = PyUnicode_DATA(self); |
| 11631 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11632 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11633 | i = 0; |
| 11634 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11635 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11636 | i++; |
| 11637 | } |
| 11638 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11639 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11640 | j = len; |
| 11641 | if (striptype != LEFTSTRIP) { |
| 11642 | do { |
| 11643 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11644 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11645 | j++; |
| 11646 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11647 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11648 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11649 | } |
| 11650 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11651 | |
| 11652 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11653 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
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 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11656 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11657 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11658 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11659 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11660 | if (sep != NULL && sep != Py_None) { |
| 11661 | if (PyUnicode_Check(sep)) |
| 11662 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11663 | else { |
| 11664 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11665 | "%s arg must be None or str", |
| 11666 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11667 | return NULL; |
| 11668 | } |
| 11669 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11670 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11671 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11672 | } |
| 11673 | |
| 11674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11675 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11676 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11677 | \n\ |
| 11678 | Return a copy of the string S with leading and trailing\n\ |
| 11679 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11680 | 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] | 11681 | |
| 11682 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11683 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11684 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11685 | if (PyTuple_GET_SIZE(args) == 0) |
| 11686 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11687 | else |
| 11688 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11689 | } |
| 11690 | |
| 11691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11692 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11693 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11694 | \n\ |
| 11695 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11696 | 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] | 11697 | |
| 11698 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11699 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11700 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11701 | if (PyTuple_GET_SIZE(args) == 0) |
| 11702 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11703 | else |
| 11704 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11705 | } |
| 11706 | |
| 11707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11708 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11709 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11710 | \n\ |
| 11711 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11712 | 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] | 11713 | |
| 11714 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11715 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11716 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11717 | if (PyTuple_GET_SIZE(args) == 0) |
| 11718 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11719 | else |
| 11720 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11721 | } |
| 11722 | |
| 11723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11724 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11725 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11726 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11727 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11728 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 11730 | if (len < 1) |
| 11731 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11732 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11733 | /* no repeat, return original string */ |
| 11734 | if (len == 1) |
| 11735 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11736 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11737 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11738 | return NULL; |
| 11739 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11740 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11741 | PyErr_SetString(PyExc_OverflowError, |
| 11742 | "repeated string is too long"); |
| 11743 | return NULL; |
| 11744 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11745 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11746 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11747 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11748 | if (!u) |
| 11749 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11750 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11752 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11753 | const int kind = PyUnicode_KIND(str); |
| 11754 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11755 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11756 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11757 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11758 | } |
| 11759 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11760 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11761 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11762 | ucs2[n] = fill_char; |
| 11763 | } else { |
| 11764 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11765 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11766 | for (n = 0; n < len; ++n) |
| 11767 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11768 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11769 | } |
| 11770 | else { |
| 11771 | /* number of characters copied this far */ |
| 11772 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11773 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11774 | char *to = (char *) PyUnicode_DATA(u); |
| 11775 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11776 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11777 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11778 | n = (done <= nchars-done) ? done : nchars-done; |
| 11779 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11780 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11781 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11782 | } |
| 11783 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11784 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11785 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11786 | } |
| 11787 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11788 | PyObject * |
| 11789 | PyUnicode_Replace(PyObject *obj, |
| 11790 | PyObject *subobj, |
| 11791 | PyObject *replobj, |
| 11792 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11793 | { |
| 11794 | PyObject *self; |
| 11795 | PyObject *str1; |
| 11796 | PyObject *str2; |
| 11797 | PyObject *result; |
| 11798 | |
| 11799 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11800 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11801 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11802 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11803 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11804 | Py_DECREF(self); |
| 11805 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 | } |
| 11807 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11808 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11809 | Py_DECREF(self); |
| 11810 | Py_DECREF(str1); |
| 11811 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11812 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11813 | if (PyUnicode_READY(self) == -1 || |
| 11814 | PyUnicode_READY(str1) == -1 || |
| 11815 | PyUnicode_READY(str2) == -1) |
| 11816 | result = NULL; |
| 11817 | else |
| 11818 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | Py_DECREF(self); |
| 11820 | Py_DECREF(str1); |
| 11821 | Py_DECREF(str2); |
| 11822 | return result; |
| 11823 | } |
| 11824 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11825 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11826 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11827 | \n\ |
| 11828 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11829 | old replaced by new. If the optional argument count is\n\ |
| 11830 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11831 | |
| 11832 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11833 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11834 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11835 | PyObject *str1; |
| 11836 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11837 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11838 | PyObject *result; |
| 11839 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11840 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11841 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11842 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11843 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11844 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11845 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11846 | return NULL; |
| 11847 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11848 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11849 | Py_DECREF(str1); |
| 11850 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11851 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11852 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11853 | result = NULL; |
| 11854 | else |
| 11855 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11856 | |
| 11857 | Py_DECREF(str1); |
| 11858 | Py_DECREF(str2); |
| 11859 | return result; |
| 11860 | } |
| 11861 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11862 | static PyObject * |
| 11863 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11864 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11865 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11866 | Py_ssize_t isize; |
| 11867 | Py_ssize_t osize, squote, dquote, i, o; |
| 11868 | Py_UCS4 max, quote; |
| 11869 | int ikind, okind; |
| 11870 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11871 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11872 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11873 | return NULL; |
| 11874 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11875 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11876 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11877 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11878 | /* Compute length of output, quote characters, and |
| 11879 | maximum character */ |
| 11880 | osize = 2; /* quotes */ |
| 11881 | max = 127; |
| 11882 | squote = dquote = 0; |
| 11883 | ikind = PyUnicode_KIND(unicode); |
| 11884 | for (i = 0; i < isize; i++) { |
| 11885 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11886 | switch (ch) { |
| 11887 | case '\'': squote++; osize++; break; |
| 11888 | case '"': dquote++; osize++; break; |
| 11889 | case '\\': case '\t': case '\r': case '\n': |
| 11890 | osize += 2; break; |
| 11891 | default: |
| 11892 | /* Fast-path ASCII */ |
| 11893 | if (ch < ' ' || ch == 0x7f) |
| 11894 | osize += 4; /* \xHH */ |
| 11895 | else if (ch < 0x7f) |
| 11896 | osize++; |
| 11897 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11898 | osize++; |
| 11899 | max = ch > max ? ch : max; |
| 11900 | } |
| 11901 | else if (ch < 0x100) |
| 11902 | osize += 4; /* \xHH */ |
| 11903 | else if (ch < 0x10000) |
| 11904 | osize += 6; /* \uHHHH */ |
| 11905 | else |
| 11906 | osize += 10; /* \uHHHHHHHH */ |
| 11907 | } |
| 11908 | } |
| 11909 | |
| 11910 | quote = '\''; |
| 11911 | if (squote) { |
| 11912 | if (dquote) |
| 11913 | /* Both squote and dquote present. Use squote, |
| 11914 | and escape them */ |
| 11915 | osize += squote; |
| 11916 | else |
| 11917 | quote = '"'; |
| 11918 | } |
| 11919 | |
| 11920 | repr = PyUnicode_New(osize, max); |
| 11921 | if (repr == NULL) |
| 11922 | return NULL; |
| 11923 | okind = PyUnicode_KIND(repr); |
| 11924 | odata = PyUnicode_DATA(repr); |
| 11925 | |
| 11926 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11927 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11928 | |
| 11929 | for (i = 0, o = 1; i < isize; i++) { |
| 11930 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11931 | |
| 11932 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11933 | if ((ch == quote) || (ch == '\\')) { |
| 11934 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11935 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11936 | continue; |
| 11937 | } |
| 11938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11939 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11940 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11941 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11942 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11943 | } |
| 11944 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11945 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11946 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11947 | } |
| 11948 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11949 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11950 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11951 | } |
| 11952 | |
| 11953 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11954 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11955 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11956 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11957 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 11958 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11959 | } |
| 11960 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11961 | /* Copy ASCII characters as-is */ |
| 11962 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11963 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11964 | } |
| 11965 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11966 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11967 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11968 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11969 | (categories Z* and C* except ASCII space) |
| 11970 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11971 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11972 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11973 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11974 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11975 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11976 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 11977 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11978 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11979 | /* Map 16-bit characters to '\uxxxx' */ |
| 11980 | else if (ch <= 0xffff) { |
| 11981 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11982 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 11983 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 11984 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 11985 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11986 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11987 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11988 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11989 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11990 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 11991 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 11992 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 11993 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11994 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 11995 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 11996 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 11997 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11998 | } |
| 11999 | } |
| 12000 | /* Copy characters as-is */ |
| 12001 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12002 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12003 | } |
| 12004 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12005 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12006 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12007 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12008 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12009 | } |
| 12010 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12011 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12012 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12013 | \n\ |
| 12014 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12015 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12016 | arguments start and end are interpreted as in slice notation.\n\ |
| 12017 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12018 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12019 | |
| 12020 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12021 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12022 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12023 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12024 | Py_ssize_t start; |
| 12025 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12026 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12027 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12028 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12029 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12030 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12031 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12032 | if (PyUnicode_READY(self) == -1) |
| 12033 | return NULL; |
| 12034 | if (PyUnicode_READY(substring) == -1) |
| 12035 | return NULL; |
| 12036 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12037 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12038 | |
| 12039 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12040 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | if (result == -2) |
| 12042 | return NULL; |
| 12043 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12044 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12045 | } |
| 12046 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12047 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12048 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12049 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12050 | 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] | 12051 | |
| 12052 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12053 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12054 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12055 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12056 | Py_ssize_t start; |
| 12057 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12058 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12059 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12060 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12061 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12062 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12063 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12064 | if (PyUnicode_READY(self) == -1) |
| 12065 | return NULL; |
| 12066 | if (PyUnicode_READY(substring) == -1) |
| 12067 | return NULL; |
| 12068 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12069 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12070 | |
| 12071 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12072 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12073 | if (result == -2) |
| 12074 | return NULL; |
| 12075 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12076 | if (result < 0) { |
| 12077 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12078 | return NULL; |
| 12079 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12080 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12081 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12082 | } |
| 12083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12084 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12085 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12086 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12087 | 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] | 12088 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12089 | |
| 12090 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12091 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12092 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12093 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12094 | Py_UCS4 fillchar = ' '; |
| 12095 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12096 | 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] | 12097 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12098 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12099 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12100 | return NULL; |
| 12101 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12102 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12103 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12104 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12105 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12106 | } |
| 12107 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12108 | PyObject * |
| 12109 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12110 | { |
| 12111 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12113 | s = PyUnicode_FromObject(s); |
| 12114 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12115 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12116 | if (sep != NULL) { |
| 12117 | sep = PyUnicode_FromObject(sep); |
| 12118 | if (sep == NULL) { |
| 12119 | Py_DECREF(s); |
| 12120 | return NULL; |
| 12121 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12122 | } |
| 12123 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12124 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12125 | |
| 12126 | Py_DECREF(s); |
| 12127 | Py_XDECREF(sep); |
| 12128 | return result; |
| 12129 | } |
| 12130 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12131 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12132 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12133 | \n\ |
| 12134 | Return a list of the words in S, using sep as the\n\ |
| 12135 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12136 | 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] | 12137 | whitespace string is a separator and empty strings are\n\ |
| 12138 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12139 | |
| 12140 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12141 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12142 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12143 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12144 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12145 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12146 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12147 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12148 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12149 | return NULL; |
| 12150 | |
| 12151 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12152 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12153 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12154 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12155 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12156 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12157 | } |
| 12158 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12159 | PyObject * |
| 12160 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12161 | { |
| 12162 | PyObject* str_obj; |
| 12163 | PyObject* sep_obj; |
| 12164 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12165 | int kind1, kind2, kind; |
| 12166 | void *buf1 = NULL, *buf2 = NULL; |
| 12167 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12168 | |
| 12169 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12170 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12171 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12172 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12173 | if (!sep_obj) { |
| 12174 | Py_DECREF(str_obj); |
| 12175 | return NULL; |
| 12176 | } |
| 12177 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12178 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12179 | Py_DECREF(str_obj); |
| 12180 | return NULL; |
| 12181 | } |
| 12182 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12183 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12184 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12185 | kind = Py_MAX(kind1, kind2); |
| 12186 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12187 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12188 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12189 | if (!buf1) |
| 12190 | goto onError; |
| 12191 | buf2 = PyUnicode_DATA(sep_obj); |
| 12192 | if (kind2 != kind) |
| 12193 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12194 | if (!buf2) |
| 12195 | goto onError; |
| 12196 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12197 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12198 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12199 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12200 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12201 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12202 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12203 | else |
| 12204 | 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] | 12205 | break; |
| 12206 | case PyUnicode_2BYTE_KIND: |
| 12207 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12208 | break; |
| 12209 | case PyUnicode_4BYTE_KIND: |
| 12210 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12211 | break; |
| 12212 | default: |
| 12213 | assert(0); |
| 12214 | out = 0; |
| 12215 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12216 | |
| 12217 | Py_DECREF(sep_obj); |
| 12218 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12219 | if (kind1 != kind) |
| 12220 | PyMem_Free(buf1); |
| 12221 | if (kind2 != kind) |
| 12222 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12223 | |
| 12224 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12225 | onError: |
| 12226 | Py_DECREF(sep_obj); |
| 12227 | Py_DECREF(str_obj); |
| 12228 | if (kind1 != kind && buf1) |
| 12229 | PyMem_Free(buf1); |
| 12230 | if (kind2 != kind && buf2) |
| 12231 | PyMem_Free(buf2); |
| 12232 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12233 | } |
| 12234 | |
| 12235 | |
| 12236 | PyObject * |
| 12237 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12238 | { |
| 12239 | PyObject* str_obj; |
| 12240 | PyObject* sep_obj; |
| 12241 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12242 | int kind1, kind2, kind; |
| 12243 | void *buf1 = NULL, *buf2 = NULL; |
| 12244 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12245 | |
| 12246 | str_obj = PyUnicode_FromObject(str_in); |
| 12247 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12248 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12249 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12250 | if (!sep_obj) { |
| 12251 | Py_DECREF(str_obj); |
| 12252 | return NULL; |
| 12253 | } |
| 12254 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12255 | kind1 = PyUnicode_KIND(str_in); |
| 12256 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12257 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12258 | buf1 = PyUnicode_DATA(str_in); |
| 12259 | if (kind1 != kind) |
| 12260 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12261 | if (!buf1) |
| 12262 | goto onError; |
| 12263 | buf2 = PyUnicode_DATA(sep_obj); |
| 12264 | if (kind2 != kind) |
| 12265 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12266 | if (!buf2) |
| 12267 | goto onError; |
| 12268 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12269 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12270 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12271 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12272 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12273 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12274 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12275 | else |
| 12276 | 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] | 12277 | break; |
| 12278 | case PyUnicode_2BYTE_KIND: |
| 12279 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12280 | break; |
| 12281 | case PyUnicode_4BYTE_KIND: |
| 12282 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12283 | break; |
| 12284 | default: |
| 12285 | assert(0); |
| 12286 | out = 0; |
| 12287 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12288 | |
| 12289 | Py_DECREF(sep_obj); |
| 12290 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12291 | if (kind1 != kind) |
| 12292 | PyMem_Free(buf1); |
| 12293 | if (kind2 != kind) |
| 12294 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12295 | |
| 12296 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12297 | onError: |
| 12298 | Py_DECREF(sep_obj); |
| 12299 | Py_DECREF(str_obj); |
| 12300 | if (kind1 != kind && buf1) |
| 12301 | PyMem_Free(buf1); |
| 12302 | if (kind2 != kind && buf2) |
| 12303 | PyMem_Free(buf2); |
| 12304 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12305 | } |
| 12306 | |
| 12307 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12308 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12309 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12310 | 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] | 12311 | 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] | 12312 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12313 | |
| 12314 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12315 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12316 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12317 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12318 | } |
| 12319 | |
| 12320 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12321 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12322 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12323 | 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] | 12324 | 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] | 12325 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12326 | |
| 12327 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12328 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12329 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12330 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12331 | } |
| 12332 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12333 | PyObject * |
| 12334 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12335 | { |
| 12336 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12337 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12338 | s = PyUnicode_FromObject(s); |
| 12339 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12340 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12341 | if (sep != NULL) { |
| 12342 | sep = PyUnicode_FromObject(sep); |
| 12343 | if (sep == NULL) { |
| 12344 | Py_DECREF(s); |
| 12345 | return NULL; |
| 12346 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12347 | } |
| 12348 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12349 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12350 | |
| 12351 | Py_DECREF(s); |
| 12352 | Py_XDECREF(sep); |
| 12353 | return result; |
| 12354 | } |
| 12355 | |
| 12356 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12357 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12358 | \n\ |
| 12359 | Return a list of the words in S, using sep as the\n\ |
| 12360 | delimiter string, starting at the end of the string and\n\ |
| 12361 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12362 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12363 | is a separator."); |
| 12364 | |
| 12365 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12366 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12367 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12368 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12369 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12370 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12371 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12372 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12373 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12374 | return NULL; |
| 12375 | |
| 12376 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12377 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12378 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12379 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12380 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12381 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12382 | } |
| 12383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12384 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12385 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | \n\ |
| 12387 | 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] | 12388 | 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] | 12389 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12390 | |
| 12391 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12392 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12393 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12394 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12395 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12396 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12397 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12398 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12399 | return NULL; |
| 12400 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12401 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12402 | } |
| 12403 | |
| 12404 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12405 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12406 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12407 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12408 | } |
| 12409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12410 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12411 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12412 | \n\ |
| 12413 | 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] | 12414 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12415 | |
| 12416 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12417 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12418 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12419 | if (PyUnicode_READY(self) == -1) |
| 12420 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12421 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12422 | } |
| 12423 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12424 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12425 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12426 | \n\ |
| 12427 | Return a translation table usable for str.translate().\n\ |
| 12428 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12429 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12430 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12431 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12432 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12433 | character at the same position in y. If there is a third argument, it\n\ |
| 12434 | must be a string, whose characters will be mapped to None in the result."); |
| 12435 | |
| 12436 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12437 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12438 | { |
| 12439 | PyObject *x, *y = NULL, *z = NULL; |
| 12440 | PyObject *new = NULL, *key, *value; |
| 12441 | Py_ssize_t i = 0; |
| 12442 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12443 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12444 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12445 | return NULL; |
| 12446 | new = PyDict_New(); |
| 12447 | if (!new) |
| 12448 | return NULL; |
| 12449 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12450 | int x_kind, y_kind, z_kind; |
| 12451 | void *x_data, *y_data, *z_data; |
| 12452 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12453 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12454 | if (!PyUnicode_Check(x)) { |
| 12455 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12456 | "be a string if there is a second argument"); |
| 12457 | goto err; |
| 12458 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12459 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12460 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12461 | "arguments must have equal length"); |
| 12462 | goto err; |
| 12463 | } |
| 12464 | /* 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] | 12465 | x_kind = PyUnicode_KIND(x); |
| 12466 | y_kind = PyUnicode_KIND(y); |
| 12467 | x_data = PyUnicode_DATA(x); |
| 12468 | y_data = PyUnicode_DATA(y); |
| 12469 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12470 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12471 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12472 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12473 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12474 | if (!value) { |
| 12475 | Py_DECREF(key); |
| 12476 | goto err; |
| 12477 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12478 | res = PyDict_SetItem(new, key, value); |
| 12479 | Py_DECREF(key); |
| 12480 | Py_DECREF(value); |
| 12481 | if (res < 0) |
| 12482 | goto err; |
| 12483 | } |
| 12484 | /* create entries for deleting chars in z */ |
| 12485 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12486 | z_kind = PyUnicode_KIND(z); |
| 12487 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12488 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12489 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12490 | if (!key) |
| 12491 | goto err; |
| 12492 | res = PyDict_SetItem(new, key, Py_None); |
| 12493 | Py_DECREF(key); |
| 12494 | if (res < 0) |
| 12495 | goto err; |
| 12496 | } |
| 12497 | } |
| 12498 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12499 | int kind; |
| 12500 | void *data; |
| 12501 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12502 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12503 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12504 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12505 | "to maketrans it must be a dict"); |
| 12506 | goto err; |
| 12507 | } |
| 12508 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12509 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12510 | if (PyUnicode_Check(key)) { |
| 12511 | /* convert string keys to integer keys */ |
| 12512 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12513 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12514 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12515 | "table must be of length 1"); |
| 12516 | goto err; |
| 12517 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12518 | kind = PyUnicode_KIND(key); |
| 12519 | data = PyUnicode_DATA(key); |
| 12520 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12521 | if (!newkey) |
| 12522 | goto err; |
| 12523 | res = PyDict_SetItem(new, newkey, value); |
| 12524 | Py_DECREF(newkey); |
| 12525 | if (res < 0) |
| 12526 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12527 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12528 | /* just keep integer keys */ |
| 12529 | if (PyDict_SetItem(new, key, value) < 0) |
| 12530 | goto err; |
| 12531 | } else { |
| 12532 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12533 | "be strings or integers"); |
| 12534 | goto err; |
| 12535 | } |
| 12536 | } |
| 12537 | } |
| 12538 | return new; |
| 12539 | err: |
| 12540 | Py_DECREF(new); |
| 12541 | return NULL; |
| 12542 | } |
| 12543 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12544 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12545 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12546 | \n\ |
| 12547 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12548 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12549 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12550 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12551 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12552 | |
| 12553 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12554 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12555 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12556 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12557 | } |
| 12558 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12559 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12560 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12561 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12562 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12563 | |
| 12564 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12565 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12566 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12567 | if (PyUnicode_READY(self) == -1) |
| 12568 | return NULL; |
| 12569 | if (PyUnicode_IS_ASCII(self)) |
| 12570 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12571 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12572 | } |
| 12573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12574 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12575 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12576 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12577 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12578 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12579 | |
| 12580 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12581 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12582 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12583 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12584 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12585 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12586 | int kind; |
| 12587 | void *data; |
| 12588 | Py_UCS4 chr; |
| 12589 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12590 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12591 | return NULL; |
| 12592 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12593 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12594 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12595 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12596 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12597 | return unicode_result_unchanged(self); |
| 12598 | |
| 12599 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12600 | |
| 12601 | u = pad(self, fill, 0, '0'); |
| 12602 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12603 | if (u == NULL) |
| 12604 | return NULL; |
| 12605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12606 | kind = PyUnicode_KIND(u); |
| 12607 | data = PyUnicode_DATA(u); |
| 12608 | chr = PyUnicode_READ(kind, data, fill); |
| 12609 | |
| 12610 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12611 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12612 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12613 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12614 | } |
| 12615 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12616 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12617 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12618 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12619 | |
| 12620 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12621 | static PyObject * |
| 12622 | unicode__decimal2ascii(PyObject *self) |
| 12623 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12624 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12625 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12626 | #endif |
| 12627 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12628 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12629 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12630 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12631 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12632 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12633 | With optional end, stop comparing S at that position.\n\ |
| 12634 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12635 | |
| 12636 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12637 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12638 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12639 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12640 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12641 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12642 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12643 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12644 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12645 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12646 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12647 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12648 | if (PyTuple_Check(subobj)) { |
| 12649 | Py_ssize_t i; |
| 12650 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12651 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12652 | if (substring == NULL) |
| 12653 | return NULL; |
| 12654 | result = tailmatch(self, substring, start, end, -1); |
| 12655 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12656 | if (result == -1) |
| 12657 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12658 | if (result) { |
| 12659 | Py_RETURN_TRUE; |
| 12660 | } |
| 12661 | } |
| 12662 | /* nothing matched */ |
| 12663 | Py_RETURN_FALSE; |
| 12664 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12665 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12666 | if (substring == NULL) { |
| 12667 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12668 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12669 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12670 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12671 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12672 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12673 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12674 | if (result == -1) |
| 12675 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12676 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12677 | } |
| 12678 | |
| 12679 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12680 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12681 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12682 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12683 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12684 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12685 | With optional end, stop comparing S at that position.\n\ |
| 12686 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12687 | |
| 12688 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12689 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12690 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12691 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12692 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12693 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12694 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12695 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12696 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12697 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12698 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12699 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12700 | if (PyTuple_Check(subobj)) { |
| 12701 | Py_ssize_t i; |
| 12702 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12703 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12704 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12705 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12706 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12707 | result = tailmatch(self, substring, start, end, +1); |
| 12708 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12709 | if (result == -1) |
| 12710 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12711 | if (result) { |
| 12712 | Py_RETURN_TRUE; |
| 12713 | } |
| 12714 | } |
| 12715 | Py_RETURN_FALSE; |
| 12716 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12717 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12718 | if (substring == NULL) { |
| 12719 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12720 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12721 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12722 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12723 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12724 | result = tailmatch(self, substring, start, end, +1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12725 | if (result == -1) |
| 12726 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12727 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12728 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12729 | } |
| 12730 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12731 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12732 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12733 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12734 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12735 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12736 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12737 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12738 | } |
| 12739 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12740 | void |
| 12741 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12742 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12743 | memset(writer, 0, sizeof(*writer)); |
| 12744 | #ifdef Py_DEBUG |
| 12745 | writer->kind = 5; /* invalid kind */ |
| 12746 | #endif |
| 12747 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12748 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12749 | } |
| 12750 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12751 | int |
| 12752 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12753 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12754 | { |
| 12755 | Py_ssize_t newlen; |
| 12756 | PyObject *newbuffer; |
| 12757 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12758 | assert(length > 0); |
| 12759 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12760 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12761 | PyErr_NoMemory(); |
| 12762 | return -1; |
| 12763 | } |
| 12764 | newlen = writer->pos + length; |
| 12765 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12766 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12767 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12768 | /* overallocate 25% to limit the number of resize */ |
| 12769 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12770 | newlen += newlen / 4; |
| 12771 | if (newlen < writer->min_length) |
| 12772 | newlen = writer->min_length; |
| 12773 | } |
| 12774 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12775 | if (writer->buffer == NULL) |
| 12776 | return -1; |
| 12777 | _PyUnicodeWriter_Update(writer); |
| 12778 | return 0; |
| 12779 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12780 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12781 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12782 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12783 | /* overallocate 25% to limit the number of resize */ |
| 12784 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12785 | newlen += newlen / 4; |
| 12786 | if (newlen < writer->min_length) |
| 12787 | newlen = writer->min_length; |
| 12788 | } |
| 12789 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12790 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12791 | /* resize + widen */ |
| 12792 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12793 | if (newbuffer == NULL) |
| 12794 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12795 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12796 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12797 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12798 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12799 | } |
| 12800 | else { |
| 12801 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12802 | if (newbuffer == NULL) |
| 12803 | return -1; |
| 12804 | } |
| 12805 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12806 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12807 | } |
| 12808 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12809 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12810 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12811 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12812 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12813 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12814 | writer->buffer, 0, writer->pos); |
| 12815 | Py_DECREF(writer->buffer); |
| 12816 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12817 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12818 | } |
| 12819 | return 0; |
| 12820 | } |
| 12821 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12822 | int |
| 12823 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12824 | { |
| 12825 | Py_UCS4 maxchar; |
| 12826 | Py_ssize_t len; |
| 12827 | |
| 12828 | if (PyUnicode_READY(str) == -1) |
| 12829 | return -1; |
| 12830 | len = PyUnicode_GET_LENGTH(str); |
| 12831 | if (len == 0) |
| 12832 | return 0; |
| 12833 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12834 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12835 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12836 | Py_INCREF(str); |
| 12837 | writer->buffer = str; |
| 12838 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12839 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12840 | writer->size = 0; |
| 12841 | writer->pos += len; |
| 12842 | return 0; |
| 12843 | } |
| 12844 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12845 | return -1; |
| 12846 | } |
| 12847 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12848 | str, 0, len); |
| 12849 | writer->pos += len; |
| 12850 | return 0; |
| 12851 | } |
| 12852 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 12853 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 12854 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 12855 | Py_ssize_t start, Py_ssize_t end) |
| 12856 | { |
| 12857 | Py_UCS4 maxchar; |
| 12858 | Py_ssize_t len; |
| 12859 | |
| 12860 | if (PyUnicode_READY(str) == -1) |
| 12861 | return -1; |
| 12862 | |
| 12863 | assert(0 <= start); |
| 12864 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 12865 | assert(start <= end); |
| 12866 | |
| 12867 | if (end == 0) |
| 12868 | return 0; |
| 12869 | |
| 12870 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 12871 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 12872 | |
| 12873 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 12874 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 12875 | else |
| 12876 | maxchar = writer->maxchar; |
| 12877 | len = end - start; |
| 12878 | |
| 12879 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 12880 | return -1; |
| 12881 | |
| 12882 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12883 | str, start, len); |
| 12884 | writer->pos += len; |
| 12885 | return 0; |
| 12886 | } |
| 12887 | |
| 12888 | int |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 12889 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) |
| 12890 | { |
| 12891 | Py_UCS4 maxchar; |
| 12892 | |
| 12893 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 12894 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 12895 | return -1; |
| 12896 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 12897 | writer->pos += len; |
| 12898 | return 0; |
| 12899 | } |
| 12900 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12901 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12902 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12903 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12904 | if (writer->pos == 0) { |
| 12905 | Py_XDECREF(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12906 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12907 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12908 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12909 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 12910 | return writer->buffer; |
| 12911 | } |
| 12912 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 12913 | PyObject *newbuffer; |
| 12914 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 12915 | if (newbuffer == NULL) { |
| 12916 | Py_DECREF(writer->buffer); |
| 12917 | return NULL; |
| 12918 | } |
| 12919 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12920 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 12921 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 2cb16aa | 2013-03-06 19:28:37 +0100 | [diff] [blame] | 12922 | return unicode_result_ready(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12923 | } |
| 12924 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12925 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12926 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12927 | { |
| 12928 | Py_CLEAR(writer->buffer); |
| 12929 | } |
| 12930 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12931 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12932 | |
| 12933 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12934 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12935 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12936 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12937 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12938 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12939 | PyDoc_STRVAR(format_map__doc__, |
| 12940 | "S.format_map(mapping) -> str\n\ |
| 12941 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12942 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12943 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12944 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12945 | static PyObject * |
| 12946 | unicode__format__(PyObject* self, PyObject* args) |
| 12947 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12948 | PyObject *format_spec; |
| 12949 | _PyUnicodeWriter writer; |
| 12950 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12951 | |
| 12952 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12953 | return NULL; |
| 12954 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12955 | if (PyUnicode_READY(self) == -1) |
| 12956 | return NULL; |
| 12957 | _PyUnicodeWriter_Init(&writer, 0); |
| 12958 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 12959 | self, format_spec, 0, |
| 12960 | PyUnicode_GET_LENGTH(format_spec)); |
| 12961 | if (ret == -1) { |
| 12962 | _PyUnicodeWriter_Dealloc(&writer); |
| 12963 | return NULL; |
| 12964 | } |
| 12965 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12966 | } |
| 12967 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12968 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12969 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12970 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12971 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12972 | |
| 12973 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12974 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12975 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12976 | Py_ssize_t size; |
| 12977 | |
| 12978 | /* If it's a compact object, account for base structure + |
| 12979 | character data. */ |
| 12980 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12981 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12982 | else if (PyUnicode_IS_COMPACT(v)) |
| 12983 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12984 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12985 | else { |
| 12986 | /* If it is a two-block object, account for base object, and |
| 12987 | for character block if present. */ |
| 12988 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12989 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12990 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12991 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12992 | } |
| 12993 | /* 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] | 12994 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12995 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12996 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12997 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12998 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12999 | |
| 13000 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13001 | } |
| 13002 | |
| 13003 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13004 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13005 | |
| 13006 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13007 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13008 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13009 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13010 | if (!copy) |
| 13011 | return NULL; |
| 13012 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13013 | } |
| 13014 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13015 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13016 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13017 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13018 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13019 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13020 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13021 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13022 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13023 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13024 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13025 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 13026 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 13027 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13028 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13029 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13030 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13031 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13032 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13033 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13034 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13035 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13036 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13037 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13038 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13039 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13040 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13041 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13042 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13043 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13044 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13045 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13046 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13047 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13048 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13049 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13050 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13051 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13052 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13053 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13054 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13055 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13056 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13057 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13058 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13059 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13060 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 13061 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13062 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13063 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13064 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13065 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13066 | #endif |
| 13067 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13068 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13069 | {NULL, NULL} |
| 13070 | }; |
| 13071 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13072 | static PyObject * |
| 13073 | unicode_mod(PyObject *v, PyObject *w) |
| 13074 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13075 | if (!PyUnicode_Check(v)) |
| 13076 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13077 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13078 | } |
| 13079 | |
| 13080 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13081 | 0, /*nb_add*/ |
| 13082 | 0, /*nb_subtract*/ |
| 13083 | 0, /*nb_multiply*/ |
| 13084 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13085 | }; |
| 13086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13087 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13088 | (lenfunc) unicode_length, /* sq_length */ |
| 13089 | PyUnicode_Concat, /* sq_concat */ |
| 13090 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13091 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13092 | 0, /* sq_slice */ |
| 13093 | 0, /* sq_ass_item */ |
| 13094 | 0, /* sq_ass_slice */ |
| 13095 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13096 | }; |
| 13097 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13098 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13099 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13100 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13101 | if (PyUnicode_READY(self) == -1) |
| 13102 | return NULL; |
| 13103 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13104 | if (PyIndex_Check(item)) { |
| 13105 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13106 | if (i == -1 && PyErr_Occurred()) |
| 13107 | return NULL; |
| 13108 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13109 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13110 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13111 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13112 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13113 | PyObject *result; |
| 13114 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13115 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13116 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13117 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13118 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13119 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13120 | return NULL; |
| 13121 | } |
| 13122 | |
| 13123 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13124 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13125 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13126 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13127 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13128 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13129 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13130 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13131 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13132 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13133 | src_kind = PyUnicode_KIND(self); |
| 13134 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13135 | if (!PyUnicode_IS_ASCII(self)) { |
| 13136 | kind_limit = kind_maxchar_limit(src_kind); |
| 13137 | max_char = 0; |
| 13138 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13139 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13140 | if (ch > max_char) { |
| 13141 | max_char = ch; |
| 13142 | if (max_char >= kind_limit) |
| 13143 | break; |
| 13144 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13145 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13146 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13147 | else |
| 13148 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13149 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13150 | if (result == NULL) |
| 13151 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13152 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13153 | dest_data = PyUnicode_DATA(result); |
| 13154 | |
| 13155 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13156 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13157 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13158 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13159 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13160 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13161 | } else { |
| 13162 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13163 | return NULL; |
| 13164 | } |
| 13165 | } |
| 13166 | |
| 13167 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13168 | (lenfunc)unicode_length, /* mp_length */ |
| 13169 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13170 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13171 | }; |
| 13172 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13173 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13174 | /* Helpers for PyUnicode_Format() */ |
| 13175 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13176 | struct unicode_formatter_t { |
| 13177 | PyObject *args; |
| 13178 | int args_owned; |
| 13179 | Py_ssize_t arglen, argidx; |
| 13180 | PyObject *dict; |
| 13181 | |
| 13182 | enum PyUnicode_Kind fmtkind; |
| 13183 | Py_ssize_t fmtcnt, fmtpos; |
| 13184 | void *fmtdata; |
| 13185 | PyObject *fmtstr; |
| 13186 | |
| 13187 | _PyUnicodeWriter writer; |
| 13188 | }; |
| 13189 | |
| 13190 | struct unicode_format_arg_t { |
| 13191 | Py_UCS4 ch; |
| 13192 | int flags; |
| 13193 | Py_ssize_t width; |
| 13194 | int prec; |
| 13195 | int sign; |
| 13196 | }; |
| 13197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13198 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13199 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13200 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13201 | Py_ssize_t argidx = ctx->argidx; |
| 13202 | |
| 13203 | if (argidx < ctx->arglen) { |
| 13204 | ctx->argidx++; |
| 13205 | if (ctx->arglen < 0) |
| 13206 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13207 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13208 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13209 | } |
| 13210 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13211 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13212 | return NULL; |
| 13213 | } |
| 13214 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13215 | /* 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] | 13216 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13217 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13218 | otherwise. |
| 13219 | |
| 13220 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13221 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13222 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13223 | PyObject **p_output, |
| 13224 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13225 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13226 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13227 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13228 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13229 | int prec; |
| 13230 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13232 | x = PyFloat_AsDouble(v); |
| 13233 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13234 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13235 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13236 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13237 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13238 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13239 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13240 | if (arg->flags & F_ALT) |
| 13241 | dtoa_flags = Py_DTSF_ALT; |
| 13242 | else |
| 13243 | dtoa_flags = 0; |
| 13244 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13245 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13246 | return -1; |
| 13247 | len = strlen(p); |
| 13248 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13249 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13250 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13251 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13252 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13253 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13254 | writer->pos += len; |
| 13255 | } |
| 13256 | else |
| 13257 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13258 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13259 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13260 | } |
| 13261 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13262 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13263 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13264 | * Python's regular ints. |
| 13265 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13266 | * The output string is of the form |
| 13267 | * "-"? ("0x" | "0X")? digit+ |
| 13268 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13269 | * set in flags. The case of hex digits will be correct, |
| 13270 | * There will be at least prec digits, zero-filled on the left if |
| 13271 | * necessary to get that many. |
| 13272 | * val object to be converted |
| 13273 | * flags bitmask of format flags; only F_ALT is looked at |
| 13274 | * prec minimum number of digits; 0-fill on left if needed |
| 13275 | * type a character in [duoxX]; u acts the same as d |
| 13276 | * |
| 13277 | * CAUTION: o, x and X conversions on regular ints can never |
| 13278 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13279 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13280 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13281 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13282 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13283 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13284 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13285 | Py_ssize_t i; |
| 13286 | int sign; /* 1 if '-', else 0 */ |
| 13287 | int len; /* number of characters */ |
| 13288 | Py_ssize_t llen; |
| 13289 | int numdigits; /* len == numnondigits + numdigits */ |
| 13290 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13291 | int prec = arg->prec; |
| 13292 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13293 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13294 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13295 | if (prec > INT_MAX-3) { |
| 13296 | PyErr_SetString(PyExc_OverflowError, |
| 13297 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13298 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13299 | } |
| 13300 | |
| 13301 | assert(PyLong_Check(val)); |
| 13302 | |
| 13303 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13304 | default: |
| 13305 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13306 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13307 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13308 | case 'u': |
| 13309 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13310 | if (PyBool_Check(val)) |
| 13311 | result = PyNumber_ToBase(val, 10); |
| 13312 | else |
| 13313 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13314 | break; |
| 13315 | case 'o': |
| 13316 | numnondigits = 2; |
| 13317 | result = PyNumber_ToBase(val, 8); |
| 13318 | break; |
| 13319 | case 'x': |
| 13320 | case 'X': |
| 13321 | numnondigits = 2; |
| 13322 | result = PyNumber_ToBase(val, 16); |
| 13323 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13324 | } |
| 13325 | if (!result) |
| 13326 | return NULL; |
| 13327 | |
| 13328 | assert(unicode_modifiable(result)); |
| 13329 | assert(PyUnicode_IS_READY(result)); |
| 13330 | assert(PyUnicode_IS_ASCII(result)); |
| 13331 | |
| 13332 | /* To modify the string in-place, there can only be one reference. */ |
| 13333 | if (Py_REFCNT(result) != 1) { |
| 13334 | PyErr_BadInternalCall(); |
| 13335 | return NULL; |
| 13336 | } |
| 13337 | buf = PyUnicode_DATA(result); |
| 13338 | llen = PyUnicode_GET_LENGTH(result); |
| 13339 | if (llen > INT_MAX) { |
| 13340 | PyErr_SetString(PyExc_ValueError, |
| 13341 | "string too large in _PyBytes_FormatLong"); |
| 13342 | return NULL; |
| 13343 | } |
| 13344 | len = (int)llen; |
| 13345 | sign = buf[0] == '-'; |
| 13346 | numnondigits += sign; |
| 13347 | numdigits = len - numnondigits; |
| 13348 | assert(numdigits > 0); |
| 13349 | |
| 13350 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13351 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13352 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13353 | assert(buf[sign] == '0'); |
| 13354 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13355 | buf[sign+1] == 'o'); |
| 13356 | numnondigits -= 2; |
| 13357 | buf += 2; |
| 13358 | len -= 2; |
| 13359 | if (sign) |
| 13360 | buf[0] = '-'; |
| 13361 | assert(len == numnondigits + numdigits); |
| 13362 | assert(numdigits > 0); |
| 13363 | } |
| 13364 | |
| 13365 | /* Fill with leading zeroes to meet minimum width. */ |
| 13366 | if (prec > numdigits) { |
| 13367 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13368 | numnondigits + prec); |
| 13369 | char *b1; |
| 13370 | if (!r1) { |
| 13371 | Py_DECREF(result); |
| 13372 | return NULL; |
| 13373 | } |
| 13374 | b1 = PyBytes_AS_STRING(r1); |
| 13375 | for (i = 0; i < numnondigits; ++i) |
| 13376 | *b1++ = *buf++; |
| 13377 | for (i = 0; i < prec - numdigits; i++) |
| 13378 | *b1++ = '0'; |
| 13379 | for (i = 0; i < numdigits; i++) |
| 13380 | *b1++ = *buf++; |
| 13381 | *b1 = '\0'; |
| 13382 | Py_DECREF(result); |
| 13383 | result = r1; |
| 13384 | buf = PyBytes_AS_STRING(result); |
| 13385 | len = numnondigits + prec; |
| 13386 | } |
| 13387 | |
| 13388 | /* Fix up case for hex conversions. */ |
| 13389 | if (type == 'X') { |
| 13390 | /* Need to convert all lower case letters to upper case. |
| 13391 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13392 | for (i = 0; i < len; i++) |
| 13393 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13394 | buf[i] -= 'a'-'A'; |
| 13395 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13396 | if (!PyUnicode_Check(result) |
| 13397 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13398 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13399 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13400 | Py_DECREF(result); |
| 13401 | result = unicode; |
| 13402 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13403 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13404 | if (PyUnicode_Resize(&result, len) < 0) |
| 13405 | Py_CLEAR(result); |
| 13406 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13407 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13408 | } |
| 13409 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13410 | /* Format an integer. |
| 13411 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13412 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13413 | * -1 and raise an exception on error */ |
| 13414 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13415 | mainformatlong(PyObject *v, |
| 13416 | struct unicode_format_arg_t *arg, |
| 13417 | PyObject **p_output, |
| 13418 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13419 | { |
| 13420 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13421 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13422 | |
| 13423 | if (!PyNumber_Check(v)) |
| 13424 | goto wrongtype; |
| 13425 | |
| 13426 | if (!PyLong_Check(v)) { |
| 13427 | iobj = PyNumber_Long(v); |
| 13428 | if (iobj == NULL) { |
| 13429 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13430 | goto wrongtype; |
| 13431 | return -1; |
| 13432 | } |
| 13433 | assert(PyLong_Check(iobj)); |
| 13434 | } |
| 13435 | else { |
| 13436 | iobj = v; |
| 13437 | Py_INCREF(iobj); |
| 13438 | } |
| 13439 | |
| 13440 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13441 | && arg->width == -1 && arg->prec == -1 |
| 13442 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 13443 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13444 | { |
| 13445 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13446 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13447 | int base; |
| 13448 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13449 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13450 | { |
| 13451 | default: |
| 13452 | assert(0 && "'type' not in [diuoxX]"); |
| 13453 | case 'd': |
| 13454 | case 'i': |
| 13455 | case 'u': |
| 13456 | base = 10; |
| 13457 | break; |
| 13458 | case 'o': |
| 13459 | base = 8; |
| 13460 | break; |
| 13461 | case 'x': |
| 13462 | case 'X': |
| 13463 | base = 16; |
| 13464 | break; |
| 13465 | } |
| 13466 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13467 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 13468 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13469 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13470 | } |
| 13471 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13472 | return 1; |
| 13473 | } |
| 13474 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13475 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13476 | Py_DECREF(iobj); |
| 13477 | if (res == NULL) |
| 13478 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13479 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13480 | return 0; |
| 13481 | |
| 13482 | wrongtype: |
| 13483 | PyErr_Format(PyExc_TypeError, |
| 13484 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13485 | "not %.200s", |
| 13486 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13487 | return -1; |
| 13488 | } |
| 13489 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13490 | static Py_UCS4 |
| 13491 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13492 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13493 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13494 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13495 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13496 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13497 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13498 | goto onError; |
| 13499 | } |
| 13500 | else { |
| 13501 | /* Integer input truncated to a character */ |
| 13502 | long x; |
| 13503 | x = PyLong_AsLong(v); |
| 13504 | if (x == -1 && PyErr_Occurred()) |
| 13505 | goto onError; |
| 13506 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13507 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13508 | PyErr_SetString(PyExc_OverflowError, |
| 13509 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13510 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13511 | } |
| 13512 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13513 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13514 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13515 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13516 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13517 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13518 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13519 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13520 | } |
| 13521 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13522 | /* Parse options of an argument: flags, width, precision. |
| 13523 | Handle also "%(name)" syntax. |
| 13524 | |
| 13525 | Return 0 if the argument has been formatted into arg->str. |
| 13526 | Return 1 if the argument has been written into ctx->writer, |
| 13527 | Raise an exception and return -1 on error. */ |
| 13528 | static int |
| 13529 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 13530 | struct unicode_format_arg_t *arg) |
| 13531 | { |
| 13532 | #define FORMAT_READ(ctx) \ |
| 13533 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 13534 | |
| 13535 | PyObject *v; |
| 13536 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13537 | if (arg->ch == '(') { |
| 13538 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 13539 | Py_ssize_t keystart; |
| 13540 | Py_ssize_t keylen; |
| 13541 | PyObject *key; |
| 13542 | int pcount = 1; |
| 13543 | |
| 13544 | if (ctx->dict == NULL) { |
| 13545 | PyErr_SetString(PyExc_TypeError, |
| 13546 | "format requires a mapping"); |
| 13547 | return -1; |
| 13548 | } |
| 13549 | ++ctx->fmtpos; |
| 13550 | --ctx->fmtcnt; |
| 13551 | keystart = ctx->fmtpos; |
| 13552 | /* Skip over balanced parentheses */ |
| 13553 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 13554 | arg->ch = FORMAT_READ(ctx); |
| 13555 | if (arg->ch == ')') |
| 13556 | --pcount; |
| 13557 | else if (arg->ch == '(') |
| 13558 | ++pcount; |
| 13559 | ctx->fmtpos++; |
| 13560 | } |
| 13561 | keylen = ctx->fmtpos - keystart - 1; |
| 13562 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 13563 | PyErr_SetString(PyExc_ValueError, |
| 13564 | "incomplete format key"); |
| 13565 | return -1; |
| 13566 | } |
| 13567 | key = PyUnicode_Substring(ctx->fmtstr, |
| 13568 | keystart, keystart + keylen); |
| 13569 | if (key == NULL) |
| 13570 | return -1; |
| 13571 | if (ctx->args_owned) { |
| 13572 | Py_DECREF(ctx->args); |
| 13573 | ctx->args_owned = 0; |
| 13574 | } |
| 13575 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 13576 | Py_DECREF(key); |
| 13577 | if (ctx->args == NULL) |
| 13578 | return -1; |
| 13579 | ctx->args_owned = 1; |
| 13580 | ctx->arglen = -1; |
| 13581 | ctx->argidx = -2; |
| 13582 | } |
| 13583 | |
| 13584 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13585 | while (--ctx->fmtcnt >= 0) { |
| 13586 | arg->ch = FORMAT_READ(ctx); |
| 13587 | ctx->fmtpos++; |
| 13588 | switch (arg->ch) { |
| 13589 | case '-': arg->flags |= F_LJUST; continue; |
| 13590 | case '+': arg->flags |= F_SIGN; continue; |
| 13591 | case ' ': arg->flags |= F_BLANK; continue; |
| 13592 | case '#': arg->flags |= F_ALT; continue; |
| 13593 | case '0': arg->flags |= F_ZERO; continue; |
| 13594 | } |
| 13595 | break; |
| 13596 | } |
| 13597 | |
| 13598 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13599 | if (arg->ch == '*') { |
| 13600 | v = unicode_format_getnextarg(ctx); |
| 13601 | if (v == NULL) |
| 13602 | return -1; |
| 13603 | if (!PyLong_Check(v)) { |
| 13604 | PyErr_SetString(PyExc_TypeError, |
| 13605 | "* wants int"); |
| 13606 | return -1; |
| 13607 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13608 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13609 | if (arg->width == -1 && PyErr_Occurred()) |
| 13610 | return -1; |
| 13611 | if (arg->width < 0) { |
| 13612 | arg->flags |= F_LJUST; |
| 13613 | arg->width = -arg->width; |
| 13614 | } |
| 13615 | if (--ctx->fmtcnt >= 0) { |
| 13616 | arg->ch = FORMAT_READ(ctx); |
| 13617 | ctx->fmtpos++; |
| 13618 | } |
| 13619 | } |
| 13620 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13621 | arg->width = arg->ch - '0'; |
| 13622 | while (--ctx->fmtcnt >= 0) { |
| 13623 | arg->ch = FORMAT_READ(ctx); |
| 13624 | ctx->fmtpos++; |
| 13625 | if (arg->ch < '0' || arg->ch > '9') |
| 13626 | break; |
| 13627 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 13628 | mixing signed and unsigned comparison. Since arg->ch is between |
| 13629 | '0' and '9', casting to int is safe. */ |
| 13630 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 13631 | PyErr_SetString(PyExc_ValueError, |
| 13632 | "width too big"); |
| 13633 | return -1; |
| 13634 | } |
| 13635 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 13636 | } |
| 13637 | } |
| 13638 | |
| 13639 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13640 | if (arg->ch == '.') { |
| 13641 | arg->prec = 0; |
| 13642 | if (--ctx->fmtcnt >= 0) { |
| 13643 | arg->ch = FORMAT_READ(ctx); |
| 13644 | ctx->fmtpos++; |
| 13645 | } |
| 13646 | if (arg->ch == '*') { |
| 13647 | v = unicode_format_getnextarg(ctx); |
| 13648 | if (v == NULL) |
| 13649 | return -1; |
| 13650 | if (!PyLong_Check(v)) { |
| 13651 | PyErr_SetString(PyExc_TypeError, |
| 13652 | "* wants int"); |
| 13653 | return -1; |
| 13654 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13655 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13656 | if (arg->prec == -1 && PyErr_Occurred()) |
| 13657 | return -1; |
| 13658 | if (arg->prec < 0) |
| 13659 | arg->prec = 0; |
| 13660 | if (--ctx->fmtcnt >= 0) { |
| 13661 | arg->ch = FORMAT_READ(ctx); |
| 13662 | ctx->fmtpos++; |
| 13663 | } |
| 13664 | } |
| 13665 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13666 | arg->prec = arg->ch - '0'; |
| 13667 | while (--ctx->fmtcnt >= 0) { |
| 13668 | arg->ch = FORMAT_READ(ctx); |
| 13669 | ctx->fmtpos++; |
| 13670 | if (arg->ch < '0' || arg->ch > '9') |
| 13671 | break; |
| 13672 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 13673 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 13674 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13675 | return -1; |
| 13676 | } |
| 13677 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 13678 | } |
| 13679 | } |
| 13680 | } |
| 13681 | |
| 13682 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 13683 | if (ctx->fmtcnt >= 0) { |
| 13684 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 13685 | if (--ctx->fmtcnt >= 0) { |
| 13686 | arg->ch = FORMAT_READ(ctx); |
| 13687 | ctx->fmtpos++; |
| 13688 | } |
| 13689 | } |
| 13690 | } |
| 13691 | if (ctx->fmtcnt < 0) { |
| 13692 | PyErr_SetString(PyExc_ValueError, |
| 13693 | "incomplete format"); |
| 13694 | return -1; |
| 13695 | } |
| 13696 | return 0; |
| 13697 | |
| 13698 | #undef FORMAT_READ |
| 13699 | } |
| 13700 | |
| 13701 | /* Format one argument. Supported conversion specifiers: |
| 13702 | |
| 13703 | - "s", "r", "a": any type |
| 13704 | - "i", "d", "u", "o", "x", "X": int |
| 13705 | - "e", "E", "f", "F", "g", "G": float |
| 13706 | - "c": int or str (1 character) |
| 13707 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13708 | When possible, the output is written directly into the Unicode writer |
| 13709 | (ctx->writer). A string is created when padding is required. |
| 13710 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13711 | Return 0 if the argument has been formatted into *p_str, |
| 13712 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13713 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13714 | static int |
| 13715 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 13716 | struct unicode_format_arg_t *arg, |
| 13717 | PyObject **p_str) |
| 13718 | { |
| 13719 | PyObject *v; |
| 13720 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13721 | |
| 13722 | if (ctx->fmtcnt == 0) |
| 13723 | ctx->writer.overallocate = 0; |
| 13724 | |
| 13725 | if (arg->ch == '%') { |
| 13726 | if (_PyUnicodeWriter_Prepare(writer, 1, '%') == -1) |
| 13727 | return -1; |
| 13728 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%'); |
| 13729 | writer->pos += 1; |
| 13730 | return 1; |
| 13731 | } |
| 13732 | |
| 13733 | v = unicode_format_getnextarg(ctx); |
| 13734 | if (v == NULL) |
| 13735 | return -1; |
| 13736 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13737 | |
| 13738 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13739 | case 's': |
| 13740 | case 'r': |
| 13741 | case 'a': |
| 13742 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 13743 | /* Fast path */ |
| 13744 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 13745 | return -1; |
| 13746 | return 1; |
| 13747 | } |
| 13748 | |
| 13749 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 13750 | *p_str = v; |
| 13751 | Py_INCREF(*p_str); |
| 13752 | } |
| 13753 | else { |
| 13754 | if (arg->ch == 's') |
| 13755 | *p_str = PyObject_Str(v); |
| 13756 | else if (arg->ch == 'r') |
| 13757 | *p_str = PyObject_Repr(v); |
| 13758 | else |
| 13759 | *p_str = PyObject_ASCII(v); |
| 13760 | } |
| 13761 | break; |
| 13762 | |
| 13763 | case 'i': |
| 13764 | case 'd': |
| 13765 | case 'u': |
| 13766 | case 'o': |
| 13767 | case 'x': |
| 13768 | case 'X': |
| 13769 | { |
| 13770 | int ret = mainformatlong(v, arg, p_str, writer); |
| 13771 | if (ret != 0) |
| 13772 | return ret; |
| 13773 | arg->sign = 1; |
| 13774 | break; |
| 13775 | } |
| 13776 | |
| 13777 | case 'e': |
| 13778 | case 'E': |
| 13779 | case 'f': |
| 13780 | case 'F': |
| 13781 | case 'g': |
| 13782 | case 'G': |
| 13783 | if (arg->width == -1 && arg->prec == -1 |
| 13784 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13785 | { |
| 13786 | /* Fast path */ |
| 13787 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 13788 | return -1; |
| 13789 | return 1; |
| 13790 | } |
| 13791 | |
| 13792 | arg->sign = 1; |
| 13793 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 13794 | return -1; |
| 13795 | break; |
| 13796 | |
| 13797 | case 'c': |
| 13798 | { |
| 13799 | Py_UCS4 ch = formatchar(v); |
| 13800 | if (ch == (Py_UCS4) -1) |
| 13801 | return -1; |
| 13802 | if (arg->width == -1 && arg->prec == -1) { |
| 13803 | /* Fast path */ |
| 13804 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) == -1) |
| 13805 | return -1; |
| 13806 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13807 | writer->pos += 1; |
| 13808 | return 1; |
| 13809 | } |
| 13810 | *p_str = PyUnicode_FromOrdinal(ch); |
| 13811 | break; |
| 13812 | } |
| 13813 | |
| 13814 | default: |
| 13815 | PyErr_Format(PyExc_ValueError, |
| 13816 | "unsupported format character '%c' (0x%x) " |
| 13817 | "at index %zd", |
| 13818 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 13819 | (int)arg->ch, |
| 13820 | ctx->fmtpos - 1); |
| 13821 | return -1; |
| 13822 | } |
| 13823 | if (*p_str == NULL) |
| 13824 | return -1; |
| 13825 | assert (PyUnicode_Check(*p_str)); |
| 13826 | return 0; |
| 13827 | } |
| 13828 | |
| 13829 | static int |
| 13830 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 13831 | struct unicode_format_arg_t *arg, |
| 13832 | PyObject *str) |
| 13833 | { |
| 13834 | Py_ssize_t len; |
| 13835 | enum PyUnicode_Kind kind; |
| 13836 | void *pbuf; |
| 13837 | Py_ssize_t pindex; |
| 13838 | Py_UCS4 signchar; |
| 13839 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13840 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13841 | Py_ssize_t sublen; |
| 13842 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13843 | Py_UCS4 fill; |
| 13844 | |
| 13845 | fill = ' '; |
| 13846 | if (arg->sign && arg->flags & F_ZERO) |
| 13847 | fill = '0'; |
| 13848 | |
| 13849 | if (PyUnicode_READY(str) == -1) |
| 13850 | return -1; |
| 13851 | |
| 13852 | len = PyUnicode_GET_LENGTH(str); |
| 13853 | if ((arg->width == -1 || arg->width <= len) |
| 13854 | && (arg->prec == -1 || arg->prec >= len) |
| 13855 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13856 | { |
| 13857 | /* Fast path */ |
| 13858 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 13859 | return -1; |
| 13860 | return 0; |
| 13861 | } |
| 13862 | |
| 13863 | /* Truncate the string for "s", "r" and "a" formats |
| 13864 | if the precision is set */ |
| 13865 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 13866 | if (arg->prec >= 0 && len > arg->prec) |
| 13867 | len = arg->prec; |
| 13868 | } |
| 13869 | |
| 13870 | /* Adjust sign and width */ |
| 13871 | kind = PyUnicode_KIND(str); |
| 13872 | pbuf = PyUnicode_DATA(str); |
| 13873 | pindex = 0; |
| 13874 | signchar = '\0'; |
| 13875 | if (arg->sign) { |
| 13876 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13877 | if (ch == '-' || ch == '+') { |
| 13878 | signchar = ch; |
| 13879 | len--; |
| 13880 | pindex++; |
| 13881 | } |
| 13882 | else if (arg->flags & F_SIGN) |
| 13883 | signchar = '+'; |
| 13884 | else if (arg->flags & F_BLANK) |
| 13885 | signchar = ' '; |
| 13886 | else |
| 13887 | arg->sign = 0; |
| 13888 | } |
| 13889 | if (arg->width < len) |
| 13890 | arg->width = len; |
| 13891 | |
| 13892 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13893 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13894 | if (!(arg->flags & F_LJUST)) { |
| 13895 | if (arg->sign) { |
| 13896 | if ((arg->width-1) > len) |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13897 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13898 | } |
| 13899 | else { |
| 13900 | if (arg->width > len) |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13901 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13902 | } |
| 13903 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13904 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 13905 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
| 13906 | maxchar = MAX_MAXCHAR(maxchar, strmaxchar); |
| 13907 | } |
| 13908 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13909 | buflen = arg->width; |
| 13910 | if (arg->sign && len == arg->width) |
| 13911 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 13912 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13913 | return -1; |
| 13914 | |
| 13915 | /* Write the sign if needed */ |
| 13916 | if (arg->sign) { |
| 13917 | if (fill != ' ') { |
| 13918 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 13919 | writer->pos += 1; |
| 13920 | } |
| 13921 | if (arg->width > len) |
| 13922 | arg->width--; |
| 13923 | } |
| 13924 | |
| 13925 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 13926 | if the alternate form is used. |
| 13927 | For example, write "0x" for the "%#x" format. */ |
| 13928 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 13929 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13930 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 13931 | if (fill != ' ') { |
| 13932 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 13933 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 13934 | writer->pos += 2; |
| 13935 | pindex += 2; |
| 13936 | } |
| 13937 | arg->width -= 2; |
| 13938 | if (arg->width < 0) |
| 13939 | arg->width = 0; |
| 13940 | len -= 2; |
| 13941 | } |
| 13942 | |
| 13943 | /* Pad left with the fill character if needed */ |
| 13944 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 13945 | sublen = arg->width - len; |
| 13946 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 13947 | writer->pos += sublen; |
| 13948 | arg->width = len; |
| 13949 | } |
| 13950 | |
| 13951 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 13952 | the alternate form is used */ |
| 13953 | if (fill == ' ') { |
| 13954 | if (arg->sign) { |
| 13955 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 13956 | writer->pos += 1; |
| 13957 | } |
| 13958 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 13959 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13960 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 13961 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 13962 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 13963 | writer->pos += 2; |
| 13964 | pindex += 2; |
| 13965 | } |
| 13966 | } |
| 13967 | |
| 13968 | /* Write characters */ |
| 13969 | if (len) { |
| 13970 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13971 | str, pindex, len); |
| 13972 | writer->pos += len; |
| 13973 | } |
| 13974 | |
| 13975 | /* Pad right with the fill character if needed */ |
| 13976 | if (arg->width > len) { |
| 13977 | sublen = arg->width - len; |
| 13978 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 13979 | writer->pos += sublen; |
| 13980 | } |
| 13981 | return 0; |
| 13982 | } |
| 13983 | |
| 13984 | /* Helper of PyUnicode_Format(): format one arg. |
| 13985 | Return 0 on success, raise an exception and return -1 on error. */ |
| 13986 | static int |
| 13987 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 13988 | { |
| 13989 | struct unicode_format_arg_t arg; |
| 13990 | PyObject *str; |
| 13991 | int ret; |
| 13992 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13993 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 13994 | arg.flags = 0; |
| 13995 | arg.width = -1; |
| 13996 | arg.prec = -1; |
| 13997 | arg.sign = 0; |
| 13998 | str = NULL; |
| 13999 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14000 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14001 | if (ret == -1) |
| 14002 | return -1; |
| 14003 | |
| 14004 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14005 | if (ret == -1) |
| 14006 | return -1; |
| 14007 | |
| 14008 | if (ret != 1) { |
| 14009 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14010 | Py_DECREF(str); |
| 14011 | if (ret == -1) |
| 14012 | return -1; |
| 14013 | } |
| 14014 | |
| 14015 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14016 | PyErr_SetString(PyExc_TypeError, |
| 14017 | "not all arguments converted during string formatting"); |
| 14018 | return -1; |
| 14019 | } |
| 14020 | return 0; |
| 14021 | } |
| 14022 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14023 | PyObject * |
| 14024 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14025 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14026 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14028 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14029 | PyErr_BadInternalCall(); |
| 14030 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14031 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14032 | |
| 14033 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14034 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14035 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14036 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14037 | Py_DECREF(ctx.fmtstr); |
| 14038 | return NULL; |
| 14039 | } |
| 14040 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14041 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14042 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14043 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14044 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14045 | _PyUnicodeWriter_Init(&ctx.writer, ctx.fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14046 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14047 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14048 | ctx.arglen = PyTuple_Size(args); |
| 14049 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14050 | } |
| 14051 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14052 | ctx.arglen = -1; |
| 14053 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14054 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14055 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14056 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14057 | ctx.dict = args; |
| 14058 | else |
| 14059 | ctx.dict = NULL; |
| 14060 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14061 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14062 | while (--ctx.fmtcnt >= 0) { |
| 14063 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14064 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14065 | |
| 14066 | nonfmtpos = ctx.fmtpos++; |
| 14067 | while (ctx.fmtcnt >= 0 && |
| 14068 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14069 | ctx.fmtpos++; |
| 14070 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14071 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14072 | if (ctx.fmtcnt < 0) { |
| 14073 | ctx.fmtpos--; |
| 14074 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14075 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14076 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14077 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14078 | nonfmtpos, ctx.fmtpos) < 0) |
| 14079 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14080 | } |
| 14081 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14082 | ctx.fmtpos++; |
| 14083 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14084 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14085 | } |
| 14086 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14087 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14088 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14089 | PyErr_SetString(PyExc_TypeError, |
| 14090 | "not all arguments converted during string formatting"); |
| 14091 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14092 | } |
| 14093 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14094 | if (ctx.args_owned) { |
| 14095 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14096 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14097 | Py_DECREF(ctx.fmtstr); |
| 14098 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14099 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14100 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14101 | Py_DECREF(ctx.fmtstr); |
| 14102 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14103 | if (ctx.args_owned) { |
| 14104 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14105 | } |
| 14106 | return NULL; |
| 14107 | } |
| 14108 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14109 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14110 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14111 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14112 | static PyObject * |
| 14113 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14114 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14115 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14116 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14117 | char *encoding = NULL; |
| 14118 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14119 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14120 | if (type != &PyUnicode_Type) |
| 14121 | return unicode_subtype_new(type, args, kwds); |
| 14122 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14123 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14124 | return NULL; |
| 14125 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14126 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14127 | if (encoding == NULL && errors == NULL) |
| 14128 | return PyObject_Str(x); |
| 14129 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14130 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14131 | } |
| 14132 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14133 | static PyObject * |
| 14134 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14135 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14136 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14137 | Py_ssize_t length, char_size; |
| 14138 | int share_wstr, share_utf8; |
| 14139 | unsigned int kind; |
| 14140 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14141 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14142 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14143 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14144 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14145 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14146 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14147 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14148 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14149 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14150 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14151 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14152 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14153 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14154 | if (self == NULL) { |
| 14155 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14156 | return NULL; |
| 14157 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14158 | kind = PyUnicode_KIND(unicode); |
| 14159 | length = PyUnicode_GET_LENGTH(unicode); |
| 14160 | |
| 14161 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14162 | #ifdef Py_DEBUG |
| 14163 | _PyUnicode_HASH(self) = -1; |
| 14164 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14165 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14166 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14167 | _PyUnicode_STATE(self).interned = 0; |
| 14168 | _PyUnicode_STATE(self).kind = kind; |
| 14169 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14170 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14171 | _PyUnicode_STATE(self).ready = 1; |
| 14172 | _PyUnicode_WSTR(self) = NULL; |
| 14173 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14174 | _PyUnicode_UTF8(self) = NULL; |
| 14175 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14176 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14177 | |
| 14178 | share_utf8 = 0; |
| 14179 | share_wstr = 0; |
| 14180 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14181 | char_size = 1; |
| 14182 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14183 | share_utf8 = 1; |
| 14184 | } |
| 14185 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14186 | char_size = 2; |
| 14187 | if (sizeof(wchar_t) == 2) |
| 14188 | share_wstr = 1; |
| 14189 | } |
| 14190 | else { |
| 14191 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14192 | char_size = 4; |
| 14193 | if (sizeof(wchar_t) == 4) |
| 14194 | share_wstr = 1; |
| 14195 | } |
| 14196 | |
| 14197 | /* Ensure we won't overflow the length. */ |
| 14198 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14199 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14200 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14201 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14202 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14203 | if (data == NULL) { |
| 14204 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14205 | goto onError; |
| 14206 | } |
| 14207 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14208 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14209 | if (share_utf8) { |
| 14210 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14211 | _PyUnicode_UTF8(self) = data; |
| 14212 | } |
| 14213 | if (share_wstr) { |
| 14214 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14215 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14216 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14217 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14218 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14219 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14220 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14221 | #ifdef Py_DEBUG |
| 14222 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14223 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14224 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14225 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14226 | |
| 14227 | onError: |
| 14228 | Py_DECREF(unicode); |
| 14229 | Py_DECREF(self); |
| 14230 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14231 | } |
| 14232 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14233 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14234 | "str(object='') -> str\n\ |
| 14235 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14236 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14237 | Create a new string object from the given object. If encoding or\n\ |
| 14238 | errors is specified, then the object must expose a data buffer\n\ |
| 14239 | that will be decoded using the given encoding and error handler.\n\ |
| 14240 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14241 | or repr(object).\n\ |
| 14242 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14243 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14244 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14245 | static PyObject *unicode_iter(PyObject *seq); |
| 14246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14247 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14248 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14249 | "str", /* tp_name */ |
| 14250 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14251 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14252 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14253 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14254 | 0, /* tp_print */ |
| 14255 | 0, /* tp_getattr */ |
| 14256 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14257 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14258 | unicode_repr, /* tp_repr */ |
| 14259 | &unicode_as_number, /* tp_as_number */ |
| 14260 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14261 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14262 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14263 | 0, /* tp_call*/ |
| 14264 | (reprfunc) unicode_str, /* tp_str */ |
| 14265 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14266 | 0, /* tp_setattro */ |
| 14267 | 0, /* tp_as_buffer */ |
| 14268 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14269 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14270 | unicode_doc, /* tp_doc */ |
| 14271 | 0, /* tp_traverse */ |
| 14272 | 0, /* tp_clear */ |
| 14273 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14274 | 0, /* tp_weaklistoffset */ |
| 14275 | unicode_iter, /* tp_iter */ |
| 14276 | 0, /* tp_iternext */ |
| 14277 | unicode_methods, /* tp_methods */ |
| 14278 | 0, /* tp_members */ |
| 14279 | 0, /* tp_getset */ |
| 14280 | &PyBaseObject_Type, /* tp_base */ |
| 14281 | 0, /* tp_dict */ |
| 14282 | 0, /* tp_descr_get */ |
| 14283 | 0, /* tp_descr_set */ |
| 14284 | 0, /* tp_dictoffset */ |
| 14285 | 0, /* tp_init */ |
| 14286 | 0, /* tp_alloc */ |
| 14287 | unicode_new, /* tp_new */ |
| 14288 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14289 | }; |
| 14290 | |
| 14291 | /* Initialize the Unicode implementation */ |
| 14292 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14293 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14294 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14295 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14296 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14297 | 0x000A, /* LINE FEED */ |
| 14298 | 0x000D, /* CARRIAGE RETURN */ |
| 14299 | 0x001C, /* FILE SEPARATOR */ |
| 14300 | 0x001D, /* GROUP SEPARATOR */ |
| 14301 | 0x001E, /* RECORD SEPARATOR */ |
| 14302 | 0x0085, /* NEXT LINE */ |
| 14303 | 0x2028, /* LINE SEPARATOR */ |
| 14304 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14305 | }; |
| 14306 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14307 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14308 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14309 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14310 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14311 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14312 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14313 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14314 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14315 | |
| 14316 | /* initialize the linebreak bloom filter */ |
| 14317 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14318 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14319 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14320 | |
| 14321 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14322 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14323 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14324 | Py_FatalError("Can't initialize field name iterator type"); |
| 14325 | |
| 14326 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14327 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14328 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14329 | #ifdef HAVE_MBCS |
| 14330 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14331 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14332 | PyErr_SetFromWindowsErr(0); |
| 14333 | return -1; |
| 14334 | } |
| 14335 | #endif |
| 14336 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14337 | } |
| 14338 | |
| 14339 | /* Finalize the Unicode implementation */ |
| 14340 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14341 | int |
| 14342 | PyUnicode_ClearFreeList(void) |
| 14343 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14344 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14345 | } |
| 14346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14347 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14348 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14349 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14350 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14351 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14352 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14353 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14354 | for (i = 0; i < 256; i++) |
| 14355 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14356 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14357 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14358 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14359 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14360 | void |
| 14361 | PyUnicode_InternInPlace(PyObject **p) |
| 14362 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14363 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14364 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14365 | #ifdef Py_DEBUG |
| 14366 | assert(s != NULL); |
| 14367 | assert(_PyUnicode_CHECK(s)); |
| 14368 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14369 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14370 | return; |
| 14371 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14372 | /* If it's a subclass, we don't really know what putting |
| 14373 | it in the interned dict might do. */ |
| 14374 | if (!PyUnicode_CheckExact(s)) |
| 14375 | return; |
| 14376 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14377 | return; |
| 14378 | if (interned == NULL) { |
| 14379 | interned = PyDict_New(); |
| 14380 | if (interned == NULL) { |
| 14381 | PyErr_Clear(); /* Don't leave an exception */ |
| 14382 | return; |
| 14383 | } |
| 14384 | } |
| 14385 | /* It might be that the GetItem call fails even |
| 14386 | though the key is present in the dictionary, |
| 14387 | namely when this happens during a stack overflow. */ |
| 14388 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14389 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14390 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14391 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14392 | if (t) { |
| 14393 | Py_INCREF(t); |
| 14394 | Py_DECREF(*p); |
| 14395 | *p = t; |
| 14396 | return; |
| 14397 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14398 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14399 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14400 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14401 | PyErr_Clear(); |
| 14402 | PyThreadState_GET()->recursion_critical = 0; |
| 14403 | return; |
| 14404 | } |
| 14405 | PyThreadState_GET()->recursion_critical = 0; |
| 14406 | /* The two references in interned are not counted by refcnt. |
| 14407 | The deallocator will take care of this */ |
| 14408 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14409 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14410 | } |
| 14411 | |
| 14412 | void |
| 14413 | PyUnicode_InternImmortal(PyObject **p) |
| 14414 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14415 | PyUnicode_InternInPlace(p); |
| 14416 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14417 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14418 | Py_INCREF(*p); |
| 14419 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14420 | } |
| 14421 | |
| 14422 | PyObject * |
| 14423 | PyUnicode_InternFromString(const char *cp) |
| 14424 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14425 | PyObject *s = PyUnicode_FromString(cp); |
| 14426 | if (s == NULL) |
| 14427 | return NULL; |
| 14428 | PyUnicode_InternInPlace(&s); |
| 14429 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14430 | } |
| 14431 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14432 | void |
| 14433 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14434 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14435 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14436 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14437 | Py_ssize_t i, n; |
| 14438 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14439 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14440 | if (interned == NULL || !PyDict_Check(interned)) |
| 14441 | return; |
| 14442 | keys = PyDict_Keys(interned); |
| 14443 | if (keys == NULL || !PyList_Check(keys)) { |
| 14444 | PyErr_Clear(); |
| 14445 | return; |
| 14446 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14447 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14448 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14449 | detector, interned unicode strings are not forcibly deallocated; |
| 14450 | rather, we give them their stolen references back, and then clear |
| 14451 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14452 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14453 | n = PyList_GET_SIZE(keys); |
| 14454 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14455 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14456 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14457 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14458 | if (PyUnicode_READY(s) == -1) { |
| 14459 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14460 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14461 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14462 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14463 | case SSTATE_NOT_INTERNED: |
| 14464 | /* XXX Shouldn't happen */ |
| 14465 | break; |
| 14466 | case SSTATE_INTERNED_IMMORTAL: |
| 14467 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14468 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14469 | break; |
| 14470 | case SSTATE_INTERNED_MORTAL: |
| 14471 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14472 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14473 | break; |
| 14474 | default: |
| 14475 | Py_FatalError("Inconsistent interned string state."); |
| 14476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14477 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14478 | } |
| 14479 | fprintf(stderr, "total size of all interned strings: " |
| 14480 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14481 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14482 | Py_DECREF(keys); |
| 14483 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14484 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14485 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14486 | |
| 14487 | |
| 14488 | /********************* Unicode Iterator **************************/ |
| 14489 | |
| 14490 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14491 | PyObject_HEAD |
| 14492 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14493 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14494 | } unicodeiterobject; |
| 14495 | |
| 14496 | static void |
| 14497 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14498 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14499 | _PyObject_GC_UNTRACK(it); |
| 14500 | Py_XDECREF(it->it_seq); |
| 14501 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14502 | } |
| 14503 | |
| 14504 | static int |
| 14505 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14506 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14507 | Py_VISIT(it->it_seq); |
| 14508 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14509 | } |
| 14510 | |
| 14511 | static PyObject * |
| 14512 | unicodeiter_next(unicodeiterobject *it) |
| 14513 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14514 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14515 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14516 | assert(it != NULL); |
| 14517 | seq = it->it_seq; |
| 14518 | if (seq == NULL) |
| 14519 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14520 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14521 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14522 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14523 | int kind = PyUnicode_KIND(seq); |
| 14524 | void *data = PyUnicode_DATA(seq); |
| 14525 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14526 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14527 | if (item != NULL) |
| 14528 | ++it->it_index; |
| 14529 | return item; |
| 14530 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14531 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14532 | Py_DECREF(seq); |
| 14533 | it->it_seq = NULL; |
| 14534 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14535 | } |
| 14536 | |
| 14537 | static PyObject * |
| 14538 | unicodeiter_len(unicodeiterobject *it) |
| 14539 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14540 | Py_ssize_t len = 0; |
| 14541 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14542 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14543 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14544 | } |
| 14545 | |
| 14546 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14547 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14548 | static PyObject * |
| 14549 | unicodeiter_reduce(unicodeiterobject *it) |
| 14550 | { |
| 14551 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14552 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14553 | it->it_seq, it->it_index); |
| 14554 | } else { |
| 14555 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14556 | if (u == NULL) |
| 14557 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14558 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14559 | } |
| 14560 | } |
| 14561 | |
| 14562 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14563 | |
| 14564 | static PyObject * |
| 14565 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14566 | { |
| 14567 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14568 | if (index == -1 && PyErr_Occurred()) |
| 14569 | return NULL; |
| 14570 | if (index < 0) |
| 14571 | index = 0; |
| 14572 | it->it_index = index; |
| 14573 | Py_RETURN_NONE; |
| 14574 | } |
| 14575 | |
| 14576 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14577 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14578 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14579 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14580 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14581 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14582 | reduce_doc}, |
| 14583 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14584 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14585 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14586 | }; |
| 14587 | |
| 14588 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14589 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14590 | "str_iterator", /* tp_name */ |
| 14591 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14592 | 0, /* tp_itemsize */ |
| 14593 | /* methods */ |
| 14594 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14595 | 0, /* tp_print */ |
| 14596 | 0, /* tp_getattr */ |
| 14597 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14598 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14599 | 0, /* tp_repr */ |
| 14600 | 0, /* tp_as_number */ |
| 14601 | 0, /* tp_as_sequence */ |
| 14602 | 0, /* tp_as_mapping */ |
| 14603 | 0, /* tp_hash */ |
| 14604 | 0, /* tp_call */ |
| 14605 | 0, /* tp_str */ |
| 14606 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14607 | 0, /* tp_setattro */ |
| 14608 | 0, /* tp_as_buffer */ |
| 14609 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14610 | 0, /* tp_doc */ |
| 14611 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14612 | 0, /* tp_clear */ |
| 14613 | 0, /* tp_richcompare */ |
| 14614 | 0, /* tp_weaklistoffset */ |
| 14615 | PyObject_SelfIter, /* tp_iter */ |
| 14616 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14617 | unicodeiter_methods, /* tp_methods */ |
| 14618 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14619 | }; |
| 14620 | |
| 14621 | static PyObject * |
| 14622 | unicode_iter(PyObject *seq) |
| 14623 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14624 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14625 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14626 | if (!PyUnicode_Check(seq)) { |
| 14627 | PyErr_BadInternalCall(); |
| 14628 | return NULL; |
| 14629 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14630 | if (PyUnicode_READY(seq) == -1) |
| 14631 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14632 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14633 | if (it == NULL) |
| 14634 | return NULL; |
| 14635 | it->it_index = 0; |
| 14636 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14637 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14638 | _PyObject_GC_TRACK(it); |
| 14639 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14640 | } |
| 14641 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14642 | |
| 14643 | size_t |
| 14644 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14645 | { |
| 14646 | int res = 0; |
| 14647 | while(*u++) |
| 14648 | res++; |
| 14649 | return res; |
| 14650 | } |
| 14651 | |
| 14652 | Py_UNICODE* |
| 14653 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14654 | { |
| 14655 | Py_UNICODE *u = s1; |
| 14656 | while ((*u++ = *s2++)); |
| 14657 | return s1; |
| 14658 | } |
| 14659 | |
| 14660 | Py_UNICODE* |
| 14661 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14662 | { |
| 14663 | Py_UNICODE *u = s1; |
| 14664 | while ((*u++ = *s2++)) |
| 14665 | if (n-- == 0) |
| 14666 | break; |
| 14667 | return s1; |
| 14668 | } |
| 14669 | |
| 14670 | Py_UNICODE* |
| 14671 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14672 | { |
| 14673 | Py_UNICODE *u1 = s1; |
| 14674 | u1 += Py_UNICODE_strlen(u1); |
| 14675 | Py_UNICODE_strcpy(u1, s2); |
| 14676 | return s1; |
| 14677 | } |
| 14678 | |
| 14679 | int |
| 14680 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14681 | { |
| 14682 | while (*s1 && *s2 && *s1 == *s2) |
| 14683 | s1++, s2++; |
| 14684 | if (*s1 && *s2) |
| 14685 | return (*s1 < *s2) ? -1 : +1; |
| 14686 | if (*s1) |
| 14687 | return 1; |
| 14688 | if (*s2) |
| 14689 | return -1; |
| 14690 | return 0; |
| 14691 | } |
| 14692 | |
| 14693 | int |
| 14694 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14695 | { |
| 14696 | register Py_UNICODE u1, u2; |
| 14697 | for (; n != 0; n--) { |
| 14698 | u1 = *s1; |
| 14699 | u2 = *s2; |
| 14700 | if (u1 != u2) |
| 14701 | return (u1 < u2) ? -1 : +1; |
| 14702 | if (u1 == '\0') |
| 14703 | return 0; |
| 14704 | s1++; |
| 14705 | s2++; |
| 14706 | } |
| 14707 | return 0; |
| 14708 | } |
| 14709 | |
| 14710 | Py_UNICODE* |
| 14711 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14712 | { |
| 14713 | const Py_UNICODE *p; |
| 14714 | for (p = s; *p; p++) |
| 14715 | if (*p == c) |
| 14716 | return (Py_UNICODE*)p; |
| 14717 | return NULL; |
| 14718 | } |
| 14719 | |
| 14720 | Py_UNICODE* |
| 14721 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14722 | { |
| 14723 | const Py_UNICODE *p; |
| 14724 | p = s + Py_UNICODE_strlen(s); |
| 14725 | while (p != s) { |
| 14726 | p--; |
| 14727 | if (*p == c) |
| 14728 | return (Py_UNICODE*)p; |
| 14729 | } |
| 14730 | return NULL; |
| 14731 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14732 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14733 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14734 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14735 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14736 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14737 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14739 | if (!PyUnicode_Check(unicode)) { |
| 14740 | PyErr_BadArgument(); |
| 14741 | return NULL; |
| 14742 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14743 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14744 | if (u == NULL) |
| 14745 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14746 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14747 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14748 | PyErr_NoMemory(); |
| 14749 | return NULL; |
| 14750 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14751 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14752 | size *= sizeof(Py_UNICODE); |
| 14753 | copy = PyMem_Malloc(size); |
| 14754 | if (copy == NULL) { |
| 14755 | PyErr_NoMemory(); |
| 14756 | return NULL; |
| 14757 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14758 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14759 | return copy; |
| 14760 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14761 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14762 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14763 | to the string.Formatter class implemented in Python. */ |
| 14764 | |
| 14765 | static PyMethodDef _string_methods[] = { |
| 14766 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14767 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14768 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14769 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14770 | {NULL, NULL} |
| 14771 | }; |
| 14772 | |
| 14773 | static struct PyModuleDef _string_module = { |
| 14774 | PyModuleDef_HEAD_INIT, |
| 14775 | "_string", |
| 14776 | PyDoc_STR("string helper module"), |
| 14777 | 0, |
| 14778 | _string_methods, |
| 14779 | NULL, |
| 14780 | NULL, |
| 14781 | NULL, |
| 14782 | NULL |
| 14783 | }; |
| 14784 | |
| 14785 | PyMODINIT_FUNC |
| 14786 | PyInit__string(void) |
| 14787 | { |
| 14788 | return PyModule_Create(&_string_module); |
| 14789 | } |
| 14790 | |
| 14791 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14792 | #ifdef __cplusplus |
| 14793 | } |
| 14794 | #endif |