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" |
Raymond Hettinger | ac2ef65 | 2015-07-04 16:04:44 -0700 | [diff] [blame] | 45 | #include "stringlib/eq.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 47 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 48 | #include <windows.h> |
| 49 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 50 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 51 | /*[clinic input] |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 52 | class str "PyUnicodeObject *" "&PyUnicode_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 53 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 54 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=604e916854800fa8]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 55 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 56 | /* --- Globals ------------------------------------------------------------ |
| 57 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 58 | NOTE: In the interpreter's initialization phase, some globals are currently |
| 59 | initialized dynamically as needed. In the process Unicode objects may |
| 60 | be created before the Unicode type is ready. |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 61 | |
| 62 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 64 | |
| 65 | #ifdef __cplusplus |
| 66 | extern "C" { |
| 67 | #endif |
| 68 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 69 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 70 | #define MAX_UNICODE 0x10ffff |
| 71 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 72 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 73 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 74 | #else |
| 75 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 76 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 77 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 78 | #define _PyUnicode_UTF8(op) \ |
| 79 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 80 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 81 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 82 | assert(PyUnicode_IS_READY(op)), \ |
| 83 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 84 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 85 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 86 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 87 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 88 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 89 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 90 | assert(PyUnicode_IS_READY(op)), \ |
| 91 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 92 | ((PyASCIIObject*)(op))->length : \ |
| 93 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 94 | #define _PyUnicode_WSTR(op) \ |
| 95 | (((PyASCIIObject*)(op))->wstr) |
| 96 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 97 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 98 | #define _PyUnicode_LENGTH(op) \ |
| 99 | (((PyASCIIObject *)(op))->length) |
| 100 | #define _PyUnicode_STATE(op) \ |
| 101 | (((PyASCIIObject *)(op))->state) |
| 102 | #define _PyUnicode_HASH(op) \ |
| 103 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 104 | #define _PyUnicode_KIND(op) \ |
| 105 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 106 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 107 | #define _PyUnicode_GET_LENGTH(op) \ |
| 108 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 109 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 110 | #define _PyUnicode_DATA_ANY(op) \ |
| 111 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 112 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 113 | #undef PyUnicode_READY |
| 114 | #define PyUnicode_READY(op) \ |
| 115 | (assert(_PyUnicode_CHECK(op)), \ |
| 116 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 117 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 118 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 119 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 120 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 121 | (assert(_PyUnicode_CHECK(op)), \ |
| 122 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 123 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 124 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 125 | (assert(_PyUnicode_CHECK(op)), \ |
| 126 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 127 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 128 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 129 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 130 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 131 | ((!PyUnicode_IS_COMPACT_ASCII(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 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) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 138 | ((_PyUnicode_WSTR(op) && \ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 139 | (!PyUnicode_IS_READY(op) || \ |
| 140 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 141 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 142 | /* Generic helper macro to convert characters of different types. |
| 143 | from_type and to_type have to be valid type names, begin and end |
| 144 | are pointers to the source characters which should be of type |
| 145 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 146 | buffer where the result characters are written to. */ |
| 147 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 148 | do { \ |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 149 | to_type *_to = (to_type *)(to); \ |
| 150 | const from_type *_iter = (from_type *)(begin); \ |
| 151 | const from_type *_end = (from_type *)(end); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 152 | Py_ssize_t n = (_end) - (_iter); \ |
| 153 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 154 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 155 | while (_iter < (_unrolled_end)) { \ |
| 156 | _to[0] = (to_type) _iter[0]; \ |
| 157 | _to[1] = (to_type) _iter[1]; \ |
| 158 | _to[2] = (to_type) _iter[2]; \ |
| 159 | _to[3] = (to_type) _iter[3]; \ |
| 160 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 161 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 162 | while (_iter < (_end)) \ |
| 163 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 164 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 165 | |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 166 | #ifdef MS_WINDOWS |
| 167 | /* On Windows, overallocate by 50% is the best factor */ |
| 168 | # define OVERALLOCATE_FACTOR 2 |
| 169 | #else |
| 170 | /* On Linux, overallocate by 25% is the best factor */ |
| 171 | # define OVERALLOCATE_FACTOR 4 |
| 172 | #endif |
| 173 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 174 | /* This dictionary holds all interned unicode strings. Note that references |
| 175 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 176 | When the interned string reaches a refcnt of 0 the string deallocation |
| 177 | function will delete the reference from this dictionary. |
| 178 | |
| 179 | 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] | 180 | 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] | 181 | */ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 182 | static PyObject *interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 183 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | /* The empty Unicode object is shared to improve performance. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 185 | static PyObject *unicode_empty = NULL; |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 186 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 187 | #define _Py_INCREF_UNICODE_EMPTY() \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 188 | do { \ |
| 189 | if (unicode_empty != NULL) \ |
| 190 | Py_INCREF(unicode_empty); \ |
| 191 | else { \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 192 | unicode_empty = PyUnicode_New(0, 0); \ |
| 193 | if (unicode_empty != NULL) { \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 194 | Py_INCREF(unicode_empty); \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 195 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); \ |
| 196 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 197 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 198 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 199 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 200 | #define _Py_RETURN_UNICODE_EMPTY() \ |
| 201 | do { \ |
| 202 | _Py_INCREF_UNICODE_EMPTY(); \ |
| 203 | return unicode_empty; \ |
| 204 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 205 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 206 | /* Forward declaration */ |
| 207 | Py_LOCAL_INLINE(int) |
| 208 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch); |
| 209 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 210 | /* List of static strings. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 211 | static _Py_Identifier *static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 212 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 213 | /* Single character Unicode strings in the Latin-1 range are being |
| 214 | shared as well. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 215 | static PyObject *unicode_latin1[256] = {NULL}; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 216 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 217 | /* Fast detection of the most frequent whitespace characters */ |
| 218 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 219 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 220 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 221 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 222 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 223 | /* case 0x000C: * FORM FEED */ |
| 224 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 225 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 226 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 227 | /* case 0x001C: * FILE SEPARATOR */ |
| 228 | /* case 0x001D: * GROUP SEPARATOR */ |
| 229 | /* case 0x001E: * RECORD SEPARATOR */ |
| 230 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 231 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 232 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 233 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 234 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 235 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 236 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 237 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 238 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 239 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 240 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 241 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 242 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 243 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 244 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 245 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 248 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 249 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 250 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 251 | static int unicode_modifiable(PyObject *unicode); |
| 252 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 253 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 254 | static PyObject * |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 255 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 256 | static PyObject * |
| 257 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 258 | static PyObject * |
| 259 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 260 | |
| 261 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 262 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 263 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 264 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 265 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 266 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 267 | static void |
| 268 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 269 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 270 | PyObject *unicode, |
| 271 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 272 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 273 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 274 | /* Same for linebreaks */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 275 | static const unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 276 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 277 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 278 | /* 0x000B, * LINE TABULATION */ |
| 279 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 280 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 281 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 282 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 283 | /* 0x001C, * FILE SEPARATOR */ |
| 284 | /* 0x001D, * GROUP SEPARATOR */ |
| 285 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 286 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 287 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 288 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 289 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 290 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 291 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 292 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 293 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 294 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 295 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 296 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 297 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 298 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 299 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 302 | #include "clinic/unicodeobject.c.h" |
| 303 | |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 304 | typedef enum { |
| 305 | _Py_ERROR_UNKNOWN=0, |
| 306 | _Py_ERROR_STRICT, |
| 307 | _Py_ERROR_SURROGATEESCAPE, |
| 308 | _Py_ERROR_REPLACE, |
| 309 | _Py_ERROR_IGNORE, |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 310 | _Py_ERROR_BACKSLASHREPLACE, |
| 311 | _Py_ERROR_SURROGATEPASS, |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 312 | _Py_ERROR_XMLCHARREFREPLACE, |
| 313 | _Py_ERROR_OTHER |
| 314 | } _Py_error_handler; |
| 315 | |
| 316 | static _Py_error_handler |
| 317 | get_error_handler(const char *errors) |
| 318 | { |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 319 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 320 | return _Py_ERROR_STRICT; |
| 321 | if (strcmp(errors, "surrogateescape") == 0) |
| 322 | return _Py_ERROR_SURROGATEESCAPE; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 323 | if (strcmp(errors, "replace") == 0) |
| 324 | return _Py_ERROR_REPLACE; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 325 | if (strcmp(errors, "ignore") == 0) |
| 326 | return _Py_ERROR_IGNORE; |
| 327 | if (strcmp(errors, "backslashreplace") == 0) |
| 328 | return _Py_ERROR_BACKSLASHREPLACE; |
| 329 | if (strcmp(errors, "surrogatepass") == 0) |
| 330 | return _Py_ERROR_SURROGATEPASS; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 331 | if (strcmp(errors, "xmlcharrefreplace") == 0) |
| 332 | return _Py_ERROR_XMLCHARREFREPLACE; |
| 333 | return _Py_ERROR_OTHER; |
| 334 | } |
| 335 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 336 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 337 | 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] | 338 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 339 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 340 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 341 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 342 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 343 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 344 | /* This is actually an illegal character, so it should |
| 345 | not be passed to unichr. */ |
| 346 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 347 | #endif |
| 348 | } |
| 349 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 350 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 351 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 352 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 353 | { |
| 354 | PyASCIIObject *ascii; |
| 355 | unsigned int kind; |
| 356 | |
| 357 | assert(PyUnicode_Check(op)); |
| 358 | |
| 359 | ascii = (PyASCIIObject *)op; |
| 360 | kind = ascii->state.kind; |
| 361 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 362 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 363 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 364 | assert(ascii->state.ready == 1); |
| 365 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 366 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 367 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 368 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 369 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 370 | if (ascii->state.compact == 1) { |
| 371 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 372 | assert(kind == PyUnicode_1BYTE_KIND |
| 373 | || kind == PyUnicode_2BYTE_KIND |
| 374 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 375 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 376 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 377 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 378 | } |
| 379 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 380 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 381 | |
| 382 | data = unicode->data.any; |
| 383 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 384 | assert(ascii->length == 0); |
| 385 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 386 | assert(ascii->state.compact == 0); |
| 387 | assert(ascii->state.ascii == 0); |
| 388 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 389 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 390 | assert(ascii->wstr != NULL); |
| 391 | assert(data == NULL); |
| 392 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 393 | } |
| 394 | else { |
| 395 | assert(kind == PyUnicode_1BYTE_KIND |
| 396 | || kind == PyUnicode_2BYTE_KIND |
| 397 | || kind == PyUnicode_4BYTE_KIND); |
| 398 | assert(ascii->state.compact == 0); |
| 399 | assert(ascii->state.ready == 1); |
| 400 | assert(data != NULL); |
| 401 | if (ascii->state.ascii) { |
| 402 | assert (compact->utf8 == data); |
| 403 | assert (compact->utf8_length == ascii->length); |
| 404 | } |
| 405 | else |
| 406 | assert (compact->utf8 != data); |
| 407 | } |
| 408 | } |
| 409 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 410 | if ( |
| 411 | #if SIZEOF_WCHAR_T == 2 |
| 412 | kind == PyUnicode_2BYTE_KIND |
| 413 | #else |
| 414 | kind == PyUnicode_4BYTE_KIND |
| 415 | #endif |
| 416 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 417 | { |
| 418 | assert(ascii->wstr == data); |
| 419 | assert(compact->wstr_length == ascii->length); |
| 420 | } else |
| 421 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 422 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 423 | |
| 424 | if (compact->utf8 == NULL) |
| 425 | assert(compact->utf8_length == 0); |
| 426 | if (ascii->wstr == NULL) |
| 427 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 428 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 429 | /* check that the best kind is used */ |
| 430 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 431 | { |
| 432 | Py_ssize_t i; |
| 433 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 434 | void *data; |
| 435 | Py_UCS4 ch; |
| 436 | |
| 437 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 438 | for (i=0; i < ascii->length; i++) |
| 439 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 440 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 441 | if (ch > maxchar) |
| 442 | maxchar = ch; |
| 443 | } |
| 444 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 445 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 446 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 447 | assert(maxchar <= 255); |
| 448 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 449 | else |
| 450 | assert(maxchar < 128); |
| 451 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 452 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 453 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 454 | assert(maxchar <= 0xFFFF); |
| 455 | } |
| 456 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 457 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 458 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 459 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 460 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 461 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 462 | return 1; |
| 463 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 464 | #endif |
| 465 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 466 | static PyObject* |
| 467 | unicode_result_wchar(PyObject *unicode) |
| 468 | { |
| 469 | #ifndef Py_DEBUG |
| 470 | Py_ssize_t len; |
| 471 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 472 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 473 | if (len == 0) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 474 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 475 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | if (len == 1) { |
| 479 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 480 | if ((Py_UCS4)ch < 256) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 481 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 482 | Py_DECREF(unicode); |
| 483 | return latin1_char; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (_PyUnicode_Ready(unicode) < 0) { |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 488 | Py_DECREF(unicode); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 489 | return NULL; |
| 490 | } |
| 491 | #else |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 492 | assert(Py_REFCNT(unicode) == 1); |
| 493 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 494 | /* don't make the result ready in debug mode to ensure that the caller |
| 495 | makes the string ready before using it */ |
| 496 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 497 | #endif |
| 498 | return unicode; |
| 499 | } |
| 500 | |
| 501 | static PyObject* |
| 502 | unicode_result_ready(PyObject *unicode) |
| 503 | { |
| 504 | Py_ssize_t length; |
| 505 | |
| 506 | length = PyUnicode_GET_LENGTH(unicode); |
| 507 | if (length == 0) { |
| 508 | if (unicode != unicode_empty) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 509 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 510 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 511 | } |
| 512 | return unicode_empty; |
| 513 | } |
| 514 | |
| 515 | if (length == 1) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 516 | void *data = PyUnicode_DATA(unicode); |
| 517 | int kind = PyUnicode_KIND(unicode); |
| 518 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 519 | if (ch < 256) { |
| 520 | PyObject *latin1_char = unicode_latin1[ch]; |
| 521 | if (latin1_char != NULL) { |
| 522 | if (unicode != latin1_char) { |
| 523 | Py_INCREF(latin1_char); |
| 524 | Py_DECREF(unicode); |
| 525 | } |
| 526 | return latin1_char; |
| 527 | } |
| 528 | else { |
| 529 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 530 | Py_INCREF(unicode); |
| 531 | unicode_latin1[ch] = unicode; |
| 532 | return unicode; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 538 | return unicode; |
| 539 | } |
| 540 | |
| 541 | static PyObject* |
| 542 | unicode_result(PyObject *unicode) |
| 543 | { |
| 544 | assert(_PyUnicode_CHECK(unicode)); |
| 545 | if (PyUnicode_IS_READY(unicode)) |
| 546 | return unicode_result_ready(unicode); |
| 547 | else |
| 548 | return unicode_result_wchar(unicode); |
| 549 | } |
| 550 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 551 | static PyObject* |
| 552 | unicode_result_unchanged(PyObject *unicode) |
| 553 | { |
| 554 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 555 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 556 | return NULL; |
| 557 | Py_INCREF(unicode); |
| 558 | return unicode; |
| 559 | } |
| 560 | else |
| 561 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 562 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 563 | } |
| 564 | |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 565 | /* Implementation of the "backslashreplace" error handler for 8-bit encodings: |
| 566 | ASCII, Latin1, UTF-8, etc. */ |
| 567 | static char* |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 568 | backslashreplace(_PyBytesWriter *writer, char *str, |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 569 | PyObject *unicode, Py_ssize_t collstart, Py_ssize_t collend) |
| 570 | { |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 571 | Py_ssize_t size, i; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 572 | Py_UCS4 ch; |
| 573 | enum PyUnicode_Kind kind; |
| 574 | void *data; |
| 575 | |
| 576 | assert(PyUnicode_IS_READY(unicode)); |
| 577 | kind = PyUnicode_KIND(unicode); |
| 578 | data = PyUnicode_DATA(unicode); |
| 579 | |
| 580 | size = 0; |
| 581 | /* determine replacement size */ |
| 582 | for (i = collstart; i < collend; ++i) { |
| 583 | Py_ssize_t incr; |
| 584 | |
| 585 | ch = PyUnicode_READ(kind, data, i); |
| 586 | if (ch < 0x100) |
| 587 | incr = 2+2; |
| 588 | else if (ch < 0x10000) |
| 589 | incr = 2+4; |
| 590 | else { |
| 591 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 3fa36ff | 2015-10-09 03:37:11 +0200 | [diff] [blame] | 592 | incr = 2+8; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 593 | } |
| 594 | if (size > PY_SSIZE_T_MAX - incr) { |
| 595 | PyErr_SetString(PyExc_OverflowError, |
| 596 | "encoded result is too long for a Python string"); |
| 597 | return NULL; |
| 598 | } |
| 599 | size += incr; |
| 600 | } |
| 601 | |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 602 | str = _PyBytesWriter_Prepare(writer, str, size); |
| 603 | if (str == NULL) |
| 604 | return NULL; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 605 | |
| 606 | /* generate replacement */ |
| 607 | for (i = collstart; i < collend; ++i) { |
| 608 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | 797485e | 2015-10-09 03:17:30 +0200 | [diff] [blame] | 609 | *str++ = '\\'; |
| 610 | if (ch >= 0x00010000) { |
| 611 | *str++ = 'U'; |
| 612 | *str++ = Py_hexdigits[(ch>>28)&0xf]; |
| 613 | *str++ = Py_hexdigits[(ch>>24)&0xf]; |
| 614 | *str++ = Py_hexdigits[(ch>>20)&0xf]; |
| 615 | *str++ = Py_hexdigits[(ch>>16)&0xf]; |
| 616 | *str++ = Py_hexdigits[(ch>>12)&0xf]; |
| 617 | *str++ = Py_hexdigits[(ch>>8)&0xf]; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 618 | } |
Victor Stinner | 797485e | 2015-10-09 03:17:30 +0200 | [diff] [blame] | 619 | else if (ch >= 0x100) { |
| 620 | *str++ = 'u'; |
| 621 | *str++ = Py_hexdigits[(ch>>12)&0xf]; |
| 622 | *str++ = Py_hexdigits[(ch>>8)&0xf]; |
| 623 | } |
| 624 | else |
| 625 | *str++ = 'x'; |
| 626 | *str++ = Py_hexdigits[(ch>>4)&0xf]; |
| 627 | *str++ = Py_hexdigits[ch&0xf]; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 628 | } |
| 629 | return str; |
| 630 | } |
| 631 | |
| 632 | /* Implementation of the "xmlcharrefreplace" error handler for 8-bit encodings: |
| 633 | ASCII, Latin1, UTF-8, etc. */ |
| 634 | static char* |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 635 | xmlcharrefreplace(_PyBytesWriter *writer, char *str, |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 636 | PyObject *unicode, Py_ssize_t collstart, Py_ssize_t collend) |
| 637 | { |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 638 | Py_ssize_t size, i; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 639 | Py_UCS4 ch; |
| 640 | enum PyUnicode_Kind kind; |
| 641 | void *data; |
| 642 | |
| 643 | assert(PyUnicode_IS_READY(unicode)); |
| 644 | kind = PyUnicode_KIND(unicode); |
| 645 | data = PyUnicode_DATA(unicode); |
| 646 | |
| 647 | size = 0; |
| 648 | /* determine replacement size */ |
| 649 | for (i = collstart; i < collend; ++i) { |
| 650 | Py_ssize_t incr; |
| 651 | |
| 652 | ch = PyUnicode_READ(kind, data, i); |
| 653 | if (ch < 10) |
| 654 | incr = 2+1+1; |
| 655 | else if (ch < 100) |
| 656 | incr = 2+2+1; |
| 657 | else if (ch < 1000) |
| 658 | incr = 2+3+1; |
| 659 | else if (ch < 10000) |
| 660 | incr = 2+4+1; |
| 661 | else if (ch < 100000) |
| 662 | incr = 2+5+1; |
| 663 | else if (ch < 1000000) |
| 664 | incr = 2+6+1; |
| 665 | else { |
| 666 | assert(ch <= MAX_UNICODE); |
| 667 | incr = 2+7+1; |
| 668 | } |
| 669 | if (size > PY_SSIZE_T_MAX - incr) { |
| 670 | PyErr_SetString(PyExc_OverflowError, |
| 671 | "encoded result is too long for a Python string"); |
| 672 | return NULL; |
| 673 | } |
| 674 | size += incr; |
| 675 | } |
| 676 | |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 677 | str = _PyBytesWriter_Prepare(writer, str, size); |
| 678 | if (str == NULL) |
| 679 | return NULL; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 680 | |
| 681 | /* generate replacement */ |
| 682 | for (i = collstart; i < collend; ++i) { |
| 683 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
| 684 | } |
| 685 | return str; |
| 686 | } |
| 687 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 688 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 689 | |
| 690 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 691 | to keep things simple, we use a single bitmask, using the least 5 |
| 692 | bits from each unicode characters as the bit index. */ |
| 693 | |
| 694 | /* the linebreak mask is set up by Unicode_Init below */ |
| 695 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 696 | #if LONG_BIT >= 128 |
| 697 | #define BLOOM_WIDTH 128 |
| 698 | #elif LONG_BIT >= 64 |
| 699 | #define BLOOM_WIDTH 64 |
| 700 | #elif LONG_BIT >= 32 |
| 701 | #define BLOOM_WIDTH 32 |
| 702 | #else |
| 703 | #error "LONG_BIT is smaller than 32" |
| 704 | #endif |
| 705 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 706 | #define BLOOM_MASK unsigned long |
| 707 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 708 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 709 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 710 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 711 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 712 | #define BLOOM_LINEBREAK(ch) \ |
| 713 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 714 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 715 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 716 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 717 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 718 | { |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 719 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN) \ |
| 720 | do { \ |
| 721 | TYPE *data = (TYPE *)PTR; \ |
| 722 | TYPE *end = data + LEN; \ |
| 723 | Py_UCS4 ch; \ |
| 724 | for (; data != end; data++) { \ |
| 725 | ch = *data; \ |
| 726 | MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ |
| 727 | } \ |
| 728 | break; \ |
| 729 | } while (0) |
| 730 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 731 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 732 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 733 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 734 | |
| 735 | mask = 0; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 736 | switch (kind) { |
| 737 | case PyUnicode_1BYTE_KIND: |
| 738 | BLOOM_UPDATE(Py_UCS1, mask, ptr, len); |
| 739 | break; |
| 740 | case PyUnicode_2BYTE_KIND: |
| 741 | BLOOM_UPDATE(Py_UCS2, mask, ptr, len); |
| 742 | break; |
| 743 | case PyUnicode_4BYTE_KIND: |
| 744 | BLOOM_UPDATE(Py_UCS4, mask, ptr, len); |
| 745 | break; |
| 746 | default: |
| 747 | assert(0); |
| 748 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 749 | return mask; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 750 | |
| 751 | #undef BLOOM_UPDATE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 754 | static int |
| 755 | ensure_unicode(PyObject *obj) |
| 756 | { |
| 757 | if (!PyUnicode_Check(obj)) { |
| 758 | PyErr_Format(PyExc_TypeError, |
| 759 | "must be str, not %.100s", |
| 760 | Py_TYPE(obj)->tp_name); |
| 761 | return -1; |
| 762 | } |
| 763 | return PyUnicode_READY(obj); |
| 764 | } |
| 765 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 766 | /* Compilation of templated routines */ |
| 767 | |
| 768 | #include "stringlib/asciilib.h" |
| 769 | #include "stringlib/fastsearch.h" |
| 770 | #include "stringlib/partition.h" |
| 771 | #include "stringlib/split.h" |
| 772 | #include "stringlib/count.h" |
| 773 | #include "stringlib/find.h" |
| 774 | #include "stringlib/find_max_char.h" |
| 775 | #include "stringlib/localeutil.h" |
| 776 | #include "stringlib/undef.h" |
| 777 | |
| 778 | #include "stringlib/ucs1lib.h" |
| 779 | #include "stringlib/fastsearch.h" |
| 780 | #include "stringlib/partition.h" |
| 781 | #include "stringlib/split.h" |
| 782 | #include "stringlib/count.h" |
| 783 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 784 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 785 | #include "stringlib/find_max_char.h" |
| 786 | #include "stringlib/localeutil.h" |
| 787 | #include "stringlib/undef.h" |
| 788 | |
| 789 | #include "stringlib/ucs2lib.h" |
| 790 | #include "stringlib/fastsearch.h" |
| 791 | #include "stringlib/partition.h" |
| 792 | #include "stringlib/split.h" |
| 793 | #include "stringlib/count.h" |
| 794 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 795 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 796 | #include "stringlib/find_max_char.h" |
| 797 | #include "stringlib/localeutil.h" |
| 798 | #include "stringlib/undef.h" |
| 799 | |
| 800 | #include "stringlib/ucs4lib.h" |
| 801 | #include "stringlib/fastsearch.h" |
| 802 | #include "stringlib/partition.h" |
| 803 | #include "stringlib/split.h" |
| 804 | #include "stringlib/count.h" |
| 805 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 806 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 807 | #include "stringlib/find_max_char.h" |
| 808 | #include "stringlib/localeutil.h" |
| 809 | #include "stringlib/undef.h" |
| 810 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 811 | #include "stringlib/unicodedefs.h" |
| 812 | #include "stringlib/fastsearch.h" |
| 813 | #include "stringlib/count.h" |
| 814 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 815 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 816 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 817 | /* --- Unicode Object ----------------------------------------------------- */ |
| 818 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 819 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 820 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 821 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 822 | Py_LOCAL_INLINE(Py_ssize_t) findchar(const void *s, int kind, |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 823 | Py_ssize_t size, Py_UCS4 ch, |
| 824 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 825 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 826 | switch (kind) { |
| 827 | case PyUnicode_1BYTE_KIND: |
Serhiy Storchaka | 413fdce | 2015-11-14 15:42:17 +0200 | [diff] [blame] | 828 | if ((Py_UCS1) ch != ch) |
| 829 | return -1; |
| 830 | if (direction > 0) |
| 831 | return ucs1lib_find_char((Py_UCS1 *) s, size, (Py_UCS1) ch); |
| 832 | else |
| 833 | return ucs1lib_rfind_char((Py_UCS1 *) s, size, (Py_UCS1) ch); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 834 | case PyUnicode_2BYTE_KIND: |
Serhiy Storchaka | 413fdce | 2015-11-14 15:42:17 +0200 | [diff] [blame] | 835 | if ((Py_UCS2) ch != ch) |
| 836 | return -1; |
| 837 | if (direction > 0) |
| 838 | return ucs2lib_find_char((Py_UCS2 *) s, size, (Py_UCS2) ch); |
| 839 | else |
| 840 | return ucs2lib_rfind_char((Py_UCS2 *) s, size, (Py_UCS2) ch); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 841 | case PyUnicode_4BYTE_KIND: |
Serhiy Storchaka | 413fdce | 2015-11-14 15:42:17 +0200 | [diff] [blame] | 842 | if (direction > 0) |
| 843 | return ucs4lib_find_char((Py_UCS4 *) s, size, ch); |
| 844 | else |
| 845 | return ucs4lib_rfind_char((Py_UCS4 *) s, size, ch); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 846 | default: |
| 847 | assert(0); |
| 848 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 849 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 850 | } |
| 851 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 852 | #ifdef Py_DEBUG |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 853 | /* Fill the data of a Unicode string with invalid characters to detect bugs |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 854 | earlier. |
| 855 | |
| 856 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 857 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 858 | invalid character in Unicode 6.0. */ |
| 859 | static void |
| 860 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 861 | { |
| 862 | int kind = PyUnicode_KIND(unicode); |
| 863 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 864 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 865 | if (length <= old_length) |
| 866 | return; |
| 867 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 868 | } |
| 869 | #endif |
| 870 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 871 | static PyObject* |
| 872 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 873 | { |
| 874 | Py_ssize_t char_size; |
| 875 | Py_ssize_t struct_size; |
| 876 | Py_ssize_t new_size; |
| 877 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 878 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 879 | #ifdef Py_DEBUG |
| 880 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 881 | #endif |
| 882 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 883 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 884 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 885 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 886 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 887 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 888 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 889 | struct_size = sizeof(PyASCIIObject); |
| 890 | else |
| 891 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 892 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 893 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 894 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 895 | PyErr_NoMemory(); |
| 896 | return NULL; |
| 897 | } |
| 898 | new_size = (struct_size + (length + 1) * char_size); |
| 899 | |
Serhiy Storchaka | 7aa6908 | 2015-12-03 01:02:03 +0200 | [diff] [blame] | 900 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) { |
| 901 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 902 | _PyUnicode_UTF8(unicode) = NULL; |
| 903 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 904 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 905 | _Py_DEC_REFTOTAL; |
| 906 | _Py_ForgetReference(unicode); |
| 907 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 908 | new_unicode = (PyObject *)PyObject_REALLOC(unicode, new_size); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 909 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 910 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 911 | PyErr_NoMemory(); |
| 912 | return NULL; |
| 913 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 914 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 915 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 916 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 917 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 918 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 919 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 920 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 921 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 922 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 923 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 924 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 925 | _PyUnicode_WSTR(unicode) = NULL; |
Victor Stinner | 5bc03a6 | 2016-01-27 16:56:53 +0100 | [diff] [blame] | 926 | if (!PyUnicode_IS_ASCII(unicode)) |
| 927 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 928 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 929 | #ifdef Py_DEBUG |
| 930 | unicode_fill_invalid(unicode, old_length); |
| 931 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 932 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 933 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 934 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 935 | return unicode; |
| 936 | } |
| 937 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 938 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 939 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 940 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 941 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 942 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 943 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 944 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 945 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 946 | if (PyUnicode_IS_READY(unicode)) { |
| 947 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 948 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 949 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 950 | #ifdef Py_DEBUG |
| 951 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 952 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 953 | |
| 954 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 955 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 956 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 957 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 958 | |
| 959 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 960 | PyErr_NoMemory(); |
| 961 | return -1; |
| 962 | } |
| 963 | new_size = (length + 1) * char_size; |
| 964 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 965 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 966 | { |
| 967 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 968 | _PyUnicode_UTF8(unicode) = NULL; |
| 969 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 970 | } |
| 971 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 972 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 973 | if (data == NULL) { |
| 974 | PyErr_NoMemory(); |
| 975 | return -1; |
| 976 | } |
| 977 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 978 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 979 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 980 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 981 | } |
| 982 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 983 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 984 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 985 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 986 | _PyUnicode_LENGTH(unicode) = length; |
| 987 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 988 | #ifdef Py_DEBUG |
| 989 | unicode_fill_invalid(unicode, old_length); |
| 990 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 991 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 992 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 993 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 994 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 995 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 996 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 997 | |
| 998 | /* check for integer overflow */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 999 | if (length > PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(wchar_t) - 1) { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 1000 | PyErr_NoMemory(); |
| 1001 | return -1; |
| 1002 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1003 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 1004 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1005 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 1006 | if (!wstr) { |
| 1007 | PyErr_NoMemory(); |
| 1008 | return -1; |
| 1009 | } |
| 1010 | _PyUnicode_WSTR(unicode) = wstr; |
| 1011 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 1012 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1013 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1014 | return 0; |
| 1015 | } |
| 1016 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1017 | static PyObject* |
| 1018 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 1019 | { |
| 1020 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1021 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1022 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1023 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1024 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1025 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1026 | |
| 1027 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1028 | if (copy == NULL) |
| 1029 | return NULL; |
| 1030 | |
| 1031 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1032 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1033 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 1034 | } |
| 1035 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1036 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1037 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1038 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1039 | if (w == NULL) |
| 1040 | return NULL; |
| 1041 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 1042 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 1043 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 1044 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1045 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1049 | /* 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] | 1050 | Ux0000 terminated; some code (e.g. new_identifier) |
| 1051 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1052 | |
| 1053 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1054 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1055 | |
| 1056 | */ |
| 1057 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1058 | static PyUnicodeObject * |
| 1059 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1060 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1061 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1062 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1063 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1064 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1065 | if (length == 0 && unicode_empty != NULL) { |
| 1066 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1067 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 1070 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 1071 | if (length > ((PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(Py_UNICODE)) - 1)) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 1072 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 1073 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1074 | if (length < 0) { |
| 1075 | PyErr_SetString(PyExc_SystemError, |
| 1076 | "Negative size passed to _PyUnicode_New"); |
| 1077 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1080 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 1081 | if (unicode == NULL) |
| 1082 | return NULL; |
| 1083 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 1084 | |
| 1085 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 1086 | _PyUnicode_HASH(unicode) = -1; |
| 1087 | _PyUnicode_STATE(unicode).interned = 0; |
| 1088 | _PyUnicode_STATE(unicode).kind = 0; |
| 1089 | _PyUnicode_STATE(unicode).compact = 0; |
| 1090 | _PyUnicode_STATE(unicode).ready = 0; |
| 1091 | _PyUnicode_STATE(unicode).ascii = 0; |
| 1092 | _PyUnicode_DATA_ANY(unicode) = NULL; |
| 1093 | _PyUnicode_LENGTH(unicode) = 0; |
| 1094 | _PyUnicode_UTF8(unicode) = NULL; |
| 1095 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 1096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1097 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 1098 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1099 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1100 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1101 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 1102 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1103 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 1104 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1105 | * the caller fails before initializing str -- unicode_resize() |
| 1106 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 1107 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 1108 | * We don't want unicode_resize to read uninitialized memory in |
| 1109 | * that case. |
| 1110 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1111 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 1112 | _PyUnicode_WSTR(unicode)[length] = 0; |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 1113 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1114 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1115 | return unicode; |
| 1116 | } |
| 1117 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1118 | static const char* |
| 1119 | unicode_kind_name(PyObject *unicode) |
| 1120 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 1121 | /* don't check consistency: unicode_kind_name() is called from |
| 1122 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1123 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 1124 | { |
| 1125 | if (!PyUnicode_IS_READY(unicode)) |
| 1126 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1127 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1128 | { |
| 1129 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1130 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1131 | return "legacy ascii"; |
| 1132 | else |
| 1133 | return "legacy latin1"; |
| 1134 | case PyUnicode_2BYTE_KIND: |
| 1135 | return "legacy UCS2"; |
| 1136 | case PyUnicode_4BYTE_KIND: |
| 1137 | return "legacy UCS4"; |
| 1138 | default: |
| 1139 | return "<legacy invalid kind>"; |
| 1140 | } |
| 1141 | } |
| 1142 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1143 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1144 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1145 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1146 | return "ascii"; |
| 1147 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1148 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1149 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1150 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1151 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1152 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1153 | default: |
| 1154 | return "<invalid compact kind>"; |
| 1155 | } |
| 1156 | } |
| 1157 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1158 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1159 | /* Functions wrapping macros for use in debugger */ |
| 1160 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1161 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | void *_PyUnicode_compact_data(void *unicode) { |
| 1165 | return _PyUnicode_COMPACT_DATA(unicode); |
| 1166 | } |
| 1167 | void *_PyUnicode_data(void *unicode){ |
| 1168 | printf("obj %p\n", unicode); |
| 1169 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 1170 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 1171 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 1172 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 1173 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 1174 | return PyUnicode_DATA(unicode); |
| 1175 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1176 | |
| 1177 | void |
| 1178 | _PyUnicode_Dump(PyObject *op) |
| 1179 | { |
| 1180 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1181 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 1182 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 1183 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1184 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1185 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1186 | { |
| 1187 | if (ascii->state.ascii) |
| 1188 | data = (ascii + 1); |
| 1189 | else |
| 1190 | data = (compact + 1); |
| 1191 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1192 | else |
| 1193 | data = unicode->data.any; |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1194 | printf("%s: len=%" PY_FORMAT_SIZE_T "u, ", |
| 1195 | unicode_kind_name(op), ascii->length); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1196 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1197 | if (ascii->wstr == data) |
| 1198 | printf("shared "); |
| 1199 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1200 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1201 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1202 | printf(" (%" PY_FORMAT_SIZE_T "u), ", compact->wstr_length); |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1203 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1204 | printf("shared "); |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1205 | printf("utf8=%p (%" PY_FORMAT_SIZE_T "u)", |
| 1206 | compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1207 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1208 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1209 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1210 | #endif |
| 1211 | |
| 1212 | PyObject * |
| 1213 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1214 | { |
| 1215 | PyObject *obj; |
| 1216 | PyCompactUnicodeObject *unicode; |
| 1217 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1218 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1219 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1220 | Py_ssize_t char_size; |
| 1221 | Py_ssize_t struct_size; |
| 1222 | |
| 1223 | /* Optimization for empty strings */ |
| 1224 | if (size == 0 && unicode_empty != NULL) { |
| 1225 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1226 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1229 | is_ascii = 0; |
| 1230 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1231 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1232 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1233 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1234 | char_size = 1; |
| 1235 | is_ascii = 1; |
| 1236 | struct_size = sizeof(PyASCIIObject); |
| 1237 | } |
| 1238 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1239 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1240 | char_size = 1; |
| 1241 | } |
| 1242 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1243 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1244 | char_size = 2; |
| 1245 | if (sizeof(wchar_t) == 2) |
| 1246 | is_sharing = 1; |
| 1247 | } |
| 1248 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1249 | if (maxchar > MAX_UNICODE) { |
| 1250 | PyErr_SetString(PyExc_SystemError, |
| 1251 | "invalid maximum character passed to PyUnicode_New"); |
| 1252 | return NULL; |
| 1253 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1254 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1255 | char_size = 4; |
| 1256 | if (sizeof(wchar_t) == 4) |
| 1257 | is_sharing = 1; |
| 1258 | } |
| 1259 | |
| 1260 | /* Ensure we won't overflow the size. */ |
| 1261 | if (size < 0) { |
| 1262 | PyErr_SetString(PyExc_SystemError, |
| 1263 | "Negative size passed to PyUnicode_New"); |
| 1264 | return NULL; |
| 1265 | } |
| 1266 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1267 | return PyErr_NoMemory(); |
| 1268 | |
| 1269 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1270 | * PyObject_New() so we are able to allocate space for the object and |
| 1271 | * it's data buffer. |
| 1272 | */ |
| 1273 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1274 | if (obj == NULL) |
| 1275 | return PyErr_NoMemory(); |
| 1276 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1277 | if (obj == NULL) |
| 1278 | return NULL; |
| 1279 | |
| 1280 | unicode = (PyCompactUnicodeObject *)obj; |
| 1281 | if (is_ascii) |
| 1282 | data = ((PyASCIIObject*)obj) + 1; |
| 1283 | else |
| 1284 | data = unicode + 1; |
| 1285 | _PyUnicode_LENGTH(unicode) = size; |
| 1286 | _PyUnicode_HASH(unicode) = -1; |
| 1287 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1288 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1289 | _PyUnicode_STATE(unicode).compact = 1; |
| 1290 | _PyUnicode_STATE(unicode).ready = 1; |
| 1291 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1292 | if (is_ascii) { |
| 1293 | ((char*)data)[size] = 0; |
| 1294 | _PyUnicode_WSTR(unicode) = NULL; |
| 1295 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1296 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1297 | ((char*)data)[size] = 0; |
| 1298 | _PyUnicode_WSTR(unicode) = NULL; |
| 1299 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1300 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1301 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1302 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1303 | else { |
| 1304 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1305 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1306 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1307 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1308 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1309 | ((Py_UCS4*)data)[size] = 0; |
| 1310 | if (is_sharing) { |
| 1311 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1312 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1313 | } |
| 1314 | else { |
| 1315 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1316 | _PyUnicode_WSTR(unicode) = NULL; |
| 1317 | } |
| 1318 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1319 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1320 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1321 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1322 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1323 | return obj; |
| 1324 | } |
| 1325 | |
| 1326 | #if SIZEOF_WCHAR_T == 2 |
| 1327 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1328 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1329 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1330 | |
| 1331 | This function assumes that unicode can hold one more code point than wstr |
| 1332 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1333 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1334 | 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] | 1335 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1336 | { |
| 1337 | const wchar_t *iter; |
| 1338 | Py_UCS4 *ucs4_out; |
| 1339 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1340 | assert(unicode != NULL); |
| 1341 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1342 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1343 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1344 | |
| 1345 | for (iter = begin; iter < end; ) { |
| 1346 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1347 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1348 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1349 | && (iter+1) < end |
| 1350 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1351 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1352 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | iter += 2; |
| 1354 | } |
| 1355 | else { |
| 1356 | *ucs4_out++ = *iter; |
| 1357 | iter++; |
| 1358 | } |
| 1359 | } |
| 1360 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1361 | _PyUnicode_GET_LENGTH(unicode))); |
| 1362 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1363 | } |
| 1364 | #endif |
| 1365 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1366 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1367 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1368 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1369 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1370 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1371 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1372 | return -1; |
| 1373 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1374 | return 0; |
| 1375 | } |
| 1376 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1377 | static int |
| 1378 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1379 | PyObject *from, Py_ssize_t from_start, |
| 1380 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1381 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1382 | unsigned int from_kind, to_kind; |
| 1383 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1384 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1385 | assert(0 <= how_many); |
| 1386 | assert(0 <= from_start); |
| 1387 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1388 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1389 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1390 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1391 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1392 | assert(PyUnicode_Check(to)); |
| 1393 | assert(PyUnicode_IS_READY(to)); |
| 1394 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1395 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1396 | if (how_many == 0) |
| 1397 | return 0; |
| 1398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1399 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1400 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1401 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1402 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1403 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1404 | #ifdef Py_DEBUG |
| 1405 | if (!check_maxchar |
| 1406 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1407 | { |
| 1408 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1409 | Py_UCS4 ch; |
| 1410 | Py_ssize_t i; |
| 1411 | for (i=0; i < how_many; i++) { |
| 1412 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1413 | assert(ch <= to_maxchar); |
| 1414 | } |
| 1415 | } |
| 1416 | #endif |
| 1417 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1418 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1419 | if (check_maxchar |
| 1420 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1421 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1422 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1423 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1424 | Py_UCS4 max_char; |
| 1425 | max_char = ucs1lib_find_max_char(from_data, |
| 1426 | (Py_UCS1*)from_data + how_many); |
| 1427 | if (max_char >= 128) |
| 1428 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1429 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1430 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1431 | (char*)from_data + from_kind * from_start, |
| 1432 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1433 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1434 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1435 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1436 | { |
| 1437 | _PyUnicode_CONVERT_BYTES( |
| 1438 | Py_UCS1, Py_UCS2, |
| 1439 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1440 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1441 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1442 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1443 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1444 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1445 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1446 | { |
| 1447 | _PyUnicode_CONVERT_BYTES( |
| 1448 | Py_UCS1, Py_UCS4, |
| 1449 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1450 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1451 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1452 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1453 | } |
| 1454 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1455 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1456 | { |
| 1457 | _PyUnicode_CONVERT_BYTES( |
| 1458 | Py_UCS2, Py_UCS4, |
| 1459 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1460 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1461 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1462 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1463 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1464 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1465 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1466 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1467 | if (!check_maxchar) { |
| 1468 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1469 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1470 | { |
| 1471 | _PyUnicode_CONVERT_BYTES( |
| 1472 | Py_UCS2, Py_UCS1, |
| 1473 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1474 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1475 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1476 | ); |
| 1477 | } |
| 1478 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1479 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1480 | { |
| 1481 | _PyUnicode_CONVERT_BYTES( |
| 1482 | Py_UCS4, Py_UCS1, |
| 1483 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1484 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1485 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1486 | ); |
| 1487 | } |
| 1488 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1489 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1490 | { |
| 1491 | _PyUnicode_CONVERT_BYTES( |
| 1492 | Py_UCS4, Py_UCS2, |
| 1493 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1494 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1495 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1496 | ); |
| 1497 | } |
| 1498 | else { |
| 1499 | assert(0); |
| 1500 | return -1; |
| 1501 | } |
| 1502 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1503 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1504 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1505 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1506 | Py_ssize_t i; |
| 1507 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1508 | for (i=0; i < how_many; i++) { |
| 1509 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1510 | if (ch > to_maxchar) |
| 1511 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1512 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1513 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1514 | } |
| 1515 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1516 | return 0; |
| 1517 | } |
| 1518 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1519 | void |
| 1520 | _PyUnicode_FastCopyCharacters( |
| 1521 | PyObject *to, Py_ssize_t to_start, |
| 1522 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1523 | { |
| 1524 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1525 | } |
| 1526 | |
| 1527 | Py_ssize_t |
| 1528 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1529 | PyObject *from, Py_ssize_t from_start, |
| 1530 | Py_ssize_t how_many) |
| 1531 | { |
| 1532 | int err; |
| 1533 | |
| 1534 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1535 | PyErr_BadInternalCall(); |
| 1536 | return -1; |
| 1537 | } |
| 1538 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1539 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1540 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1541 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1542 | return -1; |
| 1543 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1544 | if (from_start < 0) { |
| 1545 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1546 | return -1; |
| 1547 | } |
| 1548 | if (to_start < 0) { |
| 1549 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1550 | return -1; |
| 1551 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1552 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1553 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1554 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 1555 | "Cannot write %zi characters at %zi " |
| 1556 | "in a string of %zi characters", |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1557 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1558 | return -1; |
| 1559 | } |
| 1560 | |
| 1561 | if (how_many == 0) |
| 1562 | return 0; |
| 1563 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1564 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1565 | return -1; |
| 1566 | |
| 1567 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1568 | if (err) { |
| 1569 | PyErr_Format(PyExc_SystemError, |
| 1570 | "Cannot copy %s characters " |
| 1571 | "into a string of %s characters", |
| 1572 | unicode_kind_name(from), |
| 1573 | unicode_kind_name(to)); |
| 1574 | return -1; |
| 1575 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1576 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1579 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1580 | correct string length can be computed before converting a string to UCS4. |
| 1581 | This function counts single surrogates as a character and not as a pair. |
| 1582 | |
| 1583 | Return 0 on success, or -1 on error. */ |
| 1584 | static int |
| 1585 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1586 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1587 | { |
| 1588 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1589 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1590 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1591 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1592 | *num_surrogates = 0; |
| 1593 | *maxchar = 0; |
| 1594 | |
| 1595 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1596 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1597 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1598 | && (iter+1) < end |
| 1599 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1600 | { |
| 1601 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1602 | ++(*num_surrogates); |
| 1603 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1604 | } |
| 1605 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1606 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1607 | { |
| 1608 | ch = *iter; |
| 1609 | iter++; |
| 1610 | } |
| 1611 | if (ch > *maxchar) { |
| 1612 | *maxchar = ch; |
| 1613 | if (*maxchar > MAX_UNICODE) { |
| 1614 | PyErr_Format(PyExc_ValueError, |
| 1615 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1616 | ch); |
| 1617 | return -1; |
| 1618 | } |
| 1619 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1620 | } |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1624 | int |
| 1625 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1626 | { |
| 1627 | wchar_t *end; |
| 1628 | Py_UCS4 maxchar = 0; |
| 1629 | Py_ssize_t num_surrogates; |
| 1630 | #if SIZEOF_WCHAR_T == 2 |
| 1631 | Py_ssize_t length_wo_surrogates; |
| 1632 | #endif |
| 1633 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1634 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1635 | strings were created using _PyObject_New() and where no canonical |
| 1636 | representation (the str field) has been set yet aka strings |
| 1637 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1638 | assert(_PyUnicode_CHECK(unicode)); |
| 1639 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1640 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1641 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1642 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1643 | /* Actually, it should neither be interned nor be anything else: */ |
| 1644 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1645 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1646 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1647 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1648 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1649 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1650 | |
| 1651 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1652 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1653 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1654 | PyErr_NoMemory(); |
| 1655 | return -1; |
| 1656 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1657 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1658 | _PyUnicode_WSTR(unicode), end, |
| 1659 | PyUnicode_1BYTE_DATA(unicode)); |
| 1660 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1661 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1662 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1663 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1664 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1665 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1666 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1667 | } |
| 1668 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1669 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1670 | _PyUnicode_UTF8(unicode) = NULL; |
| 1671 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1672 | } |
| 1673 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1674 | _PyUnicode_WSTR(unicode) = NULL; |
| 1675 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1676 | } |
| 1677 | /* In this case we might have to convert down from 4-byte native |
| 1678 | wchar_t to 2-byte unicode. */ |
| 1679 | else if (maxchar < 65536) { |
| 1680 | assert(num_surrogates == 0 && |
| 1681 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1682 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1683 | #if SIZEOF_WCHAR_T == 2 |
| 1684 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1685 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1686 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1687 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1688 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1689 | _PyUnicode_UTF8(unicode) = NULL; |
| 1690 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1691 | #else |
| 1692 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1693 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1694 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1695 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1696 | PyErr_NoMemory(); |
| 1697 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1698 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1699 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1700 | _PyUnicode_WSTR(unicode), end, |
| 1701 | PyUnicode_2BYTE_DATA(unicode)); |
| 1702 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1703 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1704 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1705 | _PyUnicode_UTF8(unicode) = NULL; |
| 1706 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1707 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1708 | _PyUnicode_WSTR(unicode) = NULL; |
| 1709 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1710 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1711 | } |
| 1712 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1713 | else { |
| 1714 | #if SIZEOF_WCHAR_T == 2 |
| 1715 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1716 | new normalized 4-byte version. */ |
| 1717 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 1718 | if (length_wo_surrogates > PY_SSIZE_T_MAX / 4 - 1) { |
| 1719 | PyErr_NoMemory(); |
| 1720 | return -1; |
| 1721 | } |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1722 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1723 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1724 | PyErr_NoMemory(); |
| 1725 | return -1; |
| 1726 | } |
| 1727 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1728 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1729 | _PyUnicode_UTF8(unicode) = NULL; |
| 1730 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1731 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1732 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1733 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1734 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1735 | _PyUnicode_WSTR(unicode) = NULL; |
| 1736 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1737 | #else |
| 1738 | assert(num_surrogates == 0); |
| 1739 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1740 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1741 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1742 | _PyUnicode_UTF8(unicode) = NULL; |
| 1743 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1744 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1745 | #endif |
| 1746 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1747 | } |
| 1748 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1749 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1750 | return 0; |
| 1751 | } |
| 1752 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1753 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1754 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1755 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1756 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1757 | case SSTATE_NOT_INTERNED: |
| 1758 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1759 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1760 | case SSTATE_INTERNED_MORTAL: |
| 1761 | /* revive dead object temporarily for DelItem */ |
| 1762 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1763 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1764 | Py_FatalError( |
| 1765 | "deletion of interned string failed"); |
| 1766 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1767 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1768 | case SSTATE_INTERNED_IMMORTAL: |
| 1769 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1770 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1771 | default: |
| 1772 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1775 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1776 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1777 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1778 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1779 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1780 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1781 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1782 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1785 | #ifdef Py_DEBUG |
| 1786 | static int |
| 1787 | unicode_is_singleton(PyObject *unicode) |
| 1788 | { |
| 1789 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1790 | if (unicode == unicode_empty) |
| 1791 | return 1; |
| 1792 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1793 | { |
| 1794 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1795 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1796 | return 1; |
| 1797 | } |
| 1798 | return 0; |
| 1799 | } |
| 1800 | #endif |
| 1801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1802 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1803 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1804 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1805 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1806 | if (Py_REFCNT(unicode) != 1) |
| 1807 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1808 | if (_PyUnicode_HASH(unicode) != -1) |
| 1809 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1810 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1811 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1812 | if (!PyUnicode_CheckExact(unicode)) |
| 1813 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1814 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1815 | /* singleton refcount is greater than 1 */ |
| 1816 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1817 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1818 | return 1; |
| 1819 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1820 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1821 | static int |
| 1822 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1823 | { |
| 1824 | PyObject *unicode; |
| 1825 | Py_ssize_t old_length; |
| 1826 | |
| 1827 | assert(p_unicode != NULL); |
| 1828 | unicode = *p_unicode; |
| 1829 | |
| 1830 | assert(unicode != NULL); |
| 1831 | assert(PyUnicode_Check(unicode)); |
| 1832 | assert(0 <= length); |
| 1833 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1834 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1835 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1836 | else |
| 1837 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1838 | if (old_length == length) |
| 1839 | return 0; |
| 1840 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1841 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1842 | _Py_INCREF_UNICODE_EMPTY(); |
| 1843 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1844 | return -1; |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 1845 | Py_SETREF(*p_unicode, unicode_empty); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1846 | return 0; |
| 1847 | } |
| 1848 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1849 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1850 | PyObject *copy = resize_copy(unicode, length); |
| 1851 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1852 | return -1; |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 1853 | Py_SETREF(*p_unicode, copy); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1854 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1857 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1858 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1859 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1860 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1861 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1862 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1863 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1864 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1867 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1868 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1869 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1870 | PyObject *unicode; |
| 1871 | if (p_unicode == NULL) { |
| 1872 | PyErr_BadInternalCall(); |
| 1873 | return -1; |
| 1874 | } |
| 1875 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1876 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1877 | { |
| 1878 | PyErr_BadInternalCall(); |
| 1879 | return -1; |
| 1880 | } |
| 1881 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1882 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1883 | |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 1884 | /* Copy an ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1885 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1886 | WARNING: The function doesn't copy the terminating null character and |
| 1887 | doesn't check the maximum character (may write a latin1 character in an |
| 1888 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1889 | static void |
| 1890 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1891 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1892 | { |
| 1893 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1894 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1895 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1896 | |
| 1897 | switch (kind) { |
| 1898 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1899 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1900 | #ifdef Py_DEBUG |
| 1901 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1902 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1903 | (const Py_UCS1*)str, |
| 1904 | (const Py_UCS1*)str + len); |
| 1905 | assert(maxchar < 128); |
| 1906 | } |
| 1907 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1908 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1909 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1910 | } |
| 1911 | case PyUnicode_2BYTE_KIND: { |
| 1912 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1913 | Py_UCS2 *ucs2 = start; |
| 1914 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1915 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1916 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1917 | *ucs2 = (Py_UCS2)*str; |
| 1918 | |
| 1919 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1920 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1921 | } |
| 1922 | default: { |
| 1923 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1924 | Py_UCS4 *ucs4 = start; |
| 1925 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1926 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1927 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1928 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1929 | *ucs4 = (Py_UCS4)*str; |
| 1930 | |
| 1931 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1936 | static PyObject* |
| 1937 | get_latin1_char(unsigned char ch) |
| 1938 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1939 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1940 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1941 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1942 | if (!unicode) |
| 1943 | return NULL; |
| 1944 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1945 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | unicode_latin1[ch] = unicode; |
| 1947 | } |
| 1948 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1949 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1950 | } |
| 1951 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1952 | static PyObject* |
| 1953 | unicode_char(Py_UCS4 ch) |
| 1954 | { |
| 1955 | PyObject *unicode; |
| 1956 | |
| 1957 | assert(ch <= MAX_UNICODE); |
| 1958 | |
Victor Stinner | f3b46b4 | 2014-01-03 13:16:00 +0100 | [diff] [blame] | 1959 | if (ch < 256) |
| 1960 | return get_latin1_char(ch); |
| 1961 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1962 | unicode = PyUnicode_New(1, ch); |
| 1963 | if (unicode == NULL) |
| 1964 | return NULL; |
| 1965 | switch (PyUnicode_KIND(unicode)) { |
| 1966 | case PyUnicode_1BYTE_KIND: |
| 1967 | PyUnicode_1BYTE_DATA(unicode)[0] = (Py_UCS1)ch; |
| 1968 | break; |
| 1969 | case PyUnicode_2BYTE_KIND: |
| 1970 | PyUnicode_2BYTE_DATA(unicode)[0] = (Py_UCS2)ch; |
| 1971 | break; |
| 1972 | default: |
| 1973 | assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1974 | PyUnicode_4BYTE_DATA(unicode)[0] = ch; |
| 1975 | } |
| 1976 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1977 | return unicode; |
| 1978 | } |
| 1979 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1980 | PyObject * |
| 1981 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1982 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1983 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1984 | Py_UCS4 maxchar = 0; |
| 1985 | Py_ssize_t num_surrogates; |
| 1986 | |
| 1987 | if (u == NULL) |
| 1988 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1989 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1990 | /* If the Unicode data is known at construction time, we can apply |
| 1991 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1992 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1993 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1994 | if (size == 0) |
| 1995 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1996 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1997 | /* Single character Unicode objects in the Latin-1 range are |
| 1998 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1999 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2000 | return get_latin1_char((unsigned char)*u); |
| 2001 | |
| 2002 | /* If not empty and not single character, copy the Unicode data |
| 2003 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 2004 | if (find_maxchar_surrogates(u, u + size, |
| 2005 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2006 | return NULL; |
| 2007 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2008 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2009 | if (!unicode) |
| 2010 | return NULL; |
| 2011 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2012 | switch (PyUnicode_KIND(unicode)) { |
| 2013 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 2014 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2015 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 2016 | break; |
| 2017 | case PyUnicode_2BYTE_KIND: |
| 2018 | #if Py_UNICODE_SIZE == 2 |
| 2019 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 2020 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 2021 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2022 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 2023 | #endif |
| 2024 | break; |
| 2025 | case PyUnicode_4BYTE_KIND: |
| 2026 | #if SIZEOF_WCHAR_T == 2 |
| 2027 | /* This is the only case which has to process surrogates, thus |
| 2028 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 2029 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2030 | #else |
| 2031 | assert(num_surrogates == 0); |
| 2032 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 2033 | #endif |
| 2034 | break; |
| 2035 | default: |
| 2036 | assert(0 && "Impossible state"); |
| 2037 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2038 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2039 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2042 | PyObject * |
| 2043 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 2044 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2045 | if (size < 0) { |
| 2046 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2047 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2048 | return NULL; |
| 2049 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2050 | if (u != NULL) |
| 2051 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 2052 | else |
| 2053 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2056 | PyObject * |
| 2057 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2058 | { |
| 2059 | size_t size = strlen(u); |
| 2060 | if (size > PY_SSIZE_T_MAX) { |
| 2061 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 2062 | return NULL; |
| 2063 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2064 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2067 | PyObject * |
| 2068 | _PyUnicode_FromId(_Py_Identifier *id) |
| 2069 | { |
| 2070 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 2071 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 2072 | strlen(id->string), |
| 2073 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2074 | if (!id->object) |
| 2075 | return NULL; |
| 2076 | PyUnicode_InternInPlace(&id->object); |
| 2077 | assert(!id->next); |
| 2078 | id->next = static_strings; |
| 2079 | static_strings = id; |
| 2080 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2081 | return id->object; |
| 2082 | } |
| 2083 | |
| 2084 | void |
| 2085 | _PyUnicode_ClearStaticStrings() |
| 2086 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2087 | _Py_Identifier *tmp, *s = static_strings; |
| 2088 | while (s) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 2089 | Py_CLEAR(s->object); |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2090 | tmp = s->next; |
| 2091 | s->next = NULL; |
| 2092 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2093 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2094 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2095 | } |
| 2096 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 2097 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2098 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2099 | PyObject* |
| 2100 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2101 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2102 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2103 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 2104 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 2105 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2106 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 2107 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 2108 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 2109 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2110 | unicode = PyUnicode_New(size, 127); |
| 2111 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2112 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2113 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 2114 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 2115 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2116 | } |
| 2117 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2118 | static Py_UCS4 |
| 2119 | kind_maxchar_limit(unsigned int kind) |
| 2120 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2121 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2122 | case PyUnicode_1BYTE_KIND: |
| 2123 | return 0x80; |
| 2124 | case PyUnicode_2BYTE_KIND: |
| 2125 | return 0x100; |
| 2126 | case PyUnicode_4BYTE_KIND: |
| 2127 | return 0x10000; |
| 2128 | default: |
| 2129 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2130 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2134 | Py_LOCAL_INLINE(Py_UCS4) |
| 2135 | align_maxchar(Py_UCS4 maxchar) |
| 2136 | { |
| 2137 | if (maxchar <= 127) |
| 2138 | return 127; |
| 2139 | else if (maxchar <= 255) |
| 2140 | return 255; |
| 2141 | else if (maxchar <= 65535) |
| 2142 | return 65535; |
| 2143 | else |
| 2144 | return MAX_UNICODE; |
| 2145 | } |
| 2146 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2147 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2148 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2149 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2150 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2151 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2152 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2153 | if (size == 0) |
| 2154 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2155 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 2156 | if (size == 1) |
| 2157 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2158 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2159 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2160 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2161 | if (!res) |
| 2162 | return NULL; |
| 2163 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2164 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2165 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2168 | static PyObject* |
| 2169 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2170 | { |
| 2171 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2172 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2173 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2174 | if (size == 0) |
| 2175 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2176 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2177 | if (size == 1) |
| 2178 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2179 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2180 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2181 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2182 | if (!res) |
| 2183 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2184 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2185 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2186 | else { |
| 2187 | _PyUnicode_CONVERT_BYTES( |
| 2188 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 2189 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2190 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2191 | return res; |
| 2192 | } |
| 2193 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2194 | static PyObject* |
| 2195 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2196 | { |
| 2197 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2198 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2199 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2200 | if (size == 0) |
| 2201 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2202 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2203 | if (size == 1) |
| 2204 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2205 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2206 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2207 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2208 | if (!res) |
| 2209 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2210 | if (max_char < 256) |
| 2211 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2212 | PyUnicode_1BYTE_DATA(res)); |
| 2213 | else if (max_char < 0x10000) |
| 2214 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2215 | PyUnicode_2BYTE_DATA(res)); |
| 2216 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2217 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2218 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2219 | return res; |
| 2220 | } |
| 2221 | |
| 2222 | PyObject* |
| 2223 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2224 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2225 | if (size < 0) { |
| 2226 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2227 | return NULL; |
| 2228 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2229 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2230 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2231 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2232 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2233 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2234 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2235 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2236 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2237 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2238 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2239 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2240 | } |
| 2241 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2242 | Py_UCS4 |
| 2243 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2244 | { |
| 2245 | enum PyUnicode_Kind kind; |
| 2246 | void *startptr, *endptr; |
| 2247 | |
| 2248 | assert(PyUnicode_IS_READY(unicode)); |
| 2249 | assert(0 <= start); |
| 2250 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2251 | assert(start <= end); |
| 2252 | |
| 2253 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2254 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2255 | |
| 2256 | if (start == end) |
| 2257 | return 127; |
| 2258 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2259 | if (PyUnicode_IS_ASCII(unicode)) |
| 2260 | return 127; |
| 2261 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2262 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2263 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2264 | endptr = (char *)startptr + end * kind; |
| 2265 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2266 | switch(kind) { |
| 2267 | case PyUnicode_1BYTE_KIND: |
| 2268 | return ucs1lib_find_max_char(startptr, endptr); |
| 2269 | case PyUnicode_2BYTE_KIND: |
| 2270 | return ucs2lib_find_max_char(startptr, endptr); |
| 2271 | case PyUnicode_4BYTE_KIND: |
| 2272 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2273 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2274 | assert(0); |
| 2275 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2276 | } |
| 2277 | } |
| 2278 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2279 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2280 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2281 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2282 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2283 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2284 | { |
| 2285 | PyObject *unicode, *copy; |
| 2286 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2287 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2288 | unsigned int kind; |
| 2289 | |
| 2290 | assert(p_unicode != NULL); |
| 2291 | unicode = *p_unicode; |
| 2292 | assert(PyUnicode_IS_READY(unicode)); |
| 2293 | if (PyUnicode_IS_ASCII(unicode)) |
| 2294 | return; |
| 2295 | |
| 2296 | len = PyUnicode_GET_LENGTH(unicode); |
| 2297 | kind = PyUnicode_KIND(unicode); |
| 2298 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2299 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2300 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2301 | if (max_char >= 128) |
| 2302 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2303 | } |
| 2304 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2305 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2306 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2307 | if (max_char >= 256) |
| 2308 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2309 | } |
| 2310 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2311 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2312 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2313 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2314 | if (max_char >= 0x10000) |
| 2315 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2316 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2317 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2318 | if (copy != NULL) |
| 2319 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2320 | Py_DECREF(unicode); |
| 2321 | *p_unicode = copy; |
| 2322 | } |
| 2323 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2324 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2325 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2326 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2327 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2328 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2329 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2330 | if (!PyUnicode_Check(unicode)) { |
| 2331 | PyErr_BadInternalCall(); |
| 2332 | return NULL; |
| 2333 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2334 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2335 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2336 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2337 | length = PyUnicode_GET_LENGTH(unicode); |
| 2338 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2339 | if (!copy) |
| 2340 | return NULL; |
| 2341 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2342 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2343 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2344 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2345 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2346 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2347 | } |
| 2348 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2349 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2350 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2351 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2352 | |
| 2353 | void* |
| 2354 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2355 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2356 | Py_ssize_t len; |
| 2357 | void *result; |
| 2358 | unsigned int skind; |
| 2359 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2360 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2361 | return NULL; |
| 2362 | |
| 2363 | len = PyUnicode_GET_LENGTH(s); |
| 2364 | skind = PyUnicode_KIND(s); |
| 2365 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2366 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2367 | return NULL; |
| 2368 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2369 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2370 | case PyUnicode_2BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2371 | result = PyMem_New(Py_UCS2, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2372 | if (!result) |
| 2373 | return PyErr_NoMemory(); |
| 2374 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2375 | _PyUnicode_CONVERT_BYTES( |
| 2376 | Py_UCS1, Py_UCS2, |
| 2377 | PyUnicode_1BYTE_DATA(s), |
| 2378 | PyUnicode_1BYTE_DATA(s) + len, |
| 2379 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2380 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2381 | case PyUnicode_4BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2382 | result = PyMem_New(Py_UCS4, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2383 | if (!result) |
| 2384 | return PyErr_NoMemory(); |
| 2385 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2386 | _PyUnicode_CONVERT_BYTES( |
| 2387 | Py_UCS2, Py_UCS4, |
| 2388 | PyUnicode_2BYTE_DATA(s), |
| 2389 | PyUnicode_2BYTE_DATA(s) + len, |
| 2390 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2391 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2392 | else { |
| 2393 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2394 | _PyUnicode_CONVERT_BYTES( |
| 2395 | Py_UCS1, Py_UCS4, |
| 2396 | PyUnicode_1BYTE_DATA(s), |
| 2397 | PyUnicode_1BYTE_DATA(s) + len, |
| 2398 | result); |
| 2399 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2400 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2401 | default: |
| 2402 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2403 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2404 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2405 | return NULL; |
| 2406 | } |
| 2407 | |
| 2408 | static Py_UCS4* |
| 2409 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2410 | int copy_null) |
| 2411 | { |
| 2412 | int kind; |
| 2413 | void *data; |
| 2414 | Py_ssize_t len, targetlen; |
| 2415 | if (PyUnicode_READY(string) == -1) |
| 2416 | return NULL; |
| 2417 | kind = PyUnicode_KIND(string); |
| 2418 | data = PyUnicode_DATA(string); |
| 2419 | len = PyUnicode_GET_LENGTH(string); |
| 2420 | targetlen = len; |
| 2421 | if (copy_null) |
| 2422 | targetlen++; |
| 2423 | if (!target) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2424 | target = PyMem_New(Py_UCS4, targetlen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2425 | if (!target) { |
| 2426 | PyErr_NoMemory(); |
| 2427 | return NULL; |
| 2428 | } |
| 2429 | } |
| 2430 | else { |
| 2431 | if (targetsize < targetlen) { |
| 2432 | PyErr_Format(PyExc_SystemError, |
| 2433 | "string is longer than the buffer"); |
| 2434 | if (copy_null && 0 < targetsize) |
| 2435 | target[0] = 0; |
| 2436 | return NULL; |
| 2437 | } |
| 2438 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2439 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2440 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2441 | _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] | 2442 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2443 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2444 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2445 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2446 | } |
| 2447 | else { |
| 2448 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2449 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2450 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2451 | if (copy_null) |
| 2452 | target[len] = 0; |
| 2453 | return target; |
| 2454 | } |
| 2455 | |
| 2456 | Py_UCS4* |
| 2457 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2458 | int copy_null) |
| 2459 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2460 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2461 | PyErr_BadInternalCall(); |
| 2462 | return NULL; |
| 2463 | } |
| 2464 | return as_ucs4(string, target, targetsize, copy_null); |
| 2465 | } |
| 2466 | |
| 2467 | Py_UCS4* |
| 2468 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2469 | { |
| 2470 | return as_ucs4(string, NULL, 0, 1); |
| 2471 | } |
| 2472 | |
| 2473 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2474 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2475 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2476 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2477 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2478 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2479 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2480 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2481 | PyErr_BadInternalCall(); |
| 2482 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2485 | if (size == -1) { |
| 2486 | size = wcslen(w); |
| 2487 | } |
| 2488 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2489 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2492 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2493 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2494 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2495 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2496 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2497 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2498 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2499 | static int |
| 2500 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2501 | Py_ssize_t width, Py_ssize_t precision) |
| 2502 | { |
| 2503 | Py_ssize_t length, fill, arglen; |
| 2504 | Py_UCS4 maxchar; |
| 2505 | |
| 2506 | if (PyUnicode_READY(str) == -1) |
| 2507 | return -1; |
| 2508 | |
| 2509 | length = PyUnicode_GET_LENGTH(str); |
| 2510 | if ((precision == -1 || precision >= length) |
| 2511 | && width <= length) |
| 2512 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2513 | |
| 2514 | if (precision != -1) |
| 2515 | length = Py_MIN(precision, length); |
| 2516 | |
| 2517 | arglen = Py_MAX(length, width); |
| 2518 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2519 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2520 | else |
| 2521 | maxchar = writer->maxchar; |
| 2522 | |
| 2523 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2524 | return -1; |
| 2525 | |
| 2526 | if (width > length) { |
| 2527 | fill = width - length; |
| 2528 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2529 | return -1; |
| 2530 | writer->pos += fill; |
| 2531 | } |
| 2532 | |
| 2533 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2534 | str, 0, length); |
| 2535 | writer->pos += length; |
| 2536 | return 0; |
| 2537 | } |
| 2538 | |
| 2539 | static int |
| 2540 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2541 | Py_ssize_t width, Py_ssize_t precision) |
| 2542 | { |
| 2543 | /* UTF-8 */ |
| 2544 | Py_ssize_t length; |
| 2545 | PyObject *unicode; |
| 2546 | int res; |
| 2547 | |
| 2548 | length = strlen(str); |
| 2549 | if (precision != -1) |
| 2550 | length = Py_MIN(length, precision); |
| 2551 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2552 | if (unicode == NULL) |
| 2553 | return -1; |
| 2554 | |
| 2555 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2556 | Py_DECREF(unicode); |
| 2557 | return res; |
| 2558 | } |
| 2559 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2560 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2561 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2562 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2563 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2564 | const char *p; |
| 2565 | Py_ssize_t len; |
| 2566 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2567 | Py_ssize_t width; |
| 2568 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2569 | int longflag; |
| 2570 | int longlongflag; |
| 2571 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2572 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2573 | |
| 2574 | p = f; |
| 2575 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2576 | zeropad = 0; |
| 2577 | if (*f == '0') { |
| 2578 | zeropad = 1; |
| 2579 | f++; |
| 2580 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2581 | |
| 2582 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2583 | width = -1; |
| 2584 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2585 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2586 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2587 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2588 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2589 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2590 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2591 | return NULL; |
| 2592 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2593 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2594 | f++; |
| 2595 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2596 | } |
| 2597 | precision = -1; |
| 2598 | if (*f == '.') { |
| 2599 | f++; |
| 2600 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2601 | precision = (*f - '0'); |
| 2602 | f++; |
| 2603 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2604 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2605 | PyErr_SetString(PyExc_ValueError, |
| 2606 | "precision too big"); |
| 2607 | return NULL; |
| 2608 | } |
| 2609 | precision = (precision * 10) + (*f - '0'); |
| 2610 | f++; |
| 2611 | } |
| 2612 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2613 | if (*f == '%') { |
| 2614 | /* "%.3%s" => f points to "3" */ |
| 2615 | f--; |
| 2616 | } |
| 2617 | } |
| 2618 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2619 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2620 | f--; |
| 2621 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2622 | |
| 2623 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2624 | longflag = 0; |
| 2625 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2626 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2627 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2628 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2629 | longflag = 1; |
| 2630 | ++f; |
| 2631 | } |
| 2632 | #ifdef HAVE_LONG_LONG |
| 2633 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2634 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2635 | longlongflag = 1; |
| 2636 | f += 2; |
| 2637 | } |
| 2638 | #endif |
| 2639 | } |
| 2640 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2641 | 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] | 2642 | size_tflag = 1; |
| 2643 | ++f; |
| 2644 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2645 | |
| 2646 | if (f[1] == '\0') |
| 2647 | writer->overallocate = 0; |
| 2648 | |
| 2649 | switch (*f) { |
| 2650 | case 'c': |
| 2651 | { |
| 2652 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2653 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2654 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2655 | "character argument not in range(0x110000)"); |
| 2656 | return NULL; |
| 2657 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2658 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2659 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2660 | break; |
| 2661 | } |
| 2662 | |
| 2663 | case 'i': |
| 2664 | case 'd': |
| 2665 | case 'u': |
| 2666 | case 'x': |
| 2667 | { |
| 2668 | /* used by sprintf */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2669 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2670 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2671 | |
| 2672 | if (*f == 'u') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2673 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2674 | len = sprintf(buffer, "%lu", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2675 | va_arg(*vargs, unsigned long)); |
| 2676 | #ifdef HAVE_LONG_LONG |
| 2677 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2678 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2679 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2680 | #endif |
| 2681 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2682 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2683 | va_arg(*vargs, size_t)); |
| 2684 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2685 | len = sprintf(buffer, "%u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2686 | va_arg(*vargs, unsigned int)); |
| 2687 | } |
| 2688 | else if (*f == 'x') { |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2689 | len = sprintf(buffer, "%x", va_arg(*vargs, int)); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2690 | } |
| 2691 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2692 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2693 | len = sprintf(buffer, "%li", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2694 | va_arg(*vargs, long)); |
| 2695 | #ifdef HAVE_LONG_LONG |
| 2696 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2697 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2698 | va_arg(*vargs, PY_LONG_LONG)); |
| 2699 | #endif |
| 2700 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2701 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2702 | va_arg(*vargs, Py_ssize_t)); |
| 2703 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2704 | len = sprintf(buffer, "%i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2705 | va_arg(*vargs, int)); |
| 2706 | } |
| 2707 | assert(len >= 0); |
| 2708 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2709 | if (precision < len) |
| 2710 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2711 | |
| 2712 | arglen = Py_MAX(precision, width); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2713 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2714 | return NULL; |
| 2715 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2716 | if (width > precision) { |
| 2717 | Py_UCS4 fillchar; |
| 2718 | fill = width - precision; |
| 2719 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2720 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2721 | return NULL; |
| 2722 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2723 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2724 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2725 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2726 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2727 | return NULL; |
| 2728 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2729 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2730 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2731 | if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) |
| 2732 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2733 | break; |
| 2734 | } |
| 2735 | |
| 2736 | case 'p': |
| 2737 | { |
| 2738 | char number[MAX_LONG_LONG_CHARS]; |
| 2739 | |
| 2740 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2741 | assert(len >= 0); |
| 2742 | |
| 2743 | /* %p is ill-defined: ensure leading 0x. */ |
| 2744 | if (number[1] == 'X') |
| 2745 | number[1] = 'x'; |
| 2746 | else if (number[1] != 'x') { |
| 2747 | memmove(number + 2, number, |
| 2748 | strlen(number) + 1); |
| 2749 | number[0] = '0'; |
| 2750 | number[1] = 'x'; |
| 2751 | len += 2; |
| 2752 | } |
| 2753 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2754 | if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2755 | return NULL; |
| 2756 | break; |
| 2757 | } |
| 2758 | |
| 2759 | case 's': |
| 2760 | { |
| 2761 | /* UTF-8 */ |
| 2762 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2763 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2764 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2765 | break; |
| 2766 | } |
| 2767 | |
| 2768 | case 'U': |
| 2769 | { |
| 2770 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2771 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2772 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2773 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2774 | return NULL; |
| 2775 | break; |
| 2776 | } |
| 2777 | |
| 2778 | case 'V': |
| 2779 | { |
| 2780 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2781 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2782 | if (obj) { |
| 2783 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2784 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2785 | return NULL; |
| 2786 | } |
| 2787 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2788 | assert(str != NULL); |
| 2789 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2790 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2791 | } |
| 2792 | break; |
| 2793 | } |
| 2794 | |
| 2795 | case 'S': |
| 2796 | { |
| 2797 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2798 | PyObject *str; |
| 2799 | assert(obj); |
| 2800 | str = PyObject_Str(obj); |
| 2801 | if (!str) |
| 2802 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2803 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2804 | Py_DECREF(str); |
| 2805 | return NULL; |
| 2806 | } |
| 2807 | Py_DECREF(str); |
| 2808 | break; |
| 2809 | } |
| 2810 | |
| 2811 | case 'R': |
| 2812 | { |
| 2813 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2814 | PyObject *repr; |
| 2815 | assert(obj); |
| 2816 | repr = PyObject_Repr(obj); |
| 2817 | if (!repr) |
| 2818 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2819 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2820 | Py_DECREF(repr); |
| 2821 | return NULL; |
| 2822 | } |
| 2823 | Py_DECREF(repr); |
| 2824 | break; |
| 2825 | } |
| 2826 | |
| 2827 | case 'A': |
| 2828 | { |
| 2829 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2830 | PyObject *ascii; |
| 2831 | assert(obj); |
| 2832 | ascii = PyObject_ASCII(obj); |
| 2833 | if (!ascii) |
| 2834 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2835 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2836 | Py_DECREF(ascii); |
| 2837 | return NULL; |
| 2838 | } |
| 2839 | Py_DECREF(ascii); |
| 2840 | break; |
| 2841 | } |
| 2842 | |
| 2843 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2844 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2845 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2846 | break; |
| 2847 | |
| 2848 | default: |
| 2849 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2850 | of the format string to the output string. (we cannot just |
| 2851 | skip the code, since there's no way to know what's in the |
| 2852 | argument list) */ |
| 2853 | len = strlen(p); |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2854 | if (_PyUnicodeWriter_WriteLatin1String(writer, p, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2855 | return NULL; |
| 2856 | f = p+len; |
| 2857 | return f; |
| 2858 | } |
| 2859 | |
| 2860 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2861 | return f; |
| 2862 | } |
| 2863 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2864 | PyObject * |
| 2865 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2866 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2867 | va_list vargs2; |
| 2868 | const char *f; |
| 2869 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2870 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2871 | _PyUnicodeWriter_Init(&writer); |
| 2872 | writer.min_length = strlen(format) + 100; |
| 2873 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2874 | |
| 2875 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2876 | Copy it to be able to pass a reference to a subfunction. */ |
| 2877 | Py_VA_COPY(vargs2, vargs); |
| 2878 | |
| 2879 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2880 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2881 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2882 | if (f == NULL) |
| 2883 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2884 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2885 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2886 | const char *p; |
| 2887 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2888 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2889 | p = f; |
| 2890 | do |
| 2891 | { |
| 2892 | if ((unsigned char)*p > 127) { |
| 2893 | PyErr_Format(PyExc_ValueError, |
| 2894 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2895 | "string, got a non-ASCII byte: 0x%02x", |
| 2896 | (unsigned char)*p); |
| 2897 | return NULL; |
| 2898 | } |
| 2899 | p++; |
| 2900 | } |
| 2901 | while (*p != '\0' && *p != '%'); |
| 2902 | len = p - f; |
| 2903 | |
| 2904 | if (*p == '\0') |
| 2905 | writer.overallocate = 0; |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2906 | |
| 2907 | if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2908 | goto fail; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2909 | |
| 2910 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2911 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2912 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2913 | return _PyUnicodeWriter_Finish(&writer); |
| 2914 | |
| 2915 | fail: |
| 2916 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2917 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2920 | PyObject * |
| 2921 | PyUnicode_FromFormat(const char *format, ...) |
| 2922 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2923 | PyObject* ret; |
| 2924 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2925 | |
| 2926 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2927 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2928 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2929 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2930 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2931 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2932 | va_end(vargs); |
| 2933 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2936 | #ifdef HAVE_WCHAR_H |
| 2937 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2938 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2939 | convert a Unicode object to a wide character string. |
| 2940 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2941 | - 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] | 2942 | character) required to convert the unicode object. Ignore size argument. |
| 2943 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2944 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2945 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2946 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2947 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2948 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2949 | wchar_t *w, |
| 2950 | Py_ssize_t size) |
| 2951 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2952 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2953 | const wchar_t *wstr; |
| 2954 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2955 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2956 | if (wstr == NULL) |
| 2957 | return -1; |
| 2958 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2959 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2960 | if (size > res) |
| 2961 | size = res + 1; |
| 2962 | else |
| 2963 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2964 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2965 | return res; |
| 2966 | } |
| 2967 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2968 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2972 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2973 | wchar_t *w, |
| 2974 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2975 | { |
| 2976 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2977 | PyErr_BadInternalCall(); |
| 2978 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2979 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2980 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2983 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2984 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2985 | Py_ssize_t *size) |
| 2986 | { |
| 2987 | wchar_t* buffer; |
| 2988 | Py_ssize_t buflen; |
| 2989 | |
| 2990 | if (unicode == NULL) { |
| 2991 | PyErr_BadInternalCall(); |
| 2992 | return NULL; |
| 2993 | } |
| 2994 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2995 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2996 | if (buflen == -1) |
| 2997 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2998 | buffer = PyMem_NEW(wchar_t, buflen); |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2999 | if (buffer == NULL) { |
| 3000 | PyErr_NoMemory(); |
| 3001 | return NULL; |
| 3002 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3003 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 3004 | if (buflen == -1) { |
| 3005 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3006 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 3007 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 3008 | if (size != NULL) |
| 3009 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 3010 | return buffer; |
| 3011 | } |
| 3012 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3013 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3014 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3015 | PyObject * |
| 3016 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3017 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3018 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3019 | PyErr_SetString(PyExc_ValueError, |
| 3020 | "chr() arg not in range(0x110000)"); |
| 3021 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3022 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 3023 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 3024 | return unicode_char((Py_UCS4)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3027 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3028 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3029 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3030 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3031 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3032 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3033 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 3034 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3035 | Py_INCREF(obj); |
| 3036 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3037 | } |
| 3038 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3039 | /* For a Unicode subtype that's not a Unicode object, |
| 3040 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 3041 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3042 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3043 | PyErr_Format(PyExc_TypeError, |
| 3044 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3045 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3046 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3049 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3050 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3051 | const char *encoding, |
| 3052 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3053 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3054 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3055 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3056 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3057 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3058 | PyErr_BadInternalCall(); |
| 3059 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3061 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3062 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3063 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3064 | if (PyBytes_GET_SIZE(obj) == 0) |
| 3065 | _Py_RETURN_UNICODE_EMPTY(); |
| 3066 | v = PyUnicode_Decode( |
| 3067 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3068 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3069 | return v; |
| 3070 | } |
| 3071 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3072 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3073 | PyErr_SetString(PyExc_TypeError, |
| 3074 | "decoding str is not supported"); |
| 3075 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3076 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3077 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3078 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3079 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3080 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 3081 | "decoding to str: need a bytes-like object, %.80s found", |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3082 | Py_TYPE(obj)->tp_name); |
| 3083 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3084 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3085 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3086 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3087 | PyBuffer_Release(&buffer); |
| 3088 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3089 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3090 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3091 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3092 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3093 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3096 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3097 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3098 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3099 | int |
| 3100 | _Py_normalize_encoding(const char *encoding, |
| 3101 | char *lower, |
| 3102 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3103 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3104 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3105 | char *l; |
| 3106 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3107 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3108 | if (encoding == NULL) { |
Victor Stinner | 66b3270 | 2013-11-07 23:12:23 +0100 | [diff] [blame] | 3109 | /* 6 == strlen("utf-8") + 1 */ |
Victor Stinner | df23e30 | 2013-11-07 13:33:36 +0100 | [diff] [blame] | 3110 | if (lower_len < 6) |
| 3111 | return 0; |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3112 | strcpy(lower, "utf-8"); |
| 3113 | return 1; |
| 3114 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3115 | e = encoding; |
| 3116 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3117 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3118 | while (*e) { |
| 3119 | if (l == l_end) |
| 3120 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3121 | if (Py_ISUPPER(*e)) { |
| 3122 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3123 | } |
| 3124 | else if (*e == '_') { |
| 3125 | *l++ = '-'; |
| 3126 | e++; |
| 3127 | } |
| 3128 | else { |
| 3129 | *l++ = *e++; |
| 3130 | } |
| 3131 | } |
| 3132 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3133 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3136 | PyObject * |
| 3137 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3138 | Py_ssize_t size, |
| 3139 | const char *encoding, |
| 3140 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3141 | { |
| 3142 | PyObject *buffer = NULL, *unicode; |
| 3143 | Py_buffer info; |
| 3144 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3145 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3146 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3147 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3148 | if ((strcmp(lower, "utf-8") == 0) || |
| 3149 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3150 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3151 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3152 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3153 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3154 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3155 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3156 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3157 | else if (strcmp(lower, "mbcs") == 0) |
| 3158 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3159 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3160 | else if (strcmp(lower, "ascii") == 0) |
| 3161 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3162 | else if (strcmp(lower, "utf-16") == 0) |
| 3163 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3164 | else if (strcmp(lower, "utf-32") == 0) |
| 3165 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3166 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3167 | |
| 3168 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3169 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3170 | 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] | 3171 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3172 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3173 | if (buffer == NULL) |
| 3174 | goto onError; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3175 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3176 | if (unicode == NULL) |
| 3177 | goto onError; |
| 3178 | if (!PyUnicode_Check(unicode)) { |
| 3179 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3180 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3181 | "use codecs.decode() to decode to arbitrary types", |
| 3182 | encoding, |
| 3183 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3184 | Py_DECREF(unicode); |
| 3185 | goto onError; |
| 3186 | } |
| 3187 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3188 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3189 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3190 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3191 | Py_XDECREF(buffer); |
| 3192 | return NULL; |
| 3193 | } |
| 3194 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3195 | PyObject * |
| 3196 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3197 | const char *encoding, |
| 3198 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3199 | { |
| 3200 | PyObject *v; |
| 3201 | |
| 3202 | if (!PyUnicode_Check(unicode)) { |
| 3203 | PyErr_BadArgument(); |
| 3204 | goto onError; |
| 3205 | } |
| 3206 | |
| 3207 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3208 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3209 | |
| 3210 | /* Decode via the codec registry */ |
| 3211 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3212 | if (v == NULL) |
| 3213 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3214 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3215 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3216 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3217 | return NULL; |
| 3218 | } |
| 3219 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3220 | PyObject * |
| 3221 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3222 | const char *encoding, |
| 3223 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3224 | { |
| 3225 | PyObject *v; |
| 3226 | |
| 3227 | if (!PyUnicode_Check(unicode)) { |
| 3228 | PyErr_BadArgument(); |
| 3229 | goto onError; |
| 3230 | } |
| 3231 | |
| 3232 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3233 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3234 | |
| 3235 | /* Decode via the codec registry */ |
| 3236 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3237 | if (v == NULL) |
| 3238 | goto onError; |
| 3239 | if (!PyUnicode_Check(v)) { |
| 3240 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3241 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3242 | "use codecs.decode() to decode to arbitrary types", |
| 3243 | encoding, |
| 3244 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3245 | Py_DECREF(v); |
| 3246 | goto onError; |
| 3247 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3248 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3249 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3250 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3251 | return NULL; |
| 3252 | } |
| 3253 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3254 | PyObject * |
| 3255 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3256 | Py_ssize_t size, |
| 3257 | const char *encoding, |
| 3258 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3259 | { |
| 3260 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3261 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3262 | unicode = PyUnicode_FromUnicode(s, size); |
| 3263 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3264 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3265 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3266 | Py_DECREF(unicode); |
| 3267 | return v; |
| 3268 | } |
| 3269 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3270 | PyObject * |
| 3271 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3272 | const char *encoding, |
| 3273 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3274 | { |
| 3275 | PyObject *v; |
| 3276 | |
| 3277 | if (!PyUnicode_Check(unicode)) { |
| 3278 | PyErr_BadArgument(); |
| 3279 | goto onError; |
| 3280 | } |
| 3281 | |
| 3282 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3283 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3284 | |
| 3285 | /* Encode via the codec registry */ |
| 3286 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3287 | if (v == NULL) |
| 3288 | goto onError; |
| 3289 | return v; |
| 3290 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3291 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3292 | return NULL; |
| 3293 | } |
| 3294 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3295 | static size_t |
| 3296 | wcstombs_errorpos(const wchar_t *wstr) |
| 3297 | { |
| 3298 | size_t len; |
| 3299 | #if SIZEOF_WCHAR_T == 2 |
| 3300 | wchar_t buf[3]; |
| 3301 | #else |
| 3302 | wchar_t buf[2]; |
| 3303 | #endif |
| 3304 | char outbuf[MB_LEN_MAX]; |
| 3305 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3306 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3307 | #if SIZEOF_WCHAR_T == 2 |
| 3308 | buf[2] = 0; |
| 3309 | #else |
| 3310 | buf[1] = 0; |
| 3311 | #endif |
| 3312 | start = wstr; |
| 3313 | while (*wstr != L'\0') |
| 3314 | { |
| 3315 | previous = wstr; |
| 3316 | #if SIZEOF_WCHAR_T == 2 |
| 3317 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3318 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3319 | { |
| 3320 | buf[0] = wstr[0]; |
| 3321 | buf[1] = wstr[1]; |
| 3322 | wstr += 2; |
| 3323 | } |
| 3324 | else { |
| 3325 | buf[0] = *wstr; |
| 3326 | buf[1] = 0; |
| 3327 | wstr++; |
| 3328 | } |
| 3329 | #else |
| 3330 | buf[0] = *wstr; |
| 3331 | wstr++; |
| 3332 | #endif |
| 3333 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3334 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3335 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3339 | return 0; |
| 3340 | } |
| 3341 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3342 | static int |
| 3343 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3344 | { |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3345 | _Py_error_handler error_handler = get_error_handler(errors); |
| 3346 | switch (error_handler) |
| 3347 | { |
| 3348 | case _Py_ERROR_STRICT: |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3349 | *surrogateescape = 0; |
| 3350 | return 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3351 | case _Py_ERROR_SURROGATEESCAPE: |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3352 | *surrogateescape = 1; |
| 3353 | return 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3354 | default: |
| 3355 | PyErr_Format(PyExc_ValueError, |
| 3356 | "only 'strict' and 'surrogateescape' error handlers " |
| 3357 | "are supported, not '%s'", |
| 3358 | errors); |
| 3359 | return -1; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3360 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3361 | } |
| 3362 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3363 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3364 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3365 | { |
| 3366 | Py_ssize_t wlen, wlen2; |
| 3367 | wchar_t *wstr; |
| 3368 | PyObject *bytes = NULL; |
| 3369 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3370 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3371 | PyObject *exc; |
| 3372 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3373 | int surrogateescape; |
| 3374 | |
| 3375 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3376 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3377 | |
| 3378 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3379 | if (wstr == NULL) |
| 3380 | return NULL; |
| 3381 | |
| 3382 | wlen2 = wcslen(wstr); |
| 3383 | if (wlen2 != wlen) { |
| 3384 | PyMem_Free(wstr); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3385 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3386 | return NULL; |
| 3387 | } |
| 3388 | |
| 3389 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3390 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3391 | char *str; |
| 3392 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3393 | str = Py_EncodeLocale(wstr, &error_pos); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3394 | if (str == NULL) { |
| 3395 | if (error_pos == (size_t)-1) { |
| 3396 | PyErr_NoMemory(); |
| 3397 | PyMem_Free(wstr); |
| 3398 | return NULL; |
| 3399 | } |
| 3400 | else { |
| 3401 | goto encode_error; |
| 3402 | } |
| 3403 | } |
| 3404 | PyMem_Free(wstr); |
| 3405 | |
| 3406 | bytes = PyBytes_FromString(str); |
| 3407 | PyMem_Free(str); |
| 3408 | } |
| 3409 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3410 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3411 | size_t len, len2; |
| 3412 | |
| 3413 | len = wcstombs(NULL, wstr, 0); |
| 3414 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3415 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3416 | goto encode_error; |
| 3417 | } |
| 3418 | |
| 3419 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3420 | if (bytes == NULL) { |
| 3421 | PyMem_Free(wstr); |
| 3422 | return NULL; |
| 3423 | } |
| 3424 | |
| 3425 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3426 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3427 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3428 | goto encode_error; |
| 3429 | } |
| 3430 | PyMem_Free(wstr); |
| 3431 | } |
| 3432 | return bytes; |
| 3433 | |
| 3434 | encode_error: |
| 3435 | errmsg = strerror(errno); |
| 3436 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3437 | |
| 3438 | if (error_pos == (size_t)-1) |
| 3439 | error_pos = wcstombs_errorpos(wstr); |
| 3440 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3441 | PyMem_Free(wstr); |
| 3442 | Py_XDECREF(bytes); |
| 3443 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3444 | if (errmsg != NULL) { |
| 3445 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3446 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3447 | if (wstr != NULL) { |
| 3448 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3449 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3450 | } else |
| 3451 | errmsg = NULL; |
| 3452 | } |
| 3453 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3454 | reason = PyUnicode_FromString( |
| 3455 | "wcstombs() encountered an unencodable " |
| 3456 | "wide character"); |
| 3457 | if (reason == NULL) |
| 3458 | return NULL; |
| 3459 | |
| 3460 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3461 | "locale", unicode, |
| 3462 | (Py_ssize_t)error_pos, |
| 3463 | (Py_ssize_t)(error_pos+1), |
| 3464 | reason); |
| 3465 | Py_DECREF(reason); |
| 3466 | if (exc != NULL) { |
| 3467 | PyCodec_StrictErrors(exc); |
| 3468 | Py_XDECREF(exc); |
| 3469 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3470 | return NULL; |
| 3471 | } |
| 3472 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3473 | PyObject * |
| 3474 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3475 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3476 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3477 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3478 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3479 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3480 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3481 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3482 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3483 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3484 | the Python codec requires to encode at least its own filename. Use the C |
| 3485 | version of the locale codec until the codec registry is initialized and |
| 3486 | the Python codec is loaded. |
| 3487 | |
| 3488 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3489 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3490 | subinterpreters. */ |
| 3491 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3492 | return PyUnicode_AsEncodedString(unicode, |
| 3493 | Py_FileSystemDefaultEncoding, |
| 3494 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3495 | } |
| 3496 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3497 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3498 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3499 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3502 | PyObject * |
| 3503 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3504 | const char *encoding, |
| 3505 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3506 | { |
| 3507 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3508 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3509 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3510 | if (!PyUnicode_Check(unicode)) { |
| 3511 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3512 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3513 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3514 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3515 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3516 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3517 | if ((strcmp(lower, "utf-8") == 0) || |
| 3518 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3519 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3520 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3521 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3522 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3523 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3524 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3525 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3526 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3527 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3528 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3529 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3530 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3531 | else if (strcmp(lower, "mbcs") == 0) |
| 3532 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3533 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3534 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3535 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3536 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3537 | |
| 3538 | /* Encode via the codec registry */ |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3539 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3540 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3541 | return NULL; |
| 3542 | |
| 3543 | /* The normal path */ |
| 3544 | if (PyBytes_Check(v)) |
| 3545 | return v; |
| 3546 | |
| 3547 | /* 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] | 3548 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3549 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3550 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3551 | |
| 3552 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3553 | "encoder %s returned bytearray instead of bytes; " |
| 3554 | "use codecs.encode() to encode to arbitrary types", |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3555 | encoding); |
| 3556 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3557 | Py_DECREF(v); |
| 3558 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3559 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3560 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3561 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3562 | Py_DECREF(v); |
| 3563 | return b; |
| 3564 | } |
| 3565 | |
| 3566 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3567 | "'%.400s' encoder returned '%.400s' instead of 'bytes'; " |
| 3568 | "use codecs.encode() to encode to arbitrary types", |
| 3569 | encoding, |
| 3570 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3571 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3572 | return NULL; |
| 3573 | } |
| 3574 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3575 | PyObject * |
| 3576 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3577 | const char *encoding, |
| 3578 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3579 | { |
| 3580 | PyObject *v; |
| 3581 | |
| 3582 | if (!PyUnicode_Check(unicode)) { |
| 3583 | PyErr_BadArgument(); |
| 3584 | goto onError; |
| 3585 | } |
| 3586 | |
| 3587 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3588 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3589 | |
| 3590 | /* Encode via the codec registry */ |
| 3591 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3592 | if (v == NULL) |
| 3593 | goto onError; |
| 3594 | if (!PyUnicode_Check(v)) { |
| 3595 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3596 | "'%.400s' encoder returned '%.400s' instead of 'str'; " |
| 3597 | "use codecs.encode() to encode to arbitrary types", |
| 3598 | encoding, |
| 3599 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3600 | Py_DECREF(v); |
| 3601 | goto onError; |
| 3602 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3603 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3604 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3605 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3606 | return NULL; |
| 3607 | } |
| 3608 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3609 | static size_t |
| 3610 | mbstowcs_errorpos(const char *str, size_t len) |
| 3611 | { |
| 3612 | #ifdef HAVE_MBRTOWC |
| 3613 | const char *start = str; |
| 3614 | mbstate_t mbs; |
| 3615 | size_t converted; |
| 3616 | wchar_t ch; |
| 3617 | |
| 3618 | memset(&mbs, 0, sizeof mbs); |
| 3619 | while (len) |
| 3620 | { |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3621 | converted = mbrtowc(&ch, str, len, &mbs); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3622 | if (converted == 0) |
| 3623 | /* Reached end of string */ |
| 3624 | break; |
| 3625 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3626 | /* Conversion error or incomplete character */ |
| 3627 | return str - start; |
| 3628 | } |
| 3629 | else { |
| 3630 | str += converted; |
| 3631 | len -= converted; |
| 3632 | } |
| 3633 | } |
| 3634 | /* failed to find the undecodable byte sequence */ |
| 3635 | return 0; |
| 3636 | #endif |
| 3637 | return 0; |
| 3638 | } |
| 3639 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3640 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3641 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3642 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3643 | { |
| 3644 | wchar_t smallbuf[256]; |
| 3645 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3646 | wchar_t *wstr; |
| 3647 | size_t wlen, wlen2; |
| 3648 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3649 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3650 | size_t error_pos; |
| 3651 | char *errmsg; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 3652 | PyObject *reason = NULL; /* initialize to prevent gcc warning */ |
| 3653 | PyObject *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3654 | |
| 3655 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3656 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3657 | |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3658 | if (str[len] != '\0' || (size_t)len != strlen(str)) { |
| 3659 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3660 | return NULL; |
| 3661 | } |
| 3662 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3663 | if (surrogateescape) { |
| 3664 | /* "surrogateescape" error handler */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3665 | wstr = Py_DecodeLocale(str, &wlen); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3666 | if (wstr == NULL) { |
| 3667 | if (wlen == (size_t)-1) |
| 3668 | PyErr_NoMemory(); |
| 3669 | else |
| 3670 | PyErr_SetFromErrno(PyExc_OSError); |
| 3671 | return NULL; |
| 3672 | } |
| 3673 | |
| 3674 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3675 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3676 | } |
| 3677 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3678 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3679 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3680 | wlen = mbstowcs(NULL, str, 0); |
| 3681 | #else |
| 3682 | wlen = len; |
| 3683 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3684 | if (wlen == (size_t)-1) |
| 3685 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3686 | if (wlen+1 <= smallbuf_len) { |
| 3687 | wstr = smallbuf; |
| 3688 | } |
| 3689 | else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3690 | wstr = PyMem_New(wchar_t, wlen+1); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3691 | if (!wstr) |
| 3692 | return PyErr_NoMemory(); |
| 3693 | } |
| 3694 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3695 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3696 | if (wlen2 == (size_t)-1) { |
| 3697 | if (wstr != smallbuf) |
| 3698 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3699 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3700 | } |
| 3701 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3702 | assert(wlen2 == wlen); |
| 3703 | #endif |
| 3704 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3705 | if (wstr != smallbuf) |
| 3706 | PyMem_Free(wstr); |
| 3707 | } |
| 3708 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3709 | |
| 3710 | decode_error: |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3711 | reason = NULL; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3712 | errmsg = strerror(errno); |
| 3713 | assert(errmsg != NULL); |
| 3714 | |
| 3715 | error_pos = mbstowcs_errorpos(str, len); |
| 3716 | if (errmsg != NULL) { |
| 3717 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3718 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3719 | if (wstr != NULL) { |
| 3720 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3721 | PyMem_RawFree(wstr); |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3722 | } |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3723 | } |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3724 | if (reason == NULL) |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3725 | reason = PyUnicode_FromString( |
| 3726 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3727 | if (reason == NULL) |
| 3728 | return NULL; |
| 3729 | |
| 3730 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3731 | "locale", str, len, |
| 3732 | (Py_ssize_t)error_pos, |
| 3733 | (Py_ssize_t)(error_pos+1), |
| 3734 | reason); |
| 3735 | Py_DECREF(reason); |
| 3736 | if (exc != NULL) { |
| 3737 | PyCodec_StrictErrors(exc); |
| 3738 | Py_XDECREF(exc); |
| 3739 | } |
| 3740 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3744 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3745 | { |
| 3746 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3747 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | |
| 3751 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3752 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3753 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3754 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3755 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3756 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3757 | PyObject* |
| 3758 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3759 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3760 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3761 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3762 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3763 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3764 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3765 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3766 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3767 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3768 | the Python codec requires to encode at least its own filename. Use the C |
| 3769 | version of the locale codec until the codec registry is initialized and |
| 3770 | the Python codec is loaded. |
| 3771 | |
| 3772 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3773 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3774 | subinterpreters. */ |
| 3775 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3776 | return PyUnicode_Decode(s, size, |
| 3777 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3778 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3779 | } |
| 3780 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3781 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3782 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3783 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3786 | |
| 3787 | int |
| 3788 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3789 | { |
| 3790 | PyObject *output = NULL; |
| 3791 | Py_ssize_t size; |
| 3792 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3793 | if (arg == NULL) { |
| 3794 | Py_DECREF(*(PyObject**)addr); |
Benjamin Peterson | a4d33b3 | 2015-11-15 21:57:39 -0800 | [diff] [blame] | 3795 | *(PyObject**)addr = NULL; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3796 | return 1; |
| 3797 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3798 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3799 | output = arg; |
| 3800 | Py_INCREF(output); |
| 3801 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 3802 | else if (PyUnicode_Check(arg)) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3803 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3804 | if (!output) |
| 3805 | return 0; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 3806 | assert(PyBytes_Check(output)); |
| 3807 | } |
| 3808 | else { |
| 3809 | PyErr_Format(PyExc_TypeError, |
| 3810 | "must be str or bytes, not %.100s", |
| 3811 | Py_TYPE(arg)->tp_name); |
| 3812 | return 0; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3813 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3814 | size = PyBytes_GET_SIZE(output); |
| 3815 | data = PyBytes_AS_STRING(output); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 3816 | if ((size_t)size != strlen(data)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3817 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3818 | Py_DECREF(output); |
| 3819 | return 0; |
| 3820 | } |
| 3821 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3822 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3826 | int |
| 3827 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3828 | { |
| 3829 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3830 | if (arg == NULL) { |
| 3831 | Py_DECREF(*(PyObject**)addr); |
| 3832 | return 1; |
| 3833 | } |
| 3834 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3835 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3836 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3837 | output = arg; |
| 3838 | Py_INCREF(output); |
| 3839 | } |
| 3840 | else { |
| 3841 | arg = PyBytes_FromObject(arg); |
| 3842 | if (!arg) |
| 3843 | return 0; |
| 3844 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3845 | PyBytes_GET_SIZE(arg)); |
| 3846 | Py_DECREF(arg); |
| 3847 | if (!output) |
| 3848 | return 0; |
| 3849 | if (!PyUnicode_Check(output)) { |
| 3850 | Py_DECREF(output); |
| 3851 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3852 | return 0; |
| 3853 | } |
| 3854 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3855 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3856 | Py_DECREF(output); |
| 3857 | return 0; |
| 3858 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3860 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3861 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3862 | Py_DECREF(output); |
| 3863 | return 0; |
| 3864 | } |
| 3865 | *(PyObject**)addr = output; |
| 3866 | return Py_CLEANUP_SUPPORTED; |
| 3867 | } |
| 3868 | |
| 3869 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3870 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3871 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3872 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3873 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3874 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3875 | if (!PyUnicode_Check(unicode)) { |
| 3876 | PyErr_BadArgument(); |
| 3877 | return NULL; |
| 3878 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3879 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3880 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3881 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3882 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3883 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 3884 | bytes = _PyUnicode_AsUTF8String(unicode, NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3885 | if (bytes == NULL) |
| 3886 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3887 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3888 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3889 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3890 | Py_DECREF(bytes); |
| 3891 | return NULL; |
| 3892 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3893 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3894 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3895 | PyBytes_AS_STRING(bytes), |
| 3896 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3897 | Py_DECREF(bytes); |
| 3898 | } |
| 3899 | |
| 3900 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3901 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3902 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3906 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3907 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3908 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3909 | } |
| 3910 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3911 | Py_UNICODE * |
| 3912 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3913 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3914 | const unsigned char *one_byte; |
| 3915 | #if SIZEOF_WCHAR_T == 4 |
| 3916 | const Py_UCS2 *two_bytes; |
| 3917 | #else |
| 3918 | const Py_UCS4 *four_bytes; |
| 3919 | const Py_UCS4 *ucs4_end; |
| 3920 | Py_ssize_t num_surrogates; |
| 3921 | #endif |
| 3922 | wchar_t *w; |
| 3923 | wchar_t *wchar_end; |
| 3924 | |
| 3925 | if (!PyUnicode_Check(unicode)) { |
| 3926 | PyErr_BadArgument(); |
| 3927 | return NULL; |
| 3928 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3929 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3930 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3931 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3932 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3933 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3934 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3935 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3936 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3937 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3938 | num_surrogates = 0; |
| 3939 | |
| 3940 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3941 | if (*four_bytes > 0xFFFF) |
| 3942 | ++num_surrogates; |
| 3943 | } |
| 3944 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3945 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3946 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3947 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3948 | PyErr_NoMemory(); |
| 3949 | return NULL; |
| 3950 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3951 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3952 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3953 | w = _PyUnicode_WSTR(unicode); |
| 3954 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3955 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3956 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3957 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3958 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3959 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3960 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3961 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3962 | } |
| 3963 | else |
| 3964 | *w = *four_bytes; |
| 3965 | |
| 3966 | if (w > wchar_end) { |
| 3967 | assert(0 && "Miscalculated string end"); |
| 3968 | } |
| 3969 | } |
| 3970 | *w = 0; |
| 3971 | #else |
| 3972 | /* sizeof(wchar_t) == 4 */ |
| 3973 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3974 | "should share memory already."); |
| 3975 | return NULL; |
| 3976 | #endif |
| 3977 | } |
| 3978 | else { |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 3979 | if ((size_t)_PyUnicode_LENGTH(unicode) > |
| 3980 | PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 3981 | PyErr_NoMemory(); |
| 3982 | return NULL; |
| 3983 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3984 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3985 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3986 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3987 | PyErr_NoMemory(); |
| 3988 | return NULL; |
| 3989 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3990 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3991 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3992 | w = _PyUnicode_WSTR(unicode); |
| 3993 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3994 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3995 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3996 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3997 | for (; w < wchar_end; ++one_byte, ++w) |
| 3998 | *w = *one_byte; |
| 3999 | /* null-terminate the wstr */ |
| 4000 | *w = 0; |
| 4001 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 4002 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4003 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 4004 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4005 | for (; w < wchar_end; ++two_bytes, ++w) |
| 4006 | *w = *two_bytes; |
| 4007 | /* null-terminate the wstr */ |
| 4008 | *w = 0; |
| 4009 | #else |
| 4010 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 4011 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 4012 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4013 | Py_FatalError("Impossible unicode object state, wstr " |
| 4014 | "and str should share memory already."); |
| 4015 | return NULL; |
| 4016 | #endif |
| 4017 | } |
| 4018 | else { |
| 4019 | assert(0 && "This should never happen."); |
| 4020 | } |
| 4021 | } |
| 4022 | } |
| 4023 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 4024 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 4025 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4028 | Py_UNICODE * |
| 4029 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4030 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4031 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4032 | } |
| 4033 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4034 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4035 | Py_ssize_t |
| 4036 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4037 | { |
| 4038 | if (!PyUnicode_Check(unicode)) { |
| 4039 | PyErr_BadArgument(); |
| 4040 | goto onError; |
| 4041 | } |
| 4042 | return PyUnicode_GET_SIZE(unicode); |
| 4043 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4044 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4045 | return -1; |
| 4046 | } |
| 4047 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4048 | Py_ssize_t |
| 4049 | PyUnicode_GetLength(PyObject *unicode) |
| 4050 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4051 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4052 | PyErr_BadArgument(); |
| 4053 | return -1; |
| 4054 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4055 | if (PyUnicode_READY(unicode) == -1) |
| 4056 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4057 | return PyUnicode_GET_LENGTH(unicode); |
| 4058 | } |
| 4059 | |
| 4060 | Py_UCS4 |
| 4061 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4062 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 4063 | void *data; |
| 4064 | int kind; |
| 4065 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4066 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4067 | PyErr_BadArgument(); |
| 4068 | return (Py_UCS4)-1; |
| 4069 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4070 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4071 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4072 | return (Py_UCS4)-1; |
| 4073 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 4074 | data = PyUnicode_DATA(unicode); |
| 4075 | kind = PyUnicode_KIND(unicode); |
| 4076 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | int |
| 4080 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4081 | { |
| 4082 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4083 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4084 | return -1; |
| 4085 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4086 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4087 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4088 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4089 | return -1; |
| 4090 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4091 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4092 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4093 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4094 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4095 | return -1; |
| 4096 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4097 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4098 | index, ch); |
| 4099 | return 0; |
| 4100 | } |
| 4101 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4102 | const char * |
| 4103 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4104 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4105 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4108 | /* create or adjust a UnicodeDecodeError */ |
| 4109 | static void |
| 4110 | make_decode_exception(PyObject **exceptionObject, |
| 4111 | const char *encoding, |
| 4112 | const char *input, Py_ssize_t length, |
| 4113 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4114 | const char *reason) |
| 4115 | { |
| 4116 | if (*exceptionObject == NULL) { |
| 4117 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4118 | encoding, input, length, startpos, endpos, reason); |
| 4119 | } |
| 4120 | else { |
| 4121 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4122 | goto onError; |
| 4123 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4124 | goto onError; |
| 4125 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4126 | goto onError; |
| 4127 | } |
| 4128 | return; |
| 4129 | |
| 4130 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4131 | Py_CLEAR(*exceptionObject); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4134 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4135 | /* error handling callback helper: |
| 4136 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4137 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4138 | and adjust various state variables. |
| 4139 | return 0 on success, -1 on error |
| 4140 | */ |
| 4141 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4142 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4143 | unicode_decode_call_errorhandler_wchar( |
| 4144 | const char *errors, PyObject **errorHandler, |
| 4145 | const char *encoding, const char *reason, |
| 4146 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4147 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4148 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4149 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4150 | static const 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] | 4151 | |
| 4152 | PyObject *restuple = NULL; |
| 4153 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4154 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4155 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4156 | Py_ssize_t requiredsize; |
| 4157 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4158 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4159 | wchar_t *repwstr; |
| 4160 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4161 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4162 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 4163 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4164 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4165 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4166 | *errorHandler = PyCodec_LookupError(errors); |
| 4167 | if (*errorHandler == NULL) |
| 4168 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4169 | } |
| 4170 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4171 | make_decode_exception(exceptionObject, |
| 4172 | encoding, |
| 4173 | *input, *inend - *input, |
| 4174 | *startinpos, *endinpos, |
| 4175 | reason); |
| 4176 | if (*exceptionObject == NULL) |
| 4177 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4178 | |
| 4179 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4180 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4181 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4182 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4183 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4184 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4185 | } |
| 4186 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4187 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4188 | |
| 4189 | /* Copy back the bytes variables, which might have been modified by the |
| 4190 | callback */ |
| 4191 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4192 | if (!inputobj) |
| 4193 | goto onError; |
| 4194 | if (!PyBytes_Check(inputobj)) { |
| 4195 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4196 | } |
| 4197 | *input = PyBytes_AS_STRING(inputobj); |
| 4198 | insize = PyBytes_GET_SIZE(inputobj); |
| 4199 | *inend = *input + insize; |
| 4200 | /* we can DECREF safely, as the exception has another reference, |
| 4201 | so the object won't go away. */ |
| 4202 | Py_DECREF(inputobj); |
| 4203 | |
| 4204 | if (newpos<0) |
| 4205 | newpos = insize+newpos; |
| 4206 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4207 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4208 | goto onError; |
| 4209 | } |
| 4210 | |
| 4211 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4212 | if (repwstr == NULL) |
| 4213 | goto onError; |
| 4214 | /* need more space? (at least enough for what we |
| 4215 | have+the replacement+the rest of the string (starting |
| 4216 | at the new input position), so we won't have to check space |
| 4217 | when there are no errors in the rest of the string) */ |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4218 | requiredsize = *outpos; |
| 4219 | if (requiredsize > PY_SSIZE_T_MAX - repwlen) |
| 4220 | goto overflow; |
| 4221 | requiredsize += repwlen; |
| 4222 | if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) |
| 4223 | goto overflow; |
| 4224 | requiredsize += insize - newpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4225 | if (requiredsize > outsize) { |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4226 | if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4227 | requiredsize = 2*outsize; |
| 4228 | if (unicode_resize(output, requiredsize) < 0) |
| 4229 | goto onError; |
| 4230 | } |
| 4231 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4232 | *outpos += repwlen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4233 | *endinpos = newpos; |
| 4234 | *inptr = *input + newpos; |
| 4235 | |
| 4236 | /* we made it! */ |
| 4237 | Py_XDECREF(restuple); |
| 4238 | return 0; |
| 4239 | |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4240 | overflow: |
| 4241 | PyErr_SetString(PyExc_OverflowError, |
| 4242 | "decoded result is too long for a Python string"); |
| 4243 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4244 | onError: |
| 4245 | Py_XDECREF(restuple); |
| 4246 | return -1; |
| 4247 | } |
| 4248 | #endif /* HAVE_MBCS */ |
| 4249 | |
| 4250 | static int |
| 4251 | unicode_decode_call_errorhandler_writer( |
| 4252 | const char *errors, PyObject **errorHandler, |
| 4253 | const char *encoding, const char *reason, |
| 4254 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4255 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4256 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4257 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4258 | static const char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4259 | |
| 4260 | PyObject *restuple = NULL; |
| 4261 | PyObject *repunicode = NULL; |
| 4262 | Py_ssize_t insize; |
| 4263 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4264 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4265 | PyObject *inputobj = NULL; |
| 4266 | |
| 4267 | if (*errorHandler == NULL) { |
| 4268 | *errorHandler = PyCodec_LookupError(errors); |
| 4269 | if (*errorHandler == NULL) |
| 4270 | goto onError; |
| 4271 | } |
| 4272 | |
| 4273 | make_decode_exception(exceptionObject, |
| 4274 | encoding, |
| 4275 | *input, *inend - *input, |
| 4276 | *startinpos, *endinpos, |
| 4277 | reason); |
| 4278 | if (*exceptionObject == NULL) |
| 4279 | goto onError; |
| 4280 | |
| 4281 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4282 | if (restuple == NULL) |
| 4283 | goto onError; |
| 4284 | if (!PyTuple_Check(restuple)) { |
| 4285 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4286 | goto onError; |
| 4287 | } |
| 4288 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4289 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4290 | |
| 4291 | /* Copy back the bytes variables, which might have been modified by the |
| 4292 | callback */ |
| 4293 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4294 | if (!inputobj) |
| 4295 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4296 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4297 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4298 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4299 | *input = PyBytes_AS_STRING(inputobj); |
| 4300 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4301 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4302 | /* we can DECREF safely, as the exception has another reference, |
| 4303 | so the object won't go away. */ |
| 4304 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4305 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4306 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4307 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4308 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4309 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4310 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4311 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4312 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4313 | if (PyUnicode_READY(repunicode) < 0) |
| 4314 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4315 | replen = PyUnicode_GET_LENGTH(repunicode); |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4316 | if (replen > 1) { |
| 4317 | writer->min_length += replen - 1; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4318 | writer->overallocate = 1; |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4319 | if (_PyUnicodeWriter_Prepare(writer, writer->min_length, |
| 4320 | PyUnicode_MAX_CHAR_VALUE(repunicode)) == -1) |
| 4321 | goto onError; |
| 4322 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4323 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4324 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4325 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4326 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4327 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4328 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4329 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4330 | Py_XDECREF(restuple); |
| 4331 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4332 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4333 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4334 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4335 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4338 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4339 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4340 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4341 | |
| 4342 | /* Three simple macros defining base-64. */ |
| 4343 | |
| 4344 | /* Is c a base-64 character? */ |
| 4345 | |
| 4346 | #define IS_BASE64(c) \ |
| 4347 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4348 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4349 | ((c) >= '0' && (c) <= '9') || \ |
| 4350 | (c) == '+' || (c) == '/') |
| 4351 | |
| 4352 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4353 | |
| 4354 | #define FROM_BASE64(c) \ |
| 4355 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4356 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4357 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4358 | (c) == '+' ? 62 : 63) |
| 4359 | |
| 4360 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4361 | |
| 4362 | #define TO_BASE64(n) \ |
| 4363 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4364 | |
| 4365 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4366 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4367 | * byte not decoding to itself is the + which begins a base64 |
| 4368 | * string. */ |
| 4369 | |
| 4370 | #define DECODE_DIRECT(c) \ |
| 4371 | ((c) <= 127 && (c) != '+') |
| 4372 | |
| 4373 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4374 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4375 | * the above). See RFC2152. This array identifies these different |
| 4376 | * sets: |
| 4377 | * 0 : "Set D" |
| 4378 | * alphanumeric and '(),-./:? |
| 4379 | * 1 : "Set O" |
| 4380 | * !"#$%&*;<=>@[]^_`{|} |
| 4381 | * 2 : "whitespace" |
| 4382 | * ht nl cr sp |
| 4383 | * 3 : special (must be base64 encoded) |
| 4384 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4385 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4386 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4387 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4388 | char utf7_category[128] = { |
| 4389 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4390 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4391 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4392 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4393 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4394 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4395 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4396 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4397 | /* @ A B C D E F G H I J K L M N O */ |
| 4398 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4399 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4400 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4401 | /* ` a b c d e f g h i j k l m n o */ |
| 4402 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4403 | /* p q r s t u v w x y z { | } ~ del */ |
| 4404 | 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] | 4405 | }; |
| 4406 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4407 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4408 | * answer depends on whether we are encoding set O as itself, and also |
| 4409 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4410 | * clear that the answers to these questions vary between |
| 4411 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4412 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4413 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4414 | ((c) < 128 && (c) > 0 && \ |
| 4415 | ((utf7_category[(c)] == 0) || \ |
| 4416 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4417 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4418 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4419 | PyObject * |
| 4420 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4421 | Py_ssize_t size, |
| 4422 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4423 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4424 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4425 | } |
| 4426 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4427 | /* The decoder. The only state we preserve is our read position, |
| 4428 | * i.e. how many characters we have consumed. So if we end in the |
| 4429 | * middle of a shift sequence we have to back off the read position |
| 4430 | * and the output to the beginning of the sequence, otherwise we lose |
| 4431 | * all the shift state (seen bits, number of bits seen, high |
| 4432 | * surrogate). */ |
| 4433 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4434 | PyObject * |
| 4435 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4436 | Py_ssize_t size, |
| 4437 | const char *errors, |
| 4438 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4439 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4440 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4441 | Py_ssize_t startinpos; |
| 4442 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4443 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4444 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4445 | const char *errmsg = ""; |
| 4446 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4447 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4448 | unsigned int base64bits = 0; |
| 4449 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4450 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4451 | PyObject *errorHandler = NULL; |
| 4452 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4453 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4454 | if (size == 0) { |
| 4455 | if (consumed) |
| 4456 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4457 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4458 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4459 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4460 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4461 | _PyUnicodeWriter_Init(&writer); |
| 4462 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4463 | |
| 4464 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4465 | e = s + size; |
| 4466 | |
| 4467 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4468 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4469 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4470 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4471 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4472 | if (inShift) { /* in a base-64 section */ |
| 4473 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4474 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4475 | base64bits += 6; |
| 4476 | s++; |
| 4477 | if (base64bits >= 16) { |
| 4478 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4479 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4480 | base64bits -= 16; |
| 4481 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4482 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4483 | if (surrogate) { |
| 4484 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4485 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4486 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4487 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4488 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4489 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4490 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4491 | } |
| 4492 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4493 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4494 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4495 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4496 | } |
| 4497 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4498 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4499 | /* first surrogate */ |
| 4500 | surrogate = outCh; |
| 4501 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4502 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4503 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4504 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4505 | } |
| 4506 | } |
| 4507 | } |
| 4508 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4509 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4510 | if (base64bits > 0) { /* left-over bits */ |
| 4511 | if (base64bits >= 6) { |
| 4512 | /* We've seen at least one base-64 character */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4513 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4514 | errmsg = "partial character in shift sequence"; |
| 4515 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4516 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4517 | else { |
| 4518 | /* Some bits remain; they should be zero */ |
| 4519 | if (base64buffer != 0) { |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4520 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4521 | errmsg = "non-zero padding bits in shift sequence"; |
| 4522 | goto utf7Error; |
| 4523 | } |
| 4524 | } |
| 4525 | } |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4526 | if (surrogate && DECODE_DIRECT(ch)) { |
| 4527 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
| 4528 | goto onError; |
| 4529 | } |
| 4530 | surrogate = 0; |
| 4531 | if (ch == '-') { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4532 | /* '-' is absorbed; other terminating |
| 4533 | characters are preserved */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4534 | s++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4535 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4539 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4540 | s++; /* consume '+' */ |
| 4541 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4542 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4543 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4544 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4545 | } |
| 4546 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4547 | inShift = 1; |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4548 | surrogate = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4549 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4550 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4551 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4552 | } |
| 4553 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4554 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4555 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4556 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4557 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4558 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4559 | else { |
| 4560 | startinpos = s-starts; |
| 4561 | s++; |
| 4562 | errmsg = "unexpected special character"; |
| 4563 | goto utf7Error; |
| 4564 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4565 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4566 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4567 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4568 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4569 | errors, &errorHandler, |
| 4570 | "utf7", errmsg, |
| 4571 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4572 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4573 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4576 | /* end of string */ |
| 4577 | |
| 4578 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4579 | /* if we're in an inconsistent state, that's an error */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4580 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4581 | if (surrogate || |
| 4582 | (base64bits >= 6) || |
| 4583 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4584 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4585 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4586 | errors, &errorHandler, |
| 4587 | "utf7", "unterminated shift sequence", |
| 4588 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4589 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4590 | goto onError; |
| 4591 | if (s < e) |
| 4592 | goto restart; |
| 4593 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4594 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4595 | |
| 4596 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4597 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4598 | if (inShift) { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4599 | *consumed = startinpos; |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4600 | if (writer.pos != shiftOutStart && writer.maxchar > 127) { |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4601 | PyObject *result = PyUnicode_FromKindAndData( |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4602 | writer.kind, writer.data, shiftOutStart); |
| 4603 | Py_XDECREF(errorHandler); |
| 4604 | Py_XDECREF(exc); |
| 4605 | _PyUnicodeWriter_Dealloc(&writer); |
| 4606 | return result; |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4607 | } |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4608 | writer.pos = shiftOutStart; /* back off output */ |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4609 | } |
| 4610 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4611 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4612 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4613 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4614 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4615 | Py_XDECREF(errorHandler); |
| 4616 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4617 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4618 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4619 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4620 | Py_XDECREF(errorHandler); |
| 4621 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4622 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4623 | return NULL; |
| 4624 | } |
| 4625 | |
| 4626 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4627 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4628 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4629 | int base64SetO, |
| 4630 | int base64WhiteSpace, |
| 4631 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4632 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4633 | int kind; |
| 4634 | void *data; |
| 4635 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4636 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4637 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4638 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4639 | unsigned int base64bits = 0; |
| 4640 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4641 | char * out; |
| 4642 | char * start; |
| 4643 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4644 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4645 | return NULL; |
| 4646 | kind = PyUnicode_KIND(str); |
| 4647 | data = PyUnicode_DATA(str); |
| 4648 | len = PyUnicode_GET_LENGTH(str); |
| 4649 | |
| 4650 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4651 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4652 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4653 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4654 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4655 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4656 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4657 | if (v == NULL) |
| 4658 | return NULL; |
| 4659 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4660 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4661 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4662 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4663 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4664 | if (inShift) { |
| 4665 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4666 | /* shifting out */ |
| 4667 | if (base64bits) { /* output remaining bits */ |
| 4668 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4669 | base64buffer = 0; |
| 4670 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4671 | } |
| 4672 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4673 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4674 | so no '-' is required, except if the character is itself a '-' */ |
| 4675 | if (IS_BASE64(ch) || ch == '-') { |
| 4676 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4677 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4678 | *out++ = (char) ch; |
| 4679 | } |
| 4680 | else { |
| 4681 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4682 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4683 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4684 | else { /* not in a shift sequence */ |
| 4685 | if (ch == '+') { |
| 4686 | *out++ = '+'; |
| 4687 | *out++ = '-'; |
| 4688 | } |
| 4689 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4690 | *out++ = (char) ch; |
| 4691 | } |
| 4692 | else { |
| 4693 | *out++ = '+'; |
| 4694 | inShift = 1; |
| 4695 | goto encode_char; |
| 4696 | } |
| 4697 | } |
| 4698 | continue; |
| 4699 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4700 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4701 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4702 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4703 | /* code first surrogate */ |
| 4704 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4705 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4706 | while (base64bits >= 6) { |
| 4707 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4708 | base64bits -= 6; |
| 4709 | } |
| 4710 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4711 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4712 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4713 | base64bits += 16; |
| 4714 | base64buffer = (base64buffer << 16) | ch; |
| 4715 | while (base64bits >= 6) { |
| 4716 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4717 | base64bits -= 6; |
| 4718 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4719 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4720 | if (base64bits) |
| 4721 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4722 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4723 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4724 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4725 | return NULL; |
| 4726 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4727 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4728 | PyObject * |
| 4729 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4730 | Py_ssize_t size, |
| 4731 | int base64SetO, |
| 4732 | int base64WhiteSpace, |
| 4733 | const char *errors) |
| 4734 | { |
| 4735 | PyObject *result; |
| 4736 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4737 | if (tmp == NULL) |
| 4738 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4739 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4740 | base64WhiteSpace, errors); |
| 4741 | Py_DECREF(tmp); |
| 4742 | return result; |
| 4743 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4744 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4745 | #undef IS_BASE64 |
| 4746 | #undef FROM_BASE64 |
| 4747 | #undef TO_BASE64 |
| 4748 | #undef DECODE_DIRECT |
| 4749 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4750 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4751 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4752 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4753 | PyObject * |
| 4754 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4755 | Py_ssize_t size, |
| 4756 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4757 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4758 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4759 | } |
| 4760 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4761 | #include "stringlib/asciilib.h" |
| 4762 | #include "stringlib/codecs.h" |
| 4763 | #include "stringlib/undef.h" |
| 4764 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4765 | #include "stringlib/ucs1lib.h" |
| 4766 | #include "stringlib/codecs.h" |
| 4767 | #include "stringlib/undef.h" |
| 4768 | |
| 4769 | #include "stringlib/ucs2lib.h" |
| 4770 | #include "stringlib/codecs.h" |
| 4771 | #include "stringlib/undef.h" |
| 4772 | |
| 4773 | #include "stringlib/ucs4lib.h" |
| 4774 | #include "stringlib/codecs.h" |
| 4775 | #include "stringlib/undef.h" |
| 4776 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4777 | /* Mask to quickly check whether a C 'long' contains a |
| 4778 | non-ASCII, UTF8-encoded char. */ |
| 4779 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4780 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4781 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4782 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4783 | #else |
| 4784 | # error C 'long' size should be either 4 or 8! |
| 4785 | #endif |
| 4786 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4787 | static Py_ssize_t |
| 4788 | 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] | 4789 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4790 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4791 | 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] | 4792 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4793 | /* |
| 4794 | * Issue #17237: m68k is a bit different from most architectures in |
| 4795 | * that objects do not use "natural alignment" - for example, int and |
| 4796 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4797 | * won't work; also, tests have shown that skipping the "optimised |
| 4798 | * version" will even speed up m68k. |
| 4799 | */ |
| 4800 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4801 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4802 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4803 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4804 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4805 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4806 | /* Help allocation */ |
| 4807 | const char *_p = p; |
| 4808 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4809 | while (_p < aligned_end) { |
| 4810 | unsigned long value = *(const unsigned long *) _p; |
| 4811 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4812 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4813 | *((unsigned long *)q) = value; |
| 4814 | _p += SIZEOF_LONG; |
| 4815 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4816 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4817 | p = _p; |
| 4818 | while (p < end) { |
| 4819 | if ((unsigned char)*p & 0x80) |
| 4820 | break; |
| 4821 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4822 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4823 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4824 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4825 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4826 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4827 | while (p < end) { |
| 4828 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4829 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4830 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4831 | /* Help allocation */ |
| 4832 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4833 | while (_p < aligned_end) { |
| 4834 | unsigned long value = *(unsigned long *) _p; |
| 4835 | if (value & ASCII_CHAR_MASK) |
| 4836 | break; |
| 4837 | _p += SIZEOF_LONG; |
| 4838 | } |
| 4839 | p = _p; |
| 4840 | if (_p == end) |
| 4841 | break; |
| 4842 | } |
| 4843 | if ((unsigned char)*p & 0x80) |
| 4844 | break; |
| 4845 | ++p; |
| 4846 | } |
| 4847 | memcpy(dest, start, p - start); |
| 4848 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4849 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4850 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4851 | PyObject * |
| 4852 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4853 | Py_ssize_t size, |
| 4854 | const char *errors, |
| 4855 | Py_ssize_t *consumed) |
| 4856 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4857 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4858 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4859 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4860 | |
| 4861 | Py_ssize_t startinpos; |
| 4862 | Py_ssize_t endinpos; |
| 4863 | const char *errmsg = ""; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4864 | PyObject *error_handler_obj = NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4865 | PyObject *exc = NULL; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4866 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4867 | |
| 4868 | if (size == 0) { |
| 4869 | if (consumed) |
| 4870 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4871 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4872 | } |
| 4873 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4874 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4875 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4876 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4877 | *consumed = 1; |
| 4878 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4879 | } |
| 4880 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4881 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4882 | writer.min_length = size; |
| 4883 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4884 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4885 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4886 | writer.pos = ascii_decode(s, end, writer.data); |
| 4887 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4888 | while (s < end) { |
| 4889 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4890 | int kind = writer.kind; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4891 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4892 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4893 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4894 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4895 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4896 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4897 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4898 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4899 | } else { |
| 4900 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4901 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | switch (ch) { |
| 4905 | case 0: |
| 4906 | if (s == end || consumed) |
| 4907 | goto End; |
| 4908 | errmsg = "unexpected end of data"; |
| 4909 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4910 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4911 | break; |
| 4912 | case 1: |
| 4913 | errmsg = "invalid start byte"; |
| 4914 | startinpos = s - starts; |
| 4915 | endinpos = startinpos + 1; |
| 4916 | break; |
| 4917 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4918 | case 3: |
| 4919 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4920 | errmsg = "invalid continuation byte"; |
| 4921 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4922 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4923 | break; |
| 4924 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4925 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4926 | goto onError; |
| 4927 | continue; |
| 4928 | } |
| 4929 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4930 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 4931 | error_handler = get_error_handler(errors); |
| 4932 | |
| 4933 | switch (error_handler) { |
| 4934 | case _Py_ERROR_IGNORE: |
| 4935 | s += (endinpos - startinpos); |
| 4936 | break; |
| 4937 | |
| 4938 | case _Py_ERROR_REPLACE: |
| 4939 | if (_PyUnicodeWriter_WriteCharInline(&writer, 0xfffd) < 0) |
| 4940 | goto onError; |
| 4941 | s += (endinpos - startinpos); |
| 4942 | break; |
| 4943 | |
| 4944 | case _Py_ERROR_SURROGATEESCAPE: |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4945 | { |
| 4946 | Py_ssize_t i; |
| 4947 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4948 | if (_PyUnicodeWriter_PrepareKind(&writer, PyUnicode_2BYTE_KIND) < 0) |
| 4949 | goto onError; |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4950 | for (i=startinpos; i<endinpos; i++) { |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4951 | ch = (Py_UCS4)(unsigned char)(starts[i]); |
| 4952 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, |
| 4953 | ch + 0xdc00); |
| 4954 | writer.pos++; |
| 4955 | } |
| 4956 | s += (endinpos - startinpos); |
| 4957 | break; |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4958 | } |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4959 | |
| 4960 | default: |
| 4961 | if (unicode_decode_call_errorhandler_writer( |
| 4962 | errors, &error_handler_obj, |
| 4963 | "utf-8", errmsg, |
| 4964 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4965 | &writer)) |
| 4966 | goto onError; |
| 4967 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4968 | } |
| 4969 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4970 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4971 | if (consumed) |
| 4972 | *consumed = s - starts; |
| 4973 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4974 | Py_XDECREF(error_handler_obj); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4975 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4976 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4977 | |
| 4978 | onError: |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4979 | Py_XDECREF(error_handler_obj); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4980 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4981 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4982 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4983 | } |
| 4984 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4985 | #ifdef __APPLE__ |
| 4986 | |
| 4987 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4988 | used to decode the command line arguments on Mac OS X. |
| 4989 | |
| 4990 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4991 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4992 | |
| 4993 | wchar_t* |
| 4994 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4995 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4996 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4997 | wchar_t *unicode; |
| 4998 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4999 | |
| 5000 | /* Note: size will always be longer than the resulting Unicode |
| 5001 | character count */ |
Victor Stinner | f50e187 | 2015-03-20 11:32:24 +0100 | [diff] [blame] | 5002 | if (PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5003 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 5004 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5005 | if (!unicode) |
| 5006 | return NULL; |
| 5007 | |
| 5008 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5009 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5010 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5011 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5012 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5013 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5014 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5015 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5016 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5017 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5018 | if (ch > 0xFF) { |
| 5019 | #if SIZEOF_WCHAR_T == 4 |
| 5020 | assert(0); |
| 5021 | #else |
| 5022 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 5023 | /* compute and append the two surrogates: */ |
| 5024 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5025 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 5026 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5027 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5028 | else { |
| 5029 | if (!ch && s == e) |
| 5030 | break; |
| 5031 | /* surrogateescape */ |
| 5032 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 5033 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5034 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5035 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5036 | return unicode; |
| 5037 | } |
| 5038 | |
| 5039 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5040 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5041 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5042 | |
| 5043 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5044 | and allocate exactly as much space needed at the end. Else allocate the |
| 5045 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5046 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5047 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5048 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5049 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5050 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5051 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5052 | void *data; |
| 5053 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5054 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5055 | if (!PyUnicode_Check(unicode)) { |
| 5056 | PyErr_BadArgument(); |
| 5057 | return NULL; |
| 5058 | } |
| 5059 | |
| 5060 | if (PyUnicode_READY(unicode) == -1) |
| 5061 | return NULL; |
| 5062 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5063 | if (PyUnicode_UTF8(unicode)) |
| 5064 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5065 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5066 | |
| 5067 | kind = PyUnicode_KIND(unicode); |
| 5068 | data = PyUnicode_DATA(unicode); |
| 5069 | size = PyUnicode_GET_LENGTH(unicode); |
| 5070 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5071 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5072 | default: |
| 5073 | assert(0); |
| 5074 | case PyUnicode_1BYTE_KIND: |
| 5075 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5076 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5077 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5078 | case PyUnicode_2BYTE_KIND: |
| 5079 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5080 | case PyUnicode_4BYTE_KIND: |
| 5081 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5082 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5085 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5086 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5087 | Py_ssize_t size, |
| 5088 | const char *errors) |
| 5089 | { |
| 5090 | PyObject *v, *unicode; |
| 5091 | |
| 5092 | unicode = PyUnicode_FromUnicode(s, size); |
| 5093 | if (unicode == NULL) |
| 5094 | return NULL; |
| 5095 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5096 | Py_DECREF(unicode); |
| 5097 | return v; |
| 5098 | } |
| 5099 | |
| 5100 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5101 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5102 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5103 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5106 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5107 | |
| 5108 | PyObject * |
| 5109 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5110 | Py_ssize_t size, |
| 5111 | const char *errors, |
| 5112 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5113 | { |
| 5114 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5115 | } |
| 5116 | |
| 5117 | PyObject * |
| 5118 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5119 | Py_ssize_t size, |
| 5120 | const char *errors, |
| 5121 | int *byteorder, |
| 5122 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5123 | { |
| 5124 | const char *starts = s; |
| 5125 | Py_ssize_t startinpos; |
| 5126 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5127 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5128 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5129 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5130 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5131 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5132 | PyObject *errorHandler = NULL; |
| 5133 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5134 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5135 | q = (unsigned char *)s; |
| 5136 | e = q + size; |
| 5137 | |
| 5138 | if (byteorder) |
| 5139 | bo = *byteorder; |
| 5140 | |
| 5141 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5142 | byte order setting accordingly. In native mode, the leading BOM |
| 5143 | mark is skipped, in all other modes, it is copied to the output |
| 5144 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5145 | if (bo == 0 && size >= 4) { |
| 5146 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5147 | if (bom == 0x0000FEFF) { |
| 5148 | bo = -1; |
| 5149 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5150 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5151 | else if (bom == 0xFFFE0000) { |
| 5152 | bo = 1; |
| 5153 | q += 4; |
| 5154 | } |
| 5155 | if (byteorder) |
| 5156 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5159 | if (q == e) { |
| 5160 | if (consumed) |
| 5161 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5162 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5165 | #ifdef WORDS_BIGENDIAN |
| 5166 | le = bo < 0; |
| 5167 | #else |
| 5168 | le = bo <= 0; |
| 5169 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5170 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5171 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5172 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5173 | writer.min_length = (e - q + 3) / 4; |
| 5174 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5175 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5176 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5177 | while (1) { |
| 5178 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5179 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5180 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5181 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5182 | enum PyUnicode_Kind kind = writer.kind; |
| 5183 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5184 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5185 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5186 | if (le) { |
| 5187 | do { |
| 5188 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5189 | if (ch > maxch) |
| 5190 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5191 | if (kind != PyUnicode_1BYTE_KIND && |
| 5192 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5193 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5194 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5195 | q += 4; |
| 5196 | } while (q <= last); |
| 5197 | } |
| 5198 | else { |
| 5199 | do { |
| 5200 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5201 | if (ch > maxch) |
| 5202 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5203 | if (kind != PyUnicode_1BYTE_KIND && |
| 5204 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5205 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5206 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5207 | q += 4; |
| 5208 | } while (q <= last); |
| 5209 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5210 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5211 | } |
| 5212 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5213 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5214 | errmsg = "code point in surrogate code point range(0xd800, 0xe000)"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5215 | startinpos = ((const char *)q) - starts; |
| 5216 | endinpos = startinpos + 4; |
| 5217 | } |
| 5218 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5219 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5220 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5221 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5222 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5223 | startinpos = ((const char *)q) - starts; |
| 5224 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5225 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5226 | else { |
| 5227 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5228 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5229 | goto onError; |
| 5230 | q += 4; |
| 5231 | continue; |
| 5232 | } |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5233 | errmsg = "code point not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5234 | startinpos = ((const char *)q) - starts; |
| 5235 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5236 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5237 | |
| 5238 | /* The remaining input chars are ignored if the callback |
| 5239 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5240 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5241 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5242 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5243 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5244 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5245 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5248 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5249 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5250 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5251 | Py_XDECREF(errorHandler); |
| 5252 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5253 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5254 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5255 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5256 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5257 | Py_XDECREF(errorHandler); |
| 5258 | Py_XDECREF(exc); |
| 5259 | return NULL; |
| 5260 | } |
| 5261 | |
| 5262 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5263 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5264 | const char *errors, |
| 5265 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5266 | { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5267 | enum PyUnicode_Kind kind; |
| 5268 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5269 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5270 | PyObject *v; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5271 | PY_UINT32_T *out; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5272 | #if PY_LITTLE_ENDIAN |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5273 | int native_ordering = byteorder <= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5274 | #else |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5275 | int native_ordering = byteorder >= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5276 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5277 | const char *encoding; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5278 | Py_ssize_t nsize, pos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5279 | PyObject *errorHandler = NULL; |
| 5280 | PyObject *exc = NULL; |
| 5281 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5282 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5283 | if (!PyUnicode_Check(str)) { |
| 5284 | PyErr_BadArgument(); |
| 5285 | return NULL; |
| 5286 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5287 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5288 | return NULL; |
| 5289 | kind = PyUnicode_KIND(str); |
| 5290 | data = PyUnicode_DATA(str); |
| 5291 | len = PyUnicode_GET_LENGTH(str); |
| 5292 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5293 | if (len > PY_SSIZE_T_MAX / 4 - (byteorder == 0)) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5294 | return PyErr_NoMemory(); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5295 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5296 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5297 | if (v == NULL) |
| 5298 | return NULL; |
| 5299 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5300 | /* output buffer is 4-bytes aligned */ |
| 5301 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 4)); |
| 5302 | out = (PY_UINT32_T *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5303 | if (byteorder == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5304 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5305 | if (len == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5306 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5307 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5308 | if (byteorder == -1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5309 | encoding = "utf-32-le"; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5310 | else if (byteorder == 1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5311 | encoding = "utf-32-be"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5312 | else |
| 5313 | encoding = "utf-32"; |
| 5314 | |
| 5315 | if (kind == PyUnicode_1BYTE_KIND) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5316 | ucs1lib_utf32_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5317 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5320 | pos = 0; |
| 5321 | while (pos < len) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5322 | Py_ssize_t repsize, moreunits; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5323 | |
| 5324 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5325 | pos += ucs2lib_utf32_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5326 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5327 | } |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5328 | else { |
| 5329 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5330 | pos += ucs4lib_utf32_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5331 | &out, native_ordering); |
| 5332 | } |
| 5333 | if (pos == len) |
| 5334 | break; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5335 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5336 | rep = unicode_encode_call_errorhandler( |
| 5337 | errors, &errorHandler, |
| 5338 | encoding, "surrogates not allowed", |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5339 | str, &exc, pos, pos + 1, &pos); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5340 | if (!rep) |
| 5341 | goto error; |
| 5342 | |
| 5343 | if (PyBytes_Check(rep)) { |
| 5344 | repsize = PyBytes_GET_SIZE(rep); |
| 5345 | if (repsize & 3) { |
| 5346 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5347 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5348 | "surrogates not allowed"); |
| 5349 | goto error; |
| 5350 | } |
| 5351 | moreunits = repsize / 4; |
| 5352 | } |
| 5353 | else { |
| 5354 | assert(PyUnicode_Check(rep)); |
| 5355 | if (PyUnicode_READY(rep) < 0) |
| 5356 | goto error; |
| 5357 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5358 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5359 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5360 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5361 | "surrogates not allowed"); |
| 5362 | goto error; |
| 5363 | } |
| 5364 | } |
| 5365 | |
| 5366 | /* four bytes are reserved for each surrogate */ |
| 5367 | if (moreunits > 1) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5368 | Py_ssize_t outpos = out - (PY_UINT32_T*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5369 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5370 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5371 | /* integer overflow */ |
| 5372 | PyErr_NoMemory(); |
| 5373 | goto error; |
| 5374 | } |
| 5375 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5376 | goto error; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5377 | out = (PY_UINT32_T*) PyBytes_AS_STRING(v) + outpos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | if (PyBytes_Check(rep)) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5381 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5382 | out += moreunits; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5383 | } else /* rep is unicode */ { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5384 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5385 | ucs1lib_utf32_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5386 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | Py_CLEAR(rep); |
| 5390 | } |
| 5391 | |
| 5392 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5393 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5394 | handler is used. */ |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5395 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5396 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5397 | _PyBytes_Resize(&v, nsize); |
| 5398 | Py_XDECREF(errorHandler); |
| 5399 | Py_XDECREF(exc); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5400 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5401 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5402 | error: |
| 5403 | Py_XDECREF(rep); |
| 5404 | Py_XDECREF(errorHandler); |
| 5405 | Py_XDECREF(exc); |
| 5406 | Py_XDECREF(v); |
| 5407 | return NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5410 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5411 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5412 | Py_ssize_t size, |
| 5413 | const char *errors, |
| 5414 | int byteorder) |
| 5415 | { |
| 5416 | PyObject *result; |
| 5417 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5418 | if (tmp == NULL) |
| 5419 | return NULL; |
| 5420 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5421 | Py_DECREF(tmp); |
| 5422 | return result; |
| 5423 | } |
| 5424 | |
| 5425 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5426 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5427 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5428 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5432 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5433 | PyObject * |
| 5434 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5435 | Py_ssize_t size, |
| 5436 | const char *errors, |
| 5437 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5438 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5439 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5440 | } |
| 5441 | |
| 5442 | PyObject * |
| 5443 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5444 | Py_ssize_t size, |
| 5445 | const char *errors, |
| 5446 | int *byteorder, |
| 5447 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5448 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5449 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5450 | Py_ssize_t startinpos; |
| 5451 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5452 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5453 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5454 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5455 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5456 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5457 | PyObject *errorHandler = NULL; |
| 5458 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5459 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5461 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5462 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5463 | |
| 5464 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5465 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5466 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5467 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5468 | byte order setting accordingly. In native mode, the leading BOM |
| 5469 | mark is skipped, in all other modes, it is copied to the output |
| 5470 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5471 | if (bo == 0 && size >= 2) { |
| 5472 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5473 | if (bom == 0xFEFF) { |
| 5474 | q += 2; |
| 5475 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5476 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5477 | else if (bom == 0xFFFE) { |
| 5478 | q += 2; |
| 5479 | bo = 1; |
| 5480 | } |
| 5481 | if (byteorder) |
| 5482 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5483 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5485 | if (q == e) { |
| 5486 | if (consumed) |
| 5487 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5488 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5489 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5490 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5491 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5492 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5493 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5494 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5495 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5496 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5497 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5498 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5499 | /* Note: size will always be longer than the resulting Unicode |
| 5500 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5501 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5502 | writer.min_length = (e - q + 1) / 2; |
| 5503 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5504 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5505 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5506 | while (1) { |
| 5507 | Py_UCS4 ch = 0; |
| 5508 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5509 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5510 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5511 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5512 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5513 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5514 | native_ordering); |
| 5515 | else |
| 5516 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5517 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5518 | native_ordering); |
| 5519 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5520 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5521 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5522 | native_ordering); |
| 5523 | } else { |
| 5524 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5525 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5526 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5527 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5528 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5529 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5530 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5531 | switch (ch) |
| 5532 | { |
| 5533 | case 0: |
| 5534 | /* remaining byte at the end? (size should be even) */ |
| 5535 | if (q == e || consumed) |
| 5536 | goto End; |
| 5537 | errmsg = "truncated data"; |
| 5538 | startinpos = ((const char *)q) - starts; |
| 5539 | endinpos = ((const char *)e) - starts; |
| 5540 | break; |
| 5541 | /* The remaining input chars are ignored if the callback |
| 5542 | chooses to skip the input */ |
| 5543 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5544 | q -= 2; |
| 5545 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5546 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5547 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5548 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5549 | endinpos = ((const char *)e) - starts; |
| 5550 | break; |
| 5551 | case 2: |
| 5552 | errmsg = "illegal encoding"; |
| 5553 | startinpos = ((const char *)q) - 2 - starts; |
| 5554 | endinpos = startinpos + 2; |
| 5555 | break; |
| 5556 | case 3: |
| 5557 | errmsg = "illegal UTF-16 surrogate"; |
| 5558 | startinpos = ((const char *)q) - 4 - starts; |
| 5559 | endinpos = startinpos + 2; |
| 5560 | break; |
| 5561 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5562 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5563 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5564 | continue; |
| 5565 | } |
| 5566 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5567 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5568 | errors, |
| 5569 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5570 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5571 | &starts, |
| 5572 | (const char **)&e, |
| 5573 | &startinpos, |
| 5574 | &endinpos, |
| 5575 | &exc, |
| 5576 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5577 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5578 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5581 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5582 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5583 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5584 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5585 | Py_XDECREF(errorHandler); |
| 5586 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5587 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5589 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5590 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5591 | Py_XDECREF(errorHandler); |
| 5592 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5593 | return NULL; |
| 5594 | } |
| 5595 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5596 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5597 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5598 | const char *errors, |
| 5599 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5600 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5601 | enum PyUnicode_Kind kind; |
| 5602 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5603 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5604 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5605 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5606 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5607 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5608 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5609 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5610 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5611 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5612 | const char *encoding; |
| 5613 | Py_ssize_t nsize, pos; |
| 5614 | PyObject *errorHandler = NULL; |
| 5615 | PyObject *exc = NULL; |
| 5616 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5617 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5618 | if (!PyUnicode_Check(str)) { |
| 5619 | PyErr_BadArgument(); |
| 5620 | return NULL; |
| 5621 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5622 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5623 | return NULL; |
| 5624 | kind = PyUnicode_KIND(str); |
| 5625 | data = PyUnicode_DATA(str); |
| 5626 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5627 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5628 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5629 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5630 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5631 | const Py_UCS4 *end = in + len; |
| 5632 | while (in < end) |
| 5633 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5634 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5635 | } |
| 5636 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5637 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5638 | nsize = len + pairs + (byteorder == 0); |
| 5639 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | if (v == NULL) |
| 5641 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5643 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5644 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5645 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5646 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5647 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5648 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5649 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5650 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5651 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5652 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5653 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5654 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5655 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5656 | if (byteorder < 0) |
| 5657 | encoding = "utf-16-le"; |
| 5658 | else if (byteorder > 0) |
| 5659 | encoding = "utf-16-be"; |
| 5660 | else |
| 5661 | encoding = "utf-16"; |
| 5662 | |
| 5663 | pos = 0; |
| 5664 | while (pos < len) { |
| 5665 | Py_ssize_t repsize, moreunits; |
| 5666 | |
| 5667 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5668 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5669 | &out, native_ordering); |
| 5670 | } |
| 5671 | else { |
| 5672 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5673 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5674 | &out, native_ordering); |
| 5675 | } |
| 5676 | if (pos == len) |
| 5677 | break; |
| 5678 | |
| 5679 | rep = unicode_encode_call_errorhandler( |
| 5680 | errors, &errorHandler, |
| 5681 | encoding, "surrogates not allowed", |
| 5682 | str, &exc, pos, pos + 1, &pos); |
| 5683 | if (!rep) |
| 5684 | goto error; |
| 5685 | |
| 5686 | if (PyBytes_Check(rep)) { |
| 5687 | repsize = PyBytes_GET_SIZE(rep); |
| 5688 | if (repsize & 1) { |
| 5689 | raise_encode_exception(&exc, encoding, |
| 5690 | str, pos - 1, pos, |
| 5691 | "surrogates not allowed"); |
| 5692 | goto error; |
| 5693 | } |
| 5694 | moreunits = repsize / 2; |
| 5695 | } |
| 5696 | else { |
| 5697 | assert(PyUnicode_Check(rep)); |
| 5698 | if (PyUnicode_READY(rep) < 0) |
| 5699 | goto error; |
| 5700 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5701 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5702 | raise_encode_exception(&exc, encoding, |
| 5703 | str, pos - 1, pos, |
| 5704 | "surrogates not allowed"); |
| 5705 | goto error; |
| 5706 | } |
| 5707 | } |
| 5708 | |
| 5709 | /* two bytes are reserved for each surrogate */ |
| 5710 | if (moreunits > 1) { |
| 5711 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5712 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5713 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5714 | /* integer overflow */ |
| 5715 | PyErr_NoMemory(); |
| 5716 | goto error; |
| 5717 | } |
| 5718 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5719 | goto error; |
| 5720 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5721 | } |
| 5722 | |
| 5723 | if (PyBytes_Check(rep)) { |
| 5724 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5725 | out += moreunits; |
| 5726 | } else /* rep is unicode */ { |
| 5727 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5728 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5729 | &out, native_ordering); |
| 5730 | } |
| 5731 | |
| 5732 | Py_CLEAR(rep); |
| 5733 | } |
| 5734 | |
| 5735 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5736 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5737 | is used. */ |
| 5738 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5739 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5740 | _PyBytes_Resize(&v, nsize); |
| 5741 | Py_XDECREF(errorHandler); |
| 5742 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5743 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5744 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5745 | error: |
| 5746 | Py_XDECREF(rep); |
| 5747 | Py_XDECREF(errorHandler); |
| 5748 | Py_XDECREF(exc); |
| 5749 | Py_XDECREF(v); |
| 5750 | return NULL; |
| 5751 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5754 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5755 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5756 | Py_ssize_t size, |
| 5757 | const char *errors, |
| 5758 | int byteorder) |
| 5759 | { |
| 5760 | PyObject *result; |
| 5761 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5762 | if (tmp == NULL) |
| 5763 | return NULL; |
| 5764 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5765 | Py_DECREF(tmp); |
| 5766 | return result; |
| 5767 | } |
| 5768 | |
| 5769 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5770 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5772 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5777 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5778 | if all the escapes in the string make it still a valid ASCII string. |
| 5779 | Returns -1 if any escapes were found which cause the string to |
| 5780 | pop out of ASCII range. Otherwise returns the length of the |
| 5781 | required buffer to hold the string. |
| 5782 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5783 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5784 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5785 | { |
| 5786 | const unsigned char *p = (const unsigned char *)s; |
| 5787 | const unsigned char *end = p + size; |
| 5788 | Py_ssize_t length = 0; |
| 5789 | |
| 5790 | if (size < 0) |
| 5791 | return -1; |
| 5792 | |
| 5793 | for (; p < end; ++p) { |
| 5794 | if (*p > 127) { |
| 5795 | /* Non-ASCII */ |
| 5796 | return -1; |
| 5797 | } |
| 5798 | else if (*p != '\\') { |
| 5799 | /* Normal character */ |
| 5800 | ++length; |
| 5801 | } |
| 5802 | else { |
| 5803 | /* Backslash-escape, check next char */ |
| 5804 | ++p; |
| 5805 | /* Escape sequence reaches till end of string or |
| 5806 | non-ASCII follow-up. */ |
| 5807 | if (p >= end || *p > 127) |
| 5808 | return -1; |
| 5809 | switch (*p) { |
| 5810 | case '\n': |
| 5811 | /* backslash + \n result in zero characters */ |
| 5812 | break; |
| 5813 | case '\\': case '\'': case '\"': |
| 5814 | case 'b': case 'f': case 't': |
| 5815 | case 'n': case 'r': case 'v': case 'a': |
| 5816 | ++length; |
| 5817 | break; |
| 5818 | case '0': case '1': case '2': case '3': |
| 5819 | case '4': case '5': case '6': case '7': |
| 5820 | case 'x': case 'u': case 'U': case 'N': |
| 5821 | /* these do not guarantee ASCII characters */ |
| 5822 | return -1; |
| 5823 | default: |
| 5824 | /* count the backslash + the other character */ |
| 5825 | length += 2; |
| 5826 | } |
| 5827 | } |
| 5828 | } |
| 5829 | return length; |
| 5830 | } |
| 5831 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5832 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5833 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5834 | PyObject * |
| 5835 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5836 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5837 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5839 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5840 | Py_ssize_t startinpos; |
| 5841 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5842 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5843 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5844 | char* message; |
| 5845 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5846 | PyObject *errorHandler = NULL; |
| 5847 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5848 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5849 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5850 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5851 | if (len == 0) |
| 5852 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5853 | |
| 5854 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5855 | either the string is pure ASCII with named escapes like \n, etc. |
| 5856 | and we determined it's exact size (common case) |
| 5857 | or it contains \x, \u, ... escape sequences. then we create a |
| 5858 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5859 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5860 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5861 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5862 | } |
| 5863 | else { |
| 5864 | /* Escaped strings will always be longer than the resulting |
| 5865 | Unicode string, so we start with size here and then reduce the |
| 5866 | length after conversion to the true value. |
| 5867 | (but if the error callback returns a long replacement string |
| 5868 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5869 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5870 | } |
| 5871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5873 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5874 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5875 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | while (s < end) { |
| 5877 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5878 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5879 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | |
| 5881 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5882 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5883 | x = (unsigned char)*s; |
| 5884 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5885 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5886 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5887 | continue; |
| 5888 | } |
| 5889 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5890 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | /* \ - Escapes */ |
| 5892 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5893 | c = *s++; |
| 5894 | if (s > end) |
| 5895 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5896 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5897 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5898 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5899 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5900 | #define WRITECHAR(ch) \ |
| 5901 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5902 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5903 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5904 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5905 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5906 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5907 | case '\\': WRITECHAR('\\'); break; |
| 5908 | case '\'': WRITECHAR('\''); break; |
| 5909 | case '\"': WRITECHAR('\"'); break; |
| 5910 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5911 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5912 | case 'f': WRITECHAR('\014'); break; |
| 5913 | case 't': WRITECHAR('\t'); break; |
| 5914 | case 'n': WRITECHAR('\n'); break; |
| 5915 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5916 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5917 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5918 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5919 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5921 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | case '0': case '1': case '2': case '3': |
| 5923 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5924 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5925 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5926 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5927 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5928 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5930 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | break; |
| 5932 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5933 | /* hex escapes */ |
| 5934 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5936 | digits = 2; |
| 5937 | message = "truncated \\xXX escape"; |
| 5938 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5940 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5941 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5942 | digits = 4; |
| 5943 | message = "truncated \\uXXXX escape"; |
| 5944 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5945 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5946 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5947 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5948 | digits = 8; |
| 5949 | message = "truncated \\UXXXXXXXX escape"; |
| 5950 | hexescape: |
| 5951 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5952 | if (end - s < digits) { |
| 5953 | /* count only hex digits */ |
| 5954 | for (; s < end; ++s) { |
| 5955 | c = (unsigned char)*s; |
| 5956 | if (!Py_ISXDIGIT(c)) |
| 5957 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5958 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5959 | goto error; |
| 5960 | } |
| 5961 | for (; digits--; ++s) { |
| 5962 | c = (unsigned char)*s; |
| 5963 | if (!Py_ISXDIGIT(c)) |
| 5964 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5965 | chr = (chr<<4) & ~0xF; |
| 5966 | if (c >= '0' && c <= '9') |
| 5967 | chr += c - '0'; |
| 5968 | else if (c >= 'a' && c <= 'f') |
| 5969 | chr += 10 + c - 'a'; |
| 5970 | else |
| 5971 | chr += 10 + c - 'A'; |
| 5972 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5973 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5974 | /* _decoding_error will have already written into the |
| 5975 | target buffer. */ |
| 5976 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5977 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5978 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5979 | message = "illegal Unicode character"; |
| 5980 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5981 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5982 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5983 | break; |
| 5984 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5985 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5986 | case 'N': |
| 5987 | message = "malformed \\N character escape"; |
| 5988 | if (ucnhash_CAPI == NULL) { |
| 5989 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5990 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5991 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5992 | if (ucnhash_CAPI == NULL) |
| 5993 | goto ucnhashError; |
| 5994 | } |
| 5995 | if (*s == '{') { |
| 5996 | const char *start = s+1; |
| 5997 | /* look for the closing brace */ |
| 5998 | while (*s != '}' && s < end) |
| 5999 | s++; |
| 6000 | if (s > start && s < end && *s == '}') { |
| 6001 | /* found a name. look it up in the unicode database */ |
| 6002 | message = "unknown Unicode character name"; |
| 6003 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 6004 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 6005 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 6006 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6007 | goto store; |
| 6008 | } |
| 6009 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6010 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6011 | |
| 6012 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6013 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6014 | message = "\\ at end of string"; |
| 6015 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6016 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6017 | } |
| 6018 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6019 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 6020 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6021 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6022 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6024 | continue; |
| 6025 | |
| 6026 | error: |
| 6027 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 6028 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6029 | errors, &errorHandler, |
| 6030 | "unicodeescape", message, |
| 6031 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 6032 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6033 | goto onError; |
| 6034 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6036 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6037 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6038 | Py_XDECREF(errorHandler); |
| 6039 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6040 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6041 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6043 | PyErr_SetString( |
| 6044 | PyExc_UnicodeError, |
| 6045 | "\\N escapes not supported (can't load unicodedata module)" |
| 6046 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6047 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6048 | Py_XDECREF(errorHandler); |
| 6049 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6050 | return NULL; |
| 6051 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6052 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6053 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6054 | Py_XDECREF(errorHandler); |
| 6055 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | return NULL; |
| 6057 | } |
| 6058 | |
| 6059 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6060 | |
| 6061 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6062 | appropriate. |
| 6063 | |
| 6064 | */ |
| 6065 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6066 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6067 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6069 | Py_ssize_t i, len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6070 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6071 | int kind; |
| 6072 | void *data; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6073 | _PyBytesWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 6075 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6076 | escape. |
| 6077 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 6078 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 6079 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 6080 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6081 | */ |
| 6082 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6083 | if (!PyUnicode_Check(unicode)) { |
| 6084 | PyErr_BadArgument(); |
| 6085 | return NULL; |
| 6086 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6087 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6088 | return NULL; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6089 | |
| 6090 | _PyBytesWriter_Init(&writer); |
| 6091 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6092 | len = PyUnicode_GET_LENGTH(unicode); |
| 6093 | kind = PyUnicode_KIND(unicode); |
| 6094 | data = PyUnicode_DATA(unicode); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6095 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6096 | p = _PyBytesWriter_Alloc(&writer, len); |
| 6097 | if (p == NULL) |
| 6098 | goto error; |
| 6099 | writer.overallocate = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6101 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6102 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6103 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6104 | /* Escape backslashes */ |
| 6105 | if (ch == '\\') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6106 | /* -1: substract 1 preallocated byte */ |
| 6107 | p = _PyBytesWriter_Prepare(&writer, p, 2-1); |
| 6108 | if (p == NULL) |
| 6109 | goto error; |
| 6110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | *p++ = '\\'; |
| 6112 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6113 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6114 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6115 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6116 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6117 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6118 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6119 | |
| 6120 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 6121 | if (p == NULL) |
| 6122 | goto error; |
| 6123 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6124 | *p++ = '\\'; |
| 6125 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6126 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6127 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6128 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6129 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6130 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6131 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6132 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6133 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6134 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6135 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6138 | if (ch >= 256) { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6139 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 6140 | if (p == NULL) |
| 6141 | goto error; |
| 6142 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6143 | *p++ = '\\'; |
| 6144 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6145 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6146 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6147 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6148 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6150 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6151 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6152 | else if (ch == '\t') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6153 | p = _PyBytesWriter_Prepare(&writer, p, 2-1); |
| 6154 | if (p == NULL) |
| 6155 | goto error; |
| 6156 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6157 | *p++ = '\\'; |
| 6158 | *p++ = 't'; |
| 6159 | } |
| 6160 | else if (ch == '\n') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6161 | p = _PyBytesWriter_Prepare(&writer, p, 2-1); |
| 6162 | if (p == NULL) |
| 6163 | goto error; |
| 6164 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6165 | *p++ = '\\'; |
| 6166 | *p++ = 'n'; |
| 6167 | } |
| 6168 | else if (ch == '\r') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6169 | p = _PyBytesWriter_Prepare(&writer, p, 2-1); |
| 6170 | if (p == NULL) |
| 6171 | goto error; |
| 6172 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6173 | *p++ = '\\'; |
| 6174 | *p++ = 'r'; |
| 6175 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6176 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6177 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6178 | else if (ch < ' ' || ch >= 0x7F) { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6179 | /* -1: substract 1 preallocated byte */ |
| 6180 | p = _PyBytesWriter_Prepare(&writer, p, 4-1); |
| 6181 | if (p == NULL) |
| 6182 | goto error; |
| 6183 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6185 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6186 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6187 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6188 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6190 | /* Copy everything else as-is */ |
| 6191 | else |
| 6192 | *p++ = (char) ch; |
| 6193 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6195 | return _PyBytesWriter_Finish(&writer, p); |
| 6196 | |
| 6197 | error: |
| 6198 | _PyBytesWriter_Dealloc(&writer); |
| 6199 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6202 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6203 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6204 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6205 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6206 | PyObject *result; |
| 6207 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6208 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6210 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6211 | Py_DECREF(tmp); |
| 6212 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | } |
| 6214 | |
| 6215 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6216 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6217 | PyObject * |
| 6218 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6219 | Py_ssize_t size, |
| 6220 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6222 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6223 | Py_ssize_t startinpos; |
| 6224 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6225 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | const char *end; |
| 6227 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6228 | PyObject *errorHandler = NULL; |
| 6229 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6230 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6231 | if (size == 0) |
| 6232 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6233 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6234 | /* Escaped strings will always be longer than the resulting |
| 6235 | 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] | 6236 | length after conversion to the true value. (But decoding error |
| 6237 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6238 | _PyUnicodeWriter_Init(&writer); |
| 6239 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6240 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6241 | end = s + size; |
| 6242 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6243 | unsigned char c; |
| 6244 | Py_UCS4 x; |
| 6245 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6246 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6248 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6249 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6250 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6251 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6252 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6253 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6254 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6255 | startinpos = s-starts; |
| 6256 | |
| 6257 | /* \u-escapes are only interpreted iff the number of leading |
| 6258 | backslashes if odd */ |
| 6259 | bs = s; |
| 6260 | for (;s < end;) { |
| 6261 | if (*s != '\\') |
| 6262 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6263 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6264 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6265 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6266 | } |
| 6267 | if (((s - bs) & 1) == 0 || |
| 6268 | s >= end || |
| 6269 | (*s != 'u' && *s != 'U')) { |
| 6270 | continue; |
| 6271 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6272 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6273 | count = *s=='u' ? 4 : 8; |
| 6274 | s++; |
| 6275 | |
| 6276 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6277 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6278 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6279 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6280 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6281 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6282 | errors, &errorHandler, |
| 6283 | "rawunicodeescape", "truncated \\uXXXX", |
| 6284 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6285 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6286 | goto onError; |
| 6287 | goto nextByte; |
| 6288 | } |
| 6289 | x = (x<<4) & ~0xF; |
| 6290 | if (c >= '0' && c <= '9') |
| 6291 | x += c - '0'; |
| 6292 | else if (c >= 'a' && c <= 'f') |
| 6293 | x += 10 + c - 'a'; |
| 6294 | else |
| 6295 | x += 10 + c - 'A'; |
| 6296 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6297 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6298 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6299 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6300 | } |
| 6301 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6302 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6303 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6304 | errors, &errorHandler, |
| 6305 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6306 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6307 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6308 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6309 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | nextByte: |
| 6311 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6312 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6313 | Py_XDECREF(errorHandler); |
| 6314 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6315 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6316 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6317 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6318 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6319 | Py_XDECREF(errorHandler); |
| 6320 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6321 | return NULL; |
| 6322 | } |
| 6323 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6324 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6325 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6326 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6327 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6328 | char *p; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6329 | Py_ssize_t pos; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6330 | int kind; |
| 6331 | void *data; |
| 6332 | Py_ssize_t len; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6333 | _PyBytesWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6334 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6335 | if (!PyUnicode_Check(unicode)) { |
| 6336 | PyErr_BadArgument(); |
| 6337 | return NULL; |
| 6338 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6339 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6340 | return NULL; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6341 | |
| 6342 | _PyBytesWriter_Init(&writer); |
| 6343 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6344 | kind = PyUnicode_KIND(unicode); |
| 6345 | data = PyUnicode_DATA(unicode); |
| 6346 | len = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6347 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6348 | p = _PyBytesWriter_Alloc(&writer, len); |
| 6349 | if (p == NULL) |
| 6350 | goto error; |
| 6351 | writer.overallocate = 1; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6352 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6353 | for (pos = 0; pos < len; pos++) { |
| 6354 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6355 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6356 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6357 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6358 | |
| 6359 | /* -1: substract 1 preallocated byte */ |
| 6360 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 6361 | if (p == NULL) |
| 6362 | goto error; |
| 6363 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6364 | *p++ = '\\'; |
| 6365 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6366 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6367 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6368 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6369 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6370 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6371 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6372 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6373 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6374 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6375 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6376 | else if (ch >= 256) { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6377 | /* -1: substract 1 preallocated byte */ |
| 6378 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 6379 | if (p == NULL) |
| 6380 | goto error; |
| 6381 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6382 | *p++ = '\\'; |
| 6383 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6384 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6385 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6386 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6387 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6388 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6389 | /* Copy everything else as-is */ |
| 6390 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | *p++ = (char) ch; |
| 6392 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6393 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6394 | return _PyBytesWriter_Finish(&writer, p); |
| 6395 | |
| 6396 | error: |
| 6397 | _PyBytesWriter_Dealloc(&writer); |
| 6398 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6401 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6402 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6403 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6405 | PyObject *result; |
| 6406 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6407 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6408 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6409 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6410 | Py_DECREF(tmp); |
| 6411 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | } |
| 6413 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6414 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6415 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6416 | PyObject * |
| 6417 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6418 | Py_ssize_t size, |
| 6419 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6420 | { |
| 6421 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6422 | Py_ssize_t startinpos; |
| 6423 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6424 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6425 | const char *end; |
| 6426 | const char *reason; |
| 6427 | PyObject *errorHandler = NULL; |
| 6428 | PyObject *exc = NULL; |
| 6429 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6430 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6431 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6432 | 1)) |
| 6433 | return NULL; |
| 6434 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6435 | if (size == 0) |
| 6436 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6437 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6438 | _PyUnicodeWriter_Init(&writer); |
| 6439 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6440 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6441 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6442 | } |
| 6443 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6444 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6445 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6446 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6447 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6448 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6449 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6450 | endinpos = end-starts; |
| 6451 | reason = "truncated input"; |
| 6452 | goto error; |
| 6453 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6454 | /* We copy the raw representation one byte at a time because the |
| 6455 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6456 | ((char *) &uch)[0] = s[0]; |
| 6457 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6458 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6459 | ((char *) &uch)[2] = s[2]; |
| 6460 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6461 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6462 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6463 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6464 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6465 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6466 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6467 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6468 | reason = "illegal code point (> 0x10FFFF)"; |
| 6469 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6470 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6471 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6472 | s += Py_UNICODE_SIZE; |
| 6473 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6474 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6475 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6476 | Py_UNICODE uch2; |
| 6477 | ((char *) &uch2)[0] = s[0]; |
| 6478 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6479 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6480 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6481 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6482 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6483 | } |
| 6484 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6485 | #endif |
| 6486 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6487 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6488 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6489 | continue; |
| 6490 | |
| 6491 | error: |
| 6492 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6493 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6494 | errors, &errorHandler, |
| 6495 | "unicode_internal", reason, |
| 6496 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6497 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6498 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6501 | Py_XDECREF(errorHandler); |
| 6502 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6503 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6504 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6505 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6506 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6507 | Py_XDECREF(errorHandler); |
| 6508 | Py_XDECREF(exc); |
| 6509 | return NULL; |
| 6510 | } |
| 6511 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6512 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6513 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6514 | PyObject * |
| 6515 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6516 | Py_ssize_t size, |
| 6517 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6518 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6519 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6520 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6521 | } |
| 6522 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6523 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6524 | static void |
| 6525 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6526 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6527 | PyObject *unicode, |
| 6528 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6529 | const char *reason) |
| 6530 | { |
| 6531 | if (*exceptionObject == NULL) { |
| 6532 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6533 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6534 | encoding, unicode, startpos, endpos, reason); |
| 6535 | } |
| 6536 | else { |
| 6537 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6538 | goto onError; |
| 6539 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6540 | goto onError; |
| 6541 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6542 | goto onError; |
| 6543 | return; |
| 6544 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 6545 | Py_CLEAR(*exceptionObject); |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6546 | } |
| 6547 | } |
| 6548 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6549 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6550 | static void |
| 6551 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6552 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6553 | PyObject *unicode, |
| 6554 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6555 | const char *reason) |
| 6556 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6557 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6558 | encoding, unicode, startpos, endpos, reason); |
| 6559 | if (*exceptionObject != NULL) |
| 6560 | PyCodec_StrictErrors(*exceptionObject); |
| 6561 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6562 | |
| 6563 | /* error handling callback helper: |
| 6564 | build arguments, call the callback and check the arguments, |
| 6565 | put the result into newpos and return the replacement string, which |
| 6566 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6567 | static PyObject * |
| 6568 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6569 | PyObject **errorHandler, |
| 6570 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6571 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6572 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6573 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6574 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 6575 | static const 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] | 6576 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6577 | PyObject *restuple; |
| 6578 | PyObject *resunicode; |
| 6579 | |
| 6580 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6582 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6583 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6586 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6587 | return NULL; |
| 6588 | len = PyUnicode_GET_LENGTH(unicode); |
| 6589 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6590 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6591 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6592 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6593 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6594 | |
| 6595 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6596 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6597 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6598 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6599 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6600 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6601 | Py_DECREF(restuple); |
| 6602 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6603 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6604 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6605 | &resunicode, newpos)) { |
| 6606 | Py_DECREF(restuple); |
| 6607 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6608 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6609 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6610 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6611 | Py_DECREF(restuple); |
| 6612 | return NULL; |
| 6613 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6614 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6615 | *newpos = len + *newpos; |
| 6616 | if (*newpos<0 || *newpos>len) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 6617 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6618 | Py_DECREF(restuple); |
| 6619 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6620 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6621 | Py_INCREF(resunicode); |
| 6622 | Py_DECREF(restuple); |
| 6623 | return resunicode; |
| 6624 | } |
| 6625 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6626 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6627 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6628 | const char *errors, |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6629 | const Py_UCS4 limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6630 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6631 | /* input state */ |
| 6632 | Py_ssize_t pos=0, size; |
| 6633 | int kind; |
| 6634 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6635 | /* pointer into the output */ |
| 6636 | char *str; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6637 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6638 | const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6639 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6640 | PyObject *exc = NULL; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6641 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6642 | PyObject *rep = NULL; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6643 | /* output object */ |
| 6644 | _PyBytesWriter writer; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6645 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6646 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6647 | return NULL; |
| 6648 | size = PyUnicode_GET_LENGTH(unicode); |
| 6649 | kind = PyUnicode_KIND(unicode); |
| 6650 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6651 | /* allocate enough for a simple encoding without |
| 6652 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6653 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6654 | return PyBytes_FromStringAndSize(NULL, 0); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6655 | |
| 6656 | _PyBytesWriter_Init(&writer); |
| 6657 | str = _PyBytesWriter_Alloc(&writer, size); |
| 6658 | if (str == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6659 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6660 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6661 | while (pos < size) { |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6662 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6663 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6664 | /* can we encode this? */ |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6665 | if (ch < limit) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6666 | /* no overflow check, because we know that the space is enough */ |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6667 | *str++ = (char)ch; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6668 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6669 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6670 | else { |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6671 | Py_ssize_t newpos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6672 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6673 | Py_ssize_t collstart = pos; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6674 | Py_ssize_t collend = collstart + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6675 | /* find all unecodable characters */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6676 | |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6677 | while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6678 | ++collend; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6679 | |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6680 | /* Only overallocate the buffer if it's not the last write */ |
| 6681 | writer.overallocate = (collend < size); |
| 6682 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6683 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6684 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 6685 | error_handler = get_error_handler(errors); |
| 6686 | |
| 6687 | switch (error_handler) { |
| 6688 | case _Py_ERROR_STRICT: |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6689 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6690 | goto onError; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6691 | |
| 6692 | case _Py_ERROR_REPLACE: |
Victor Stinner | 01ada39 | 2015-10-01 21:54:51 +0200 | [diff] [blame] | 6693 | memset(str, '?', collend - collstart); |
| 6694 | str += (collend - collstart); |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6695 | /* fall through ignore error handler */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6696 | case _Py_ERROR_IGNORE: |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6697 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6698 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6699 | |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6700 | case _Py_ERROR_BACKSLASHREPLACE: |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 6701 | /* substract preallocated bytes */ |
| 6702 | writer.min_size -= (collend - collstart); |
| 6703 | str = backslashreplace(&writer, str, |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6704 | unicode, collstart, collend); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6705 | if (str == NULL) |
| 6706 | goto onError; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6707 | pos = collend; |
| 6708 | break; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6709 | |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6710 | case _Py_ERROR_XMLCHARREFREPLACE: |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 6711 | /* substract preallocated bytes */ |
| 6712 | writer.min_size -= (collend - collstart); |
| 6713 | str = xmlcharrefreplace(&writer, str, |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6714 | unicode, collstart, collend); |
| 6715 | if (str == NULL) |
| 6716 | goto onError; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6717 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6718 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6719 | |
Victor Stinner | c3713e9 | 2015-09-29 12:32:13 +0200 | [diff] [blame] | 6720 | case _Py_ERROR_SURROGATEESCAPE: |
| 6721 | for (i = collstart; i < collend; ++i) { |
| 6722 | ch = PyUnicode_READ(kind, data, i); |
| 6723 | if (ch < 0xdc80 || 0xdcff < ch) { |
| 6724 | /* Not a UTF-8b surrogate */ |
| 6725 | break; |
| 6726 | } |
| 6727 | *str++ = (char)(ch - 0xdc00); |
| 6728 | ++pos; |
| 6729 | } |
| 6730 | if (i >= collend) |
| 6731 | break; |
| 6732 | collstart = pos; |
| 6733 | assert(collstart != collend); |
| 6734 | /* fallback to general error handling */ |
| 6735 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6736 | default: |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6737 | rep = unicode_encode_call_errorhandler(errors, &error_handler_obj, |
| 6738 | encoding, reason, unicode, &exc, |
| 6739 | collstart, collend, &newpos); |
| 6740 | if (rep == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6741 | goto onError; |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6742 | |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 6743 | /* substract preallocated bytes */ |
| 6744 | writer.min_size -= 1; |
| 6745 | |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6746 | if (PyBytes_Check(rep)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6747 | /* Directly copy bytes result to output. */ |
Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 6748 | str = _PyBytesWriter_WriteBytes(&writer, str, |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6749 | PyBytes_AS_STRING(rep), |
| 6750 | PyBytes_GET_SIZE(rep)); |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 6751 | if (str == NULL) |
| 6752 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6753 | } |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6754 | else { |
| 6755 | assert(PyUnicode_Check(rep)); |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6756 | |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6757 | if (PyUnicode_READY(rep) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6758 | goto onError; |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6759 | |
| 6760 | if (PyUnicode_IS_ASCII(rep)) { |
| 6761 | /* Fast path: all characters are smaller than limit */ |
| 6762 | assert(limit >= 128); |
| 6763 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 6764 | str = _PyBytesWriter_WriteBytes(&writer, str, |
| 6765 | PyUnicode_DATA(rep), |
| 6766 | PyUnicode_GET_LENGTH(rep)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | } |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6768 | else { |
| 6769 | Py_ssize_t repsize = PyUnicode_GET_LENGTH(rep); |
| 6770 | |
| 6771 | str = _PyBytesWriter_Prepare(&writer, str, repsize); |
| 6772 | if (str == NULL) |
| 6773 | goto onError; |
| 6774 | |
| 6775 | /* check if there is anything unencodable in the |
| 6776 | replacement and copy it to the output */ |
| 6777 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6778 | ch = PyUnicode_READ_CHAR(rep, i); |
| 6779 | if (ch >= limit) { |
| 6780 | raise_encode_exception(&exc, encoding, unicode, |
| 6781 | pos, pos+1, reason); |
| 6782 | goto onError; |
| 6783 | } |
| 6784 | *str = (char)ch; |
| 6785 | } |
| 6786 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6787 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6788 | pos = newpos; |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6789 | Py_CLEAR(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6790 | } |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6791 | |
| 6792 | /* If overallocation was disabled, ensure that it was the last |
| 6793 | write. Otherwise, we missed an optimization */ |
| 6794 | assert(writer.overallocate || pos == size); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6795 | } |
| 6796 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6797 | |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6798 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6799 | Py_XDECREF(exc); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6800 | return _PyBytesWriter_Finish(&writer, str); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6801 | |
| 6802 | onError: |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6803 | Py_XDECREF(rep); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6804 | _PyBytesWriter_Dealloc(&writer); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6805 | Py_XDECREF(error_handler_obj); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6806 | Py_XDECREF(exc); |
| 6807 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6810 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6811 | PyObject * |
| 6812 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6813 | Py_ssize_t size, |
| 6814 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6815 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6816 | PyObject *result; |
| 6817 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6818 | if (unicode == NULL) |
| 6819 | return NULL; |
| 6820 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6821 | Py_DECREF(unicode); |
| 6822 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | } |
| 6824 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6825 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6826 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6827 | { |
| 6828 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6829 | PyErr_BadArgument(); |
| 6830 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6831 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6832 | if (PyUnicode_READY(unicode) == -1) |
| 6833 | return NULL; |
| 6834 | /* Fast path: if it is a one-byte string, construct |
| 6835 | bytes object directly. */ |
| 6836 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6837 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6838 | PyUnicode_GET_LENGTH(unicode)); |
| 6839 | /* Non-Latin-1 characters present. Defer to above function to |
| 6840 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6841 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6842 | } |
| 6843 | |
| 6844 | PyObject* |
| 6845 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6846 | { |
| 6847 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6848 | } |
| 6849 | |
| 6850 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6851 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6852 | PyObject * |
| 6853 | PyUnicode_DecodeASCII(const char *s, |
| 6854 | Py_ssize_t size, |
| 6855 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6857 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6858 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6859 | int kind; |
| 6860 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6861 | Py_ssize_t startinpos; |
| 6862 | Py_ssize_t endinpos; |
| 6863 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6864 | const char *e; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6865 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6866 | PyObject *exc = NULL; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6867 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6869 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6870 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6871 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6872 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6873 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6874 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6875 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6876 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6877 | writer.min_length = size; |
| 6878 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6879 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6880 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6881 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6882 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6883 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6884 | writer.pos = outpos; |
| 6885 | if (writer.pos == size) |
| 6886 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6887 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6888 | s += writer.pos; |
| 6889 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6890 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6891 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6892 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6893 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6894 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6895 | ++s; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6896 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6897 | } |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6898 | |
| 6899 | /* byte outsize range 0x00..0x7f: call the error handler */ |
| 6900 | |
| 6901 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 6902 | error_handler = get_error_handler(errors); |
| 6903 | |
| 6904 | switch (error_handler) |
| 6905 | { |
| 6906 | case _Py_ERROR_REPLACE: |
| 6907 | case _Py_ERROR_SURROGATEESCAPE: |
| 6908 | /* Fast-path: the error handler only writes one character, |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 6909 | but we may switch to UCS2 at the first write */ |
| 6910 | if (_PyUnicodeWriter_PrepareKind(&writer, PyUnicode_2BYTE_KIND) < 0) |
| 6911 | goto onError; |
| 6912 | kind = writer.kind; |
| 6913 | data = writer.data; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6914 | |
| 6915 | if (error_handler == _Py_ERROR_REPLACE) |
| 6916 | PyUnicode_WRITE(kind, data, writer.pos, 0xfffd); |
| 6917 | else |
| 6918 | PyUnicode_WRITE(kind, data, writer.pos, c + 0xdc00); |
| 6919 | writer.pos++; |
| 6920 | ++s; |
| 6921 | break; |
| 6922 | |
| 6923 | case _Py_ERROR_IGNORE: |
| 6924 | ++s; |
| 6925 | break; |
| 6926 | |
| 6927 | default: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6928 | startinpos = s-starts; |
| 6929 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6930 | if (unicode_decode_call_errorhandler_writer( |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6931 | errors, &error_handler_obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6932 | "ascii", "ordinal not in range(128)", |
| 6933 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6934 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6935 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6936 | kind = writer.kind; |
| 6937 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6938 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6939 | } |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6940 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6941 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6942 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6943 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6944 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6945 | _PyUnicodeWriter_Dealloc(&writer); |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6946 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6947 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6948 | return NULL; |
| 6949 | } |
| 6950 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6951 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6952 | PyObject * |
| 6953 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6954 | Py_ssize_t size, |
| 6955 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6956 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6957 | PyObject *result; |
| 6958 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6959 | if (unicode == NULL) |
| 6960 | return NULL; |
| 6961 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6962 | Py_DECREF(unicode); |
| 6963 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6964 | } |
| 6965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6966 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6967 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6968 | { |
| 6969 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6970 | PyErr_BadArgument(); |
| 6971 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6973 | if (PyUnicode_READY(unicode) == -1) |
| 6974 | return NULL; |
| 6975 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6976 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6977 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6978 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6979 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6980 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6981 | } |
| 6982 | |
| 6983 | PyObject * |
| 6984 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6985 | { |
| 6986 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | } |
| 6988 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6989 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6990 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6991 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6992 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6993 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6994 | #define NEED_RETRY |
| 6995 | #endif |
| 6996 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6997 | #ifndef WC_ERR_INVALID_CHARS |
| 6998 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6999 | #endif |
| 7000 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 7001 | static const char* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7002 | code_page_name(UINT code_page, PyObject **obj) |
| 7003 | { |
| 7004 | *obj = NULL; |
| 7005 | if (code_page == CP_ACP) |
| 7006 | return "mbcs"; |
| 7007 | if (code_page == CP_UTF7) |
| 7008 | return "CP_UTF7"; |
| 7009 | if (code_page == CP_UTF8) |
| 7010 | return "CP_UTF8"; |
| 7011 | |
| 7012 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 7013 | if (*obj == NULL) |
| 7014 | return NULL; |
| 7015 | return PyBytes_AS_STRING(*obj); |
| 7016 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7017 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7018 | static DWORD |
| 7019 | decode_code_page_flags(UINT code_page) |
| 7020 | { |
| 7021 | if (code_page == CP_UTF7) { |
| 7022 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7023 | return 0; |
| 7024 | } |
| 7025 | else |
| 7026 | return MB_ERR_INVALID_CHARS; |
| 7027 | } |
| 7028 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7029 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7030 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7031 | * mode. |
| 7032 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7033 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 7034 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7035 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7036 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7037 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7038 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7039 | const char *in, |
| 7040 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7041 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7042 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7043 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7044 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7045 | |
| 7046 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7047 | assert(insize > 0); |
| 7048 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7049 | if (outsize <= 0) |
| 7050 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7051 | |
| 7052 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7053 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7054 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7055 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7056 | if (*v == NULL) |
| 7057 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7058 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7059 | } |
| 7060 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7061 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7062 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7063 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7064 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7065 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7066 | } |
| 7067 | |
| 7068 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7069 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7070 | if (outsize <= 0) |
| 7071 | goto error; |
| 7072 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7073 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7074 | error: |
| 7075 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7076 | return -2; |
| 7077 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7078 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7079 | } |
| 7080 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7081 | /* |
| 7082 | * Decode a byte string from a code page into unicode object with an error |
| 7083 | * handler. |
| 7084 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7085 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7086 | * UnicodeDecodeError exception and returns -1 on error. |
| 7087 | */ |
| 7088 | static int |
| 7089 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7090 | PyObject **v, |
| 7091 | const char *in, const int size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7092 | const char *errors, int final) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7093 | { |
| 7094 | const char *startin = in; |
| 7095 | const char *endin = in + size; |
| 7096 | const DWORD flags = decode_code_page_flags(code_page); |
| 7097 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7098 | 2000 English version of the message. */ |
| 7099 | const char *reason = "No mapping for the Unicode character exists " |
| 7100 | "in the target code page."; |
| 7101 | /* each step cannot decode more than 1 character, but a character can be |
| 7102 | represented as a surrogate pair */ |
| 7103 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7104 | int insize; |
| 7105 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7106 | PyObject *errorHandler = NULL; |
| 7107 | PyObject *exc = NULL; |
| 7108 | PyObject *encoding_obj = NULL; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 7109 | const char *encoding; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7110 | DWORD err; |
| 7111 | int ret = -1; |
| 7112 | |
| 7113 | assert(size > 0); |
| 7114 | |
| 7115 | encoding = code_page_name(code_page, &encoding_obj); |
| 7116 | if (encoding == NULL) |
| 7117 | return -1; |
| 7118 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7119 | if ((errors == NULL || strcmp(errors, "strict") == 0) && final) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7120 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7121 | UnicodeDecodeError. */ |
| 7122 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7123 | if (exc != NULL) { |
| 7124 | PyCodec_StrictErrors(exc); |
| 7125 | Py_CLEAR(exc); |
| 7126 | } |
| 7127 | goto error; |
| 7128 | } |
| 7129 | |
| 7130 | if (*v == NULL) { |
| 7131 | /* Create unicode object */ |
| 7132 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7133 | PyErr_NoMemory(); |
| 7134 | goto error; |
| 7135 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7136 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7137 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7138 | if (*v == NULL) |
| 7139 | goto error; |
| 7140 | startout = PyUnicode_AS_UNICODE(*v); |
| 7141 | } |
| 7142 | else { |
| 7143 | /* Extend unicode object */ |
| 7144 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7145 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7146 | PyErr_NoMemory(); |
| 7147 | goto error; |
| 7148 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7149 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7150 | goto error; |
| 7151 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7152 | } |
| 7153 | |
| 7154 | /* Decode the byte string character per character */ |
| 7155 | out = startout; |
| 7156 | while (in < endin) |
| 7157 | { |
| 7158 | /* Decode a character */ |
| 7159 | insize = 1; |
| 7160 | do |
| 7161 | { |
| 7162 | outsize = MultiByteToWideChar(code_page, flags, |
| 7163 | in, insize, |
| 7164 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7165 | if (outsize > 0) |
| 7166 | break; |
| 7167 | err = GetLastError(); |
| 7168 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7169 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7170 | { |
| 7171 | PyErr_SetFromWindowsErr(0); |
| 7172 | goto error; |
| 7173 | } |
| 7174 | insize++; |
| 7175 | } |
| 7176 | /* 4=maximum length of a UTF-8 sequence */ |
| 7177 | while (insize <= 4 && (in + insize) <= endin); |
| 7178 | |
| 7179 | if (outsize <= 0) { |
| 7180 | Py_ssize_t startinpos, endinpos, outpos; |
| 7181 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7182 | /* last character in partial decode? */ |
| 7183 | if (in + insize >= endin && !final) |
| 7184 | break; |
| 7185 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7186 | startinpos = in - startin; |
| 7187 | endinpos = startinpos + 1; |
| 7188 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7189 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7190 | errors, &errorHandler, |
| 7191 | encoding, reason, |
| 7192 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7193 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7194 | { |
| 7195 | goto error; |
| 7196 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7197 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7198 | } |
| 7199 | else { |
| 7200 | in += insize; |
| 7201 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7202 | out += outsize; |
| 7203 | } |
| 7204 | } |
| 7205 | |
| 7206 | /* write a NUL character at the end */ |
| 7207 | *out = 0; |
| 7208 | |
| 7209 | /* Extend unicode object */ |
| 7210 | outsize = out - startout; |
| 7211 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7212 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7213 | goto error; |
Victor Stinner | e1f17c6 | 2014-07-25 14:03:03 +0200 | [diff] [blame] | 7214 | /* (in - startin) <= size and size is an int */ |
| 7215 | ret = Py_SAFE_DOWNCAST(in - startin, Py_ssize_t, int); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7216 | |
| 7217 | error: |
| 7218 | Py_XDECREF(encoding_obj); |
| 7219 | Py_XDECREF(errorHandler); |
| 7220 | Py_XDECREF(exc); |
| 7221 | return ret; |
| 7222 | } |
| 7223 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7224 | static PyObject * |
| 7225 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7226 | const char *s, Py_ssize_t size, |
| 7227 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7228 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7229 | PyObject *v = NULL; |
| 7230 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7231 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7232 | if (code_page < 0) { |
| 7233 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7234 | return NULL; |
| 7235 | } |
| 7236 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7237 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7238 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7239 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7240 | do |
| 7241 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7242 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7243 | if (size > INT_MAX) { |
| 7244 | chunk_size = INT_MAX; |
| 7245 | final = 0; |
| 7246 | done = 0; |
| 7247 | } |
| 7248 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7249 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7250 | { |
| 7251 | chunk_size = (int)size; |
| 7252 | final = (consumed == NULL); |
| 7253 | done = 1; |
| 7254 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7255 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7256 | if (chunk_size == 0 && done) { |
| 7257 | if (v != NULL) |
| 7258 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7259 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7260 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7261 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7262 | converted = decode_code_page_strict(code_page, &v, |
| 7263 | s, chunk_size); |
| 7264 | if (converted == -2) |
| 7265 | converted = decode_code_page_errors(code_page, &v, |
| 7266 | s, chunk_size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7267 | errors, final); |
| 7268 | assert(converted != 0 || done); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7269 | |
| 7270 | if (converted < 0) { |
| 7271 | Py_XDECREF(v); |
| 7272 | return NULL; |
| 7273 | } |
| 7274 | |
| 7275 | if (consumed) |
| 7276 | *consumed += converted; |
| 7277 | |
| 7278 | s += converted; |
| 7279 | size -= converted; |
| 7280 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7281 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7282 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7283 | } |
| 7284 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7285 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7286 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7287 | const char *s, |
| 7288 | Py_ssize_t size, |
| 7289 | const char *errors, |
| 7290 | Py_ssize_t *consumed) |
| 7291 | { |
| 7292 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7293 | } |
| 7294 | |
| 7295 | PyObject * |
| 7296 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7297 | Py_ssize_t size, |
| 7298 | const char *errors, |
| 7299 | Py_ssize_t *consumed) |
| 7300 | { |
| 7301 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7302 | } |
| 7303 | |
| 7304 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7305 | PyUnicode_DecodeMBCS(const char *s, |
| 7306 | Py_ssize_t size, |
| 7307 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7308 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7309 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7310 | } |
| 7311 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7312 | static DWORD |
| 7313 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7314 | { |
| 7315 | if (code_page == CP_UTF8) { |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 7316 | return WC_ERR_INVALID_CHARS; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7317 | } |
| 7318 | else if (code_page == CP_UTF7) { |
| 7319 | /* CP_UTF7 only supports flags=0 */ |
| 7320 | return 0; |
| 7321 | } |
| 7322 | else { |
| 7323 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7324 | return 0; |
| 7325 | else |
| 7326 | return WC_NO_BEST_FIT_CHARS; |
| 7327 | } |
| 7328 | } |
| 7329 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7330 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7331 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7332 | * mode. |
| 7333 | * |
| 7334 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7335 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7336 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7337 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7338 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7339 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7340 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7341 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7342 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7343 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7344 | int outsize; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7345 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7346 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7347 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7348 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7349 | /* Create a substring so that we can get the UTF-16 representation |
| 7350 | of just the slice under consideration. */ |
| 7351 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7352 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7353 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7354 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7355 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7356 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7357 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7358 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7359 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7360 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7361 | if (substring == NULL) |
| 7362 | return -1; |
| 7363 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7364 | if (p == NULL) { |
| 7365 | Py_DECREF(substring); |
| 7366 | return -1; |
| 7367 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7368 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7369 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7370 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7371 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7372 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7373 | NULL, 0, |
| 7374 | NULL, pusedDefaultChar); |
| 7375 | if (outsize <= 0) |
| 7376 | goto error; |
| 7377 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7378 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7379 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7380 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7381 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7382 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7383 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7384 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7385 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7386 | if (*outbytes == NULL) { |
| 7387 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7388 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7389 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7390 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7391 | } |
| 7392 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7393 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7394 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7395 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7396 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7397 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7398 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7399 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7400 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7401 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7402 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7403 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7404 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7405 | } |
| 7406 | |
| 7407 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7408 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7409 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7410 | out, outsize, |
| 7411 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7412 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7413 | if (outsize <= 0) |
| 7414 | goto error; |
| 7415 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7416 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7417 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7418 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7419 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7420 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7421 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7422 | return -2; |
| 7423 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7424 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7425 | } |
| 7426 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7427 | /* |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 7428 | * Encode a Unicode string to a Windows code page into a byte string using an |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7429 | * error handler. |
| 7430 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7431 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7432 | * -1 on other error. |
| 7433 | */ |
| 7434 | static int |
| 7435 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7436 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7437 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7438 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7439 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7440 | Py_ssize_t pos = unicode_offset; |
| 7441 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7442 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7443 | 2000 English version of the message. */ |
| 7444 | const char *reason = "invalid character"; |
| 7445 | /* 4=maximum length of a UTF-8 sequence */ |
| 7446 | char buffer[4]; |
| 7447 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7448 | Py_ssize_t outsize; |
| 7449 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7450 | PyObject *errorHandler = NULL; |
| 7451 | PyObject *exc = NULL; |
| 7452 | PyObject *encoding_obj = NULL; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 7453 | const char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7454 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7455 | PyObject *rep; |
| 7456 | int ret = -1; |
| 7457 | |
| 7458 | assert(insize > 0); |
| 7459 | |
| 7460 | encoding = code_page_name(code_page, &encoding_obj); |
| 7461 | if (encoding == NULL) |
| 7462 | return -1; |
| 7463 | |
| 7464 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7465 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7466 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7467 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7468 | if (exc != NULL) { |
| 7469 | PyCodec_StrictErrors(exc); |
| 7470 | Py_DECREF(exc); |
| 7471 | } |
| 7472 | Py_XDECREF(encoding_obj); |
| 7473 | return -1; |
| 7474 | } |
| 7475 | |
| 7476 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7477 | pusedDefaultChar = &usedDefaultChar; |
| 7478 | else |
| 7479 | pusedDefaultChar = NULL; |
| 7480 | |
| 7481 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7482 | PyErr_NoMemory(); |
| 7483 | goto error; |
| 7484 | } |
| 7485 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7486 | |
| 7487 | if (*outbytes == NULL) { |
| 7488 | /* Create string object */ |
| 7489 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7490 | if (*outbytes == NULL) |
| 7491 | goto error; |
| 7492 | out = PyBytes_AS_STRING(*outbytes); |
| 7493 | } |
| 7494 | else { |
| 7495 | /* Extend string object */ |
| 7496 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7497 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7498 | PyErr_NoMemory(); |
| 7499 | goto error; |
| 7500 | } |
| 7501 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7502 | goto error; |
| 7503 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7504 | } |
| 7505 | |
| 7506 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7507 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7508 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7509 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7510 | wchar_t chars[2]; |
| 7511 | int charsize; |
| 7512 | if (ch < 0x10000) { |
| 7513 | chars[0] = (wchar_t)ch; |
| 7514 | charsize = 1; |
| 7515 | } |
| 7516 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7517 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7518 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7519 | charsize = 2; |
| 7520 | } |
| 7521 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7522 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7523 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7524 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7525 | NULL, pusedDefaultChar); |
| 7526 | if (outsize > 0) { |
| 7527 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7528 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7529 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7530 | memcpy(out, buffer, outsize); |
| 7531 | out += outsize; |
| 7532 | continue; |
| 7533 | } |
| 7534 | } |
| 7535 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7536 | PyErr_SetFromWindowsErr(0); |
| 7537 | goto error; |
| 7538 | } |
| 7539 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7540 | rep = unicode_encode_call_errorhandler( |
| 7541 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7542 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7543 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7544 | if (rep == NULL) |
| 7545 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7546 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7547 | |
| 7548 | if (PyBytes_Check(rep)) { |
| 7549 | outsize = PyBytes_GET_SIZE(rep); |
| 7550 | if (outsize != 1) { |
| 7551 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7552 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7553 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7554 | Py_DECREF(rep); |
| 7555 | goto error; |
| 7556 | } |
| 7557 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7558 | } |
| 7559 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7560 | out += outsize; |
| 7561 | } |
| 7562 | else { |
| 7563 | Py_ssize_t i; |
| 7564 | enum PyUnicode_Kind kind; |
| 7565 | void *data; |
| 7566 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7567 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7568 | Py_DECREF(rep); |
| 7569 | goto error; |
| 7570 | } |
| 7571 | |
| 7572 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7573 | if (outsize != 1) { |
| 7574 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7575 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7576 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7577 | Py_DECREF(rep); |
| 7578 | goto error; |
| 7579 | } |
| 7580 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7581 | } |
| 7582 | kind = PyUnicode_KIND(rep); |
| 7583 | data = PyUnicode_DATA(rep); |
| 7584 | for (i=0; i < outsize; i++) { |
| 7585 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7586 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7587 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7588 | encoding, unicode, |
| 7589 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7590 | "unable to encode error handler result to ASCII"); |
| 7591 | Py_DECREF(rep); |
| 7592 | goto error; |
| 7593 | } |
| 7594 | *out = (unsigned char)ch; |
| 7595 | out++; |
| 7596 | } |
| 7597 | } |
| 7598 | Py_DECREF(rep); |
| 7599 | } |
| 7600 | /* write a NUL byte */ |
| 7601 | *out = 0; |
| 7602 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7603 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7604 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7605 | goto error; |
| 7606 | ret = 0; |
| 7607 | |
| 7608 | error: |
| 7609 | Py_XDECREF(encoding_obj); |
| 7610 | Py_XDECREF(errorHandler); |
| 7611 | Py_XDECREF(exc); |
| 7612 | return ret; |
| 7613 | } |
| 7614 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7615 | static PyObject * |
| 7616 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7617 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7618 | const char *errors) |
| 7619 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7620 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7621 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7622 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7623 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7624 | |
Victor Stinner | 29dacf2 | 2015-01-26 16:41:32 +0100 | [diff] [blame] | 7625 | if (!PyUnicode_Check(unicode)) { |
| 7626 | PyErr_BadArgument(); |
| 7627 | return NULL; |
| 7628 | } |
| 7629 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7630 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7631 | return NULL; |
| 7632 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7633 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7634 | if (code_page < 0) { |
| 7635 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7636 | return NULL; |
| 7637 | } |
| 7638 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7639 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7640 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7641 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7642 | offset = 0; |
| 7643 | do |
| 7644 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7645 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7646 | /* 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] | 7647 | chunks. */ |
| 7648 | if (len > INT_MAX/2) { |
| 7649 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7650 | done = 0; |
| 7651 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7652 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7653 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7654 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7655 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7656 | done = 1; |
| 7657 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7658 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7659 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7660 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7661 | errors); |
| 7662 | if (ret == -2) |
| 7663 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7664 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7665 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7666 | if (ret < 0) { |
| 7667 | Py_XDECREF(outbytes); |
| 7668 | return NULL; |
| 7669 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7670 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7671 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7672 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7673 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7674 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7675 | return outbytes; |
| 7676 | } |
| 7677 | |
| 7678 | PyObject * |
| 7679 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7680 | Py_ssize_t size, |
| 7681 | const char *errors) |
| 7682 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7683 | PyObject *unicode, *res; |
| 7684 | unicode = PyUnicode_FromUnicode(p, size); |
| 7685 | if (unicode == NULL) |
| 7686 | return NULL; |
| 7687 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7688 | Py_DECREF(unicode); |
| 7689 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7690 | } |
| 7691 | |
| 7692 | PyObject * |
| 7693 | PyUnicode_EncodeCodePage(int code_page, |
| 7694 | PyObject *unicode, |
| 7695 | const char *errors) |
| 7696 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7697 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7698 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7699 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7700 | PyObject * |
| 7701 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7702 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7703 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7704 | } |
| 7705 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7706 | #undef NEED_RETRY |
| 7707 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7708 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7709 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7710 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7711 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7712 | static int |
| 7713 | charmap_decode_string(const char *s, |
| 7714 | Py_ssize_t size, |
| 7715 | PyObject *mapping, |
| 7716 | const char *errors, |
| 7717 | _PyUnicodeWriter *writer) |
| 7718 | { |
| 7719 | const char *starts = s; |
| 7720 | const char *e; |
| 7721 | Py_ssize_t startinpos, endinpos; |
| 7722 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7723 | Py_ssize_t maplen; |
| 7724 | enum PyUnicode_Kind mapkind; |
| 7725 | void *mapdata; |
| 7726 | Py_UCS4 x; |
| 7727 | unsigned char ch; |
| 7728 | |
| 7729 | if (PyUnicode_READY(mapping) == -1) |
| 7730 | return -1; |
| 7731 | |
| 7732 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7733 | mapdata = PyUnicode_DATA(mapping); |
| 7734 | mapkind = PyUnicode_KIND(mapping); |
| 7735 | |
| 7736 | e = s + size; |
| 7737 | |
| 7738 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7739 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7740 | * is disabled in encoding aliases, latin1 is preferred because |
| 7741 | * its implementation is faster. */ |
| 7742 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7743 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7744 | Py_UCS4 maxchar = writer->maxchar; |
| 7745 | |
| 7746 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7747 | while (s < e) { |
| 7748 | ch = *s; |
| 7749 | x = mapdata_ucs1[ch]; |
| 7750 | if (x > maxchar) { |
| 7751 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7752 | goto onError; |
| 7753 | maxchar = writer->maxchar; |
| 7754 | outdata = (Py_UCS1 *)writer->data; |
| 7755 | } |
| 7756 | outdata[writer->pos] = x; |
| 7757 | writer->pos++; |
| 7758 | ++s; |
| 7759 | } |
| 7760 | return 0; |
| 7761 | } |
| 7762 | |
| 7763 | while (s < e) { |
| 7764 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7765 | enum PyUnicode_Kind outkind = writer->kind; |
| 7766 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7767 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7768 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7769 | Py_UCS4 maxchar = writer->maxchar; |
| 7770 | while (s < e) { |
| 7771 | ch = *s; |
| 7772 | x = mapdata_ucs2[ch]; |
| 7773 | if (x > maxchar) |
| 7774 | goto Error; |
| 7775 | outdata[writer->pos] = x; |
| 7776 | writer->pos++; |
| 7777 | ++s; |
| 7778 | } |
| 7779 | break; |
| 7780 | } |
| 7781 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7782 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7783 | while (s < e) { |
| 7784 | ch = *s; |
| 7785 | x = mapdata_ucs2[ch]; |
| 7786 | if (x == 0xFFFE) |
| 7787 | goto Error; |
| 7788 | outdata[writer->pos] = x; |
| 7789 | writer->pos++; |
| 7790 | ++s; |
| 7791 | } |
| 7792 | break; |
| 7793 | } |
| 7794 | } |
| 7795 | ch = *s; |
| 7796 | |
| 7797 | if (ch < maplen) |
| 7798 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7799 | else |
| 7800 | x = 0xfffe; /* invalid value */ |
| 7801 | Error: |
| 7802 | if (x == 0xfffe) |
| 7803 | { |
| 7804 | /* undefined mapping */ |
| 7805 | startinpos = s-starts; |
| 7806 | endinpos = startinpos+1; |
| 7807 | if (unicode_decode_call_errorhandler_writer( |
| 7808 | errors, &errorHandler, |
| 7809 | "charmap", "character maps to <undefined>", |
| 7810 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7811 | writer)) { |
| 7812 | goto onError; |
| 7813 | } |
| 7814 | continue; |
| 7815 | } |
| 7816 | |
| 7817 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7818 | goto onError; |
| 7819 | ++s; |
| 7820 | } |
| 7821 | Py_XDECREF(errorHandler); |
| 7822 | Py_XDECREF(exc); |
| 7823 | return 0; |
| 7824 | |
| 7825 | onError: |
| 7826 | Py_XDECREF(errorHandler); |
| 7827 | Py_XDECREF(exc); |
| 7828 | return -1; |
| 7829 | } |
| 7830 | |
| 7831 | static int |
| 7832 | charmap_decode_mapping(const char *s, |
| 7833 | Py_ssize_t size, |
| 7834 | PyObject *mapping, |
| 7835 | const char *errors, |
| 7836 | _PyUnicodeWriter *writer) |
| 7837 | { |
| 7838 | const char *starts = s; |
| 7839 | const char *e; |
| 7840 | Py_ssize_t startinpos, endinpos; |
| 7841 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7842 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7843 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7844 | |
| 7845 | e = s + size; |
| 7846 | |
| 7847 | while (s < e) { |
| 7848 | ch = *s; |
| 7849 | |
| 7850 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7851 | key = PyLong_FromLong((long)ch); |
| 7852 | if (key == NULL) |
| 7853 | goto onError; |
| 7854 | |
| 7855 | item = PyObject_GetItem(mapping, key); |
| 7856 | Py_DECREF(key); |
| 7857 | if (item == NULL) { |
| 7858 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7859 | /* No mapping found means: mapping is undefined. */ |
| 7860 | PyErr_Clear(); |
| 7861 | goto Undefined; |
| 7862 | } else |
| 7863 | goto onError; |
| 7864 | } |
| 7865 | |
| 7866 | /* Apply mapping */ |
| 7867 | if (item == Py_None) |
| 7868 | goto Undefined; |
| 7869 | if (PyLong_Check(item)) { |
| 7870 | long value = PyLong_AS_LONG(item); |
| 7871 | if (value == 0xFFFE) |
| 7872 | goto Undefined; |
| 7873 | if (value < 0 || value > MAX_UNICODE) { |
| 7874 | PyErr_Format(PyExc_TypeError, |
| 7875 | "character mapping must be in range(0x%lx)", |
| 7876 | (unsigned long)MAX_UNICODE + 1); |
| 7877 | goto onError; |
| 7878 | } |
| 7879 | |
| 7880 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7881 | goto onError; |
| 7882 | } |
| 7883 | else if (PyUnicode_Check(item)) { |
| 7884 | if (PyUnicode_READY(item) == -1) |
| 7885 | goto onError; |
| 7886 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7887 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7888 | if (value == 0xFFFE) |
| 7889 | goto Undefined; |
| 7890 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7891 | goto onError; |
| 7892 | } |
| 7893 | else { |
| 7894 | writer->overallocate = 1; |
| 7895 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7896 | goto onError; |
| 7897 | } |
| 7898 | } |
| 7899 | else { |
| 7900 | /* wrong return value */ |
| 7901 | PyErr_SetString(PyExc_TypeError, |
| 7902 | "character mapping must return integer, None or str"); |
| 7903 | goto onError; |
| 7904 | } |
| 7905 | Py_CLEAR(item); |
| 7906 | ++s; |
| 7907 | continue; |
| 7908 | |
| 7909 | Undefined: |
| 7910 | /* undefined mapping */ |
| 7911 | Py_CLEAR(item); |
| 7912 | startinpos = s-starts; |
| 7913 | endinpos = startinpos+1; |
| 7914 | if (unicode_decode_call_errorhandler_writer( |
| 7915 | errors, &errorHandler, |
| 7916 | "charmap", "character maps to <undefined>", |
| 7917 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7918 | writer)) { |
| 7919 | goto onError; |
| 7920 | } |
| 7921 | } |
| 7922 | Py_XDECREF(errorHandler); |
| 7923 | Py_XDECREF(exc); |
| 7924 | return 0; |
| 7925 | |
| 7926 | onError: |
| 7927 | Py_XDECREF(item); |
| 7928 | Py_XDECREF(errorHandler); |
| 7929 | Py_XDECREF(exc); |
| 7930 | return -1; |
| 7931 | } |
| 7932 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7933 | PyObject * |
| 7934 | PyUnicode_DecodeCharmap(const char *s, |
| 7935 | Py_ssize_t size, |
| 7936 | PyObject *mapping, |
| 7937 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7938 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7939 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7940 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7941 | /* Default to Latin-1 */ |
| 7942 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7943 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7944 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7945 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7946 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7947 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7948 | writer.min_length = size; |
| 7949 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7950 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7951 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7952 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7953 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7954 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7955 | } |
| 7956 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7957 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7958 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7959 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7960 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7961 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7962 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7963 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7964 | return NULL; |
| 7965 | } |
| 7966 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7967 | /* Charmap encoding: the lookup table */ |
| 7968 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7969 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7970 | PyObject_HEAD |
| 7971 | unsigned char level1[32]; |
| 7972 | int count2, count3; |
| 7973 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7974 | }; |
| 7975 | |
| 7976 | static PyObject* |
| 7977 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7978 | { |
| 7979 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7980 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7981 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7982 | } |
| 7983 | |
| 7984 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7985 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7986 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7987 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7988 | }; |
| 7989 | |
| 7990 | static void |
| 7991 | encoding_map_dealloc(PyObject* o) |
| 7992 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7993 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7994 | } |
| 7995 | |
| 7996 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7997 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7998 | "EncodingMap", /*tp_name*/ |
| 7999 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 8000 | 0, /*tp_itemsize*/ |
| 8001 | /* methods */ |
| 8002 | encoding_map_dealloc, /*tp_dealloc*/ |
| 8003 | 0, /*tp_print*/ |
| 8004 | 0, /*tp_getattr*/ |
| 8005 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 8006 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8007 | 0, /*tp_repr*/ |
| 8008 | 0, /*tp_as_number*/ |
| 8009 | 0, /*tp_as_sequence*/ |
| 8010 | 0, /*tp_as_mapping*/ |
| 8011 | 0, /*tp_hash*/ |
| 8012 | 0, /*tp_call*/ |
| 8013 | 0, /*tp_str*/ |
| 8014 | 0, /*tp_getattro*/ |
| 8015 | 0, /*tp_setattro*/ |
| 8016 | 0, /*tp_as_buffer*/ |
| 8017 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 8018 | 0, /*tp_doc*/ |
| 8019 | 0, /*tp_traverse*/ |
| 8020 | 0, /*tp_clear*/ |
| 8021 | 0, /*tp_richcompare*/ |
| 8022 | 0, /*tp_weaklistoffset*/ |
| 8023 | 0, /*tp_iter*/ |
| 8024 | 0, /*tp_iternext*/ |
| 8025 | encoding_map_methods, /*tp_methods*/ |
| 8026 | 0, /*tp_members*/ |
| 8027 | 0, /*tp_getset*/ |
| 8028 | 0, /*tp_base*/ |
| 8029 | 0, /*tp_dict*/ |
| 8030 | 0, /*tp_descr_get*/ |
| 8031 | 0, /*tp_descr_set*/ |
| 8032 | 0, /*tp_dictoffset*/ |
| 8033 | 0, /*tp_init*/ |
| 8034 | 0, /*tp_alloc*/ |
| 8035 | 0, /*tp_new*/ |
| 8036 | 0, /*tp_free*/ |
| 8037 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8038 | }; |
| 8039 | |
| 8040 | PyObject* |
| 8041 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 8042 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8043 | PyObject *result; |
| 8044 | struct encoding_map *mresult; |
| 8045 | int i; |
| 8046 | int need_dict = 0; |
| 8047 | unsigned char level1[32]; |
| 8048 | unsigned char level2[512]; |
| 8049 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8050 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8051 | int kind; |
| 8052 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8053 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8054 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8055 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8056 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8057 | PyErr_BadArgument(); |
| 8058 | return NULL; |
| 8059 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8060 | kind = PyUnicode_KIND(string); |
| 8061 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8062 | length = PyUnicode_GET_LENGTH(string); |
| 8063 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8064 | memset(level1, 0xFF, sizeof level1); |
| 8065 | memset(level2, 0xFF, sizeof level2); |
| 8066 | |
| 8067 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8068 | or if there are non-BMP characters, we need to use |
| 8069 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8070 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8071 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8072 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8073 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8074 | ch = PyUnicode_READ(kind, data, i); |
| 8075 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8076 | need_dict = 1; |
| 8077 | break; |
| 8078 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8079 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8080 | /* unmapped character */ |
| 8081 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8082 | l1 = ch >> 11; |
| 8083 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8084 | if (level1[l1] == 0xFF) |
| 8085 | level1[l1] = count2++; |
| 8086 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8087 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8091 | need_dict = 1; |
| 8092 | |
| 8093 | if (need_dict) { |
| 8094 | PyObject *result = PyDict_New(); |
| 8095 | PyObject *key, *value; |
| 8096 | if (!result) |
| 8097 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8098 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8099 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8100 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8101 | if (!key || !value) |
| 8102 | goto failed1; |
| 8103 | if (PyDict_SetItem(result, key, value) == -1) |
| 8104 | goto failed1; |
| 8105 | Py_DECREF(key); |
| 8106 | Py_DECREF(value); |
| 8107 | } |
| 8108 | return result; |
| 8109 | failed1: |
| 8110 | Py_XDECREF(key); |
| 8111 | Py_XDECREF(value); |
| 8112 | Py_DECREF(result); |
| 8113 | return NULL; |
| 8114 | } |
| 8115 | |
| 8116 | /* Create a three-level trie */ |
| 8117 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8118 | 16*count2 + 128*count3 - 1); |
| 8119 | if (!result) |
| 8120 | return PyErr_NoMemory(); |
| 8121 | PyObject_Init(result, &EncodingMapType); |
| 8122 | mresult = (struct encoding_map*)result; |
| 8123 | mresult->count2 = count2; |
| 8124 | mresult->count3 = count3; |
| 8125 | mlevel1 = mresult->level1; |
| 8126 | mlevel2 = mresult->level23; |
| 8127 | mlevel3 = mresult->level23 + 16*count2; |
| 8128 | memcpy(mlevel1, level1, 32); |
| 8129 | memset(mlevel2, 0xFF, 16*count2); |
| 8130 | memset(mlevel3, 0, 128*count3); |
| 8131 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8132 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8133 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8134 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8135 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8136 | /* unmapped character */ |
| 8137 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8138 | o1 = ch>>11; |
| 8139 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8140 | i2 = 16*mlevel1[o1] + o2; |
| 8141 | if (mlevel2[i2] == 0xFF) |
| 8142 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8143 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8144 | i3 = 128*mlevel2[i2] + o3; |
| 8145 | mlevel3[i3] = i; |
| 8146 | } |
| 8147 | return result; |
| 8148 | } |
| 8149 | |
| 8150 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8151 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8152 | { |
| 8153 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8154 | int l1 = c>>11; |
| 8155 | int l2 = (c>>7) & 0xF; |
| 8156 | int l3 = c & 0x7F; |
| 8157 | int i; |
| 8158 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8159 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8161 | if (c == 0) |
| 8162 | return 0; |
| 8163 | /* level 1*/ |
| 8164 | i = map->level1[l1]; |
| 8165 | if (i == 0xFF) { |
| 8166 | return -1; |
| 8167 | } |
| 8168 | /* level 2*/ |
| 8169 | i = map->level23[16*i+l2]; |
| 8170 | if (i == 0xFF) { |
| 8171 | return -1; |
| 8172 | } |
| 8173 | /* level 3 */ |
| 8174 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8175 | if (i == 0) { |
| 8176 | return -1; |
| 8177 | } |
| 8178 | return i; |
| 8179 | } |
| 8180 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8181 | /* Lookup the character ch in the mapping. If the character |
| 8182 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8183 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8184 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8185 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8186 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8187 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8188 | PyObject *x; |
| 8189 | |
| 8190 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8191 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8192 | x = PyObject_GetItem(mapping, w); |
| 8193 | Py_DECREF(w); |
| 8194 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8195 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8196 | /* No mapping found means: mapping is undefined. */ |
| 8197 | PyErr_Clear(); |
| 8198 | x = Py_None; |
| 8199 | Py_INCREF(x); |
| 8200 | return x; |
| 8201 | } else |
| 8202 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8203 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8204 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8205 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8206 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8207 | long value = PyLong_AS_LONG(x); |
| 8208 | if (value < 0 || value > 255) { |
| 8209 | PyErr_SetString(PyExc_TypeError, |
| 8210 | "character mapping must be in range(256)"); |
| 8211 | Py_DECREF(x); |
| 8212 | return NULL; |
| 8213 | } |
| 8214 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8215 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8216 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8217 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8218 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8219 | /* wrong return value */ |
| 8220 | PyErr_Format(PyExc_TypeError, |
| 8221 | "character mapping must return integer, bytes or None, not %.400s", |
| 8222 | x->ob_type->tp_name); |
| 8223 | Py_DECREF(x); |
| 8224 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8225 | } |
| 8226 | } |
| 8227 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8228 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8229 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8230 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8231 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8232 | /* exponentially overallocate to minimize reallocations */ |
| 8233 | if (requiredsize < 2*outsize) |
| 8234 | requiredsize = 2*outsize; |
| 8235 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8236 | return -1; |
| 8237 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8238 | } |
| 8239 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8240 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8241 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8242 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8243 | /* 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] | 8244 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8245 | space is available. Return a new reference to the object that |
| 8246 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8247 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8248 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8249 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8250 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8251 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8252 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8253 | PyObject *rep; |
| 8254 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8255 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8256 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8257 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8258 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8259 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8260 | if (res == -1) |
| 8261 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8262 | if (outsize<requiredsize) |
| 8263 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8264 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8265 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8266 | outstart[(*outpos)++] = (char)res; |
| 8267 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8268 | } |
| 8269 | |
| 8270 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8271 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8272 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8273 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8274 | Py_DECREF(rep); |
| 8275 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8276 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8277 | if (PyLong_Check(rep)) { |
| 8278 | Py_ssize_t requiredsize = *outpos+1; |
| 8279 | if (outsize<requiredsize) |
| 8280 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8281 | Py_DECREF(rep); |
| 8282 | return enc_EXCEPTION; |
| 8283 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8284 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8285 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8286 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8287 | else { |
| 8288 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8289 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8290 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8291 | if (outsize<requiredsize) |
| 8292 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8293 | Py_DECREF(rep); |
| 8294 | return enc_EXCEPTION; |
| 8295 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8296 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8297 | memcpy(outstart + *outpos, repchars, repsize); |
| 8298 | *outpos += repsize; |
| 8299 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8300 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8301 | Py_DECREF(rep); |
| 8302 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8303 | } |
| 8304 | |
| 8305 | /* handle an error in PyUnicode_EncodeCharmap |
| 8306 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8307 | static int |
| 8308 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8309 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8310 | PyObject **exceptionObject, |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8311 | _Py_error_handler *error_handler, PyObject **error_handler_obj, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8312 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8313 | { |
| 8314 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8315 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8316 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8317 | enum PyUnicode_Kind kind; |
| 8318 | void *data; |
| 8319 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8320 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8321 | Py_ssize_t collstartpos = *inpos; |
| 8322 | Py_ssize_t collendpos = *inpos+1; |
| 8323 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8324 | char *encoding = "charmap"; |
| 8325 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8326 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8327 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8328 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8329 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8330 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8331 | return -1; |
| 8332 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8333 | /* find all unencodable characters */ |
| 8334 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8335 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8336 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8337 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8338 | val = encoding_map_lookup(ch, mapping); |
| 8339 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8340 | break; |
| 8341 | ++collendpos; |
| 8342 | continue; |
| 8343 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8344 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8345 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8346 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 | if (rep==NULL) |
| 8348 | return -1; |
| 8349 | else if (rep!=Py_None) { |
| 8350 | Py_DECREF(rep); |
| 8351 | break; |
| 8352 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8353 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8354 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8355 | } |
| 8356 | /* cache callback name lookup |
| 8357 | * (if not done yet, i.e. it's the first error) */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8358 | if (*error_handler == _Py_ERROR_UNKNOWN) |
| 8359 | *error_handler = get_error_handler(errors); |
| 8360 | |
| 8361 | switch (*error_handler) { |
| 8362 | case _Py_ERROR_STRICT: |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8363 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8364 | return -1; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8365 | |
| 8366 | case _Py_ERROR_REPLACE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8367 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8368 | x = charmapencode_output('?', mapping, res, respos); |
| 8369 | if (x==enc_EXCEPTION) { |
| 8370 | return -1; |
| 8371 | } |
| 8372 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8373 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8374 | return -1; |
| 8375 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8376 | } |
| 8377 | /* fall through */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8378 | case _Py_ERROR_IGNORE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8379 | *inpos = collendpos; |
| 8380 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8381 | |
| 8382 | case _Py_ERROR_XMLCHARREFREPLACE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8383 | /* generate replacement (temporarily (mis)uses p) */ |
| 8384 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8385 | char buffer[2+29+1+1]; |
| 8386 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8387 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8388 | for (cp = buffer; *cp; ++cp) { |
| 8389 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8390 | if (x==enc_EXCEPTION) |
| 8391 | return -1; |
| 8392 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8393 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8394 | return -1; |
| 8395 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8396 | } |
| 8397 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8398 | *inpos = collendpos; |
| 8399 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8400 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8401 | default: |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8402 | repunicode = unicode_encode_call_errorhandler(errors, error_handler_obj, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8403 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8404 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8405 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8406 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8407 | if (PyBytes_Check(repunicode)) { |
| 8408 | /* Directly copy bytes result to output. */ |
| 8409 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8410 | Py_ssize_t requiredsize; |
| 8411 | repsize = PyBytes_Size(repunicode); |
| 8412 | requiredsize = *respos + repsize; |
| 8413 | if (requiredsize > outsize) |
| 8414 | /* Make room for all additional bytes. */ |
| 8415 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8416 | Py_DECREF(repunicode); |
| 8417 | return -1; |
| 8418 | } |
| 8419 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8420 | PyBytes_AsString(repunicode), repsize); |
| 8421 | *respos += repsize; |
| 8422 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8423 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8424 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8425 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8426 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8427 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8428 | Py_DECREF(repunicode); |
| 8429 | return -1; |
| 8430 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8431 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8432 | data = PyUnicode_DATA(repunicode); |
| 8433 | kind = PyUnicode_KIND(repunicode); |
| 8434 | for (index = 0; index < repsize; index++) { |
| 8435 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8436 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8438 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8439 | return -1; |
| 8440 | } |
| 8441 | else if (x==enc_FAILED) { |
| 8442 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8443 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8444 | return -1; |
| 8445 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8446 | } |
| 8447 | *inpos = newpos; |
| 8448 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8449 | } |
| 8450 | return 0; |
| 8451 | } |
| 8452 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8453 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8454 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8455 | PyObject *mapping, |
| 8456 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8457 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8458 | /* output object */ |
| 8459 | PyObject *res = NULL; |
| 8460 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8461 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8462 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8463 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8464 | Py_ssize_t respos = 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8465 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8466 | PyObject *exc = NULL; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8467 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8468 | void *data; |
| 8469 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8470 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8471 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8472 | return NULL; |
| 8473 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8474 | data = PyUnicode_DATA(unicode); |
| 8475 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8476 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8477 | /* Default to Latin-1 */ |
| 8478 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8479 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8480 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8481 | /* allocate enough for a simple encoding without |
| 8482 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8483 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8484 | if (res == NULL) |
| 8485 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8486 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8487 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8489 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8490 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8491 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8492 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8493 | if (x==enc_EXCEPTION) /* error */ |
| 8494 | goto onError; |
| 8495 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8496 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8497 | &exc, |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8498 | &error_handler, &error_handler_obj, errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | &res, &respos)) { |
| 8500 | goto onError; |
| 8501 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8502 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8503 | else |
| 8504 | /* done with this character => adjust input position */ |
| 8505 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8507 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8508 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8509 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8510 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8511 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8512 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8513 | Py_XDECREF(exc); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8514 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8515 | return res; |
| 8516 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8517 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8518 | Py_XDECREF(res); |
| 8519 | Py_XDECREF(exc); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8520 | Py_XDECREF(error_handler_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8521 | return NULL; |
| 8522 | } |
| 8523 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8524 | /* Deprecated */ |
| 8525 | PyObject * |
| 8526 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8527 | Py_ssize_t size, |
| 8528 | PyObject *mapping, |
| 8529 | const char *errors) |
| 8530 | { |
| 8531 | PyObject *result; |
| 8532 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8533 | if (unicode == NULL) |
| 8534 | return NULL; |
| 8535 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8536 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8537 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8538 | } |
| 8539 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8540 | PyObject * |
| 8541 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8542 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | { |
| 8544 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 | PyErr_BadArgument(); |
| 8546 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8547 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8548 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8549 | } |
| 8550 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8551 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8552 | static void |
| 8553 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8554 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8555 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8556 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8557 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8558 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8559 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8560 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8561 | } |
| 8562 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8563 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8564 | goto onError; |
| 8565 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8566 | goto onError; |
| 8567 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8568 | goto onError; |
| 8569 | return; |
| 8570 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 8571 | Py_CLEAR(*exceptionObject); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | } |
| 8573 | } |
| 8574 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8575 | /* error handling callback helper: |
| 8576 | build arguments, call the callback and check the arguments, |
| 8577 | put the result into newpos and return the replacement string, which |
| 8578 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8579 | static PyObject * |
| 8580 | unicode_translate_call_errorhandler(const char *errors, |
| 8581 | PyObject **errorHandler, |
| 8582 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8583 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8584 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8585 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8586 | { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 8587 | static const 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] | 8588 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8589 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8590 | PyObject *restuple; |
| 8591 | PyObject *resunicode; |
| 8592 | |
| 8593 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8594 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8595 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8596 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8597 | } |
| 8598 | |
| 8599 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8600 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8601 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8602 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8603 | |
| 8604 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8605 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8606 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8607 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8608 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8609 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8610 | Py_DECREF(restuple); |
| 8611 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8612 | } |
| 8613 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8614 | &resunicode, &i_newpos)) { |
| 8615 | Py_DECREF(restuple); |
| 8616 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8617 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8618 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8619 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8620 | else |
| 8621 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8622 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 8623 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8624 | Py_DECREF(restuple); |
| 8625 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8626 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8627 | Py_INCREF(resunicode); |
| 8628 | Py_DECREF(restuple); |
| 8629 | return resunicode; |
| 8630 | } |
| 8631 | |
| 8632 | /* Lookup the character ch in the mapping and put the result in result, |
| 8633 | which must be decrefed by the caller. |
| 8634 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8635 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8636 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8637 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8638 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8639 | PyObject *x; |
| 8640 | |
| 8641 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8642 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8643 | x = PyObject_GetItem(mapping, w); |
| 8644 | Py_DECREF(w); |
| 8645 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8646 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8647 | /* No mapping found means: use 1:1 mapping. */ |
| 8648 | PyErr_Clear(); |
| 8649 | *result = NULL; |
| 8650 | return 0; |
| 8651 | } else |
| 8652 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8653 | } |
| 8654 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8655 | *result = x; |
| 8656 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8657 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8658 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8659 | long value = PyLong_AS_LONG(x); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8660 | if (value < 0 || value > MAX_UNICODE) { |
| 8661 | PyErr_Format(PyExc_ValueError, |
| 8662 | "character mapping must be in range(0x%x)", |
| 8663 | MAX_UNICODE+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8664 | Py_DECREF(x); |
| 8665 | return -1; |
| 8666 | } |
| 8667 | *result = x; |
| 8668 | return 0; |
| 8669 | } |
| 8670 | else if (PyUnicode_Check(x)) { |
| 8671 | *result = x; |
| 8672 | return 0; |
| 8673 | } |
| 8674 | else { |
| 8675 | /* wrong return value */ |
| 8676 | PyErr_SetString(PyExc_TypeError, |
| 8677 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8678 | Py_DECREF(x); |
| 8679 | return -1; |
| 8680 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8681 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8682 | |
| 8683 | /* lookup the character, write the result into the writer. |
| 8684 | Return 1 if the result was written into the writer, return 0 if the mapping |
| 8685 | was undefined, raise an exception return -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8686 | static int |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8687 | charmaptranslate_output(Py_UCS4 ch, PyObject *mapping, |
| 8688 | _PyUnicodeWriter *writer) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8689 | { |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8690 | PyObject *item; |
| 8691 | |
| 8692 | if (charmaptranslate_lookup(ch, mapping, &item)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8693 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8694 | |
| 8695 | if (item == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8696 | /* not found => default to 1:1 mapping */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8697 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8698 | return -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8699 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8700 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8701 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8702 | |
| 8703 | if (item == Py_None) { |
| 8704 | Py_DECREF(item); |
| 8705 | return 0; |
| 8706 | } |
| 8707 | |
| 8708 | if (PyLong_Check(item)) { |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8709 | long ch = (Py_UCS4)PyLong_AS_LONG(item); |
| 8710 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8711 | used it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8712 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
| 8713 | Py_DECREF(item); |
| 8714 | return -1; |
| 8715 | } |
| 8716 | Py_DECREF(item); |
| 8717 | return 1; |
| 8718 | } |
| 8719 | |
| 8720 | if (!PyUnicode_Check(item)) { |
| 8721 | Py_DECREF(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8722 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8723 | } |
| 8724 | |
| 8725 | if (_PyUnicodeWriter_WriteStr(writer, item) < 0) { |
| 8726 | Py_DECREF(item); |
| 8727 | return -1; |
| 8728 | } |
| 8729 | |
| 8730 | Py_DECREF(item); |
| 8731 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8732 | } |
| 8733 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8734 | static int |
| 8735 | unicode_fast_translate_lookup(PyObject *mapping, Py_UCS1 ch, |
| 8736 | Py_UCS1 *translate) |
| 8737 | { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8738 | PyObject *item = NULL; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8739 | int ret = 0; |
| 8740 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8741 | if (charmaptranslate_lookup(ch, mapping, &item)) { |
| 8742 | return -1; |
| 8743 | } |
| 8744 | |
| 8745 | if (item == Py_None) { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8746 | /* deletion */ |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8747 | translate[ch] = 0xfe; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8748 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8749 | else if (item == NULL) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8750 | /* not found => default to 1:1 mapping */ |
| 8751 | translate[ch] = ch; |
| 8752 | return 1; |
| 8753 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8754 | else if (PyLong_Check(item)) { |
Victor Stinner | 4dd2525 | 2014-04-08 09:14:21 +0200 | [diff] [blame] | 8755 | long replace = PyLong_AS_LONG(item); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8756 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8757 | used it */ |
| 8758 | if (127 < replace) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8759 | /* invalid character or character outside ASCII: |
| 8760 | skip the fast translate */ |
| 8761 | goto exit; |
| 8762 | } |
| 8763 | translate[ch] = (Py_UCS1)replace; |
| 8764 | } |
| 8765 | else if (PyUnicode_Check(item)) { |
| 8766 | Py_UCS4 replace; |
| 8767 | |
| 8768 | if (PyUnicode_READY(item) == -1) { |
| 8769 | Py_DECREF(item); |
| 8770 | return -1; |
| 8771 | } |
| 8772 | if (PyUnicode_GET_LENGTH(item) != 1) |
| 8773 | goto exit; |
| 8774 | |
| 8775 | replace = PyUnicode_READ_CHAR(item, 0); |
| 8776 | if (replace > 127) |
| 8777 | goto exit; |
| 8778 | translate[ch] = (Py_UCS1)replace; |
| 8779 | } |
| 8780 | else { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8781 | /* not None, NULL, long or unicode */ |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8782 | goto exit; |
| 8783 | } |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8784 | ret = 1; |
| 8785 | |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8786 | exit: |
| 8787 | Py_DECREF(item); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8788 | return ret; |
| 8789 | } |
| 8790 | |
| 8791 | /* Fast path for ascii => ascii translation. Return 1 if the whole string |
| 8792 | was translated into writer, return 0 if the input string was partially |
| 8793 | translated into writer, raise an exception and return -1 on error. */ |
| 8794 | static int |
| 8795 | unicode_fast_translate(PyObject *input, PyObject *mapping, |
Victor Stinner | 6c9aa8f | 2016-03-01 21:30:30 +0100 | [diff] [blame] | 8796 | _PyUnicodeWriter *writer, int ignore, |
| 8797 | Py_ssize_t *input_pos) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8798 | { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8799 | Py_UCS1 ascii_table[128], ch, ch2; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8800 | Py_ssize_t len; |
| 8801 | Py_UCS1 *in, *end, *out; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8802 | int res = 0; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8803 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8804 | len = PyUnicode_GET_LENGTH(input); |
| 8805 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8806 | memset(ascii_table, 0xff, 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8807 | |
| 8808 | in = PyUnicode_1BYTE_DATA(input); |
| 8809 | end = in + len; |
| 8810 | |
| 8811 | assert(PyUnicode_IS_ASCII(writer->buffer)); |
| 8812 | assert(PyUnicode_GET_LENGTH(writer->buffer) == len); |
| 8813 | out = PyUnicode_1BYTE_DATA(writer->buffer); |
| 8814 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8815 | for (; in < end; in++) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8816 | ch = *in; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8817 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8818 | if (ch2 == 0xff) { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8819 | int translate = unicode_fast_translate_lookup(mapping, ch, |
| 8820 | ascii_table); |
| 8821 | if (translate < 0) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8822 | return -1; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8823 | if (translate == 0) |
| 8824 | goto exit; |
| 8825 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8826 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8827 | if (ch2 == 0xfe) { |
| 8828 | if (ignore) |
| 8829 | continue; |
| 8830 | goto exit; |
| 8831 | } |
| 8832 | assert(ch2 < 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8833 | *out = ch2; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8834 | out++; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8835 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8836 | res = 1; |
| 8837 | |
| 8838 | exit: |
| 8839 | writer->pos = out - PyUnicode_1BYTE_DATA(writer->buffer); |
Victor Stinner | 6c9aa8f | 2016-03-01 21:30:30 +0100 | [diff] [blame] | 8840 | *input_pos = in - PyUnicode_1BYTE_DATA(input); |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8841 | return res; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8842 | } |
| 8843 | |
Victor Stinner | 3222da2 | 2015-10-01 22:07:32 +0200 | [diff] [blame] | 8844 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8845 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8846 | PyObject *mapping, |
| 8847 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8848 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8849 | /* input object */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8850 | char *data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8851 | Py_ssize_t size, i; |
| 8852 | int kind; |
| 8853 | /* output buffer */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8854 | _PyUnicodeWriter writer; |
| 8855 | /* error handler */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8856 | char *reason = "character maps to <undefined>"; |
| 8857 | PyObject *errorHandler = NULL; |
| 8858 | PyObject *exc = NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8859 | int ignore; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8860 | int res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8861 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8862 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8863 | PyErr_BadArgument(); |
| 8864 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8865 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8866 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8867 | if (PyUnicode_READY(input) == -1) |
| 8868 | return NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8869 | data = (char*)PyUnicode_DATA(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8870 | kind = PyUnicode_KIND(input); |
| 8871 | size = PyUnicode_GET_LENGTH(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8872 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 8873 | if (size == 0) |
| 8874 | return PyUnicode_FromObject(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8876 | /* allocate enough for a simple 1:1 translation without |
| 8877 | replacements, if we need more, we'll resize */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8878 | _PyUnicodeWriter_Init(&writer); |
| 8879 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8880 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8881 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8882 | ignore = (errors != NULL && strcmp(errors, "ignore") == 0); |
| 8883 | |
Victor Stinner | 3379867 | 2016-03-01 21:59:58 +0100 | [diff] [blame] | 8884 | if (PyUnicode_READY(input) == -1) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8885 | return NULL; |
Victor Stinner | 3379867 | 2016-03-01 21:59:58 +0100 | [diff] [blame] | 8886 | if (PyUnicode_IS_ASCII(input)) { |
| 8887 | res = unicode_fast_translate(input, mapping, &writer, ignore, &i); |
| 8888 | if (res < 0) { |
| 8889 | _PyUnicodeWriter_Dealloc(&writer); |
| 8890 | return NULL; |
| 8891 | } |
| 8892 | if (res == 1) |
| 8893 | return _PyUnicodeWriter_Finish(&writer); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8894 | } |
Victor Stinner | 3379867 | 2016-03-01 21:59:58 +0100 | [diff] [blame] | 8895 | else { |
| 8896 | i = 0; |
| 8897 | } |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8898 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8899 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8900 | /* try to encode it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8901 | int translate; |
| 8902 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8903 | Py_ssize_t newpos; |
| 8904 | /* startpos for collecting untranslatable chars */ |
| 8905 | Py_ssize_t collstart; |
| 8906 | Py_ssize_t collend; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8907 | Py_UCS4 ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8908 | |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8909 | ch = PyUnicode_READ(kind, data, i); |
| 8910 | translate = charmaptranslate_output(ch, mapping, &writer); |
| 8911 | if (translate < 0) |
| 8912 | goto onError; |
| 8913 | |
| 8914 | if (translate != 0) { |
| 8915 | /* it worked => adjust input pointer */ |
| 8916 | ++i; |
| 8917 | continue; |
| 8918 | } |
| 8919 | |
| 8920 | /* untranslatable character */ |
| 8921 | collstart = i; |
| 8922 | collend = i+1; |
| 8923 | |
| 8924 | /* find all untranslatable characters */ |
| 8925 | while (collend < size) { |
| 8926 | PyObject *x; |
| 8927 | ch = PyUnicode_READ(kind, data, collend); |
| 8928 | if (charmaptranslate_lookup(ch, mapping, &x)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8929 | goto onError; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8930 | Py_XDECREF(x); |
| 8931 | if (x != Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8932 | break; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8933 | ++collend; |
| 8934 | } |
| 8935 | |
| 8936 | if (ignore) { |
| 8937 | i = collend; |
| 8938 | } |
| 8939 | else { |
| 8940 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 8941 | reason, input, &exc, |
| 8942 | collstart, collend, &newpos); |
| 8943 | if (repunicode == NULL) |
| 8944 | goto onError; |
| 8945 | if (_PyUnicodeWriter_WriteStr(&writer, repunicode) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | Py_DECREF(repunicode); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8947 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8948 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8949 | Py_DECREF(repunicode); |
| 8950 | i = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8951 | } |
| 8952 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8953 | Py_XDECREF(exc); |
| 8954 | Py_XDECREF(errorHandler); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8955 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8956 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8957 | onError: |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8958 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8959 | Py_XDECREF(exc); |
| 8960 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | return NULL; |
| 8962 | } |
| 8963 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8964 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8965 | PyObject * |
| 8966 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8967 | Py_ssize_t size, |
| 8968 | PyObject *mapping, |
| 8969 | const char *errors) |
| 8970 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8971 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8972 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8973 | if (!unicode) |
| 8974 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8975 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8976 | Py_DECREF(unicode); |
| 8977 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8978 | } |
| 8979 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8980 | PyObject * |
| 8981 | PyUnicode_Translate(PyObject *str, |
| 8982 | PyObject *mapping, |
| 8983 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 8985 | if (ensure_unicode(str) < 0) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8986 | return NULL; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 8987 | return _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8988 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8989 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8990 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8991 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | { |
| 8993 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8994 | called as a callback from fixup() which does it already. */ |
| 8995 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8996 | const int kind = PyUnicode_KIND(self); |
| 8997 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8998 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8999 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9000 | Py_ssize_t i; |
| 9001 | |
| 9002 | for (i = 0; i < len; ++i) { |
| 9003 | ch = PyUnicode_READ(kind, data, i); |
| 9004 | fixed = 0; |
| 9005 | if (ch > 127) { |
| 9006 | if (Py_UNICODE_ISSPACE(ch)) |
| 9007 | fixed = ' '; |
| 9008 | else { |
| 9009 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9010 | if (decimal >= 0) |
| 9011 | fixed = '0' + decimal; |
| 9012 | } |
| 9013 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9014 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9015 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9016 | PyUnicode_WRITE(kind, data, i, fixed); |
| 9017 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9018 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9019 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9020 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9021 | } |
| 9022 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9023 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9024 | } |
| 9025 | |
| 9026 | PyObject * |
| 9027 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 9028 | { |
| 9029 | if (!PyUnicode_Check(unicode)) { |
| 9030 | PyErr_BadInternalCall(); |
| 9031 | return NULL; |
| 9032 | } |
| 9033 | if (PyUnicode_READY(unicode) == -1) |
| 9034 | return NULL; |
| 9035 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 9036 | /* If the string is already ASCII, just return the same string */ |
| 9037 | Py_INCREF(unicode); |
| 9038 | return unicode; |
| 9039 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9040 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9041 | } |
| 9042 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9043 | PyObject * |
| 9044 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 9045 | Py_ssize_t length) |
| 9046 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9047 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9048 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9049 | Py_UCS4 maxchar; |
| 9050 | enum PyUnicode_Kind kind; |
| 9051 | void *data; |
| 9052 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 9053 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9054 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 9055 | Py_UCS4 ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9056 | if (ch > 127) { |
| 9057 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9058 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9059 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9060 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9061 | } |
| 9062 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9063 | |
| 9064 | /* Copy to a new string */ |
| 9065 | decimal = PyUnicode_New(length, maxchar); |
| 9066 | if (decimal == NULL) |
| 9067 | return decimal; |
| 9068 | kind = PyUnicode_KIND(decimal); |
| 9069 | data = PyUnicode_DATA(decimal); |
| 9070 | /* Iterate over code points */ |
| 9071 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 9072 | Py_UCS4 ch = s[i]; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9073 | if (ch > 127) { |
| 9074 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9075 | if (decimal >= 0) |
| 9076 | ch = '0' + decimal; |
| 9077 | } |
| 9078 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9079 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9080 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9081 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9082 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9083 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9084 | int |
| 9085 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9086 | Py_ssize_t length, |
| 9087 | char *output, |
| 9088 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9089 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9090 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9091 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9092 | enum PyUnicode_Kind kind; |
| 9093 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9094 | |
| 9095 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9096 | PyErr_BadArgument(); |
| 9097 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9098 | } |
| 9099 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9100 | unicode = PyUnicode_FromUnicode(s, length); |
| 9101 | if (unicode == NULL) |
| 9102 | return -1; |
| 9103 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9104 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9105 | Py_DECREF(unicode); |
| 9106 | return -1; |
| 9107 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9108 | kind = PyUnicode_KIND(unicode); |
| 9109 | data = PyUnicode_DATA(unicode); |
| 9110 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9111 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9112 | PyObject *exc; |
| 9113 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9114 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9115 | Py_ssize_t startpos; |
| 9116 | |
| 9117 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9118 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9119 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9120 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9121 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9122 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9123 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9124 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9125 | if (decimal >= 0) { |
| 9126 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9127 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9128 | continue; |
| 9129 | } |
| 9130 | if (0 < ch && ch < 256) { |
| 9131 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9132 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9133 | continue; |
| 9134 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9135 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9136 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9137 | exc = NULL; |
| 9138 | raise_encode_exception(&exc, "decimal", unicode, |
| 9139 | startpos, startpos+1, |
| 9140 | "invalid decimal Unicode string"); |
| 9141 | Py_XDECREF(exc); |
| 9142 | Py_DECREF(unicode); |
| 9143 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9144 | } |
| 9145 | /* 0-terminate the output string */ |
| 9146 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9147 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9148 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9149 | } |
| 9150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9151 | /* --- Helpers ------------------------------------------------------------ */ |
| 9152 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9153 | /* helper macro to fixup start/end slice values */ |
| 9154 | #define ADJUST_INDICES(start, end, len) \ |
| 9155 | if (end > len) \ |
| 9156 | end = len; \ |
| 9157 | else if (end < 0) { \ |
| 9158 | end += len; \ |
| 9159 | if (end < 0) \ |
| 9160 | end = 0; \ |
| 9161 | } \ |
| 9162 | if (start < 0) { \ |
| 9163 | start += len; \ |
| 9164 | if (start < 0) \ |
| 9165 | start = 0; \ |
| 9166 | } |
| 9167 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9168 | static Py_ssize_t |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9169 | any_find_slice(PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9170 | Py_ssize_t start, |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9171 | Py_ssize_t end, |
| 9172 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9173 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9174 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9175 | void *buf1, *buf2; |
| 9176 | Py_ssize_t len1, len2, result; |
| 9177 | |
| 9178 | kind1 = PyUnicode_KIND(s1); |
| 9179 | kind2 = PyUnicode_KIND(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9180 | if (kind1 < kind2) |
| 9181 | return -1; |
| 9182 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9183 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9184 | len2 = PyUnicode_GET_LENGTH(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9185 | ADJUST_INDICES(start, end, len1); |
| 9186 | if (end - start < len2) |
| 9187 | return -1; |
| 9188 | |
| 9189 | buf1 = PyUnicode_DATA(s1); |
| 9190 | buf2 = PyUnicode_DATA(s2); |
| 9191 | if (len2 == 1) { |
| 9192 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 9193 | result = findchar((const char *)buf1 + kind1*start, |
| 9194 | kind1, end - start, ch, direction); |
| 9195 | if (result == -1) |
| 9196 | return -1; |
| 9197 | else |
| 9198 | return start + result; |
| 9199 | } |
| 9200 | |
| 9201 | if (kind2 != kind1) { |
| 9202 | buf2 = _PyUnicode_AsKind(s2, kind1); |
| 9203 | if (!buf2) |
| 9204 | return -2; |
| 9205 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9206 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9207 | if (direction > 0) { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9208 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9209 | case PyUnicode_1BYTE_KIND: |
| 9210 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9211 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9212 | else |
| 9213 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9214 | break; |
| 9215 | case PyUnicode_2BYTE_KIND: |
| 9216 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9217 | break; |
| 9218 | case PyUnicode_4BYTE_KIND: |
| 9219 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9220 | break; |
| 9221 | default: |
| 9222 | assert(0); result = -2; |
| 9223 | } |
| 9224 | } |
| 9225 | else { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9226 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9227 | case PyUnicode_1BYTE_KIND: |
| 9228 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9229 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9230 | else |
| 9231 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9232 | break; |
| 9233 | case PyUnicode_2BYTE_KIND: |
| 9234 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9235 | break; |
| 9236 | case PyUnicode_4BYTE_KIND: |
| 9237 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9238 | break; |
| 9239 | default: |
| 9240 | assert(0); result = -2; |
| 9241 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9242 | } |
| 9243 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9244 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9245 | PyMem_Free(buf2); |
| 9246 | |
| 9247 | return result; |
| 9248 | } |
| 9249 | |
| 9250 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9251 | _PyUnicode_InsertThousandsGrouping( |
| 9252 | PyObject *unicode, Py_ssize_t index, |
| 9253 | Py_ssize_t n_buffer, |
| 9254 | void *digits, Py_ssize_t n_digits, |
| 9255 | Py_ssize_t min_width, |
| 9256 | const char *grouping, PyObject *thousands_sep, |
| 9257 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9258 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9259 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9260 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9261 | Py_ssize_t thousands_sep_len; |
| 9262 | Py_ssize_t len; |
| 9263 | |
| 9264 | if (unicode != NULL) { |
| 9265 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9266 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9267 | } |
| 9268 | else { |
| 9269 | kind = PyUnicode_1BYTE_KIND; |
| 9270 | data = NULL; |
| 9271 | } |
| 9272 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9273 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9274 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9275 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9276 | if (thousands_sep_kind < kind) { |
| 9277 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9278 | if (!thousands_sep_data) |
| 9279 | return -1; |
| 9280 | } |
| 9281 | else { |
| 9282 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9283 | if (!data) |
| 9284 | return -1; |
| 9285 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9286 | } |
| 9287 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9288 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9289 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9290 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9291 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9292 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9293 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9294 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9295 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9296 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9297 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9298 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9299 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9300 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9301 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9302 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9303 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9304 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9305 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9306 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9307 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9308 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9309 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9310 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9311 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9312 | break; |
| 9313 | default: |
| 9314 | assert(0); |
| 9315 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9316 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9317 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9318 | if (thousands_sep_kind < kind) |
| 9319 | PyMem_Free(thousands_sep_data); |
| 9320 | else |
| 9321 | PyMem_Free(data); |
| 9322 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9323 | if (unicode == NULL) { |
| 9324 | *maxchar = 127; |
| 9325 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9326 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9327 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9328 | } |
| 9329 | } |
| 9330 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9331 | } |
| 9332 | |
| 9333 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9334 | Py_ssize_t |
| 9335 | PyUnicode_Count(PyObject *str, |
| 9336 | PyObject *substr, |
| 9337 | Py_ssize_t start, |
| 9338 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9339 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9340 | Py_ssize_t result; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9341 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9342 | void *buf1 = NULL, *buf2 = NULL; |
| 9343 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9344 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9345 | if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9346 | return -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9347 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9348 | kind1 = PyUnicode_KIND(str); |
| 9349 | kind2 = PyUnicode_KIND(substr); |
| 9350 | if (kind1 < kind2) |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9351 | return 0; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9352 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9353 | len1 = PyUnicode_GET_LENGTH(str); |
| 9354 | len2 = PyUnicode_GET_LENGTH(substr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9355 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9356 | if (end - start < len2) |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9357 | return 0; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9358 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9359 | buf1 = PyUnicode_DATA(str); |
| 9360 | buf2 = PyUnicode_DATA(substr); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9361 | if (kind2 != kind1) { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9362 | buf2 = _PyUnicode_AsKind(substr, kind1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9363 | if (!buf2) |
| 9364 | goto onError; |
| 9365 | } |
| 9366 | |
| 9367 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9368 | case PyUnicode_1BYTE_KIND: |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9369 | if (PyUnicode_IS_ASCII(str) && PyUnicode_IS_ASCII(substr)) |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9370 | result = asciilib_count( |
| 9371 | ((Py_UCS1*)buf1) + start, end - start, |
| 9372 | buf2, len2, PY_SSIZE_T_MAX |
| 9373 | ); |
| 9374 | else |
| 9375 | result = ucs1lib_count( |
| 9376 | ((Py_UCS1*)buf1) + start, end - start, |
| 9377 | buf2, len2, PY_SSIZE_T_MAX |
| 9378 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9379 | break; |
| 9380 | case PyUnicode_2BYTE_KIND: |
| 9381 | result = ucs2lib_count( |
| 9382 | ((Py_UCS2*)buf1) + start, end - start, |
| 9383 | buf2, len2, PY_SSIZE_T_MAX |
| 9384 | ); |
| 9385 | break; |
| 9386 | case PyUnicode_4BYTE_KIND: |
| 9387 | result = ucs4lib_count( |
| 9388 | ((Py_UCS4*)buf1) + start, end - start, |
| 9389 | buf2, len2, PY_SSIZE_T_MAX |
| 9390 | ); |
| 9391 | break; |
| 9392 | default: |
| 9393 | assert(0); result = 0; |
| 9394 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9395 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9396 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9397 | PyMem_Free(buf2); |
| 9398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9399 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9400 | onError: |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9401 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9402 | PyMem_Free(buf2); |
| 9403 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9404 | } |
| 9405 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9406 | Py_ssize_t |
| 9407 | PyUnicode_Find(PyObject *str, |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9408 | PyObject *substr, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9409 | Py_ssize_t start, |
| 9410 | Py_ssize_t end, |
| 9411 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9412 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9413 | if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9414 | return -2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9415 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9416 | return any_find_slice(str, substr, start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9417 | } |
| 9418 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9419 | Py_ssize_t |
| 9420 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9421 | Py_ssize_t start, Py_ssize_t end, |
| 9422 | int direction) |
| 9423 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9424 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9425 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9426 | if (PyUnicode_READY(str) == -1) |
| 9427 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9428 | if (start < 0 || end < 0) { |
| 9429 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9430 | return -2; |
| 9431 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9432 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9433 | end = PyUnicode_GET_LENGTH(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9434 | if (start >= end) |
| 9435 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9436 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9437 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9438 | kind, end-start, ch, direction); |
| 9439 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9440 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9441 | else |
| 9442 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9443 | } |
| 9444 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9445 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9446 | tailmatch(PyObject *self, |
| 9447 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9448 | Py_ssize_t start, |
| 9449 | Py_ssize_t end, |
| 9450 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9451 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9452 | int kind_self; |
| 9453 | int kind_sub; |
| 9454 | void *data_self; |
| 9455 | void *data_sub; |
| 9456 | Py_ssize_t offset; |
| 9457 | Py_ssize_t i; |
| 9458 | Py_ssize_t end_sub; |
| 9459 | |
| 9460 | if (PyUnicode_READY(self) == -1 || |
| 9461 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9462 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9464 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9465 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9466 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9467 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9468 | |
Serhiy Storchaka | d4ea03c | 2015-05-31 09:15:51 +0300 | [diff] [blame] | 9469 | if (PyUnicode_GET_LENGTH(substring) == 0) |
| 9470 | return 1; |
| 9471 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9472 | kind_self = PyUnicode_KIND(self); |
| 9473 | data_self = PyUnicode_DATA(self); |
| 9474 | kind_sub = PyUnicode_KIND(substring); |
| 9475 | data_sub = PyUnicode_DATA(substring); |
| 9476 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9477 | |
| 9478 | if (direction > 0) |
| 9479 | offset = end; |
| 9480 | else |
| 9481 | offset = start; |
| 9482 | |
| 9483 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9484 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9485 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9486 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9487 | /* If both are of the same kind, memcmp is sufficient */ |
| 9488 | if (kind_self == kind_sub) { |
| 9489 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9490 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9491 | data_sub, |
| 9492 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9493 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9494 | } |
Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 9495 | /* otherwise we have to compare each character by first accessing it */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9496 | else { |
| 9497 | /* We do not need to compare 0 and len(substring)-1 because |
| 9498 | the if statement above ensured already that they are equal |
| 9499 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9500 | for (i = 1; i < end_sub; ++i) { |
| 9501 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9502 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9503 | return 0; |
| 9504 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9505 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9506 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9507 | } |
| 9508 | |
| 9509 | return 0; |
| 9510 | } |
| 9511 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9512 | Py_ssize_t |
| 9513 | PyUnicode_Tailmatch(PyObject *str, |
| 9514 | PyObject *substr, |
| 9515 | Py_ssize_t start, |
| 9516 | Py_ssize_t end, |
| 9517 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9518 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9519 | if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9520 | return -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9521 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 9522 | return tailmatch(str, substr, start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9523 | } |
| 9524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9525 | /* Apply fixfct filter to the Unicode object self and return a |
| 9526 | reference to the modified object */ |
| 9527 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9528 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9529 | fixup(PyObject *self, |
| 9530 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9532 | PyObject *u; |
| 9533 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9534 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9535 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9536 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9537 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9538 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9539 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9540 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9541 | /* fix functions return the new maximum character in a string, |
| 9542 | if the kind of the resulting unicode object does not change, |
| 9543 | everything is fine. Otherwise we need to change the string kind |
| 9544 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9545 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9546 | |
| 9547 | if (maxchar_new == 0) { |
| 9548 | /* no changes */; |
| 9549 | if (PyUnicode_CheckExact(self)) { |
| 9550 | Py_DECREF(u); |
| 9551 | Py_INCREF(self); |
| 9552 | return self; |
| 9553 | } |
| 9554 | else |
| 9555 | return u; |
| 9556 | } |
| 9557 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9558 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9559 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9560 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9561 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9562 | |
| 9563 | /* In case the maximum character changed, we need to |
| 9564 | convert the string to the new category. */ |
| 9565 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9566 | if (v == NULL) { |
| 9567 | Py_DECREF(u); |
| 9568 | return NULL; |
| 9569 | } |
| 9570 | if (maxchar_new > maxchar_old) { |
| 9571 | /* If the maxchar increased so that the kind changed, not all |
| 9572 | characters are representable anymore and we need to fix the |
| 9573 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9574 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9575 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9576 | maxchar_old = fixfct(v); |
| 9577 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9578 | } |
| 9579 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9580 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9581 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9582 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9583 | Py_DECREF(u); |
| 9584 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9585 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9586 | } |
| 9587 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9588 | static PyObject * |
| 9589 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9590 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9591 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9592 | char *resdata, *data = PyUnicode_DATA(self); |
| 9593 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9594 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9595 | res = PyUnicode_New(len, 127); |
| 9596 | if (res == NULL) |
| 9597 | return NULL; |
| 9598 | resdata = PyUnicode_DATA(res); |
| 9599 | if (lower) |
| 9600 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9601 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9602 | _Py_bytes_upper(resdata, data, len); |
| 9603 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9604 | } |
| 9605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9606 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9607 | 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] | 9608 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9609 | Py_ssize_t j; |
| 9610 | int final_sigma; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 9611 | Py_UCS4 c = 0; /* initialize to prevent gcc warning */ |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9612 | /* 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] | 9613 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9614 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9615 | |
| 9616 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9617 | */ |
| 9618 | for (j = i - 1; j >= 0; j--) { |
| 9619 | c = PyUnicode_READ(kind, data, j); |
| 9620 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9621 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9622 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9623 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9624 | if (final_sigma) { |
| 9625 | for (j = i + 1; j < length; j++) { |
| 9626 | c = PyUnicode_READ(kind, data, j); |
| 9627 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9628 | break; |
| 9629 | } |
| 9630 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9631 | } |
| 9632 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9633 | } |
| 9634 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9635 | static int |
| 9636 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9637 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9638 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9639 | /* Obscure special case. */ |
| 9640 | if (c == 0x3A3) { |
| 9641 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9642 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9643 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9644 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9645 | } |
| 9646 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9647 | static Py_ssize_t |
| 9648 | 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] | 9649 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9650 | Py_ssize_t i, k = 0; |
| 9651 | int n_res, j; |
| 9652 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9653 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9654 | c = PyUnicode_READ(kind, data, 0); |
| 9655 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9656 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9657 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9658 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9659 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9660 | for (i = 1; i < length; i++) { |
| 9661 | c = PyUnicode_READ(kind, data, i); |
| 9662 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9663 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9664 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9665 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9666 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9667 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9668 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9669 | } |
| 9670 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9671 | static Py_ssize_t |
| 9672 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9673 | Py_ssize_t i, k = 0; |
| 9674 | |
| 9675 | for (i = 0; i < length; i++) { |
| 9676 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9677 | int n_res, j; |
| 9678 | if (Py_UNICODE_ISUPPER(c)) { |
| 9679 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9680 | } |
| 9681 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9682 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9683 | } |
| 9684 | else { |
| 9685 | n_res = 1; |
| 9686 | mapped[0] = c; |
| 9687 | } |
| 9688 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9689 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9690 | res[k++] = mapped[j]; |
| 9691 | } |
| 9692 | } |
| 9693 | return k; |
| 9694 | } |
| 9695 | |
| 9696 | static Py_ssize_t |
| 9697 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9698 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9699 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9700 | Py_ssize_t i, k = 0; |
| 9701 | |
| 9702 | for (i = 0; i < length; i++) { |
| 9703 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9704 | int n_res, j; |
| 9705 | if (lower) |
| 9706 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9707 | else |
| 9708 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9709 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9710 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9711 | res[k++] = mapped[j]; |
| 9712 | } |
| 9713 | } |
| 9714 | return k; |
| 9715 | } |
| 9716 | |
| 9717 | static Py_ssize_t |
| 9718 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9719 | { |
| 9720 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9721 | } |
| 9722 | |
| 9723 | static Py_ssize_t |
| 9724 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9725 | { |
| 9726 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9727 | } |
| 9728 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9729 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9730 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9731 | { |
| 9732 | Py_ssize_t i, k = 0; |
| 9733 | |
| 9734 | for (i = 0; i < length; i++) { |
| 9735 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9736 | Py_UCS4 mapped[3]; |
| 9737 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9738 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9739 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9740 | res[k++] = mapped[j]; |
| 9741 | } |
| 9742 | } |
| 9743 | return k; |
| 9744 | } |
| 9745 | |
| 9746 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9747 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9748 | { |
| 9749 | Py_ssize_t i, k = 0; |
| 9750 | int previous_is_cased; |
| 9751 | |
| 9752 | previous_is_cased = 0; |
| 9753 | for (i = 0; i < length; i++) { |
| 9754 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9755 | Py_UCS4 mapped[3]; |
| 9756 | int n_res, j; |
| 9757 | |
| 9758 | if (previous_is_cased) |
| 9759 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9760 | else |
| 9761 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9762 | |
| 9763 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9764 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9765 | res[k++] = mapped[j]; |
| 9766 | } |
| 9767 | |
| 9768 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9769 | } |
| 9770 | return k; |
| 9771 | } |
| 9772 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9773 | static PyObject * |
| 9774 | case_operation(PyObject *self, |
| 9775 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9776 | { |
| 9777 | PyObject *res = NULL; |
| 9778 | Py_ssize_t length, newlength = 0; |
| 9779 | int kind, outkind; |
| 9780 | void *data, *outdata; |
| 9781 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9782 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9783 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9784 | |
| 9785 | kind = PyUnicode_KIND(self); |
| 9786 | data = PyUnicode_DATA(self); |
| 9787 | length = PyUnicode_GET_LENGTH(self); |
Antoine Pitrou | 4e33424 | 2014-10-15 23:14:53 +0200 | [diff] [blame] | 9788 | if ((size_t) length > PY_SSIZE_T_MAX / (3 * sizeof(Py_UCS4))) { |
Benjamin Peterson | e1bd38c | 2014-10-15 11:47:36 -0400 | [diff] [blame] | 9789 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 9790 | return NULL; |
| 9791 | } |
Benjamin Peterson | 1e211ff | 2014-10-15 12:17:21 -0400 | [diff] [blame] | 9792 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9793 | if (tmp == NULL) |
| 9794 | return PyErr_NoMemory(); |
| 9795 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9796 | res = PyUnicode_New(newlength, maxchar); |
| 9797 | if (res == NULL) |
| 9798 | goto leave; |
| 9799 | tmpend = tmp + newlength; |
| 9800 | outdata = PyUnicode_DATA(res); |
| 9801 | outkind = PyUnicode_KIND(res); |
| 9802 | switch (outkind) { |
| 9803 | case PyUnicode_1BYTE_KIND: |
| 9804 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9805 | break; |
| 9806 | case PyUnicode_2BYTE_KIND: |
| 9807 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9808 | break; |
| 9809 | case PyUnicode_4BYTE_KIND: |
| 9810 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9811 | break; |
| 9812 | default: |
| 9813 | assert(0); |
| 9814 | break; |
| 9815 | } |
| 9816 | leave: |
| 9817 | PyMem_FREE(tmp); |
| 9818 | return res; |
| 9819 | } |
| 9820 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9821 | PyObject * |
| 9822 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9823 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9824 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9825 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9826 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9827 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9828 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9829 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9830 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9831 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9832 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9833 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9834 | int use_memcpy; |
| 9835 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9836 | PyObject *last_obj; |
| 9837 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9838 | |
Benjamin Peterson | 9743b2c | 2014-02-15 13:02:52 -0500 | [diff] [blame] | 9839 | fseq = PySequence_Fast(seq, "can only join an iterable"); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9840 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9841 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9842 | } |
| 9843 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9844 | /* NOTE: the following code can't call back into Python code, |
| 9845 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9846 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9847 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9848 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9849 | /* If empty sequence, return u"". */ |
| 9850 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9851 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9852 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9853 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9854 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9855 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9856 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9857 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9858 | if (seqlen == 1) { |
| 9859 | if (PyUnicode_CheckExact(items[0])) { |
| 9860 | res = items[0]; |
| 9861 | Py_INCREF(res); |
| 9862 | Py_DECREF(fseq); |
| 9863 | return res; |
| 9864 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9865 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9866 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9867 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9868 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9869 | /* Set up sep and seplen */ |
| 9870 | if (separator == NULL) { |
| 9871 | /* fall back to a blank space separator */ |
| 9872 | sep = PyUnicode_FromOrdinal(' '); |
| 9873 | if (!sep) |
| 9874 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9875 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9876 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9877 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9878 | else { |
| 9879 | if (!PyUnicode_Check(separator)) { |
| 9880 | PyErr_Format(PyExc_TypeError, |
| 9881 | "separator: expected str instance," |
| 9882 | " %.80s found", |
| 9883 | Py_TYPE(separator)->tp_name); |
| 9884 | goto onError; |
| 9885 | } |
| 9886 | if (PyUnicode_READY(separator)) |
| 9887 | goto onError; |
| 9888 | sep = separator; |
| 9889 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9890 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9891 | /* inc refcount to keep this code path symmetric with the |
| 9892 | above case of a blank separator */ |
| 9893 | Py_INCREF(sep); |
| 9894 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9895 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9896 | } |
| 9897 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9898 | /* There are at least two things to join, or else we have a subclass |
| 9899 | * of str in the sequence. |
| 9900 | * Do a pre-pass to figure out the total amount of space we'll |
| 9901 | * need (sz), and see whether all argument are strings. |
| 9902 | */ |
| 9903 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9904 | #ifdef Py_DEBUG |
| 9905 | use_memcpy = 0; |
| 9906 | #else |
| 9907 | use_memcpy = 1; |
| 9908 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9909 | for (i = 0; i < seqlen; i++) { |
| 9910 | const Py_ssize_t old_sz = sz; |
| 9911 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9912 | if (!PyUnicode_Check(item)) { |
| 9913 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 9914 | "sequence item %zd: expected str instance," |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9915 | " %.80s found", |
| 9916 | i, Py_TYPE(item)->tp_name); |
| 9917 | goto onError; |
| 9918 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9919 | if (PyUnicode_READY(item) == -1) |
| 9920 | goto onError; |
| 9921 | sz += PyUnicode_GET_LENGTH(item); |
| 9922 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9923 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9924 | if (i != 0) |
| 9925 | sz += seplen; |
| 9926 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9927 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9928 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9929 | goto onError; |
| 9930 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9931 | if (use_memcpy && last_obj != NULL) { |
| 9932 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9933 | use_memcpy = 0; |
| 9934 | } |
| 9935 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9936 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9937 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9938 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9939 | if (res == NULL) |
| 9940 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9941 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9942 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9943 | #ifdef Py_DEBUG |
| 9944 | use_memcpy = 0; |
| 9945 | #else |
| 9946 | if (use_memcpy) { |
| 9947 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9948 | kind = PyUnicode_KIND(res); |
| 9949 | if (seplen != 0) |
| 9950 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9951 | } |
| 9952 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9953 | if (use_memcpy) { |
| 9954 | for (i = 0; i < seqlen; ++i) { |
| 9955 | Py_ssize_t itemlen; |
| 9956 | item = items[i]; |
| 9957 | |
| 9958 | /* Copy item, and maybe the separator. */ |
| 9959 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9960 | Py_MEMCPY(res_data, |
| 9961 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9962 | kind * seplen); |
| 9963 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9964 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9965 | |
| 9966 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9967 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9968 | Py_MEMCPY(res_data, |
| 9969 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9970 | kind * itemlen); |
| 9971 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9972 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9973 | } |
| 9974 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9975 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9976 | } |
| 9977 | else { |
| 9978 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9979 | Py_ssize_t itemlen; |
| 9980 | item = items[i]; |
| 9981 | |
| 9982 | /* Copy item, and maybe the separator. */ |
| 9983 | if (i && seplen != 0) { |
| 9984 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9985 | res_offset += seplen; |
| 9986 | } |
| 9987 | |
| 9988 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9989 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9990 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9991 | res_offset += itemlen; |
| 9992 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9993 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9994 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9995 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9996 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9997 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9998 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9999 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10000 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10001 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10002 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10003 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10004 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10005 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10006 | return NULL; |
| 10007 | } |
| 10008 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | #define FILL(kind, data, value, start, length) \ |
| 10010 | do { \ |
| 10011 | Py_ssize_t i_ = 0; \ |
| 10012 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 10013 | switch ((kind)) { \ |
| 10014 | case PyUnicode_1BYTE_KIND: { \ |
| 10015 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 10016 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | break; \ |
| 10018 | } \ |
| 10019 | case PyUnicode_2BYTE_KIND: { \ |
| 10020 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 10021 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10022 | break; \ |
| 10023 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10024 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10025 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 10026 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10027 | break; \ |
| 10028 | } \ |
Serhiy Storchaka | 133b11b | 2014-12-01 18:56:28 +0200 | [diff] [blame] | 10029 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10030 | } \ |
| 10031 | } while (0) |
| 10032 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10033 | void |
| 10034 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10035 | Py_UCS4 fill_char) |
| 10036 | { |
| 10037 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 10038 | const void *data = PyUnicode_DATA(unicode); |
| 10039 | assert(PyUnicode_IS_READY(unicode)); |
| 10040 | assert(unicode_modifiable(unicode)); |
| 10041 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 10042 | assert(start >= 0); |
| 10043 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 10044 | FILL(kind, data, fill_char, start, length); |
| 10045 | } |
| 10046 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10047 | Py_ssize_t |
| 10048 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10049 | Py_UCS4 fill_char) |
| 10050 | { |
| 10051 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10052 | |
| 10053 | if (!PyUnicode_Check(unicode)) { |
| 10054 | PyErr_BadInternalCall(); |
| 10055 | return -1; |
| 10056 | } |
| 10057 | if (PyUnicode_READY(unicode) == -1) |
| 10058 | return -1; |
| 10059 | if (unicode_check_modifiable(unicode)) |
| 10060 | return -1; |
| 10061 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10062 | if (start < 0) { |
| 10063 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10064 | return -1; |
| 10065 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10066 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 10067 | PyErr_SetString(PyExc_ValueError, |
| 10068 | "fill character is bigger than " |
| 10069 | "the string maximum character"); |
| 10070 | return -1; |
| 10071 | } |
| 10072 | |
| 10073 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 10074 | length = Py_MIN(maxlen, length); |
| 10075 | if (length <= 0) |
| 10076 | return 0; |
| 10077 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10078 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10079 | return length; |
| 10080 | } |
| 10081 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10082 | static PyObject * |
| 10083 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10084 | Py_ssize_t left, |
| 10085 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10086 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10087 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10088 | PyObject *u; |
| 10089 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10090 | int kind; |
| 10091 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10092 | |
| 10093 | if (left < 0) |
| 10094 | left = 0; |
| 10095 | if (right < 0) |
| 10096 | right = 0; |
| 10097 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10098 | if (left == 0 && right == 0) |
| 10099 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10100 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10101 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10102 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10103 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10104 | return NULL; |
| 10105 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10106 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10107 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10108 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10109 | if (!u) |
| 10110 | return NULL; |
| 10111 | |
| 10112 | kind = PyUnicode_KIND(u); |
| 10113 | data = PyUnicode_DATA(u); |
| 10114 | if (left) |
| 10115 | FILL(kind, data, fill, 0, left); |
| 10116 | if (right) |
| 10117 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10118 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10119 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10120 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10121 | } |
| 10122 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10123 | PyObject * |
| 10124 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10125 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10126 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10127 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 10128 | if (ensure_unicode(string) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10129 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10130 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10131 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10133 | if (PyUnicode_IS_ASCII(string)) |
| 10134 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10135 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10136 | PyUnicode_GET_LENGTH(string), keepends); |
| 10137 | else |
| 10138 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10139 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10140 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | break; |
| 10142 | case PyUnicode_2BYTE_KIND: |
| 10143 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10144 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10145 | PyUnicode_GET_LENGTH(string), keepends); |
| 10146 | break; |
| 10147 | case PyUnicode_4BYTE_KIND: |
| 10148 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10149 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | PyUnicode_GET_LENGTH(string), keepends); |
| 10151 | break; |
| 10152 | default: |
| 10153 | assert(0); |
| 10154 | list = 0; |
| 10155 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10156 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10157 | } |
| 10158 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10159 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10160 | split(PyObject *self, |
| 10161 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10162 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10163 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10164 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10165 | void *buf1, *buf2; |
| 10166 | Py_ssize_t len1, len2; |
| 10167 | PyObject* out; |
| 10168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10169 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10170 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | if (PyUnicode_READY(self) == -1) |
| 10173 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10174 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10175 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10176 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10177 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10178 | if (PyUnicode_IS_ASCII(self)) |
| 10179 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10180 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10181 | PyUnicode_GET_LENGTH(self), maxcount |
| 10182 | ); |
| 10183 | else |
| 10184 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10185 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10186 | PyUnicode_GET_LENGTH(self), maxcount |
| 10187 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10188 | case PyUnicode_2BYTE_KIND: |
| 10189 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10190 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | PyUnicode_GET_LENGTH(self), maxcount |
| 10192 | ); |
| 10193 | case PyUnicode_4BYTE_KIND: |
| 10194 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10195 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10196 | PyUnicode_GET_LENGTH(self), maxcount |
| 10197 | ); |
| 10198 | default: |
| 10199 | assert(0); |
| 10200 | return NULL; |
| 10201 | } |
| 10202 | |
| 10203 | if (PyUnicode_READY(substring) == -1) |
| 10204 | return NULL; |
| 10205 | |
| 10206 | kind1 = PyUnicode_KIND(self); |
| 10207 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | len1 = PyUnicode_GET_LENGTH(self); |
| 10209 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10210 | if (kind1 < kind2 || len1 < len2) { |
| 10211 | out = PyList_New(1); |
| 10212 | if (out == NULL) |
| 10213 | return NULL; |
| 10214 | Py_INCREF(self); |
| 10215 | PyList_SET_ITEM(out, 0, self); |
| 10216 | return out; |
| 10217 | } |
| 10218 | buf1 = PyUnicode_DATA(self); |
| 10219 | buf2 = PyUnicode_DATA(substring); |
| 10220 | if (kind2 != kind1) { |
| 10221 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10222 | if (!buf2) |
| 10223 | return NULL; |
| 10224 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10226 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10228 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10229 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10230 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10231 | else |
| 10232 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10233 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10234 | break; |
| 10235 | case PyUnicode_2BYTE_KIND: |
| 10236 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10237 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 | break; |
| 10239 | case PyUnicode_4BYTE_KIND: |
| 10240 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10241 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10242 | break; |
| 10243 | default: |
| 10244 | out = NULL; |
| 10245 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10246 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10247 | PyMem_Free(buf2); |
| 10248 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10249 | } |
| 10250 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10251 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10252 | rsplit(PyObject *self, |
| 10253 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10254 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10255 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10256 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10257 | void *buf1, *buf2; |
| 10258 | Py_ssize_t len1, len2; |
| 10259 | PyObject* out; |
| 10260 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10261 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10262 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10264 | if (PyUnicode_READY(self) == -1) |
| 10265 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10266 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10267 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10268 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10269 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10270 | if (PyUnicode_IS_ASCII(self)) |
| 10271 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10272 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10273 | PyUnicode_GET_LENGTH(self), maxcount |
| 10274 | ); |
| 10275 | else |
| 10276 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10277 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10278 | PyUnicode_GET_LENGTH(self), maxcount |
| 10279 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10280 | case PyUnicode_2BYTE_KIND: |
| 10281 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10282 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 | PyUnicode_GET_LENGTH(self), maxcount |
| 10284 | ); |
| 10285 | case PyUnicode_4BYTE_KIND: |
| 10286 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10287 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 | PyUnicode_GET_LENGTH(self), maxcount |
| 10289 | ); |
| 10290 | default: |
| 10291 | assert(0); |
| 10292 | return NULL; |
| 10293 | } |
| 10294 | |
| 10295 | if (PyUnicode_READY(substring) == -1) |
| 10296 | return NULL; |
| 10297 | |
| 10298 | kind1 = PyUnicode_KIND(self); |
| 10299 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10300 | len1 = PyUnicode_GET_LENGTH(self); |
| 10301 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10302 | if (kind1 < kind2 || len1 < len2) { |
| 10303 | out = PyList_New(1); |
| 10304 | if (out == NULL) |
| 10305 | return NULL; |
| 10306 | Py_INCREF(self); |
| 10307 | PyList_SET_ITEM(out, 0, self); |
| 10308 | return out; |
| 10309 | } |
| 10310 | buf1 = PyUnicode_DATA(self); |
| 10311 | buf2 = PyUnicode_DATA(substring); |
| 10312 | if (kind2 != kind1) { |
| 10313 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10314 | if (!buf2) |
| 10315 | return NULL; |
| 10316 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10317 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10318 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10319 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10320 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10321 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10322 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10323 | else |
| 10324 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10325 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10326 | break; |
| 10327 | case PyUnicode_2BYTE_KIND: |
| 10328 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10329 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10330 | break; |
| 10331 | case PyUnicode_4BYTE_KIND: |
| 10332 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10333 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10334 | break; |
| 10335 | default: |
| 10336 | out = NULL; |
| 10337 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10338 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | PyMem_Free(buf2); |
| 10340 | return out; |
| 10341 | } |
| 10342 | |
| 10343 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10344 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10345 | 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] | 10346 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10347 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10348 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10349 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10350 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10351 | else |
| 10352 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10353 | case PyUnicode_2BYTE_KIND: |
| 10354 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10355 | case PyUnicode_4BYTE_KIND: |
| 10356 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10357 | } |
| 10358 | assert(0); |
| 10359 | return -1; |
| 10360 | } |
| 10361 | |
| 10362 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10363 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10364 | 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] | 10365 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10366 | switch (kind) { |
| 10367 | case PyUnicode_1BYTE_KIND: |
| 10368 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10369 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10370 | else |
| 10371 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10372 | case PyUnicode_2BYTE_KIND: |
| 10373 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10374 | case PyUnicode_4BYTE_KIND: |
| 10375 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10376 | } |
| 10377 | assert(0); |
| 10378 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10379 | } |
| 10380 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10381 | static void |
| 10382 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10383 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10384 | { |
| 10385 | int kind = PyUnicode_KIND(u); |
| 10386 | void *data = PyUnicode_DATA(u); |
| 10387 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10388 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10389 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10390 | (Py_UCS1 *)data + len, |
| 10391 | u1, u2, maxcount); |
| 10392 | } |
| 10393 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10394 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10395 | (Py_UCS2 *)data + len, |
| 10396 | u1, u2, maxcount); |
| 10397 | } |
| 10398 | else { |
| 10399 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10400 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10401 | (Py_UCS4 *)data + len, |
| 10402 | u1, u2, maxcount); |
| 10403 | } |
| 10404 | } |
| 10405 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10406 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10407 | replace(PyObject *self, PyObject *str1, |
| 10408 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10409 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10410 | PyObject *u; |
| 10411 | char *sbuf = PyUnicode_DATA(self); |
| 10412 | char *buf1 = PyUnicode_DATA(str1); |
| 10413 | char *buf2 = PyUnicode_DATA(str2); |
| 10414 | int srelease = 0, release1 = 0, release2 = 0; |
| 10415 | int skind = PyUnicode_KIND(self); |
| 10416 | int kind1 = PyUnicode_KIND(str1); |
| 10417 | int kind2 = PyUnicode_KIND(str2); |
| 10418 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10419 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10420 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10421 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10422 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10423 | |
| 10424 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10425 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10426 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10427 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10428 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10429 | if (str1 == str2) |
| 10430 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10432 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10433 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10434 | if (maxchar < maxchar_str1) |
| 10435 | /* substring too wide to be present */ |
| 10436 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10437 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10438 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10439 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10440 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10441 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10442 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10443 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10444 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10446 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10447 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10448 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10449 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10450 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10451 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10452 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10453 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10454 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10455 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10456 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10458 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10459 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10460 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10461 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10462 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10463 | } |
| 10464 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10465 | int rkind = skind; |
| 10466 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10467 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10469 | if (kind1 < rkind) { |
| 10470 | /* widen substring */ |
| 10471 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10472 | if (!buf1) goto error; |
| 10473 | release1 = 1; |
| 10474 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10475 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10476 | if (i < 0) |
| 10477 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10478 | if (rkind > kind2) { |
| 10479 | /* widen replacement */ |
| 10480 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10481 | if (!buf2) goto error; |
| 10482 | release2 = 1; |
| 10483 | } |
| 10484 | else if (rkind < kind2) { |
| 10485 | /* widen self and buf1 */ |
| 10486 | rkind = kind2; |
| 10487 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10488 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10490 | if (!sbuf) goto error; |
| 10491 | srelease = 1; |
| 10492 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10493 | if (!buf1) goto error; |
| 10494 | release1 = 1; |
| 10495 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10496 | u = PyUnicode_New(slen, maxchar); |
| 10497 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10498 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10499 | assert(PyUnicode_KIND(u) == rkind); |
| 10500 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10501 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10502 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10503 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10504 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10505 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10506 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10507 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10508 | |
| 10509 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10510 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10511 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10512 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10513 | if (i == -1) |
| 10514 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10515 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10516 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10517 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10518 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10519 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10520 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10521 | } |
| 10522 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10523 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10524 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10525 | int rkind = skind; |
| 10526 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10527 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10528 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10529 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10530 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10531 | if (!buf1) goto error; |
| 10532 | release1 = 1; |
| 10533 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10534 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10535 | if (n == 0) |
| 10536 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10537 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10538 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10539 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10540 | if (!buf2) goto error; |
| 10541 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10542 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10543 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10544 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10545 | rkind = kind2; |
| 10546 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10547 | if (!sbuf) goto error; |
| 10548 | srelease = 1; |
| 10549 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10550 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10551 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10552 | if (!buf1) goto error; |
| 10553 | release1 = 1; |
| 10554 | } |
| 10555 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10556 | PyUnicode_GET_LENGTH(str1))); */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10557 | if (len1 < len2 && len2 - len1 > (PY_SSIZE_T_MAX - slen) / n) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10558 | PyErr_SetString(PyExc_OverflowError, |
| 10559 | "replace string is too long"); |
| 10560 | goto error; |
| 10561 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10562 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10563 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10564 | _Py_INCREF_UNICODE_EMPTY(); |
| 10565 | if (!unicode_empty) |
| 10566 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10567 | u = unicode_empty; |
| 10568 | goto done; |
| 10569 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10570 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10571 | PyErr_SetString(PyExc_OverflowError, |
| 10572 | "replace string is too long"); |
| 10573 | goto error; |
| 10574 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10575 | u = PyUnicode_New(new_size, maxchar); |
| 10576 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10577 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10578 | assert(PyUnicode_KIND(u) == rkind); |
| 10579 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10580 | ires = i = 0; |
| 10581 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10582 | while (n-- > 0) { |
| 10583 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10584 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10585 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10586 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10587 | if (j == -1) |
| 10588 | break; |
| 10589 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10590 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10591 | memcpy(res + rkind * ires, |
| 10592 | sbuf + rkind * i, |
| 10593 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10594 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10595 | } |
| 10596 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10597 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10598 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10599 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10600 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10601 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10602 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10603 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10604 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10605 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10606 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10607 | memcpy(res + rkind * ires, |
| 10608 | sbuf + rkind * i, |
| 10609 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10610 | } |
| 10611 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10612 | /* interleave */ |
| 10613 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10614 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10615 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10616 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10617 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10618 | if (--n <= 0) |
| 10619 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10620 | memcpy(res + rkind * ires, |
| 10621 | sbuf + rkind * i, |
| 10622 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10623 | ires++; |
| 10624 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10625 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10626 | memcpy(res + rkind * ires, |
| 10627 | sbuf + rkind * i, |
| 10628 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10629 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10630 | } |
| 10631 | |
| 10632 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10633 | unicode_adjust_maxchar(&u); |
| 10634 | if (u == NULL) |
| 10635 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10636 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10637 | |
| 10638 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10639 | if (srelease) |
| 10640 | PyMem_FREE(sbuf); |
| 10641 | if (release1) |
| 10642 | PyMem_FREE(buf1); |
| 10643 | if (release2) |
| 10644 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10645 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10646 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10647 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10648 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10649 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10650 | if (srelease) |
| 10651 | PyMem_FREE(sbuf); |
| 10652 | if (release1) |
| 10653 | PyMem_FREE(buf1); |
| 10654 | if (release2) |
| 10655 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10656 | return unicode_result_unchanged(self); |
| 10657 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10658 | error: |
| 10659 | if (srelease && sbuf) |
| 10660 | PyMem_FREE(sbuf); |
| 10661 | if (release1 && buf1) |
| 10662 | PyMem_FREE(buf1); |
| 10663 | if (release2 && buf2) |
| 10664 | PyMem_FREE(buf2); |
| 10665 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10666 | } |
| 10667 | |
| 10668 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10669 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10670 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10671 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10672 | \n\ |
| 10673 | 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] | 10674 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10675 | |
| 10676 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10677 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10678 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10679 | if (PyUnicode_READY(self) == -1) |
| 10680 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10681 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10682 | } |
| 10683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10684 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10685 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10686 | \n\ |
| 10687 | 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] | 10688 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10689 | |
| 10690 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10691 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10692 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10693 | if (PyUnicode_READY(self) == -1) |
| 10694 | return NULL; |
| 10695 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10696 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10697 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10698 | } |
| 10699 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10700 | PyDoc_STRVAR(casefold__doc__, |
| 10701 | "S.casefold() -> str\n\ |
| 10702 | \n\ |
| 10703 | Return a version of S suitable for caseless comparisons."); |
| 10704 | |
| 10705 | static PyObject * |
| 10706 | unicode_casefold(PyObject *self) |
| 10707 | { |
| 10708 | if (PyUnicode_READY(self) == -1) |
| 10709 | return NULL; |
| 10710 | if (PyUnicode_IS_ASCII(self)) |
| 10711 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10712 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10713 | } |
| 10714 | |
| 10715 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 10716 | /* Argument converter. Accepts a single Unicode character. */ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10717 | |
| 10718 | static int |
| 10719 | convert_uc(PyObject *obj, void *addr) |
| 10720 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10721 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10722 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 10723 | if (!PyUnicode_Check(obj)) { |
| 10724 | PyErr_Format(PyExc_TypeError, |
| 10725 | "The fill character must be a unicode character, " |
| 10726 | "not %.100s", Py_TYPE(obj)->tp_name); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10727 | return 0; |
| 10728 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 10729 | if (PyUnicode_READY(obj) < 0) |
| 10730 | return 0; |
| 10731 | if (PyUnicode_GET_LENGTH(obj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10732 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10733 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10734 | return 0; |
| 10735 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 10736 | *fillcharloc = PyUnicode_READ_CHAR(obj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10737 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10738 | } |
| 10739 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10740 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10741 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10742 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10743 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10744 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10745 | |
| 10746 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10747 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10748 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10749 | Py_ssize_t marg, left; |
| 10750 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10751 | Py_UCS4 fillchar = ' '; |
| 10752 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10753 | 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] | 10754 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10755 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10756 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10757 | return NULL; |
| 10758 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10759 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10760 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10761 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10762 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10763 | left = marg / 2 + (marg & width & 1); |
| 10764 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10765 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10766 | } |
| 10767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10768 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10769 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10770 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10771 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10772 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10773 | #define COMPARE(TYPE1, TYPE2) \ |
| 10774 | do { \ |
| 10775 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10776 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10777 | TYPE1* end = p1 + len; \ |
| 10778 | Py_UCS4 c1, c2; \ |
| 10779 | for (; p1 != end; p1++, p2++) { \ |
| 10780 | c1 = *p1; \ |
| 10781 | c2 = *p2; \ |
| 10782 | if (c1 != c2) \ |
| 10783 | return (c1 < c2) ? -1 : 1; \ |
| 10784 | } \ |
| 10785 | } \ |
| 10786 | while (0) |
| 10787 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10788 | int kind1, kind2; |
| 10789 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10790 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10792 | kind1 = PyUnicode_KIND(str1); |
| 10793 | kind2 = PyUnicode_KIND(str2); |
| 10794 | data1 = PyUnicode_DATA(str1); |
| 10795 | data2 = PyUnicode_DATA(str2); |
| 10796 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10797 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10798 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10799 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10800 | switch(kind1) { |
| 10801 | case PyUnicode_1BYTE_KIND: |
| 10802 | { |
| 10803 | switch(kind2) { |
| 10804 | case PyUnicode_1BYTE_KIND: |
| 10805 | { |
| 10806 | int cmp = memcmp(data1, data2, len); |
| 10807 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10808 | if (cmp < 0) |
| 10809 | return -1; |
| 10810 | if (cmp > 0) |
| 10811 | return 1; |
| 10812 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10813 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10814 | case PyUnicode_2BYTE_KIND: |
| 10815 | COMPARE(Py_UCS1, Py_UCS2); |
| 10816 | break; |
| 10817 | case PyUnicode_4BYTE_KIND: |
| 10818 | COMPARE(Py_UCS1, Py_UCS4); |
| 10819 | break; |
| 10820 | default: |
| 10821 | assert(0); |
| 10822 | } |
| 10823 | break; |
| 10824 | } |
| 10825 | case PyUnicode_2BYTE_KIND: |
| 10826 | { |
| 10827 | switch(kind2) { |
| 10828 | case PyUnicode_1BYTE_KIND: |
| 10829 | COMPARE(Py_UCS2, Py_UCS1); |
| 10830 | break; |
| 10831 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10832 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10833 | COMPARE(Py_UCS2, Py_UCS2); |
| 10834 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10835 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10836 | case PyUnicode_4BYTE_KIND: |
| 10837 | COMPARE(Py_UCS2, Py_UCS4); |
| 10838 | break; |
| 10839 | default: |
| 10840 | assert(0); |
| 10841 | } |
| 10842 | break; |
| 10843 | } |
| 10844 | case PyUnicode_4BYTE_KIND: |
| 10845 | { |
| 10846 | switch(kind2) { |
| 10847 | case PyUnicode_1BYTE_KIND: |
| 10848 | COMPARE(Py_UCS4, Py_UCS1); |
| 10849 | break; |
| 10850 | case PyUnicode_2BYTE_KIND: |
| 10851 | COMPARE(Py_UCS4, Py_UCS2); |
| 10852 | break; |
| 10853 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10854 | { |
| 10855 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10856 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10857 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10858 | if (cmp < 0) |
| 10859 | return -1; |
| 10860 | if (cmp > 0) |
| 10861 | return 1; |
| 10862 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10863 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10864 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10865 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10866 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10867 | default: |
| 10868 | assert(0); |
| 10869 | } |
| 10870 | break; |
| 10871 | } |
| 10872 | default: |
| 10873 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10874 | } |
| 10875 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10876 | if (len1 == len2) |
| 10877 | return 0; |
| 10878 | if (len1 < len2) |
| 10879 | return -1; |
| 10880 | else |
| 10881 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10882 | |
| 10883 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10884 | } |
| 10885 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10886 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10887 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10888 | { |
| 10889 | int kind; |
| 10890 | void *data1, *data2; |
| 10891 | Py_ssize_t len; |
| 10892 | int cmp; |
| 10893 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10894 | len = PyUnicode_GET_LENGTH(str1); |
| 10895 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10896 | return 0; |
| 10897 | kind = PyUnicode_KIND(str1); |
| 10898 | if (PyUnicode_KIND(str2) != kind) |
| 10899 | return 0; |
| 10900 | data1 = PyUnicode_DATA(str1); |
| 10901 | data2 = PyUnicode_DATA(str2); |
| 10902 | |
| 10903 | cmp = memcmp(data1, data2, len * kind); |
| 10904 | return (cmp == 0); |
| 10905 | } |
| 10906 | |
| 10907 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10908 | int |
| 10909 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10910 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10911 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10912 | if (PyUnicode_READY(left) == -1 || |
| 10913 | PyUnicode_READY(right) == -1) |
| 10914 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10915 | |
| 10916 | /* a string is equal to itself */ |
| 10917 | if (left == right) |
| 10918 | return 0; |
| 10919 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10920 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10921 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10922 | PyErr_Format(PyExc_TypeError, |
| 10923 | "Can't compare %.100s and %.100s", |
| 10924 | left->ob_type->tp_name, |
| 10925 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10926 | return -1; |
| 10927 | } |
| 10928 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10929 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10930 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10931 | { |
| 10932 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10933 | if (right_str == NULL) |
| 10934 | return -1; |
| 10935 | return PyUnicode_Compare(left, right_str); |
| 10936 | } |
| 10937 | |
| 10938 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10939 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10940 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10941 | Py_ssize_t i; |
| 10942 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10943 | Py_UCS4 chr; |
| 10944 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10945 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10946 | if (PyUnicode_READY(uni) == -1) |
| 10947 | return -1; |
| 10948 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10949 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10950 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10951 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10952 | size_t len, len2 = strlen(str); |
| 10953 | int cmp; |
| 10954 | |
| 10955 | len = Py_MIN(len1, len2); |
| 10956 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 10957 | if (cmp != 0) { |
| 10958 | if (cmp < 0) |
| 10959 | return -1; |
| 10960 | else |
| 10961 | return 1; |
| 10962 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10963 | if (len1 > len2) |
| 10964 | return 1; /* uni is longer */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10965 | if (len1 < len2) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10966 | return -1; /* str is longer */ |
| 10967 | return 0; |
| 10968 | } |
| 10969 | else { |
| 10970 | void *data = PyUnicode_DATA(uni); |
| 10971 | /* Compare Unicode string and source character set string */ |
| 10972 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 10973 | if (chr != (unsigned char)str[i]) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10974 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10975 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10976 | to C strings identical up to that point. */ |
| 10977 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 10978 | return 1; /* uni is longer */ |
| 10979 | if (str[i]) |
| 10980 | return -1; /* str is longer */ |
| 10981 | return 0; |
| 10982 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10983 | } |
| 10984 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10985 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10986 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10987 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10988 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10989 | PyObject * |
| 10990 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10991 | { |
| 10992 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10993 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10994 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10995 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10996 | Py_RETURN_NOTIMPLEMENTED; |
| 10997 | |
| 10998 | if (PyUnicode_READY(left) == -1 || |
| 10999 | PyUnicode_READY(right) == -1) |
| 11000 | return NULL; |
| 11001 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 11002 | if (left == right) { |
| 11003 | switch (op) { |
| 11004 | case Py_EQ: |
| 11005 | case Py_LE: |
| 11006 | case Py_GE: |
| 11007 | /* a string is equal to itself */ |
| 11008 | v = Py_True; |
| 11009 | break; |
| 11010 | case Py_NE: |
| 11011 | case Py_LT: |
| 11012 | case Py_GT: |
| 11013 | v = Py_False; |
| 11014 | break; |
| 11015 | default: |
| 11016 | PyErr_BadArgument(); |
| 11017 | return NULL; |
| 11018 | } |
| 11019 | } |
| 11020 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11021 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 11022 | result ^= (op == Py_NE); |
| 11023 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11024 | } |
| 11025 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 11026 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11027 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11028 | /* Convert the return value to a Boolean */ |
| 11029 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11030 | case Py_LE: |
| 11031 | v = TEST_COND(result <= 0); |
| 11032 | break; |
| 11033 | case Py_GE: |
| 11034 | v = TEST_COND(result >= 0); |
| 11035 | break; |
| 11036 | case Py_LT: |
| 11037 | v = TEST_COND(result == -1); |
| 11038 | break; |
| 11039 | case Py_GT: |
| 11040 | v = TEST_COND(result == 1); |
| 11041 | break; |
| 11042 | default: |
| 11043 | PyErr_BadArgument(); |
| 11044 | return NULL; |
| 11045 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11046 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11047 | Py_INCREF(v); |
| 11048 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11049 | } |
| 11050 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11051 | int |
Raymond Hettinger | ac2ef65 | 2015-07-04 16:04:44 -0700 | [diff] [blame] | 11052 | _PyUnicode_EQ(PyObject *aa, PyObject *bb) |
| 11053 | { |
| 11054 | return unicode_eq(aa, bb); |
| 11055 | } |
| 11056 | |
| 11057 | int |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11058 | PyUnicode_Contains(PyObject *str, PyObject *substr) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11059 | { |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11060 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11061 | void *buf1, *buf2; |
| 11062 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11063 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11064 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11065 | if (!PyUnicode_Check(substr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11066 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11067 | "'in <string>' requires string as left operand, not %.100s", |
| 11068 | Py_TYPE(substr)->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11069 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11070 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11071 | if (PyUnicode_READY(substr) == -1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11072 | return -1; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11073 | if (ensure_unicode(str) < 0) |
| 11074 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11075 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11076 | kind1 = PyUnicode_KIND(str); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11077 | kind2 = PyUnicode_KIND(substr); |
| 11078 | if (kind1 < kind2) |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11079 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11080 | len1 = PyUnicode_GET_LENGTH(str); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11081 | len2 = PyUnicode_GET_LENGTH(substr); |
| 11082 | if (len1 < len2) |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11083 | return 0; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11084 | buf1 = PyUnicode_DATA(str); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11085 | buf2 = PyUnicode_DATA(substr); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11086 | if (len2 == 1) { |
| 11087 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 11088 | result = findchar((const char *)buf1, kind1, len1, ch, 1) != -1; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11089 | return result; |
| 11090 | } |
| 11091 | if (kind2 != kind1) { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11092 | buf2 = _PyUnicode_AsKind(substr, kind1); |
| 11093 | if (!buf2) |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11094 | return -1; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11095 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11096 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11097 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11098 | case PyUnicode_1BYTE_KIND: |
| 11099 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11100 | break; |
| 11101 | case PyUnicode_2BYTE_KIND: |
| 11102 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11103 | break; |
| 11104 | case PyUnicode_4BYTE_KIND: |
| 11105 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11106 | break; |
| 11107 | default: |
| 11108 | result = -1; |
| 11109 | assert(0); |
| 11110 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11111 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11112 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11113 | PyMem_Free(buf2); |
| 11114 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11115 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11116 | } |
| 11117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11118 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 11119 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11120 | PyObject * |
| 11121 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11122 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11123 | PyObject *result; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11124 | Py_UCS4 maxchar, maxchar2; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11125 | Py_ssize_t left_len, right_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11126 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11127 | if (ensure_unicode(left) < 0 || ensure_unicode(right) < 0) |
| 11128 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11129 | |
| 11130 | /* Shortcuts */ |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11131 | if (left == unicode_empty) |
| 11132 | return PyUnicode_FromObject(right); |
| 11133 | if (right == unicode_empty) |
| 11134 | return PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11135 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11136 | left_len = PyUnicode_GET_LENGTH(left); |
| 11137 | right_len = PyUnicode_GET_LENGTH(right); |
| 11138 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11139 | PyErr_SetString(PyExc_OverflowError, |
| 11140 | "strings are too large to concat"); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11141 | return NULL; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11142 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11143 | new_len = left_len + right_len; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11144 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11145 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11146 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11147 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11148 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11149 | /* Concat the two Unicode strings */ |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11150 | result = PyUnicode_New(new_len, maxchar); |
| 11151 | if (result == NULL) |
| 11152 | return NULL; |
| 11153 | _PyUnicode_FastCopyCharacters(result, 0, left, 0, left_len); |
| 11154 | _PyUnicode_FastCopyCharacters(result, left_len, right, 0, right_len); |
| 11155 | assert(_PyUnicode_CheckConsistency(result, 1)); |
| 11156 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11157 | } |
| 11158 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11159 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11160 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11161 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11162 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11163 | Py_UCS4 maxchar, maxchar2; |
| 11164 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11165 | |
| 11166 | if (p_left == NULL) { |
| 11167 | if (!PyErr_Occurred()) |
| 11168 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11169 | return; |
| 11170 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11171 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11172 | if (right == NULL || left == NULL |
| 11173 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11174 | if (!PyErr_Occurred()) |
| 11175 | PyErr_BadInternalCall(); |
| 11176 | goto error; |
| 11177 | } |
| 11178 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11179 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11180 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11181 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11182 | goto error; |
| 11183 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11184 | /* Shortcuts */ |
| 11185 | if (left == unicode_empty) { |
| 11186 | Py_DECREF(left); |
| 11187 | Py_INCREF(right); |
| 11188 | *p_left = right; |
| 11189 | return; |
| 11190 | } |
| 11191 | if (right == unicode_empty) |
| 11192 | return; |
| 11193 | |
| 11194 | left_len = PyUnicode_GET_LENGTH(left); |
| 11195 | right_len = PyUnicode_GET_LENGTH(right); |
| 11196 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11197 | PyErr_SetString(PyExc_OverflowError, |
| 11198 | "strings are too large to concat"); |
| 11199 | goto error; |
| 11200 | } |
| 11201 | new_len = left_len + right_len; |
| 11202 | |
| 11203 | if (unicode_modifiable(left) |
| 11204 | && PyUnicode_CheckExact(right) |
| 11205 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11206 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11207 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11208 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11209 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11210 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11211 | { |
| 11212 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11213 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11214 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11215 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11216 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11217 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11218 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11219 | else { |
| 11220 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11221 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11222 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11223 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11224 | /* Concat the two Unicode strings */ |
| 11225 | res = PyUnicode_New(new_len, maxchar); |
| 11226 | if (res == NULL) |
| 11227 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11228 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11229 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11230 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11231 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11232 | } |
| 11233 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11234 | return; |
| 11235 | |
| 11236 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11237 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11238 | } |
| 11239 | |
| 11240 | void |
| 11241 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11242 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11243 | PyUnicode_Append(pleft, right); |
| 11244 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11245 | } |
| 11246 | |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 11247 | /* |
| 11248 | Wraps stringlib_parse_args_finds() and additionally ensures that the |
| 11249 | first argument is a unicode object. |
| 11250 | */ |
| 11251 | |
| 11252 | Py_LOCAL_INLINE(int) |
| 11253 | parse_args_finds_unicode(const char * function_name, PyObject *args, |
| 11254 | PyObject **substring, |
| 11255 | Py_ssize_t *start, Py_ssize_t *end) |
| 11256 | { |
| 11257 | if(stringlib_parse_args_finds(function_name, args, substring, |
| 11258 | start, end)) { |
| 11259 | if (ensure_unicode(*substring) < 0) |
| 11260 | return 0; |
| 11261 | return 1; |
| 11262 | } |
| 11263 | return 0; |
| 11264 | } |
| 11265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11266 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11267 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11268 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11269 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11270 | 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] | 11271 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11272 | |
| 11273 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11274 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11276 | PyObject *substring = NULL; /* initialize to fix a compiler warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11277 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11278 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | PyObject *result; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11280 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11281 | void *buf1, *buf2; |
| 11282 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11283 | |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 11284 | if (!parse_args_finds_unicode("count", args, &substring, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11285 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11286 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11287 | kind1 = PyUnicode_KIND(self); |
| 11288 | kind2 = PyUnicode_KIND(substring); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11289 | if (kind1 < kind2) |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11290 | return PyLong_FromLong(0); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11291 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11292 | len1 = PyUnicode_GET_LENGTH(self); |
| 11293 | len2 = PyUnicode_GET_LENGTH(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11294 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11295 | if (end - start < len2) |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11296 | return PyLong_FromLong(0); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11297 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11298 | buf1 = PyUnicode_DATA(self); |
| 11299 | buf2 = PyUnicode_DATA(substring); |
| 11300 | if (kind2 != kind1) { |
| 11301 | buf2 = _PyUnicode_AsKind(substring, kind1); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11302 | if (!buf2) |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11303 | return NULL; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11304 | } |
| 11305 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11306 | case PyUnicode_1BYTE_KIND: |
| 11307 | iresult = ucs1lib_count( |
| 11308 | ((Py_UCS1*)buf1) + start, end - start, |
| 11309 | buf2, len2, PY_SSIZE_T_MAX |
| 11310 | ); |
| 11311 | break; |
| 11312 | case PyUnicode_2BYTE_KIND: |
| 11313 | iresult = ucs2lib_count( |
| 11314 | ((Py_UCS2*)buf1) + start, end - start, |
| 11315 | buf2, len2, PY_SSIZE_T_MAX |
| 11316 | ); |
| 11317 | break; |
| 11318 | case PyUnicode_4BYTE_KIND: |
| 11319 | iresult = ucs4lib_count( |
| 11320 | ((Py_UCS4*)buf1) + start, end - start, |
| 11321 | buf2, len2, PY_SSIZE_T_MAX |
| 11322 | ); |
| 11323 | break; |
| 11324 | default: |
| 11325 | assert(0); iresult = 0; |
| 11326 | } |
| 11327 | |
| 11328 | result = PyLong_FromSsize_t(iresult); |
| 11329 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11330 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11331 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11332 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11333 | return result; |
| 11334 | } |
| 11335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11336 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11337 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11338 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11339 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11340 | 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] | 11341 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11342 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11343 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11344 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 | |
| 11346 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11347 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11348 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11349 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | char *encoding = NULL; |
| 11351 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11352 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11353 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11354 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11355 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11356 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11357 | } |
| 11358 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11359 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11360 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11361 | \n\ |
| 11362 | 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] | 11363 | 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] | 11364 | |
| 11365 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11366 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11367 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11368 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11369 | Py_UCS4 ch; |
| 11370 | PyObject *u; |
| 11371 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11372 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11374 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11375 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11376 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11377 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11378 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11379 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11380 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11381 | if (PyUnicode_READY(self) == -1) |
| 11382 | return NULL; |
| 11383 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11384 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11385 | src_len = PyUnicode_GET_LENGTH(self); |
| 11386 | i = j = line_pos = 0; |
| 11387 | kind = PyUnicode_KIND(self); |
| 11388 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11389 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11390 | for (; i < src_len; i++) { |
| 11391 | ch = PyUnicode_READ(kind, src_data, i); |
| 11392 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11393 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11394 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11395 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11396 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11397 | goto overflow; |
| 11398 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11399 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11400 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11401 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11402 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11403 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11404 | goto overflow; |
| 11405 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11407 | if (ch == '\n' || ch == '\r') |
| 11408 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11410 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11411 | if (!found) |
| 11412 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11413 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11414 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11415 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11416 | if (!u) |
| 11417 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11418 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11419 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11420 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11422 | for (; i < src_len; i++) { |
| 11423 | ch = PyUnicode_READ(kind, src_data, i); |
| 11424 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11425 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11426 | incr = tabsize - (line_pos % tabsize); |
| 11427 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11428 | FILL(kind, dest_data, ' ', j, incr); |
| 11429 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11430 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11431 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11432 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11433 | line_pos++; |
| 11434 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11435 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11436 | if (ch == '\n' || ch == '\r') |
| 11437 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11438 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11439 | } |
| 11440 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11441 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11442 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11443 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11444 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11445 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11446 | } |
| 11447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11448 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11449 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11450 | \n\ |
| 11451 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11452 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11453 | arguments start and end are interpreted as in slice notation.\n\ |
| 11454 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11455 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | |
| 11457 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11458 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11460 | /* initialize variables to prevent gcc warning */ |
| 11461 | PyObject *substring = NULL; |
| 11462 | Py_ssize_t start = 0; |
| 11463 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11464 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11465 | |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 11466 | if (!parse_args_finds_unicode("find", args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11468 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11469 | if (PyUnicode_READY(self) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11470 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11472 | result = any_find_slice(self, substring, start, end, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11474 | if (result == -2) |
| 11475 | return NULL; |
| 11476 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11477 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11478 | } |
| 11479 | |
| 11480 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11481 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11482 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11483 | void *data; |
| 11484 | enum PyUnicode_Kind kind; |
| 11485 | Py_UCS4 ch; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11486 | |
| 11487 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11488 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11489 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11490 | } |
| 11491 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11492 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11493 | return NULL; |
| 11494 | } |
| 11495 | kind = PyUnicode_KIND(self); |
| 11496 | data = PyUnicode_DATA(self); |
| 11497 | ch = PyUnicode_READ(kind, data, index); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 11498 | return unicode_char(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11499 | } |
| 11500 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11501 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11502 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11503 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11504 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11505 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11506 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11507 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11508 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11509 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11510 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11511 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11512 | if (_PyUnicode_HASH(self) != -1) |
| 11513 | return _PyUnicode_HASH(self); |
| 11514 | if (PyUnicode_READY(self) == -1) |
| 11515 | return -1; |
| 11516 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11517 | /* |
| 11518 | We make the hash of the empty string be 0, rather than using |
| 11519 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11520 | */ |
| 11521 | if (len == 0) { |
| 11522 | _PyUnicode_HASH(self) = 0; |
| 11523 | return 0; |
| 11524 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11525 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11526 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11527 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11528 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 | } |
| 11530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11531 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11532 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11534 | 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] | 11535 | |
| 11536 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11537 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11538 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11539 | /* initialize variables to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11540 | Py_ssize_t result; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11541 | PyObject *substring = NULL; |
| 11542 | Py_ssize_t start = 0; |
| 11543 | Py_ssize_t end = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11544 | |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 11545 | if (!parse_args_finds_unicode("index", args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11546 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11547 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11548 | if (PyUnicode_READY(self) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11549 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11550 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 11551 | result = any_find_slice(self, substring, start, end, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11552 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11553 | if (result == -2) |
| 11554 | return NULL; |
| 11555 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11556 | if (result < 0) { |
| 11557 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11558 | return NULL; |
| 11559 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11560 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11561 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | } |
| 11563 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11564 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11565 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11566 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11567 | 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] | 11568 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11569 | |
| 11570 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11571 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11572 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11573 | Py_ssize_t i, length; |
| 11574 | int kind; |
| 11575 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11576 | int cased; |
| 11577 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11578 | if (PyUnicode_READY(self) == -1) |
| 11579 | return NULL; |
| 11580 | length = PyUnicode_GET_LENGTH(self); |
| 11581 | kind = PyUnicode_KIND(self); |
| 11582 | data = PyUnicode_DATA(self); |
| 11583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11584 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11585 | if (length == 1) |
| 11586 | return PyBool_FromLong( |
| 11587 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11588 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11589 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11590 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11591 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11592 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11593 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11594 | for (i = 0; i < length; i++) { |
| 11595 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11596 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11597 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11598 | return PyBool_FromLong(0); |
| 11599 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11600 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11601 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11602 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11603 | } |
| 11604 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11605 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11606 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11607 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11608 | 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] | 11609 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11610 | |
| 11611 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11612 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11613 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11614 | Py_ssize_t i, length; |
| 11615 | int kind; |
| 11616 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11617 | int cased; |
| 11618 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11619 | if (PyUnicode_READY(self) == -1) |
| 11620 | return NULL; |
| 11621 | length = PyUnicode_GET_LENGTH(self); |
| 11622 | kind = PyUnicode_KIND(self); |
| 11623 | data = PyUnicode_DATA(self); |
| 11624 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11625 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11626 | if (length == 1) |
| 11627 | return PyBool_FromLong( |
| 11628 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11629 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11630 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11631 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11632 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11633 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11634 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11635 | for (i = 0; i < length; i++) { |
| 11636 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11637 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11638 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11639 | return PyBool_FromLong(0); |
| 11640 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11641 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11642 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11643 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11644 | } |
| 11645 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11646 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11647 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11648 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11649 | Return True if S is a titlecased string and there is at least one\n\ |
| 11650 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11651 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11652 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11653 | |
| 11654 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11655 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11656 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11657 | Py_ssize_t i, length; |
| 11658 | int kind; |
| 11659 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11660 | int cased, previous_is_cased; |
| 11661 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11662 | if (PyUnicode_READY(self) == -1) |
| 11663 | return NULL; |
| 11664 | length = PyUnicode_GET_LENGTH(self); |
| 11665 | kind = PyUnicode_KIND(self); |
| 11666 | data = PyUnicode_DATA(self); |
| 11667 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11668 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11669 | if (length == 1) { |
| 11670 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11671 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11672 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11673 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11674 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11675 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11676 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11677 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11678 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11679 | cased = 0; |
| 11680 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11681 | for (i = 0; i < length; i++) { |
| 11682 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11683 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11684 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11685 | if (previous_is_cased) |
| 11686 | return PyBool_FromLong(0); |
| 11687 | previous_is_cased = 1; |
| 11688 | cased = 1; |
| 11689 | } |
| 11690 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11691 | if (!previous_is_cased) |
| 11692 | return PyBool_FromLong(0); |
| 11693 | previous_is_cased = 1; |
| 11694 | cased = 1; |
| 11695 | } |
| 11696 | else |
| 11697 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11698 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11699 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11700 | } |
| 11701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11702 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11703 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11704 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11705 | Return True if all characters in S are whitespace\n\ |
| 11706 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11707 | |
| 11708 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11709 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11710 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11711 | Py_ssize_t i, length; |
| 11712 | int kind; |
| 11713 | void *data; |
| 11714 | |
| 11715 | if (PyUnicode_READY(self) == -1) |
| 11716 | return NULL; |
| 11717 | length = PyUnicode_GET_LENGTH(self); |
| 11718 | kind = PyUnicode_KIND(self); |
| 11719 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11721 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11722 | if (length == 1) |
| 11723 | return PyBool_FromLong( |
| 11724 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11725 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11726 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11727 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11728 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11730 | for (i = 0; i < length; i++) { |
| 11731 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11732 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11733 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11734 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11735 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11736 | } |
| 11737 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11738 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11739 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11740 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11741 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11742 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11743 | |
| 11744 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11745 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11746 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11747 | Py_ssize_t i, length; |
| 11748 | int kind; |
| 11749 | void *data; |
| 11750 | |
| 11751 | if (PyUnicode_READY(self) == -1) |
| 11752 | return NULL; |
| 11753 | length = PyUnicode_GET_LENGTH(self); |
| 11754 | kind = PyUnicode_KIND(self); |
| 11755 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11756 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11757 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11758 | if (length == 1) |
| 11759 | return PyBool_FromLong( |
| 11760 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11761 | |
| 11762 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11763 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11764 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11765 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11766 | for (i = 0; i < length; i++) { |
| 11767 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11768 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11769 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11770 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11771 | } |
| 11772 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11773 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11774 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11775 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11776 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11777 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11778 | |
| 11779 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11780 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11781 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11782 | int kind; |
| 11783 | void *data; |
| 11784 | Py_ssize_t len, i; |
| 11785 | |
| 11786 | if (PyUnicode_READY(self) == -1) |
| 11787 | return NULL; |
| 11788 | |
| 11789 | kind = PyUnicode_KIND(self); |
| 11790 | data = PyUnicode_DATA(self); |
| 11791 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11792 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11793 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11794 | if (len == 1) { |
| 11795 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11796 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11797 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11798 | |
| 11799 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11800 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11801 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11802 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11803 | for (i = 0; i < len; i++) { |
| 11804 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11805 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11806 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11807 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11808 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11809 | } |
| 11810 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11811 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11812 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11813 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11814 | 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] | 11815 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | |
| 11817 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11818 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11820 | Py_ssize_t i, length; |
| 11821 | int kind; |
| 11822 | void *data; |
| 11823 | |
| 11824 | if (PyUnicode_READY(self) == -1) |
| 11825 | return NULL; |
| 11826 | length = PyUnicode_GET_LENGTH(self); |
| 11827 | kind = PyUnicode_KIND(self); |
| 11828 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11829 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11830 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11831 | if (length == 1) |
| 11832 | return PyBool_FromLong( |
| 11833 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11834 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11835 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11836 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11837 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11838 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11839 | for (i = 0; i < length; i++) { |
| 11840 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11841 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11842 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11843 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11844 | } |
| 11845 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11846 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11847 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11848 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11849 | Return True if all characters in S are digits\n\ |
| 11850 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11851 | |
| 11852 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11853 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11854 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11855 | Py_ssize_t i, length; |
| 11856 | int kind; |
| 11857 | void *data; |
| 11858 | |
| 11859 | if (PyUnicode_READY(self) == -1) |
| 11860 | return NULL; |
| 11861 | length = PyUnicode_GET_LENGTH(self); |
| 11862 | kind = PyUnicode_KIND(self); |
| 11863 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11865 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11866 | if (length == 1) { |
| 11867 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11868 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11869 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11871 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11872 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11873 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11874 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11875 | for (i = 0; i < length; i++) { |
| 11876 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11877 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11878 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11879 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11880 | } |
| 11881 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11882 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11883 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11884 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11885 | 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] | 11886 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11887 | |
| 11888 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11889 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11890 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11891 | Py_ssize_t i, length; |
| 11892 | int kind; |
| 11893 | void *data; |
| 11894 | |
| 11895 | if (PyUnicode_READY(self) == -1) |
| 11896 | return NULL; |
| 11897 | length = PyUnicode_GET_LENGTH(self); |
| 11898 | kind = PyUnicode_KIND(self); |
| 11899 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11900 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11901 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11902 | if (length == 1) |
| 11903 | return PyBool_FromLong( |
| 11904 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11905 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11906 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11907 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11908 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11909 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11910 | for (i = 0; i < length; i++) { |
| 11911 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11912 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11913 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11914 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11915 | } |
| 11916 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11917 | int |
| 11918 | PyUnicode_IsIdentifier(PyObject *self) |
| 11919 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11920 | int kind; |
| 11921 | void *data; |
| 11922 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11923 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11924 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11925 | if (PyUnicode_READY(self) == -1) { |
| 11926 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11927 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11928 | } |
| 11929 | |
| 11930 | /* Special case for empty strings */ |
| 11931 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11932 | return 0; |
| 11933 | kind = PyUnicode_KIND(self); |
| 11934 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11935 | |
| 11936 | /* PEP 3131 says that the first character must be in |
| 11937 | XID_Start and subsequent characters in XID_Continue, |
| 11938 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11939 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11940 | letters, digits, underscore). However, given the current |
| 11941 | definition of XID_Start and XID_Continue, it is sufficient |
| 11942 | to check just for these, except that _ must be allowed |
| 11943 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11944 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11945 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11946 | return 0; |
| 11947 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11948 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11949 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11950 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11951 | return 1; |
| 11952 | } |
| 11953 | |
| 11954 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11955 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11956 | \n\ |
| 11957 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11958 | to the language definition.\n\ |
| 11959 | \n\ |
| 11960 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11961 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11962 | |
| 11963 | static PyObject* |
| 11964 | unicode_isidentifier(PyObject *self) |
| 11965 | { |
| 11966 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11967 | } |
| 11968 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11969 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11970 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11971 | \n\ |
| 11972 | Return True if all characters in S are considered\n\ |
| 11973 | printable in repr() or S is empty, False otherwise."); |
| 11974 | |
| 11975 | static PyObject* |
| 11976 | unicode_isprintable(PyObject *self) |
| 11977 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11978 | Py_ssize_t i, length; |
| 11979 | int kind; |
| 11980 | void *data; |
| 11981 | |
| 11982 | if (PyUnicode_READY(self) == -1) |
| 11983 | return NULL; |
| 11984 | length = PyUnicode_GET_LENGTH(self); |
| 11985 | kind = PyUnicode_KIND(self); |
| 11986 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11987 | |
| 11988 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11989 | if (length == 1) |
| 11990 | return PyBool_FromLong( |
| 11991 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11992 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11993 | for (i = 0; i < length; i++) { |
| 11994 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11995 | Py_RETURN_FALSE; |
| 11996 | } |
| 11997 | } |
| 11998 | Py_RETURN_TRUE; |
| 11999 | } |
| 12000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12001 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 12002 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12003 | \n\ |
| 12004 | 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] | 12005 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12006 | |
| 12007 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12008 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12009 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12010 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12011 | } |
| 12012 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12013 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12014 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12015 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12016 | if (PyUnicode_READY(self) == -1) |
| 12017 | return -1; |
| 12018 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12019 | } |
| 12020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12021 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12022 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12023 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12024 | 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] | 12025 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12026 | |
| 12027 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12028 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12029 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12030 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12031 | Py_UCS4 fillchar = ' '; |
| 12032 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12033 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12034 | return NULL; |
| 12035 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12036 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12037 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12038 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12039 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12040 | return unicode_result_unchanged(self); |
| 12041 | |
| 12042 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12043 | } |
| 12044 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12045 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12046 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12047 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12048 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12049 | |
| 12050 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12051 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12052 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12053 | if (PyUnicode_READY(self) == -1) |
| 12054 | return NULL; |
| 12055 | if (PyUnicode_IS_ASCII(self)) |
| 12056 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12057 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12058 | } |
| 12059 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12060 | #define LEFTSTRIP 0 |
| 12061 | #define RIGHTSTRIP 1 |
| 12062 | #define BOTHSTRIP 2 |
| 12063 | |
| 12064 | /* Arrays indexed by above */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12065 | static const char * const stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12066 | |
| 12067 | #define STRIPNAME(i) (stripformat[i]+3) |
| 12068 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12069 | /* externally visible for str.strip(unicode) */ |
| 12070 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12071 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12072 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12073 | void *data; |
| 12074 | int kind; |
| 12075 | Py_ssize_t i, j, len; |
| 12076 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12077 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12078 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12079 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 12080 | return NULL; |
| 12081 | |
| 12082 | kind = PyUnicode_KIND(self); |
| 12083 | data = PyUnicode_DATA(self); |
| 12084 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12085 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12086 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 12087 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12088 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12089 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12090 | i = 0; |
| 12091 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12092 | while (i < len) { |
| 12093 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12094 | if (!BLOOM(sepmask, ch)) |
| 12095 | break; |
| 12096 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12097 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12098 | i++; |
| 12099 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12100 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12101 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12102 | j = len; |
| 12103 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12104 | j--; |
| 12105 | while (j >= i) { |
| 12106 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12107 | if (!BLOOM(sepmask, ch)) |
| 12108 | break; |
| 12109 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12110 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12111 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12112 | } |
| 12113 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12114 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12115 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12116 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12117 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12118 | } |
| 12119 | |
| 12120 | PyObject* |
| 12121 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12122 | { |
| 12123 | unsigned char *data; |
| 12124 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12125 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12126 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12127 | if (PyUnicode_READY(self) == -1) |
| 12128 | return NULL; |
| 12129 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12130 | length = PyUnicode_GET_LENGTH(self); |
| 12131 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12132 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12133 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12134 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12135 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12136 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12137 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12138 | return NULL; |
| 12139 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12140 | if (start >= length || end < start) |
| 12141 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12142 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12143 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12144 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12145 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12146 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12147 | } |
| 12148 | else { |
| 12149 | kind = PyUnicode_KIND(self); |
| 12150 | data = PyUnicode_1BYTE_DATA(self); |
| 12151 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12152 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12153 | length); |
| 12154 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12155 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12156 | |
| 12157 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12158 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12159 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12160 | Py_ssize_t len, i, j; |
| 12161 | |
| 12162 | if (PyUnicode_READY(self) == -1) |
| 12163 | return NULL; |
| 12164 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12165 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12166 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12167 | if (PyUnicode_IS_ASCII(self)) { |
| 12168 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12169 | |
| 12170 | i = 0; |
| 12171 | if (striptype != RIGHTSTRIP) { |
| 12172 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12173 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12174 | if (!_Py_ascii_whitespace[ch]) |
| 12175 | break; |
| 12176 | i++; |
| 12177 | } |
| 12178 | } |
| 12179 | |
| 12180 | j = len; |
| 12181 | if (striptype != LEFTSTRIP) { |
| 12182 | j--; |
| 12183 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12184 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12185 | if (!_Py_ascii_whitespace[ch]) |
| 12186 | break; |
| 12187 | j--; |
| 12188 | } |
| 12189 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12190 | } |
| 12191 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12192 | else { |
| 12193 | int kind = PyUnicode_KIND(self); |
| 12194 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12195 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12196 | i = 0; |
| 12197 | if (striptype != RIGHTSTRIP) { |
| 12198 | while (i < len) { |
| 12199 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12200 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12201 | break; |
| 12202 | i++; |
| 12203 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12204 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12205 | |
| 12206 | j = len; |
| 12207 | if (striptype != LEFTSTRIP) { |
| 12208 | j--; |
| 12209 | while (j >= i) { |
| 12210 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12211 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12212 | break; |
| 12213 | j--; |
| 12214 | } |
| 12215 | j++; |
| 12216 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12217 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12218 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12219 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | } |
| 12221 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12222 | |
| 12223 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12224 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12225 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12226 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12227 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12228 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12229 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12230 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12231 | if (sep != NULL && sep != Py_None) { |
| 12232 | if (PyUnicode_Check(sep)) |
| 12233 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12234 | else { |
| 12235 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12236 | "%s arg must be None or str", |
| 12237 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12238 | return NULL; |
| 12239 | } |
| 12240 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12241 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12242 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12243 | } |
| 12244 | |
| 12245 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12246 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12247 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12248 | \n\ |
| 12249 | Return a copy of the string S with leading and trailing\n\ |
| 12250 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12251 | 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] | 12252 | |
| 12253 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12254 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12255 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12256 | if (PyTuple_GET_SIZE(args) == 0) |
| 12257 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12258 | else |
| 12259 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12260 | } |
| 12261 | |
| 12262 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12263 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12264 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12265 | \n\ |
| 12266 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12267 | 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] | 12268 | |
| 12269 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12270 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12271 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12272 | if (PyTuple_GET_SIZE(args) == 0) |
| 12273 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12274 | else |
| 12275 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12276 | } |
| 12277 | |
| 12278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12279 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12280 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12281 | \n\ |
| 12282 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12283 | 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] | 12284 | |
| 12285 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12286 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12287 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12288 | if (PyTuple_GET_SIZE(args) == 0) |
| 12289 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12290 | else |
| 12291 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12292 | } |
| 12293 | |
| 12294 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12295 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12296 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12297 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12298 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12299 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12300 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12301 | if (len < 1) |
| 12302 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12303 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12304 | /* no repeat, return original string */ |
| 12305 | if (len == 1) |
| 12306 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12307 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12308 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12309 | return NULL; |
| 12310 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12311 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12312 | PyErr_SetString(PyExc_OverflowError, |
| 12313 | "repeated string is too long"); |
| 12314 | return NULL; |
| 12315 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12316 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12317 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12318 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12319 | if (!u) |
| 12320 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12321 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12322 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12323 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12324 | const int kind = PyUnicode_KIND(str); |
| 12325 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12326 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12327 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12328 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12329 | } |
| 12330 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12331 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12332 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12333 | ucs2[n] = fill_char; |
| 12334 | } else { |
| 12335 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12336 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12337 | for (n = 0; n < len; ++n) |
| 12338 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12339 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12340 | } |
| 12341 | else { |
| 12342 | /* number of characters copied this far */ |
| 12343 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12344 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12345 | char *to = (char *) PyUnicode_DATA(u); |
| 12346 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12347 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12348 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12349 | n = (done <= nchars-done) ? done : nchars-done; |
| 12350 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12351 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12352 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12353 | } |
| 12354 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12355 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12356 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12357 | } |
| 12358 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12359 | PyObject * |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12360 | PyUnicode_Replace(PyObject *str, |
| 12361 | PyObject *substr, |
| 12362 | PyObject *replstr, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12363 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12364 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12365 | if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0 || |
| 12366 | ensure_unicode(replstr) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12367 | return NULL; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12368 | return replace(str, substr, replstr, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12369 | } |
| 12370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12371 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12372 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12373 | \n\ |
| 12374 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12375 | old replaced by new. If the optional argument count is\n\ |
| 12376 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12377 | |
| 12378 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12379 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12380 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12381 | PyObject *str1; |
| 12382 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12383 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12384 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12385 | if (!PyArg_ParseTuple(args, "UU|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12387 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12388 | return NULL; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12389 | return replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12390 | } |
| 12391 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12392 | static PyObject * |
| 12393 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12394 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12395 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12396 | Py_ssize_t isize; |
| 12397 | Py_ssize_t osize, squote, dquote, i, o; |
| 12398 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12399 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12400 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12402 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12403 | return NULL; |
| 12404 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12405 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12406 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12407 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12408 | /* Compute length of output, quote characters, and |
| 12409 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12410 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12411 | max = 127; |
| 12412 | squote = dquote = 0; |
| 12413 | ikind = PyUnicode_KIND(unicode); |
| 12414 | for (i = 0; i < isize; i++) { |
| 12415 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12416 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12417 | switch (ch) { |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12418 | case '\'': squote++; break; |
| 12419 | case '"': dquote++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12420 | case '\\': case '\t': case '\r': case '\n': |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12421 | incr = 2; |
| 12422 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12423 | default: |
| 12424 | /* Fast-path ASCII */ |
| 12425 | if (ch < ' ' || ch == 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12426 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12427 | else if (ch < 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12428 | ; |
| 12429 | else if (Py_UNICODE_ISPRINTABLE(ch)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12430 | max = ch > max ? ch : max; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12431 | else if (ch < 0x100) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12432 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12433 | else if (ch < 0x10000) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12434 | incr = 6; /* \uHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12435 | else |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12436 | incr = 10; /* \uHHHHHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12437 | } |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12438 | if (osize > PY_SSIZE_T_MAX - incr) { |
| 12439 | PyErr_SetString(PyExc_OverflowError, |
| 12440 | "string is too long to generate repr"); |
| 12441 | return NULL; |
| 12442 | } |
| 12443 | osize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12444 | } |
| 12445 | |
| 12446 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12447 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12448 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12449 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12450 | if (dquote) |
| 12451 | /* Both squote and dquote present. Use squote, |
| 12452 | and escape them */ |
| 12453 | osize += squote; |
| 12454 | else |
| 12455 | quote = '"'; |
| 12456 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12457 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12458 | |
| 12459 | repr = PyUnicode_New(osize, max); |
| 12460 | if (repr == NULL) |
| 12461 | return NULL; |
| 12462 | okind = PyUnicode_KIND(repr); |
| 12463 | odata = PyUnicode_DATA(repr); |
| 12464 | |
| 12465 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12466 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12467 | if (unchanged) { |
| 12468 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12469 | unicode, 0, |
| 12470 | isize); |
| 12471 | } |
| 12472 | else { |
| 12473 | for (i = 0, o = 1; i < isize; i++) { |
| 12474 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12475 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12476 | /* Escape quotes and backslashes */ |
| 12477 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12478 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12479 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12480 | continue; |
| 12481 | } |
| 12482 | |
| 12483 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12484 | if (ch == '\t') { |
| 12485 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12486 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12487 | } |
| 12488 | else if (ch == '\n') { |
| 12489 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12490 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12491 | } |
| 12492 | else if (ch == '\r') { |
| 12493 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12494 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12495 | } |
| 12496 | |
| 12497 | /* Map non-printable US ASCII to '\xhh' */ |
| 12498 | else if (ch < ' ' || ch == 0x7F) { |
| 12499 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12500 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12501 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12502 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12503 | } |
| 12504 | |
| 12505 | /* Copy ASCII characters as-is */ |
| 12506 | else if (ch < 0x7F) { |
| 12507 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12508 | } |
| 12509 | |
| 12510 | /* Non-ASCII characters */ |
| 12511 | else { |
| 12512 | /* Map Unicode whitespace and control characters |
| 12513 | (categories Z* and C* except ASCII space) |
| 12514 | */ |
| 12515 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12516 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12517 | /* Map 8-bit characters to '\xhh' */ |
| 12518 | if (ch <= 0xff) { |
| 12519 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12520 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12521 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12522 | } |
| 12523 | /* Map 16-bit characters to '\uxxxx' */ |
| 12524 | else if (ch <= 0xffff) { |
| 12525 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12526 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12527 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12528 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12529 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12530 | } |
| 12531 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12532 | else { |
| 12533 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12534 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12535 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12536 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12537 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12538 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12539 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12540 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12541 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12542 | } |
| 12543 | } |
| 12544 | /* Copy characters as-is */ |
| 12545 | else { |
| 12546 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12547 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12548 | } |
| 12549 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12550 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12551 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12552 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12553 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12554 | } |
| 12555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12556 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12557 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12558 | \n\ |
| 12559 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12560 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12561 | arguments start and end are interpreted as in slice notation.\n\ |
| 12562 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12563 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12564 | |
| 12565 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12566 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12567 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12568 | /* initialize variables to prevent gcc warning */ |
| 12569 | PyObject *substring = NULL; |
| 12570 | Py_ssize_t start = 0; |
| 12571 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12572 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 12574 | if (!parse_args_finds_unicode("rfind", args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12575 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12576 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12577 | if (PyUnicode_READY(self) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12578 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12579 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12580 | result = any_find_slice(self, substring, start, end, -1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12581 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12582 | if (result == -2) |
| 12583 | return NULL; |
| 12584 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12585 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12586 | } |
| 12587 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12588 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12589 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12590 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12591 | 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] | 12592 | |
| 12593 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12594 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12595 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12596 | /* initialize variables to prevent gcc warning */ |
| 12597 | PyObject *substring = NULL; |
| 12598 | Py_ssize_t start = 0; |
| 12599 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12600 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12601 | |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 12602 | if (!parse_args_finds_unicode("rindex", args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12603 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12604 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12605 | if (PyUnicode_READY(self) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12606 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12607 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12608 | result = any_find_slice(self, substring, start, end, -1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12610 | if (result == -2) |
| 12611 | return NULL; |
| 12612 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12613 | if (result < 0) { |
| 12614 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12615 | return NULL; |
| 12616 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12617 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12618 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12619 | } |
| 12620 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12621 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12622 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12623 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12624 | 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] | 12625 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12626 | |
| 12627 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12628 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12629 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12630 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12631 | Py_UCS4 fillchar = ' '; |
| 12632 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12633 | 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] | 12634 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12635 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12636 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12637 | return NULL; |
| 12638 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12639 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12640 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12641 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12642 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12643 | } |
| 12644 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12645 | PyObject * |
| 12646 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12647 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12648 | if (ensure_unicode(s) < 0 || (sep != NULL && ensure_unicode(sep) < 0)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12649 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12650 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12651 | return split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12652 | } |
| 12653 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12654 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12655 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12656 | \n\ |
| 12657 | Return a list of the words in S, using sep as the\n\ |
| 12658 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12659 | 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] | 12660 | whitespace string is a separator and empty strings are\n\ |
| 12661 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12662 | |
| 12663 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12664 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12665 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12666 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12667 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12668 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12669 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12670 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12671 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12672 | return NULL; |
| 12673 | |
| 12674 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12675 | return split(self, NULL, maxcount); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12676 | |
| 12677 | if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12678 | return split(self, substring, maxcount); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12679 | |
| 12680 | PyErr_Format(PyExc_TypeError, |
| 12681 | "must be str or None, not %.100s", |
| 12682 | Py_TYPE(substring)->tp_name); |
| 12683 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12684 | } |
| 12685 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12686 | PyObject * |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12687 | PyUnicode_Partition(PyObject *str_obj, PyObject *sep_obj) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12688 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12689 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12690 | int kind1, kind2; |
| 12691 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12692 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12693 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12694 | if (ensure_unicode(str_obj) < 0 || ensure_unicode(sep_obj) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12695 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12696 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12697 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12698 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12699 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12700 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12701 | if (kind1 < kind2 || len1 < len2) { |
| 12702 | _Py_INCREF_UNICODE_EMPTY(); |
| 12703 | if (!unicode_empty) |
| 12704 | out = NULL; |
| 12705 | else { |
| 12706 | out = PyTuple_Pack(3, str_obj, unicode_empty, unicode_empty); |
| 12707 | Py_DECREF(unicode_empty); |
| 12708 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12709 | return out; |
| 12710 | } |
| 12711 | buf1 = PyUnicode_DATA(str_obj); |
| 12712 | buf2 = PyUnicode_DATA(sep_obj); |
| 12713 | if (kind2 != kind1) { |
| 12714 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12715 | if (!buf2) |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12716 | return NULL; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12717 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12718 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12719 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12720 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12721 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12722 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12723 | else |
| 12724 | 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] | 12725 | break; |
| 12726 | case PyUnicode_2BYTE_KIND: |
| 12727 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12728 | break; |
| 12729 | case PyUnicode_4BYTE_KIND: |
| 12730 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12731 | break; |
| 12732 | default: |
| 12733 | assert(0); |
| 12734 | out = 0; |
| 12735 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12736 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12737 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12739 | |
| 12740 | return out; |
| 12741 | } |
| 12742 | |
| 12743 | |
| 12744 | PyObject * |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12745 | PyUnicode_RPartition(PyObject *str_obj, PyObject *sep_obj) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12746 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12747 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12748 | int kind1, kind2; |
| 12749 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12750 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12751 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12752 | if (ensure_unicode(str_obj) < 0 || ensure_unicode(sep_obj) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12753 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12754 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12755 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12756 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12757 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12758 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12759 | if (kind1 < kind2 || len1 < len2) { |
| 12760 | _Py_INCREF_UNICODE_EMPTY(); |
| 12761 | if (!unicode_empty) |
| 12762 | out = NULL; |
| 12763 | else { |
| 12764 | out = PyTuple_Pack(3, unicode_empty, unicode_empty, str_obj); |
| 12765 | Py_DECREF(unicode_empty); |
| 12766 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12767 | return out; |
| 12768 | } |
| 12769 | buf1 = PyUnicode_DATA(str_obj); |
| 12770 | buf2 = PyUnicode_DATA(sep_obj); |
| 12771 | if (kind2 != kind1) { |
| 12772 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12773 | if (!buf2) |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12774 | return NULL; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12775 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12776 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12777 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12778 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12779 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12780 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12781 | else |
| 12782 | 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] | 12783 | break; |
| 12784 | case PyUnicode_2BYTE_KIND: |
| 12785 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12786 | break; |
| 12787 | case PyUnicode_4BYTE_KIND: |
| 12788 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12789 | break; |
| 12790 | default: |
| 12791 | assert(0); |
| 12792 | out = 0; |
| 12793 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12794 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12795 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12796 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12797 | |
| 12798 | return out; |
| 12799 | } |
| 12800 | |
| 12801 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12802 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12803 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12804 | 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] | 12805 | 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] | 12806 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12807 | |
| 12808 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12809 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12810 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12811 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12812 | } |
| 12813 | |
| 12814 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12815 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12816 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12817 | 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] | 12818 | 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] | 12819 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12820 | |
| 12821 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12822 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12823 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12824 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12825 | } |
| 12826 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12827 | PyObject * |
| 12828 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12829 | { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12830 | if (ensure_unicode(s) < 0 || (sep != NULL && ensure_unicode(sep) < 0)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12831 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12832 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12833 | return rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12834 | } |
| 12835 | |
| 12836 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12837 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12838 | \n\ |
| 12839 | Return a list of the words in S, using sep as the\n\ |
| 12840 | delimiter string, starting at the end of the string and\n\ |
| 12841 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12842 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12843 | is a separator."); |
| 12844 | |
| 12845 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12846 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12847 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12848 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12849 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12850 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12851 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12852 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12853 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12854 | return NULL; |
| 12855 | |
| 12856 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12857 | return rsplit(self, NULL, maxcount); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12858 | |
| 12859 | if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12860 | return rsplit(self, substring, maxcount); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 12861 | |
| 12862 | PyErr_Format(PyExc_TypeError, |
| 12863 | "must be str or None, not %.100s", |
| 12864 | Py_TYPE(substring)->tp_name); |
| 12865 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12866 | } |
| 12867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12868 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12869 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12870 | \n\ |
| 12871 | 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] | 12872 | 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] | 12873 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12874 | |
| 12875 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12876 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12877 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12878 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12879 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12880 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12881 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12882 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12883 | return NULL; |
| 12884 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12885 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12886 | } |
| 12887 | |
| 12888 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12889 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12890 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12891 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12892 | } |
| 12893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12894 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12895 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12896 | \n\ |
| 12897 | 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] | 12898 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12899 | |
| 12900 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12901 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12902 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12903 | if (PyUnicode_READY(self) == -1) |
| 12904 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12905 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12906 | } |
| 12907 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 12908 | /*[clinic input] |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12909 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12910 | @staticmethod |
| 12911 | str.maketrans as unicode_maketrans |
| 12912 | |
| 12913 | x: object |
| 12914 | |
| 12915 | y: unicode=NULL |
| 12916 | |
| 12917 | z: unicode=NULL |
| 12918 | |
| 12919 | / |
| 12920 | |
| 12921 | Return a translation table usable for str.translate(). |
| 12922 | |
| 12923 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12924 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12925 | Character keys will be then converted to ordinals. |
| 12926 | If there are two arguments, they must be strings of equal length, and |
| 12927 | in the resulting dictionary, each character in x will be mapped to the |
| 12928 | character at the same position in y. If there is a third argument, it |
| 12929 | must be a string, whose characters will be mapped to None in the result. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 12930 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12931 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12932 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12933 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12934 | /*[clinic end generated code: output=a925c89452bd5881 input=7bfbf529a293c6c5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12935 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12936 | PyObject *new = NULL, *key, *value; |
| 12937 | Py_ssize_t i = 0; |
| 12938 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12939 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12940 | new = PyDict_New(); |
| 12941 | if (!new) |
| 12942 | return NULL; |
| 12943 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12944 | int x_kind, y_kind, z_kind; |
| 12945 | void *x_data, *y_data, *z_data; |
| 12946 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12947 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12948 | if (!PyUnicode_Check(x)) { |
| 12949 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12950 | "be a string if there is a second argument"); |
| 12951 | goto err; |
| 12952 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12953 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12954 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12955 | "arguments must have equal length"); |
| 12956 | goto err; |
| 12957 | } |
| 12958 | /* 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] | 12959 | x_kind = PyUnicode_KIND(x); |
| 12960 | y_kind = PyUnicode_KIND(y); |
| 12961 | x_data = PyUnicode_DATA(x); |
| 12962 | y_data = PyUnicode_DATA(y); |
| 12963 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12964 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12965 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12966 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12967 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12968 | if (!value) { |
| 12969 | Py_DECREF(key); |
| 12970 | goto err; |
| 12971 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12972 | res = PyDict_SetItem(new, key, value); |
| 12973 | Py_DECREF(key); |
| 12974 | Py_DECREF(value); |
| 12975 | if (res < 0) |
| 12976 | goto err; |
| 12977 | } |
| 12978 | /* create entries for deleting chars in z */ |
| 12979 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12980 | z_kind = PyUnicode_KIND(z); |
| 12981 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12982 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12983 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12984 | if (!key) |
| 12985 | goto err; |
| 12986 | res = PyDict_SetItem(new, key, Py_None); |
| 12987 | Py_DECREF(key); |
| 12988 | if (res < 0) |
| 12989 | goto err; |
| 12990 | } |
| 12991 | } |
| 12992 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12993 | int kind; |
| 12994 | void *data; |
| 12995 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12996 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12997 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12998 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12999 | "to maketrans it must be a dict"); |
| 13000 | goto err; |
| 13001 | } |
| 13002 | /* copy entries into the new dict, converting string keys to int keys */ |
| 13003 | while (PyDict_Next(x, &i, &key, &value)) { |
| 13004 | if (PyUnicode_Check(key)) { |
| 13005 | /* convert string keys to integer keys */ |
| 13006 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13007 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13008 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13009 | "table must be of length 1"); |
| 13010 | goto err; |
| 13011 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13012 | kind = PyUnicode_KIND(key); |
| 13013 | data = PyUnicode_DATA(key); |
| 13014 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13015 | if (!newkey) |
| 13016 | goto err; |
| 13017 | res = PyDict_SetItem(new, newkey, value); |
| 13018 | Py_DECREF(newkey); |
| 13019 | if (res < 0) |
| 13020 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13021 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13022 | /* just keep integer keys */ |
| 13023 | if (PyDict_SetItem(new, key, value) < 0) |
| 13024 | goto err; |
| 13025 | } else { |
| 13026 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13027 | "be strings or integers"); |
| 13028 | goto err; |
| 13029 | } |
| 13030 | } |
| 13031 | } |
| 13032 | return new; |
| 13033 | err: |
| 13034 | Py_DECREF(new); |
| 13035 | return NULL; |
| 13036 | } |
| 13037 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13038 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13039 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13040 | \n\ |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 13041 | Return a copy of the string S in which each character has been mapped\n\ |
| 13042 | through the given translation table. The table must implement\n\ |
| 13043 | lookup/indexing via __getitem__, for instance a dictionary or list,\n\ |
| 13044 | mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n\ |
| 13045 | this operation raises LookupError, the character is left untouched.\n\ |
| 13046 | Characters mapped to None are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13047 | |
| 13048 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13049 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13050 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13051 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13052 | } |
| 13053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13054 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13055 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13056 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13057 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13058 | |
| 13059 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13060 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13061 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13062 | if (PyUnicode_READY(self) == -1) |
| 13063 | return NULL; |
| 13064 | if (PyUnicode_IS_ASCII(self)) |
| 13065 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13066 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13067 | } |
| 13068 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13069 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13070 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13071 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13072 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13073 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13074 | |
| 13075 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13076 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13077 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13078 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13079 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13080 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13081 | int kind; |
| 13082 | void *data; |
| 13083 | Py_UCS4 chr; |
| 13084 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13085 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13086 | return NULL; |
| 13087 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13088 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13089 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13090 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13091 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13092 | return unicode_result_unchanged(self); |
| 13093 | |
| 13094 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13095 | |
| 13096 | u = pad(self, fill, 0, '0'); |
| 13097 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13098 | if (u == NULL) |
| 13099 | return NULL; |
| 13100 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13101 | kind = PyUnicode_KIND(u); |
| 13102 | data = PyUnicode_DATA(u); |
| 13103 | chr = PyUnicode_READ(kind, data, fill); |
| 13104 | |
| 13105 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13106 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13107 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13108 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13109 | } |
| 13110 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13111 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13112 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13113 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13114 | |
| 13115 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13116 | static PyObject * |
| 13117 | unicode__decimal2ascii(PyObject *self) |
| 13118 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13119 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13120 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13121 | #endif |
| 13122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13123 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13124 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13125 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13126 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13127 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13128 | With optional end, stop comparing S at that position.\n\ |
| 13129 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13130 | |
| 13131 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13132 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13133 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13134 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13135 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13136 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13137 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13138 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13139 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13140 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13141 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13142 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13143 | if (PyTuple_Check(subobj)) { |
| 13144 | Py_ssize_t i; |
| 13145 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 13146 | substring = PyTuple_GET_ITEM(subobj, i); |
| 13147 | if (!PyUnicode_Check(substring)) { |
| 13148 | PyErr_Format(PyExc_TypeError, |
| 13149 | "tuple for startswith must only contain str, " |
| 13150 | "not %.100s", |
| 13151 | Py_TYPE(substring)->tp_name); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13152 | return NULL; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 13153 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13154 | result = tailmatch(self, substring, start, end, -1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13155 | if (result == -1) |
| 13156 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13157 | if (result) { |
| 13158 | Py_RETURN_TRUE; |
| 13159 | } |
| 13160 | } |
| 13161 | /* nothing matched */ |
| 13162 | Py_RETURN_FALSE; |
| 13163 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 13164 | if (!PyUnicode_Check(subobj)) { |
| 13165 | PyErr_Format(PyExc_TypeError, |
| 13166 | "startswith first arg must be str or " |
| 13167 | "a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13168 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13169 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 13170 | result = tailmatch(self, subobj, start, end, -1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13171 | if (result == -1) |
| 13172 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13173 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13174 | } |
| 13175 | |
| 13176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13177 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13178 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13179 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13180 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13181 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13182 | With optional end, stop comparing S at that position.\n\ |
| 13183 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13184 | |
| 13185 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13186 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13187 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13188 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13189 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13190 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13191 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13192 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13193 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13194 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13195 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13196 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13197 | if (PyTuple_Check(subobj)) { |
| 13198 | Py_ssize_t i; |
| 13199 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 13200 | substring = PyTuple_GET_ITEM(subobj, i); |
| 13201 | if (!PyUnicode_Check(substring)) { |
| 13202 | PyErr_Format(PyExc_TypeError, |
| 13203 | "tuple for endswith must only contain str, " |
| 13204 | "not %.100s", |
| 13205 | Py_TYPE(substring)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13206 | return NULL; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 13207 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13208 | result = tailmatch(self, substring, start, end, +1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13209 | if (result == -1) |
| 13210 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13211 | if (result) { |
| 13212 | Py_RETURN_TRUE; |
| 13213 | } |
| 13214 | } |
| 13215 | Py_RETURN_FALSE; |
| 13216 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 13217 | if (!PyUnicode_Check(subobj)) { |
| 13218 | PyErr_Format(PyExc_TypeError, |
| 13219 | "endswith first arg must be str or " |
| 13220 | "a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13221 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13222 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 13223 | result = tailmatch(self, subobj, start, end, +1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13224 | if (result == -1) |
| 13225 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13226 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13227 | } |
| 13228 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13229 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13230 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13231 | { |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13232 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13233 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13234 | |
| 13235 | if (!writer->readonly) { |
| 13236 | writer->kind = PyUnicode_KIND(writer->buffer); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13237 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13238 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13239 | else { |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13240 | /* use a value smaller than PyUnicode_1BYTE_KIND() so |
| 13241 | _PyUnicodeWriter_PrepareKind() will copy the buffer. */ |
| 13242 | writer->kind = PyUnicode_WCHAR_KIND; |
| 13243 | assert(writer->kind <= PyUnicode_1BYTE_KIND); |
| 13244 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13245 | /* Copy-on-write mode: set buffer size to 0 so |
| 13246 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13247 | * next write. */ |
| 13248 | writer->size = 0; |
| 13249 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13250 | } |
| 13251 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13252 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13253 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13254 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13255 | memset(writer, 0, sizeof(*writer)); |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13256 | |
| 13257 | /* ASCII is the bare minimum */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13258 | writer->min_char = 127; |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13259 | |
| 13260 | /* use a value smaller than PyUnicode_1BYTE_KIND() so |
| 13261 | _PyUnicodeWriter_PrepareKind() will copy the buffer. */ |
| 13262 | writer->kind = PyUnicode_WCHAR_KIND; |
| 13263 | assert(writer->kind <= PyUnicode_1BYTE_KIND); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13264 | } |
| 13265 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13266 | int |
| 13267 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13268 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13269 | { |
| 13270 | Py_ssize_t newlen; |
| 13271 | PyObject *newbuffer; |
| 13272 | |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 13273 | /* ensure that the _PyUnicodeWriter_Prepare macro was used */ |
Victor Stinner | 6174474 | 2015-09-22 01:01:17 +0200 | [diff] [blame] | 13274 | assert((maxchar > writer->maxchar && length >= 0) |
| 13275 | || length > 0); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13276 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13277 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13278 | PyErr_NoMemory(); |
| 13279 | return -1; |
| 13280 | } |
| 13281 | newlen = writer->pos + length; |
| 13282 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13283 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13284 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13285 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13286 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13287 | if (writer->overallocate |
| 13288 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13289 | /* overallocate to limit the number of realloc() */ |
| 13290 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13291 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13292 | if (newlen < writer->min_length) |
| 13293 | newlen = writer->min_length; |
| 13294 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13295 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13296 | if (writer->buffer == NULL) |
| 13297 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13298 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13299 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13300 | if (writer->overallocate |
| 13301 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13302 | /* overallocate to limit the number of realloc() */ |
| 13303 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13304 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13305 | if (newlen < writer->min_length) |
| 13306 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13307 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13308 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13309 | /* resize + widen */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 13310 | maxchar = Py_MAX(maxchar, writer->maxchar); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13311 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13312 | if (newbuffer == NULL) |
| 13313 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13314 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13315 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13316 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13317 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13318 | } |
| 13319 | else { |
| 13320 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13321 | if (newbuffer == NULL) |
| 13322 | return -1; |
| 13323 | } |
| 13324 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13325 | } |
| 13326 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13327 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13328 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13329 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13330 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13331 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13332 | writer->buffer, 0, writer->pos); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 13333 | Py_SETREF(writer->buffer, newbuffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13334 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13335 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13336 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13337 | |
| 13338 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13339 | } |
| 13340 | |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 13341 | int |
| 13342 | _PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer, |
| 13343 | enum PyUnicode_Kind kind) |
| 13344 | { |
| 13345 | Py_UCS4 maxchar; |
| 13346 | |
| 13347 | /* ensure that the _PyUnicodeWriter_PrepareKind macro was used */ |
| 13348 | assert(writer->kind < kind); |
| 13349 | |
| 13350 | switch (kind) |
| 13351 | { |
| 13352 | case PyUnicode_1BYTE_KIND: maxchar = 0xff; break; |
| 13353 | case PyUnicode_2BYTE_KIND: maxchar = 0xffff; break; |
| 13354 | case PyUnicode_4BYTE_KIND: maxchar = 0x10ffff; break; |
| 13355 | default: |
| 13356 | assert(0 && "invalid kind"); |
| 13357 | return -1; |
| 13358 | } |
| 13359 | |
| 13360 | return _PyUnicodeWriter_PrepareInternal(writer, 0, maxchar); |
| 13361 | } |
| 13362 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13363 | Py_LOCAL_INLINE(int) |
| 13364 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13365 | { |
| 13366 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13367 | return -1; |
| 13368 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13369 | writer->pos++; |
| 13370 | return 0; |
| 13371 | } |
| 13372 | |
| 13373 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13374 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13375 | { |
| 13376 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13377 | } |
| 13378 | |
| 13379 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13380 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13381 | { |
| 13382 | Py_UCS4 maxchar; |
| 13383 | Py_ssize_t len; |
| 13384 | |
| 13385 | if (PyUnicode_READY(str) == -1) |
| 13386 | return -1; |
| 13387 | len = PyUnicode_GET_LENGTH(str); |
| 13388 | if (len == 0) |
| 13389 | return 0; |
| 13390 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13391 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13392 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 1912b39 | 2015-03-26 09:37:23 +0100 | [diff] [blame] | 13393 | assert(_PyUnicode_CheckConsistency(str, 1)); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13394 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13395 | Py_INCREF(str); |
| 13396 | writer->buffer = str; |
| 13397 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13398 | writer->pos += len; |
| 13399 | return 0; |
| 13400 | } |
| 13401 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13402 | return -1; |
| 13403 | } |
| 13404 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13405 | str, 0, len); |
| 13406 | writer->pos += len; |
| 13407 | return 0; |
| 13408 | } |
| 13409 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13410 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13411 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13412 | Py_ssize_t start, Py_ssize_t end) |
| 13413 | { |
| 13414 | Py_UCS4 maxchar; |
| 13415 | Py_ssize_t len; |
| 13416 | |
| 13417 | if (PyUnicode_READY(str) == -1) |
| 13418 | return -1; |
| 13419 | |
| 13420 | assert(0 <= start); |
| 13421 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13422 | assert(start <= end); |
| 13423 | |
| 13424 | if (end == 0) |
| 13425 | return 0; |
| 13426 | |
| 13427 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13428 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13429 | |
| 13430 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13431 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13432 | else |
| 13433 | maxchar = writer->maxchar; |
| 13434 | len = end - start; |
| 13435 | |
| 13436 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13437 | return -1; |
| 13438 | |
| 13439 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13440 | str, start, len); |
| 13441 | writer->pos += len; |
| 13442 | return 0; |
| 13443 | } |
| 13444 | |
| 13445 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13446 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13447 | const char *ascii, Py_ssize_t len) |
| 13448 | { |
| 13449 | if (len == -1) |
| 13450 | len = strlen(ascii); |
| 13451 | |
| 13452 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13453 | |
| 13454 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13455 | PyObject *str; |
| 13456 | |
| 13457 | str = _PyUnicode_FromASCII(ascii, len); |
| 13458 | if (str == NULL) |
| 13459 | return -1; |
| 13460 | |
| 13461 | writer->readonly = 1; |
| 13462 | writer->buffer = str; |
| 13463 | _PyUnicodeWriter_Update(writer); |
| 13464 | writer->pos += len; |
| 13465 | return 0; |
| 13466 | } |
| 13467 | |
| 13468 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13469 | return -1; |
| 13470 | |
| 13471 | switch (writer->kind) |
| 13472 | { |
| 13473 | case PyUnicode_1BYTE_KIND: |
| 13474 | { |
| 13475 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13476 | Py_UCS1 *data = writer->data; |
| 13477 | |
| 13478 | Py_MEMCPY(data + writer->pos, str, len); |
| 13479 | break; |
| 13480 | } |
| 13481 | case PyUnicode_2BYTE_KIND: |
| 13482 | { |
| 13483 | _PyUnicode_CONVERT_BYTES( |
| 13484 | Py_UCS1, Py_UCS2, |
| 13485 | ascii, ascii + len, |
| 13486 | (Py_UCS2 *)writer->data + writer->pos); |
| 13487 | break; |
| 13488 | } |
| 13489 | case PyUnicode_4BYTE_KIND: |
| 13490 | { |
| 13491 | _PyUnicode_CONVERT_BYTES( |
| 13492 | Py_UCS1, Py_UCS4, |
| 13493 | ascii, ascii + len, |
| 13494 | (Py_UCS4 *)writer->data + writer->pos); |
| 13495 | break; |
| 13496 | } |
| 13497 | default: |
| 13498 | assert(0); |
| 13499 | } |
| 13500 | |
| 13501 | writer->pos += len; |
| 13502 | return 0; |
| 13503 | } |
| 13504 | |
| 13505 | int |
| 13506 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13507 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13508 | { |
| 13509 | Py_UCS4 maxchar; |
| 13510 | |
| 13511 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13512 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13513 | return -1; |
| 13514 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13515 | writer->pos += len; |
| 13516 | return 0; |
| 13517 | } |
| 13518 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13519 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13520 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13521 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13522 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13523 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13524 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13525 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13526 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13527 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13528 | str = writer->buffer; |
| 13529 | writer->buffer = NULL; |
| 13530 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13531 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13532 | } |
Victor Stinner | 6c2cdae | 2015-10-12 13:29:43 +0200 | [diff] [blame] | 13533 | if (writer->pos == 0) { |
| 13534 | Py_CLEAR(writer->buffer); |
| 13535 | |
| 13536 | /* Get the empty Unicode string singleton ('') */ |
| 13537 | _Py_INCREF_UNICODE_EMPTY(); |
| 13538 | str = unicode_empty; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13539 | } |
Victor Stinner | 6c2cdae | 2015-10-12 13:29:43 +0200 | [diff] [blame] | 13540 | else { |
| 13541 | str = writer->buffer; |
| 13542 | writer->buffer = NULL; |
| 13543 | |
| 13544 | if (PyUnicode_GET_LENGTH(str) != writer->pos) { |
| 13545 | PyObject *str2; |
| 13546 | str2 = resize_compact(str, writer->pos); |
| 13547 | if (str2 == NULL) |
| 13548 | return NULL; |
| 13549 | str = str2; |
| 13550 | } |
| 13551 | } |
| 13552 | |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13553 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13554 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13555 | } |
| 13556 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13557 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13558 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13559 | { |
| 13560 | Py_CLEAR(writer->buffer); |
| 13561 | } |
| 13562 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13563 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13564 | |
| 13565 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13566 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13567 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13568 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13569 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13570 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13571 | PyDoc_STRVAR(format_map__doc__, |
| 13572 | "S.format_map(mapping) -> str\n\ |
| 13573 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13574 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13575 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13576 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13577 | static PyObject * |
| 13578 | unicode__format__(PyObject* self, PyObject* args) |
| 13579 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13580 | PyObject *format_spec; |
| 13581 | _PyUnicodeWriter writer; |
| 13582 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13583 | |
| 13584 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13585 | return NULL; |
| 13586 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13587 | if (PyUnicode_READY(self) == -1) |
| 13588 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13589 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13590 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13591 | self, format_spec, 0, |
| 13592 | PyUnicode_GET_LENGTH(format_spec)); |
| 13593 | if (ret == -1) { |
| 13594 | _PyUnicodeWriter_Dealloc(&writer); |
| 13595 | return NULL; |
| 13596 | } |
| 13597 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13598 | } |
| 13599 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13600 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13601 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13602 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13603 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13604 | |
| 13605 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13606 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13607 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13608 | Py_ssize_t size; |
| 13609 | |
| 13610 | /* If it's a compact object, account for base structure + |
| 13611 | character data. */ |
| 13612 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13613 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13614 | else if (PyUnicode_IS_COMPACT(v)) |
| 13615 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13616 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13617 | else { |
| 13618 | /* If it is a two-block object, account for base object, and |
| 13619 | for character block if present. */ |
| 13620 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13621 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13622 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13623 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13624 | } |
| 13625 | /* 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] | 13626 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13627 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13628 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13629 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13630 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13631 | |
| 13632 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13633 | } |
| 13634 | |
| 13635 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13636 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13637 | |
| 13638 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13639 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13640 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13641 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13642 | if (!copy) |
| 13643 | return NULL; |
| 13644 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13645 | } |
| 13646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13647 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13648 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13649 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13650 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13651 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13652 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13653 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13654 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13655 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13656 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13657 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13658 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13659 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13660 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13661 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13662 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13663 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13664 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13665 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13666 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13667 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13668 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13669 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13670 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13671 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13672 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13673 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13674 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13675 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13676 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13677 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13678 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13679 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13680 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13681 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13682 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13683 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13684 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13685 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13686 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13687 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13688 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13689 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13690 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13691 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13692 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13693 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13694 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13695 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13696 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13697 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13698 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13699 | #endif |
| 13700 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13701 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13702 | {NULL, NULL} |
| 13703 | }; |
| 13704 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13705 | static PyObject * |
| 13706 | unicode_mod(PyObject *v, PyObject *w) |
| 13707 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13708 | if (!PyUnicode_Check(v)) |
| 13709 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13710 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13711 | } |
| 13712 | |
| 13713 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13714 | 0, /*nb_add*/ |
| 13715 | 0, /*nb_subtract*/ |
| 13716 | 0, /*nb_multiply*/ |
| 13717 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13718 | }; |
| 13719 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13720 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13721 | (lenfunc) unicode_length, /* sq_length */ |
| 13722 | PyUnicode_Concat, /* sq_concat */ |
| 13723 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13724 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13725 | 0, /* sq_slice */ |
| 13726 | 0, /* sq_ass_item */ |
| 13727 | 0, /* sq_ass_slice */ |
| 13728 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13729 | }; |
| 13730 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13731 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13732 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13733 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13734 | if (PyUnicode_READY(self) == -1) |
| 13735 | return NULL; |
| 13736 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13737 | if (PyIndex_Check(item)) { |
| 13738 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13739 | if (i == -1 && PyErr_Occurred()) |
| 13740 | return NULL; |
| 13741 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13742 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13743 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13744 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13745 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13746 | PyObject *result; |
| 13747 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13748 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13749 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13751 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13752 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13753 | return NULL; |
| 13754 | } |
| 13755 | |
| 13756 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13757 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13758 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13759 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13760 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13761 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13762 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13763 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13764 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13765 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13766 | src_kind = PyUnicode_KIND(self); |
| 13767 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13768 | if (!PyUnicode_IS_ASCII(self)) { |
| 13769 | kind_limit = kind_maxchar_limit(src_kind); |
| 13770 | max_char = 0; |
| 13771 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13772 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13773 | if (ch > max_char) { |
| 13774 | max_char = ch; |
| 13775 | if (max_char >= kind_limit) |
| 13776 | break; |
| 13777 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13778 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13779 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13780 | else |
| 13781 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13782 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13783 | if (result == NULL) |
| 13784 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13785 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13786 | dest_data = PyUnicode_DATA(result); |
| 13787 | |
| 13788 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13789 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13790 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13791 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13792 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13793 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13794 | } else { |
| 13795 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13796 | return NULL; |
| 13797 | } |
| 13798 | } |
| 13799 | |
| 13800 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13801 | (lenfunc)unicode_length, /* mp_length */ |
| 13802 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13803 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13804 | }; |
| 13805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13806 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13807 | /* Helpers for PyUnicode_Format() */ |
| 13808 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13809 | struct unicode_formatter_t { |
| 13810 | PyObject *args; |
| 13811 | int args_owned; |
| 13812 | Py_ssize_t arglen, argidx; |
| 13813 | PyObject *dict; |
| 13814 | |
| 13815 | enum PyUnicode_Kind fmtkind; |
| 13816 | Py_ssize_t fmtcnt, fmtpos; |
| 13817 | void *fmtdata; |
| 13818 | PyObject *fmtstr; |
| 13819 | |
| 13820 | _PyUnicodeWriter writer; |
| 13821 | }; |
| 13822 | |
| 13823 | struct unicode_format_arg_t { |
| 13824 | Py_UCS4 ch; |
| 13825 | int flags; |
| 13826 | Py_ssize_t width; |
| 13827 | int prec; |
| 13828 | int sign; |
| 13829 | }; |
| 13830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13831 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13832 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13833 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13834 | Py_ssize_t argidx = ctx->argidx; |
| 13835 | |
| 13836 | if (argidx < ctx->arglen) { |
| 13837 | ctx->argidx++; |
| 13838 | if (ctx->arglen < 0) |
| 13839 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13840 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13841 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13842 | } |
| 13843 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13844 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13845 | return NULL; |
| 13846 | } |
| 13847 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13848 | /* 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] | 13849 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13850 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13851 | otherwise. |
| 13852 | |
| 13853 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13854 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13855 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13856 | PyObject **p_output, |
| 13857 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13858 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13859 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13860 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13861 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13862 | int prec; |
| 13863 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13865 | x = PyFloat_AsDouble(v); |
| 13866 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13867 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13868 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13869 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13870 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13871 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13872 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13873 | if (arg->flags & F_ALT) |
| 13874 | dtoa_flags = Py_DTSF_ALT; |
| 13875 | else |
| 13876 | dtoa_flags = 0; |
| 13877 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13878 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13879 | return -1; |
| 13880 | len = strlen(p); |
| 13881 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13882 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13883 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13884 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13885 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13886 | } |
| 13887 | else |
| 13888 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13889 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13890 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13891 | } |
| 13892 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13893 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13894 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13895 | * Python's regular ints. |
| 13896 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13897 | * The output string is of the form |
| 13898 | * "-"? ("0x" | "0X")? digit+ |
| 13899 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13900 | * set in flags. The case of hex digits will be correct, |
| 13901 | * There will be at least prec digits, zero-filled on the left if |
| 13902 | * necessary to get that many. |
| 13903 | * val object to be converted |
| 13904 | * flags bitmask of format flags; only F_ALT is looked at |
| 13905 | * prec minimum number of digits; 0-fill on left if needed |
| 13906 | * type a character in [duoxX]; u acts the same as d |
| 13907 | * |
| 13908 | * CAUTION: o, x and X conversions on regular ints can never |
| 13909 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13910 | */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13911 | PyObject * |
| 13912 | _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13913 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13914 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13915 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13916 | Py_ssize_t i; |
| 13917 | int sign; /* 1 if '-', else 0 */ |
| 13918 | int len; /* number of characters */ |
| 13919 | Py_ssize_t llen; |
| 13920 | int numdigits; /* len == numnondigits + numdigits */ |
| 13921 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13922 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13923 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13924 | if (prec > INT_MAX-3) { |
| 13925 | PyErr_SetString(PyExc_OverflowError, |
| 13926 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13927 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13928 | } |
| 13929 | |
| 13930 | assert(PyLong_Check(val)); |
| 13931 | |
| 13932 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13933 | default: |
| 13934 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13935 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13936 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13937 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13938 | /* int and int subclasses should print numerically when a numeric */ |
| 13939 | /* format code is used (see issue18780) */ |
| 13940 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13941 | break; |
| 13942 | case 'o': |
| 13943 | numnondigits = 2; |
| 13944 | result = PyNumber_ToBase(val, 8); |
| 13945 | break; |
| 13946 | case 'x': |
| 13947 | case 'X': |
| 13948 | numnondigits = 2; |
| 13949 | result = PyNumber_ToBase(val, 16); |
| 13950 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13951 | } |
| 13952 | if (!result) |
| 13953 | return NULL; |
| 13954 | |
| 13955 | assert(unicode_modifiable(result)); |
| 13956 | assert(PyUnicode_IS_READY(result)); |
| 13957 | assert(PyUnicode_IS_ASCII(result)); |
| 13958 | |
| 13959 | /* To modify the string in-place, there can only be one reference. */ |
| 13960 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13961 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13962 | PyErr_BadInternalCall(); |
| 13963 | return NULL; |
| 13964 | } |
| 13965 | buf = PyUnicode_DATA(result); |
| 13966 | llen = PyUnicode_GET_LENGTH(result); |
| 13967 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13968 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13969 | PyErr_SetString(PyExc_ValueError, |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13970 | "string too large in _PyUnicode_FormatLong"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13971 | return NULL; |
| 13972 | } |
| 13973 | len = (int)llen; |
| 13974 | sign = buf[0] == '-'; |
| 13975 | numnondigits += sign; |
| 13976 | numdigits = len - numnondigits; |
| 13977 | assert(numdigits > 0); |
| 13978 | |
| 13979 | /* Get rid of base marker unless F_ALT */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13980 | if (((alt) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13981 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13982 | assert(buf[sign] == '0'); |
| 13983 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13984 | buf[sign+1] == 'o'); |
| 13985 | numnondigits -= 2; |
| 13986 | buf += 2; |
| 13987 | len -= 2; |
| 13988 | if (sign) |
| 13989 | buf[0] = '-'; |
| 13990 | assert(len == numnondigits + numdigits); |
| 13991 | assert(numdigits > 0); |
| 13992 | } |
| 13993 | |
| 13994 | /* Fill with leading zeroes to meet minimum width. */ |
| 13995 | if (prec > numdigits) { |
| 13996 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13997 | numnondigits + prec); |
| 13998 | char *b1; |
| 13999 | if (!r1) { |
| 14000 | Py_DECREF(result); |
| 14001 | return NULL; |
| 14002 | } |
| 14003 | b1 = PyBytes_AS_STRING(r1); |
| 14004 | for (i = 0; i < numnondigits; ++i) |
| 14005 | *b1++ = *buf++; |
| 14006 | for (i = 0; i < prec - numdigits; i++) |
| 14007 | *b1++ = '0'; |
| 14008 | for (i = 0; i < numdigits; i++) |
| 14009 | *b1++ = *buf++; |
| 14010 | *b1 = '\0'; |
| 14011 | Py_DECREF(result); |
| 14012 | result = r1; |
| 14013 | buf = PyBytes_AS_STRING(result); |
| 14014 | len = numnondigits + prec; |
| 14015 | } |
| 14016 | |
| 14017 | /* Fix up case for hex conversions. */ |
| 14018 | if (type == 'X') { |
| 14019 | /* Need to convert all lower case letters to upper case. |
| 14020 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 14021 | for (i = 0; i < len; i++) |
| 14022 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 14023 | buf[i] -= 'a'-'A'; |
| 14024 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14025 | if (!PyUnicode_Check(result) |
| 14026 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14027 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14028 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14029 | Py_DECREF(result); |
| 14030 | result = unicode; |
| 14031 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14032 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 14033 | if (PyUnicode_Resize(&result, len) < 0) |
| 14034 | Py_CLEAR(result); |
| 14035 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14036 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14037 | } |
| 14038 | |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14039 | /* Format an integer or a float as an integer. |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14040 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14041 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14042 | * -1 and raise an exception on error */ |
| 14043 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14044 | mainformatlong(PyObject *v, |
| 14045 | struct unicode_format_arg_t *arg, |
| 14046 | PyObject **p_output, |
| 14047 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14048 | { |
| 14049 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14050 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14051 | |
| 14052 | if (!PyNumber_Check(v)) |
| 14053 | goto wrongtype; |
| 14054 | |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14055 | /* make sure number is a type of integer for o, x, and X */ |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14056 | if (!PyLong_Check(v)) { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14057 | if (type == 'o' || type == 'x' || type == 'X') { |
| 14058 | iobj = PyNumber_Index(v); |
| 14059 | if (iobj == NULL) { |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14060 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14061 | goto wrongtype; |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14062 | return -1; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14063 | } |
| 14064 | } |
| 14065 | else { |
| 14066 | iobj = PyNumber_Long(v); |
| 14067 | if (iobj == NULL ) { |
| 14068 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14069 | goto wrongtype; |
| 14070 | return -1; |
| 14071 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14072 | } |
| 14073 | assert(PyLong_Check(iobj)); |
| 14074 | } |
| 14075 | else { |
| 14076 | iobj = v; |
| 14077 | Py_INCREF(iobj); |
| 14078 | } |
| 14079 | |
| 14080 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14081 | && arg->width == -1 && arg->prec == -1 |
| 14082 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14083 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14084 | { |
| 14085 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14086 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14087 | int base; |
| 14088 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14089 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14090 | { |
| 14091 | default: |
| 14092 | assert(0 && "'type' not in [diuoxX]"); |
| 14093 | case 'd': |
| 14094 | case 'i': |
| 14095 | case 'u': |
| 14096 | base = 10; |
| 14097 | break; |
| 14098 | case 'o': |
| 14099 | base = 8; |
| 14100 | break; |
| 14101 | case 'x': |
| 14102 | case 'X': |
| 14103 | base = 16; |
| 14104 | break; |
| 14105 | } |
| 14106 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14107 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14108 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14109 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14110 | } |
| 14111 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14112 | return 1; |
| 14113 | } |
| 14114 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14115 | res = _PyUnicode_FormatLong(iobj, arg->flags & F_ALT, arg->prec, type); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14116 | Py_DECREF(iobj); |
| 14117 | if (res == NULL) |
| 14118 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14119 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14120 | return 0; |
| 14121 | |
| 14122 | wrongtype: |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14123 | switch(type) |
| 14124 | { |
| 14125 | case 'o': |
| 14126 | case 'x': |
| 14127 | case 'X': |
| 14128 | PyErr_Format(PyExc_TypeError, |
| 14129 | "%%%c format: an integer is required, " |
| 14130 | "not %.200s", |
| 14131 | type, Py_TYPE(v)->tp_name); |
| 14132 | break; |
| 14133 | default: |
| 14134 | PyErr_Format(PyExc_TypeError, |
| 14135 | "%%%c format: a number is required, " |
| 14136 | "not %.200s", |
| 14137 | type, Py_TYPE(v)->tp_name); |
| 14138 | break; |
| 14139 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14140 | return -1; |
| 14141 | } |
| 14142 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14143 | static Py_UCS4 |
| 14144 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14145 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14146 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14147 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14148 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14149 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14150 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14151 | goto onError; |
| 14152 | } |
| 14153 | else { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14154 | PyObject *iobj; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14155 | long x; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14156 | /* make sure number is a type of integer */ |
| 14157 | if (!PyLong_Check(v)) { |
| 14158 | iobj = PyNumber_Index(v); |
| 14159 | if (iobj == NULL) { |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14160 | goto onError; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14161 | } |
| 14162 | v = iobj; |
| 14163 | Py_DECREF(iobj); |
| 14164 | } |
| 14165 | /* Integer input truncated to a character */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14166 | x = PyLong_AsLong(v); |
| 14167 | if (x == -1 && PyErr_Occurred()) |
| 14168 | goto onError; |
| 14169 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14170 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14171 | PyErr_SetString(PyExc_OverflowError, |
| 14172 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14173 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14174 | } |
| 14175 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14176 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14177 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14178 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14179 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14180 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14181 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14182 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14183 | } |
| 14184 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14185 | /* Parse options of an argument: flags, width, precision. |
| 14186 | Handle also "%(name)" syntax. |
| 14187 | |
| 14188 | Return 0 if the argument has been formatted into arg->str. |
| 14189 | Return 1 if the argument has been written into ctx->writer, |
| 14190 | Raise an exception and return -1 on error. */ |
| 14191 | static int |
| 14192 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14193 | struct unicode_format_arg_t *arg) |
| 14194 | { |
| 14195 | #define FORMAT_READ(ctx) \ |
| 14196 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14197 | |
| 14198 | PyObject *v; |
| 14199 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14200 | if (arg->ch == '(') { |
| 14201 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14202 | Py_ssize_t keystart; |
| 14203 | Py_ssize_t keylen; |
| 14204 | PyObject *key; |
| 14205 | int pcount = 1; |
| 14206 | |
| 14207 | if (ctx->dict == NULL) { |
| 14208 | PyErr_SetString(PyExc_TypeError, |
| 14209 | "format requires a mapping"); |
| 14210 | return -1; |
| 14211 | } |
| 14212 | ++ctx->fmtpos; |
| 14213 | --ctx->fmtcnt; |
| 14214 | keystart = ctx->fmtpos; |
| 14215 | /* Skip over balanced parentheses */ |
| 14216 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14217 | arg->ch = FORMAT_READ(ctx); |
| 14218 | if (arg->ch == ')') |
| 14219 | --pcount; |
| 14220 | else if (arg->ch == '(') |
| 14221 | ++pcount; |
| 14222 | ctx->fmtpos++; |
| 14223 | } |
| 14224 | keylen = ctx->fmtpos - keystart - 1; |
| 14225 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14226 | PyErr_SetString(PyExc_ValueError, |
| 14227 | "incomplete format key"); |
| 14228 | return -1; |
| 14229 | } |
| 14230 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14231 | keystart, keystart + keylen); |
| 14232 | if (key == NULL) |
| 14233 | return -1; |
| 14234 | if (ctx->args_owned) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14235 | ctx->args_owned = 0; |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 14236 | Py_DECREF(ctx->args); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14237 | } |
| 14238 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14239 | Py_DECREF(key); |
| 14240 | if (ctx->args == NULL) |
| 14241 | return -1; |
| 14242 | ctx->args_owned = 1; |
| 14243 | ctx->arglen = -1; |
| 14244 | ctx->argidx = -2; |
| 14245 | } |
| 14246 | |
| 14247 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14248 | while (--ctx->fmtcnt >= 0) { |
| 14249 | arg->ch = FORMAT_READ(ctx); |
| 14250 | ctx->fmtpos++; |
| 14251 | switch (arg->ch) { |
| 14252 | case '-': arg->flags |= F_LJUST; continue; |
| 14253 | case '+': arg->flags |= F_SIGN; continue; |
| 14254 | case ' ': arg->flags |= F_BLANK; continue; |
| 14255 | case '#': arg->flags |= F_ALT; continue; |
| 14256 | case '0': arg->flags |= F_ZERO; continue; |
| 14257 | } |
| 14258 | break; |
| 14259 | } |
| 14260 | |
| 14261 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14262 | if (arg->ch == '*') { |
| 14263 | v = unicode_format_getnextarg(ctx); |
| 14264 | if (v == NULL) |
| 14265 | return -1; |
| 14266 | if (!PyLong_Check(v)) { |
| 14267 | PyErr_SetString(PyExc_TypeError, |
| 14268 | "* wants int"); |
| 14269 | return -1; |
| 14270 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14271 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14272 | if (arg->width == -1 && PyErr_Occurred()) |
| 14273 | return -1; |
| 14274 | if (arg->width < 0) { |
| 14275 | arg->flags |= F_LJUST; |
| 14276 | arg->width = -arg->width; |
| 14277 | } |
| 14278 | if (--ctx->fmtcnt >= 0) { |
| 14279 | arg->ch = FORMAT_READ(ctx); |
| 14280 | ctx->fmtpos++; |
| 14281 | } |
| 14282 | } |
| 14283 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14284 | arg->width = arg->ch - '0'; |
| 14285 | while (--ctx->fmtcnt >= 0) { |
| 14286 | arg->ch = FORMAT_READ(ctx); |
| 14287 | ctx->fmtpos++; |
| 14288 | if (arg->ch < '0' || arg->ch > '9') |
| 14289 | break; |
| 14290 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14291 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14292 | '0' and '9', casting to int is safe. */ |
| 14293 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14294 | PyErr_SetString(PyExc_ValueError, |
| 14295 | "width too big"); |
| 14296 | return -1; |
| 14297 | } |
| 14298 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14299 | } |
| 14300 | } |
| 14301 | |
| 14302 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14303 | if (arg->ch == '.') { |
| 14304 | arg->prec = 0; |
| 14305 | if (--ctx->fmtcnt >= 0) { |
| 14306 | arg->ch = FORMAT_READ(ctx); |
| 14307 | ctx->fmtpos++; |
| 14308 | } |
| 14309 | if (arg->ch == '*') { |
| 14310 | v = unicode_format_getnextarg(ctx); |
| 14311 | if (v == NULL) |
| 14312 | return -1; |
| 14313 | if (!PyLong_Check(v)) { |
| 14314 | PyErr_SetString(PyExc_TypeError, |
| 14315 | "* wants int"); |
| 14316 | return -1; |
| 14317 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14318 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14319 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14320 | return -1; |
| 14321 | if (arg->prec < 0) |
| 14322 | arg->prec = 0; |
| 14323 | if (--ctx->fmtcnt >= 0) { |
| 14324 | arg->ch = FORMAT_READ(ctx); |
| 14325 | ctx->fmtpos++; |
| 14326 | } |
| 14327 | } |
| 14328 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14329 | arg->prec = arg->ch - '0'; |
| 14330 | while (--ctx->fmtcnt >= 0) { |
| 14331 | arg->ch = FORMAT_READ(ctx); |
| 14332 | ctx->fmtpos++; |
| 14333 | if (arg->ch < '0' || arg->ch > '9') |
| 14334 | break; |
| 14335 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14336 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14337 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14338 | return -1; |
| 14339 | } |
| 14340 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14341 | } |
| 14342 | } |
| 14343 | } |
| 14344 | |
| 14345 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14346 | if (ctx->fmtcnt >= 0) { |
| 14347 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14348 | if (--ctx->fmtcnt >= 0) { |
| 14349 | arg->ch = FORMAT_READ(ctx); |
| 14350 | ctx->fmtpos++; |
| 14351 | } |
| 14352 | } |
| 14353 | } |
| 14354 | if (ctx->fmtcnt < 0) { |
| 14355 | PyErr_SetString(PyExc_ValueError, |
| 14356 | "incomplete format"); |
| 14357 | return -1; |
| 14358 | } |
| 14359 | return 0; |
| 14360 | |
| 14361 | #undef FORMAT_READ |
| 14362 | } |
| 14363 | |
| 14364 | /* Format one argument. Supported conversion specifiers: |
| 14365 | |
| 14366 | - "s", "r", "a": any type |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14367 | - "i", "d", "u": int or float |
| 14368 | - "o", "x", "X": int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14369 | - "e", "E", "f", "F", "g", "G": float |
| 14370 | - "c": int or str (1 character) |
| 14371 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14372 | When possible, the output is written directly into the Unicode writer |
| 14373 | (ctx->writer). A string is created when padding is required. |
| 14374 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14375 | Return 0 if the argument has been formatted into *p_str, |
| 14376 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14377 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14378 | static int |
| 14379 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14380 | struct unicode_format_arg_t *arg, |
| 14381 | PyObject **p_str) |
| 14382 | { |
| 14383 | PyObject *v; |
| 14384 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14385 | |
| 14386 | if (ctx->fmtcnt == 0) |
| 14387 | ctx->writer.overallocate = 0; |
| 14388 | |
| 14389 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14390 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14391 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14392 | return 1; |
| 14393 | } |
| 14394 | |
| 14395 | v = unicode_format_getnextarg(ctx); |
| 14396 | if (v == NULL) |
| 14397 | return -1; |
| 14398 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14399 | |
| 14400 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14401 | case 's': |
| 14402 | case 'r': |
| 14403 | case 'a': |
| 14404 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14405 | /* Fast path */ |
| 14406 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14407 | return -1; |
| 14408 | return 1; |
| 14409 | } |
| 14410 | |
| 14411 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14412 | *p_str = v; |
| 14413 | Py_INCREF(*p_str); |
| 14414 | } |
| 14415 | else { |
| 14416 | if (arg->ch == 's') |
| 14417 | *p_str = PyObject_Str(v); |
| 14418 | else if (arg->ch == 'r') |
| 14419 | *p_str = PyObject_Repr(v); |
| 14420 | else |
| 14421 | *p_str = PyObject_ASCII(v); |
| 14422 | } |
| 14423 | break; |
| 14424 | |
| 14425 | case 'i': |
| 14426 | case 'd': |
| 14427 | case 'u': |
| 14428 | case 'o': |
| 14429 | case 'x': |
| 14430 | case 'X': |
| 14431 | { |
| 14432 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14433 | if (ret != 0) |
| 14434 | return ret; |
| 14435 | arg->sign = 1; |
| 14436 | break; |
| 14437 | } |
| 14438 | |
| 14439 | case 'e': |
| 14440 | case 'E': |
| 14441 | case 'f': |
| 14442 | case 'F': |
| 14443 | case 'g': |
| 14444 | case 'G': |
| 14445 | if (arg->width == -1 && arg->prec == -1 |
| 14446 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14447 | { |
| 14448 | /* Fast path */ |
| 14449 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14450 | return -1; |
| 14451 | return 1; |
| 14452 | } |
| 14453 | |
| 14454 | arg->sign = 1; |
| 14455 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14456 | return -1; |
| 14457 | break; |
| 14458 | |
| 14459 | case 'c': |
| 14460 | { |
| 14461 | Py_UCS4 ch = formatchar(v); |
| 14462 | if (ch == (Py_UCS4) -1) |
| 14463 | return -1; |
| 14464 | if (arg->width == -1 && arg->prec == -1) { |
| 14465 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14466 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14467 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14468 | return 1; |
| 14469 | } |
| 14470 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14471 | break; |
| 14472 | } |
| 14473 | |
| 14474 | default: |
| 14475 | PyErr_Format(PyExc_ValueError, |
| 14476 | "unsupported format character '%c' (0x%x) " |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 14477 | "at index %zd", |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14478 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14479 | (int)arg->ch, |
| 14480 | ctx->fmtpos - 1); |
| 14481 | return -1; |
| 14482 | } |
| 14483 | if (*p_str == NULL) |
| 14484 | return -1; |
| 14485 | assert (PyUnicode_Check(*p_str)); |
| 14486 | return 0; |
| 14487 | } |
| 14488 | |
| 14489 | static int |
| 14490 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14491 | struct unicode_format_arg_t *arg, |
| 14492 | PyObject *str) |
| 14493 | { |
| 14494 | Py_ssize_t len; |
| 14495 | enum PyUnicode_Kind kind; |
| 14496 | void *pbuf; |
| 14497 | Py_ssize_t pindex; |
| 14498 | Py_UCS4 signchar; |
| 14499 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14500 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14501 | Py_ssize_t sublen; |
| 14502 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14503 | Py_UCS4 fill; |
| 14504 | |
| 14505 | fill = ' '; |
| 14506 | if (arg->sign && arg->flags & F_ZERO) |
| 14507 | fill = '0'; |
| 14508 | |
| 14509 | if (PyUnicode_READY(str) == -1) |
| 14510 | return -1; |
| 14511 | |
| 14512 | len = PyUnicode_GET_LENGTH(str); |
| 14513 | if ((arg->width == -1 || arg->width <= len) |
| 14514 | && (arg->prec == -1 || arg->prec >= len) |
| 14515 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14516 | { |
| 14517 | /* Fast path */ |
| 14518 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14519 | return -1; |
| 14520 | return 0; |
| 14521 | } |
| 14522 | |
| 14523 | /* Truncate the string for "s", "r" and "a" formats |
| 14524 | if the precision is set */ |
| 14525 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14526 | if (arg->prec >= 0 && len > arg->prec) |
| 14527 | len = arg->prec; |
| 14528 | } |
| 14529 | |
| 14530 | /* Adjust sign and width */ |
| 14531 | kind = PyUnicode_KIND(str); |
| 14532 | pbuf = PyUnicode_DATA(str); |
| 14533 | pindex = 0; |
| 14534 | signchar = '\0'; |
| 14535 | if (arg->sign) { |
| 14536 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14537 | if (ch == '-' || ch == '+') { |
| 14538 | signchar = ch; |
| 14539 | len--; |
| 14540 | pindex++; |
| 14541 | } |
| 14542 | else if (arg->flags & F_SIGN) |
| 14543 | signchar = '+'; |
| 14544 | else if (arg->flags & F_BLANK) |
| 14545 | signchar = ' '; |
| 14546 | else |
| 14547 | arg->sign = 0; |
| 14548 | } |
| 14549 | if (arg->width < len) |
| 14550 | arg->width = len; |
| 14551 | |
| 14552 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14553 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14554 | if (!(arg->flags & F_LJUST)) { |
| 14555 | if (arg->sign) { |
| 14556 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14557 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14558 | } |
| 14559 | else { |
| 14560 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14561 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14562 | } |
| 14563 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14564 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14565 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14566 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14567 | } |
| 14568 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14569 | buflen = arg->width; |
| 14570 | if (arg->sign && len == arg->width) |
| 14571 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14572 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14573 | return -1; |
| 14574 | |
| 14575 | /* Write the sign if needed */ |
| 14576 | if (arg->sign) { |
| 14577 | if (fill != ' ') { |
| 14578 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14579 | writer->pos += 1; |
| 14580 | } |
| 14581 | if (arg->width > len) |
| 14582 | arg->width--; |
| 14583 | } |
| 14584 | |
| 14585 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14586 | if the alternate form is used. |
| 14587 | For example, write "0x" for the "%#x" format. */ |
| 14588 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14589 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14590 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14591 | if (fill != ' ') { |
| 14592 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14593 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14594 | writer->pos += 2; |
| 14595 | pindex += 2; |
| 14596 | } |
| 14597 | arg->width -= 2; |
| 14598 | if (arg->width < 0) |
| 14599 | arg->width = 0; |
| 14600 | len -= 2; |
| 14601 | } |
| 14602 | |
| 14603 | /* Pad left with the fill character if needed */ |
| 14604 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14605 | sublen = arg->width - len; |
| 14606 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14607 | writer->pos += sublen; |
| 14608 | arg->width = len; |
| 14609 | } |
| 14610 | |
| 14611 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14612 | the alternate form is used */ |
| 14613 | if (fill == ' ') { |
| 14614 | if (arg->sign) { |
| 14615 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14616 | writer->pos += 1; |
| 14617 | } |
| 14618 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14619 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14620 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14621 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14622 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14623 | writer->pos += 2; |
| 14624 | pindex += 2; |
| 14625 | } |
| 14626 | } |
| 14627 | |
| 14628 | /* Write characters */ |
| 14629 | if (len) { |
| 14630 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14631 | str, pindex, len); |
| 14632 | writer->pos += len; |
| 14633 | } |
| 14634 | |
| 14635 | /* Pad right with the fill character if needed */ |
| 14636 | if (arg->width > len) { |
| 14637 | sublen = arg->width - len; |
| 14638 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14639 | writer->pos += sublen; |
| 14640 | } |
| 14641 | return 0; |
| 14642 | } |
| 14643 | |
| 14644 | /* Helper of PyUnicode_Format(): format one arg. |
| 14645 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14646 | static int |
| 14647 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14648 | { |
| 14649 | struct unicode_format_arg_t arg; |
| 14650 | PyObject *str; |
| 14651 | int ret; |
| 14652 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14653 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14654 | arg.flags = 0; |
| 14655 | arg.width = -1; |
| 14656 | arg.prec = -1; |
| 14657 | arg.sign = 0; |
| 14658 | str = NULL; |
| 14659 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14660 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14661 | if (ret == -1) |
| 14662 | return -1; |
| 14663 | |
| 14664 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14665 | if (ret == -1) |
| 14666 | return -1; |
| 14667 | |
| 14668 | if (ret != 1) { |
| 14669 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14670 | Py_DECREF(str); |
| 14671 | if (ret == -1) |
| 14672 | return -1; |
| 14673 | } |
| 14674 | |
| 14675 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14676 | PyErr_SetString(PyExc_TypeError, |
| 14677 | "not all arguments converted during string formatting"); |
| 14678 | return -1; |
| 14679 | } |
| 14680 | return 0; |
| 14681 | } |
| 14682 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14683 | PyObject * |
| 14684 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14685 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14686 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14688 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14689 | PyErr_BadInternalCall(); |
| 14690 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14691 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14692 | |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 14693 | if (ensure_unicode(format) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14694 | return NULL; |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 14695 | |
| 14696 | ctx.fmtstr = format; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14697 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14698 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14699 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14700 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14701 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14702 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14703 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14704 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14705 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14706 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14707 | ctx.arglen = PyTuple_Size(args); |
| 14708 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14709 | } |
| 14710 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14711 | ctx.arglen = -1; |
| 14712 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14713 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14714 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14715 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14716 | ctx.dict = args; |
| 14717 | else |
| 14718 | ctx.dict = NULL; |
| 14719 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14720 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14721 | while (--ctx.fmtcnt >= 0) { |
| 14722 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14723 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14724 | |
| 14725 | nonfmtpos = ctx.fmtpos++; |
| 14726 | while (ctx.fmtcnt >= 0 && |
| 14727 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14728 | ctx.fmtpos++; |
| 14729 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14730 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14731 | if (ctx.fmtcnt < 0) { |
| 14732 | ctx.fmtpos--; |
| 14733 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14734 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14735 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14736 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14737 | nonfmtpos, ctx.fmtpos) < 0) |
| 14738 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14739 | } |
| 14740 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14741 | ctx.fmtpos++; |
| 14742 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14743 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14744 | } |
| 14745 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14746 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14747 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14748 | PyErr_SetString(PyExc_TypeError, |
| 14749 | "not all arguments converted during string formatting"); |
| 14750 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14751 | } |
| 14752 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14753 | if (ctx.args_owned) { |
| 14754 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14755 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14756 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14757 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14758 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14759 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14760 | if (ctx.args_owned) { |
| 14761 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14762 | } |
| 14763 | return NULL; |
| 14764 | } |
| 14765 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14766 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14767 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14768 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14769 | static PyObject * |
| 14770 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14771 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14772 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14773 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14774 | char *encoding = NULL; |
| 14775 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14776 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14777 | if (type != &PyUnicode_Type) |
| 14778 | return unicode_subtype_new(type, args, kwds); |
| 14779 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14780 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14781 | return NULL; |
| 14782 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14783 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14784 | if (encoding == NULL && errors == NULL) |
| 14785 | return PyObject_Str(x); |
| 14786 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14787 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14788 | } |
| 14789 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14790 | static PyObject * |
| 14791 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14792 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14793 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14794 | Py_ssize_t length, char_size; |
| 14795 | int share_wstr, share_utf8; |
| 14796 | unsigned int kind; |
| 14797 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14798 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14799 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14800 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14801 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14802 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14803 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14804 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14805 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14806 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14807 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14808 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14809 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14810 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14811 | if (self == NULL) { |
| 14812 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14813 | return NULL; |
| 14814 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14815 | kind = PyUnicode_KIND(unicode); |
| 14816 | length = PyUnicode_GET_LENGTH(unicode); |
| 14817 | |
| 14818 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14819 | #ifdef Py_DEBUG |
| 14820 | _PyUnicode_HASH(self) = -1; |
| 14821 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14822 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14823 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14824 | _PyUnicode_STATE(self).interned = 0; |
| 14825 | _PyUnicode_STATE(self).kind = kind; |
| 14826 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14827 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14828 | _PyUnicode_STATE(self).ready = 1; |
| 14829 | _PyUnicode_WSTR(self) = NULL; |
| 14830 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14831 | _PyUnicode_UTF8(self) = NULL; |
| 14832 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14833 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14834 | |
| 14835 | share_utf8 = 0; |
| 14836 | share_wstr = 0; |
| 14837 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14838 | char_size = 1; |
| 14839 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14840 | share_utf8 = 1; |
| 14841 | } |
| 14842 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14843 | char_size = 2; |
| 14844 | if (sizeof(wchar_t) == 2) |
| 14845 | share_wstr = 1; |
| 14846 | } |
| 14847 | else { |
| 14848 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14849 | char_size = 4; |
| 14850 | if (sizeof(wchar_t) == 4) |
| 14851 | share_wstr = 1; |
| 14852 | } |
| 14853 | |
| 14854 | /* Ensure we won't overflow the length. */ |
| 14855 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14856 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14857 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14858 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14859 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14860 | if (data == NULL) { |
| 14861 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14862 | goto onError; |
| 14863 | } |
| 14864 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14865 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14866 | if (share_utf8) { |
| 14867 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14868 | _PyUnicode_UTF8(self) = data; |
| 14869 | } |
| 14870 | if (share_wstr) { |
| 14871 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14872 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14873 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14874 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14875 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14876 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14877 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14878 | #ifdef Py_DEBUG |
| 14879 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14880 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14881 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14882 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14883 | |
| 14884 | onError: |
| 14885 | Py_DECREF(unicode); |
| 14886 | Py_DECREF(self); |
| 14887 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14888 | } |
| 14889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14890 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14891 | "str(object='') -> str\n\ |
| 14892 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14893 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14894 | Create a new string object from the given object. If encoding or\n\ |
| 14895 | errors is specified, then the object must expose a data buffer\n\ |
| 14896 | that will be decoded using the given encoding and error handler.\n\ |
| 14897 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14898 | or repr(object).\n\ |
| 14899 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14900 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14901 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14902 | static PyObject *unicode_iter(PyObject *seq); |
| 14903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14904 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14905 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14906 | "str", /* tp_name */ |
| 14907 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14908 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14909 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14910 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14911 | 0, /* tp_print */ |
| 14912 | 0, /* tp_getattr */ |
| 14913 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14914 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14915 | unicode_repr, /* tp_repr */ |
| 14916 | &unicode_as_number, /* tp_as_number */ |
| 14917 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14918 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14919 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14920 | 0, /* tp_call*/ |
| 14921 | (reprfunc) unicode_str, /* tp_str */ |
| 14922 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14923 | 0, /* tp_setattro */ |
| 14924 | 0, /* tp_as_buffer */ |
| 14925 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14926 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14927 | unicode_doc, /* tp_doc */ |
| 14928 | 0, /* tp_traverse */ |
| 14929 | 0, /* tp_clear */ |
| 14930 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14931 | 0, /* tp_weaklistoffset */ |
| 14932 | unicode_iter, /* tp_iter */ |
| 14933 | 0, /* tp_iternext */ |
| 14934 | unicode_methods, /* tp_methods */ |
| 14935 | 0, /* tp_members */ |
| 14936 | 0, /* tp_getset */ |
| 14937 | &PyBaseObject_Type, /* tp_base */ |
| 14938 | 0, /* tp_dict */ |
| 14939 | 0, /* tp_descr_get */ |
| 14940 | 0, /* tp_descr_set */ |
| 14941 | 0, /* tp_dictoffset */ |
| 14942 | 0, /* tp_init */ |
| 14943 | 0, /* tp_alloc */ |
| 14944 | unicode_new, /* tp_new */ |
| 14945 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14946 | }; |
| 14947 | |
| 14948 | /* Initialize the Unicode implementation */ |
| 14949 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14950 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14951 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14952 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14953 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14954 | 0x000A, /* LINE FEED */ |
| 14955 | 0x000D, /* CARRIAGE RETURN */ |
| 14956 | 0x001C, /* FILE SEPARATOR */ |
| 14957 | 0x001D, /* GROUP SEPARATOR */ |
| 14958 | 0x001E, /* RECORD SEPARATOR */ |
| 14959 | 0x0085, /* NEXT LINE */ |
| 14960 | 0x2028, /* LINE SEPARATOR */ |
| 14961 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14962 | }; |
| 14963 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14964 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14965 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14966 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14967 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14968 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14969 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14970 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14971 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14972 | |
| 14973 | /* initialize the linebreak bloom filter */ |
| 14974 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14975 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14976 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14977 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14978 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14979 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14980 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14981 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14982 | Py_FatalError("Can't initialize field name iterator type"); |
| 14983 | |
| 14984 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14985 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14986 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14987 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14988 | } |
| 14989 | |
| 14990 | /* Finalize the Unicode implementation */ |
| 14991 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14992 | int |
| 14993 | PyUnicode_ClearFreeList(void) |
| 14994 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14995 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14996 | } |
| 14997 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14998 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14999 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15000 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 15001 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15002 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15003 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 15004 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15005 | for (i = 0; i < 256; i++) |
| 15006 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 15007 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15008 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15009 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 15010 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15011 | void |
| 15012 | PyUnicode_InternInPlace(PyObject **p) |
| 15013 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15014 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15015 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15016 | #ifdef Py_DEBUG |
| 15017 | assert(s != NULL); |
| 15018 | assert(_PyUnicode_CHECK(s)); |
| 15019 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15020 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15021 | return; |
| 15022 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15023 | /* If it's a subclass, we don't really know what putting |
| 15024 | it in the interned dict might do. */ |
| 15025 | if (!PyUnicode_CheckExact(s)) |
| 15026 | return; |
| 15027 | if (PyUnicode_CHECK_INTERNED(s)) |
| 15028 | return; |
| 15029 | if (interned == NULL) { |
| 15030 | interned = PyDict_New(); |
| 15031 | if (interned == NULL) { |
| 15032 | PyErr_Clear(); /* Don't leave an exception */ |
| 15033 | return; |
| 15034 | } |
| 15035 | } |
| 15036 | /* It might be that the GetItem call fails even |
| 15037 | though the key is present in the dictionary, |
| 15038 | namely when this happens during a stack overflow. */ |
| 15039 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15040 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15041 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15042 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15043 | if (t) { |
| 15044 | Py_INCREF(t); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 15045 | Py_SETREF(*p, t); |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15046 | return; |
| 15047 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15048 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15049 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15050 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15051 | PyErr_Clear(); |
| 15052 | PyThreadState_GET()->recursion_critical = 0; |
| 15053 | return; |
| 15054 | } |
| 15055 | PyThreadState_GET()->recursion_critical = 0; |
| 15056 | /* The two references in interned are not counted by refcnt. |
| 15057 | The deallocator will take care of this */ |
| 15058 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15059 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15060 | } |
| 15061 | |
| 15062 | void |
| 15063 | PyUnicode_InternImmortal(PyObject **p) |
| 15064 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15065 | PyUnicode_InternInPlace(p); |
| 15066 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 15067 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15068 | Py_INCREF(*p); |
| 15069 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15070 | } |
| 15071 | |
| 15072 | PyObject * |
| 15073 | PyUnicode_InternFromString(const char *cp) |
| 15074 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15075 | PyObject *s = PyUnicode_FromString(cp); |
| 15076 | if (s == NULL) |
| 15077 | return NULL; |
| 15078 | PyUnicode_InternInPlace(&s); |
| 15079 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15080 | } |
| 15081 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15082 | void |
| 15083 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15084 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15085 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15086 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15087 | Py_ssize_t i, n; |
| 15088 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15089 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15090 | if (interned == NULL || !PyDict_Check(interned)) |
| 15091 | return; |
| 15092 | keys = PyDict_Keys(interned); |
| 15093 | if (keys == NULL || !PyList_Check(keys)) { |
| 15094 | PyErr_Clear(); |
| 15095 | return; |
| 15096 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15097 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15098 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15099 | detector, interned unicode strings are not forcibly deallocated; |
| 15100 | rather, we give them their stolen references back, and then clear |
| 15101 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15102 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15103 | n = PyList_GET_SIZE(keys); |
| 15104 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15105 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15106 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15107 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15108 | if (PyUnicode_READY(s) == -1) { |
| 15109 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15110 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15111 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15112 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15113 | case SSTATE_NOT_INTERNED: |
| 15114 | /* XXX Shouldn't happen */ |
| 15115 | break; |
| 15116 | case SSTATE_INTERNED_IMMORTAL: |
| 15117 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15118 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15119 | break; |
| 15120 | case SSTATE_INTERNED_MORTAL: |
| 15121 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15122 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15123 | break; |
| 15124 | default: |
| 15125 | Py_FatalError("Inconsistent interned string state."); |
| 15126 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15127 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15128 | } |
| 15129 | fprintf(stderr, "total size of all interned strings: " |
| 15130 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15131 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15132 | Py_DECREF(keys); |
| 15133 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15134 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15135 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15136 | |
| 15137 | |
| 15138 | /********************* Unicode Iterator **************************/ |
| 15139 | |
| 15140 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15141 | PyObject_HEAD |
| 15142 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15143 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15144 | } unicodeiterobject; |
| 15145 | |
| 15146 | static void |
| 15147 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15148 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15149 | _PyObject_GC_UNTRACK(it); |
| 15150 | Py_XDECREF(it->it_seq); |
| 15151 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15152 | } |
| 15153 | |
| 15154 | static int |
| 15155 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15156 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15157 | Py_VISIT(it->it_seq); |
| 15158 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15159 | } |
| 15160 | |
| 15161 | static PyObject * |
| 15162 | unicodeiter_next(unicodeiterobject *it) |
| 15163 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15164 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15165 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15166 | assert(it != NULL); |
| 15167 | seq = it->it_seq; |
| 15168 | if (seq == NULL) |
| 15169 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15170 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15172 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15173 | int kind = PyUnicode_KIND(seq); |
| 15174 | void *data = PyUnicode_DATA(seq); |
| 15175 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15176 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15177 | if (item != NULL) |
| 15178 | ++it->it_index; |
| 15179 | return item; |
| 15180 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15181 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15182 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 15183 | Py_DECREF(seq); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15184 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15185 | } |
| 15186 | |
| 15187 | static PyObject * |
| 15188 | unicodeiter_len(unicodeiterobject *it) |
| 15189 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15190 | Py_ssize_t len = 0; |
| 15191 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15192 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15193 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15194 | } |
| 15195 | |
| 15196 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15197 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15198 | static PyObject * |
| 15199 | unicodeiter_reduce(unicodeiterobject *it) |
| 15200 | { |
| 15201 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15202 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15203 | it->it_seq, it->it_index); |
| 15204 | } else { |
| 15205 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15206 | if (u == NULL) |
| 15207 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15208 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15209 | } |
| 15210 | } |
| 15211 | |
| 15212 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15213 | |
| 15214 | static PyObject * |
| 15215 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15216 | { |
| 15217 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15218 | if (index == -1 && PyErr_Occurred()) |
| 15219 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 15220 | if (it->it_seq != NULL) { |
| 15221 | if (index < 0) |
| 15222 | index = 0; |
| 15223 | else if (index > PyUnicode_GET_LENGTH(it->it_seq)) |
| 15224 | index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */ |
| 15225 | it->it_index = index; |
| 15226 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15227 | Py_RETURN_NONE; |
| 15228 | } |
| 15229 | |
| 15230 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15231 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15232 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15233 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15234 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15235 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15236 | reduce_doc}, |
| 15237 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15238 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15239 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15240 | }; |
| 15241 | |
| 15242 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15243 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15244 | "str_iterator", /* tp_name */ |
| 15245 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15246 | 0, /* tp_itemsize */ |
| 15247 | /* methods */ |
| 15248 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15249 | 0, /* tp_print */ |
| 15250 | 0, /* tp_getattr */ |
| 15251 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15252 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15253 | 0, /* tp_repr */ |
| 15254 | 0, /* tp_as_number */ |
| 15255 | 0, /* tp_as_sequence */ |
| 15256 | 0, /* tp_as_mapping */ |
| 15257 | 0, /* tp_hash */ |
| 15258 | 0, /* tp_call */ |
| 15259 | 0, /* tp_str */ |
| 15260 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15261 | 0, /* tp_setattro */ |
| 15262 | 0, /* tp_as_buffer */ |
| 15263 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15264 | 0, /* tp_doc */ |
| 15265 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15266 | 0, /* tp_clear */ |
| 15267 | 0, /* tp_richcompare */ |
| 15268 | 0, /* tp_weaklistoffset */ |
| 15269 | PyObject_SelfIter, /* tp_iter */ |
| 15270 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15271 | unicodeiter_methods, /* tp_methods */ |
| 15272 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15273 | }; |
| 15274 | |
| 15275 | static PyObject * |
| 15276 | unicode_iter(PyObject *seq) |
| 15277 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15278 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15279 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15280 | if (!PyUnicode_Check(seq)) { |
| 15281 | PyErr_BadInternalCall(); |
| 15282 | return NULL; |
| 15283 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15284 | if (PyUnicode_READY(seq) == -1) |
| 15285 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15286 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15287 | if (it == NULL) |
| 15288 | return NULL; |
| 15289 | it->it_index = 0; |
| 15290 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15291 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15292 | _PyObject_GC_TRACK(it); |
| 15293 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15294 | } |
| 15295 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15296 | |
| 15297 | size_t |
| 15298 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15299 | { |
| 15300 | int res = 0; |
| 15301 | while(*u++) |
| 15302 | res++; |
| 15303 | return res; |
| 15304 | } |
| 15305 | |
| 15306 | Py_UNICODE* |
| 15307 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15308 | { |
| 15309 | Py_UNICODE *u = s1; |
| 15310 | while ((*u++ = *s2++)); |
| 15311 | return s1; |
| 15312 | } |
| 15313 | |
| 15314 | Py_UNICODE* |
| 15315 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15316 | { |
| 15317 | Py_UNICODE *u = s1; |
| 15318 | while ((*u++ = *s2++)) |
| 15319 | if (n-- == 0) |
| 15320 | break; |
| 15321 | return s1; |
| 15322 | } |
| 15323 | |
| 15324 | Py_UNICODE* |
| 15325 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15326 | { |
| 15327 | Py_UNICODE *u1 = s1; |
| 15328 | u1 += Py_UNICODE_strlen(u1); |
| 15329 | Py_UNICODE_strcpy(u1, s2); |
| 15330 | return s1; |
| 15331 | } |
| 15332 | |
| 15333 | int |
| 15334 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15335 | { |
| 15336 | while (*s1 && *s2 && *s1 == *s2) |
| 15337 | s1++, s2++; |
| 15338 | if (*s1 && *s2) |
| 15339 | return (*s1 < *s2) ? -1 : +1; |
| 15340 | if (*s1) |
| 15341 | return 1; |
| 15342 | if (*s2) |
| 15343 | return -1; |
| 15344 | return 0; |
| 15345 | } |
| 15346 | |
| 15347 | int |
| 15348 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15349 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15350 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15351 | for (; n != 0; n--) { |
| 15352 | u1 = *s1; |
| 15353 | u2 = *s2; |
| 15354 | if (u1 != u2) |
| 15355 | return (u1 < u2) ? -1 : +1; |
| 15356 | if (u1 == '\0') |
| 15357 | return 0; |
| 15358 | s1++; |
| 15359 | s2++; |
| 15360 | } |
| 15361 | return 0; |
| 15362 | } |
| 15363 | |
| 15364 | Py_UNICODE* |
| 15365 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15366 | { |
| 15367 | const Py_UNICODE *p; |
| 15368 | for (p = s; *p; p++) |
| 15369 | if (*p == c) |
| 15370 | return (Py_UNICODE*)p; |
| 15371 | return NULL; |
| 15372 | } |
| 15373 | |
| 15374 | Py_UNICODE* |
| 15375 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15376 | { |
| 15377 | const Py_UNICODE *p; |
| 15378 | p = s + Py_UNICODE_strlen(s); |
| 15379 | while (p != s) { |
| 15380 | p--; |
| 15381 | if (*p == c) |
| 15382 | return (Py_UNICODE*)p; |
| 15383 | } |
| 15384 | return NULL; |
| 15385 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15386 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15387 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15388 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15389 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15390 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15391 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15392 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15393 | if (!PyUnicode_Check(unicode)) { |
| 15394 | PyErr_BadArgument(); |
| 15395 | return NULL; |
| 15396 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15397 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15398 | if (u == NULL) |
| 15399 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15400 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 15401 | if (len > ((PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15402 | PyErr_NoMemory(); |
| 15403 | return NULL; |
| 15404 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15405 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15406 | size *= sizeof(Py_UNICODE); |
| 15407 | copy = PyMem_Malloc(size); |
| 15408 | if (copy == NULL) { |
| 15409 | PyErr_NoMemory(); |
| 15410 | return NULL; |
| 15411 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15412 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15413 | return copy; |
| 15414 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15415 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15416 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15417 | to the string.Formatter class implemented in Python. */ |
| 15418 | |
| 15419 | static PyMethodDef _string_methods[] = { |
| 15420 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15421 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15422 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15423 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15424 | {NULL, NULL} |
| 15425 | }; |
| 15426 | |
| 15427 | static struct PyModuleDef _string_module = { |
| 15428 | PyModuleDef_HEAD_INIT, |
| 15429 | "_string", |
| 15430 | PyDoc_STR("string helper module"), |
| 15431 | 0, |
| 15432 | _string_methods, |
| 15433 | NULL, |
| 15434 | NULL, |
| 15435 | NULL, |
| 15436 | NULL |
| 15437 | }; |
| 15438 | |
| 15439 | PyMODINIT_FUNC |
| 15440 | PyInit__string(void) |
| 15441 | { |
| 15442 | return PyModule_Create(&_string_module); |
| 15443 | } |
| 15444 | |
| 15445 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15446 | #ifdef __cplusplus |
| 15447 | } |
| 15448 | #endif |