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 */ |
| 275 | static 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 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 754 | /* Compilation of templated routines */ |
| 755 | |
| 756 | #include "stringlib/asciilib.h" |
| 757 | #include "stringlib/fastsearch.h" |
| 758 | #include "stringlib/partition.h" |
| 759 | #include "stringlib/split.h" |
| 760 | #include "stringlib/count.h" |
| 761 | #include "stringlib/find.h" |
| 762 | #include "stringlib/find_max_char.h" |
| 763 | #include "stringlib/localeutil.h" |
| 764 | #include "stringlib/undef.h" |
| 765 | |
| 766 | #include "stringlib/ucs1lib.h" |
| 767 | #include "stringlib/fastsearch.h" |
| 768 | #include "stringlib/partition.h" |
| 769 | #include "stringlib/split.h" |
| 770 | #include "stringlib/count.h" |
| 771 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 772 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 773 | #include "stringlib/find_max_char.h" |
| 774 | #include "stringlib/localeutil.h" |
| 775 | #include "stringlib/undef.h" |
| 776 | |
| 777 | #include "stringlib/ucs2lib.h" |
| 778 | #include "stringlib/fastsearch.h" |
| 779 | #include "stringlib/partition.h" |
| 780 | #include "stringlib/split.h" |
| 781 | #include "stringlib/count.h" |
| 782 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 783 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 784 | #include "stringlib/find_max_char.h" |
| 785 | #include "stringlib/localeutil.h" |
| 786 | #include "stringlib/undef.h" |
| 787 | |
| 788 | #include "stringlib/ucs4lib.h" |
| 789 | #include "stringlib/fastsearch.h" |
| 790 | #include "stringlib/partition.h" |
| 791 | #include "stringlib/split.h" |
| 792 | #include "stringlib/count.h" |
| 793 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 794 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 795 | #include "stringlib/find_max_char.h" |
| 796 | #include "stringlib/localeutil.h" |
| 797 | #include "stringlib/undef.h" |
| 798 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 799 | #include "stringlib/unicodedefs.h" |
| 800 | #include "stringlib/fastsearch.h" |
| 801 | #include "stringlib/count.h" |
| 802 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 803 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 805 | /* --- Unicode Object ----------------------------------------------------- */ |
| 806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 807 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 808 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 809 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 810 | Py_LOCAL_INLINE(Py_ssize_t) findchar(const void *s, int kind, |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 811 | Py_ssize_t size, Py_UCS4 ch, |
| 812 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 813 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 814 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 815 | |
| 816 | switch (kind) { |
| 817 | case PyUnicode_1BYTE_KIND: |
| 818 | { |
| 819 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 820 | if (ch1 == ch) |
| 821 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 822 | else |
| 823 | return -1; |
| 824 | } |
| 825 | case PyUnicode_2BYTE_KIND: |
| 826 | { |
| 827 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 828 | if (ch2 == ch) |
| 829 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 830 | else |
| 831 | return -1; |
| 832 | } |
| 833 | case PyUnicode_4BYTE_KIND: |
| 834 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 835 | default: |
| 836 | assert(0); |
| 837 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 838 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 841 | #ifdef Py_DEBUG |
| 842 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 843 | earlier. |
| 844 | |
| 845 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 846 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 847 | invalid character in Unicode 6.0. */ |
| 848 | static void |
| 849 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 850 | { |
| 851 | int kind = PyUnicode_KIND(unicode); |
| 852 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 853 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 854 | if (length <= old_length) |
| 855 | return; |
| 856 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 857 | } |
| 858 | #endif |
| 859 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 860 | static PyObject* |
| 861 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 862 | { |
| 863 | Py_ssize_t char_size; |
| 864 | Py_ssize_t struct_size; |
| 865 | Py_ssize_t new_size; |
| 866 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 867 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 868 | #ifdef Py_DEBUG |
| 869 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 870 | #endif |
| 871 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 872 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 873 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 874 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 875 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 876 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 877 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 878 | struct_size = sizeof(PyASCIIObject); |
| 879 | else |
| 880 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 881 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 882 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 883 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 884 | PyErr_NoMemory(); |
| 885 | return NULL; |
| 886 | } |
| 887 | new_size = (struct_size + (length + 1) * char_size); |
| 888 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 889 | _Py_DEC_REFTOTAL; |
| 890 | _Py_ForgetReference(unicode); |
| 891 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 892 | new_unicode = (PyObject *)PyObject_REALLOC(unicode, new_size); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 893 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 894 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 895 | PyErr_NoMemory(); |
| 896 | return NULL; |
| 897 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 898 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 899 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 900 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 901 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 902 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 903 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 904 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 905 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 906 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 907 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 908 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 909 | _PyUnicode_WSTR(unicode) = NULL; |
| 910 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 911 | #ifdef Py_DEBUG |
| 912 | unicode_fill_invalid(unicode, old_length); |
| 913 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 914 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 915 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 916 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 917 | return unicode; |
| 918 | } |
| 919 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 920 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 921 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 922 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 923 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 924 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 925 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 926 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 927 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 928 | if (PyUnicode_IS_READY(unicode)) { |
| 929 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 930 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 931 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 932 | #ifdef Py_DEBUG |
| 933 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 934 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 935 | |
| 936 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 937 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 938 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 939 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 940 | |
| 941 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 942 | PyErr_NoMemory(); |
| 943 | return -1; |
| 944 | } |
| 945 | new_size = (length + 1) * char_size; |
| 946 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 947 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 948 | { |
| 949 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 950 | _PyUnicode_UTF8(unicode) = NULL; |
| 951 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 952 | } |
| 953 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 954 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 955 | if (data == NULL) { |
| 956 | PyErr_NoMemory(); |
| 957 | return -1; |
| 958 | } |
| 959 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 960 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 961 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 962 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 963 | } |
| 964 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 965 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 966 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 967 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 968 | _PyUnicode_LENGTH(unicode) = length; |
| 969 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 970 | #ifdef Py_DEBUG |
| 971 | unicode_fill_invalid(unicode, old_length); |
| 972 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 973 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 974 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 975 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 976 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 977 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 978 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 979 | |
| 980 | /* check for integer overflow */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 981 | 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] | 982 | PyErr_NoMemory(); |
| 983 | return -1; |
| 984 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 985 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 986 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 987 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 988 | if (!wstr) { |
| 989 | PyErr_NoMemory(); |
| 990 | return -1; |
| 991 | } |
| 992 | _PyUnicode_WSTR(unicode) = wstr; |
| 993 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 994 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 995 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 996 | return 0; |
| 997 | } |
| 998 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 999 | static PyObject* |
| 1000 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 1001 | { |
| 1002 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1003 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1004 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1005 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1006 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1007 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1008 | |
| 1009 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1010 | if (copy == NULL) |
| 1011 | return NULL; |
| 1012 | |
| 1013 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1014 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1015 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 1016 | } |
| 1017 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1018 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1019 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1020 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1021 | if (w == NULL) |
| 1022 | return NULL; |
| 1023 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 1024 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 1025 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 1026 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1027 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1031 | /* 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] | 1032 | Ux0000 terminated; some code (e.g. new_identifier) |
| 1033 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1034 | |
| 1035 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1036 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1037 | |
| 1038 | */ |
| 1039 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1040 | static PyUnicodeObject * |
| 1041 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1042 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1043 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1045 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1046 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1047 | if (length == 0 && unicode_empty != NULL) { |
| 1048 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1049 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 1052 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 1053 | 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] | 1054 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 1055 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1056 | if (length < 0) { |
| 1057 | PyErr_SetString(PyExc_SystemError, |
| 1058 | "Negative size passed to _PyUnicode_New"); |
| 1059 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1062 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 1063 | if (unicode == NULL) |
| 1064 | return NULL; |
| 1065 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 1066 | |
| 1067 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 1068 | _PyUnicode_HASH(unicode) = -1; |
| 1069 | _PyUnicode_STATE(unicode).interned = 0; |
| 1070 | _PyUnicode_STATE(unicode).kind = 0; |
| 1071 | _PyUnicode_STATE(unicode).compact = 0; |
| 1072 | _PyUnicode_STATE(unicode).ready = 0; |
| 1073 | _PyUnicode_STATE(unicode).ascii = 0; |
| 1074 | _PyUnicode_DATA_ANY(unicode) = NULL; |
| 1075 | _PyUnicode_LENGTH(unicode) = 0; |
| 1076 | _PyUnicode_UTF8(unicode) = NULL; |
| 1077 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 1078 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1079 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 1080 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1081 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1082 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1083 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 1084 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1085 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 1086 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1087 | * the caller fails before initializing str -- unicode_resize() |
| 1088 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 1089 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 1090 | * We don't want unicode_resize to read uninitialized memory in |
| 1091 | * that case. |
| 1092 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1093 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 1094 | _PyUnicode_WSTR(unicode)[length] = 0; |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 1095 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1096 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1097 | return unicode; |
| 1098 | } |
| 1099 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1100 | static const char* |
| 1101 | unicode_kind_name(PyObject *unicode) |
| 1102 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 1103 | /* don't check consistency: unicode_kind_name() is called from |
| 1104 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1105 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 1106 | { |
| 1107 | if (!PyUnicode_IS_READY(unicode)) |
| 1108 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1109 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1110 | { |
| 1111 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1112 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1113 | return "legacy ascii"; |
| 1114 | else |
| 1115 | return "legacy latin1"; |
| 1116 | case PyUnicode_2BYTE_KIND: |
| 1117 | return "legacy UCS2"; |
| 1118 | case PyUnicode_4BYTE_KIND: |
| 1119 | return "legacy UCS4"; |
| 1120 | default: |
| 1121 | return "<legacy invalid kind>"; |
| 1122 | } |
| 1123 | } |
| 1124 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1125 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1126 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1127 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1128 | return "ascii"; |
| 1129 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1130 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1131 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1132 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1133 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1134 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1135 | default: |
| 1136 | return "<invalid compact kind>"; |
| 1137 | } |
| 1138 | } |
| 1139 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1140 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1141 | /* Functions wrapping macros for use in debugger */ |
| 1142 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1143 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | void *_PyUnicode_compact_data(void *unicode) { |
| 1147 | return _PyUnicode_COMPACT_DATA(unicode); |
| 1148 | } |
| 1149 | void *_PyUnicode_data(void *unicode){ |
| 1150 | printf("obj %p\n", unicode); |
| 1151 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 1152 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 1153 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 1154 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 1155 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 1156 | return PyUnicode_DATA(unicode); |
| 1157 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1158 | |
| 1159 | void |
| 1160 | _PyUnicode_Dump(PyObject *op) |
| 1161 | { |
| 1162 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1163 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 1164 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 1165 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1166 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1167 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1168 | { |
| 1169 | if (ascii->state.ascii) |
| 1170 | data = (ascii + 1); |
| 1171 | else |
| 1172 | data = (compact + 1); |
| 1173 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1174 | else |
| 1175 | data = unicode->data.any; |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1176 | printf("%s: len=%" PY_FORMAT_SIZE_T "u, ", |
| 1177 | unicode_kind_name(op), ascii->length); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1178 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1179 | if (ascii->wstr == data) |
| 1180 | printf("shared "); |
| 1181 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1182 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1183 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1184 | printf(" (%" PY_FORMAT_SIZE_T "u), ", compact->wstr_length); |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1185 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1186 | printf("shared "); |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1187 | printf("utf8=%p (%" PY_FORMAT_SIZE_T "u)", |
| 1188 | compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1189 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1190 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1191 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1192 | #endif |
| 1193 | |
| 1194 | PyObject * |
| 1195 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1196 | { |
| 1197 | PyObject *obj; |
| 1198 | PyCompactUnicodeObject *unicode; |
| 1199 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1200 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1201 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1202 | Py_ssize_t char_size; |
| 1203 | Py_ssize_t struct_size; |
| 1204 | |
| 1205 | /* Optimization for empty strings */ |
| 1206 | if (size == 0 && unicode_empty != NULL) { |
| 1207 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1208 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1211 | is_ascii = 0; |
| 1212 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1213 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1214 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1215 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | char_size = 1; |
| 1217 | is_ascii = 1; |
| 1218 | struct_size = sizeof(PyASCIIObject); |
| 1219 | } |
| 1220 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1221 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1222 | char_size = 1; |
| 1223 | } |
| 1224 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1225 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1226 | char_size = 2; |
| 1227 | if (sizeof(wchar_t) == 2) |
| 1228 | is_sharing = 1; |
| 1229 | } |
| 1230 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1231 | if (maxchar > MAX_UNICODE) { |
| 1232 | PyErr_SetString(PyExc_SystemError, |
| 1233 | "invalid maximum character passed to PyUnicode_New"); |
| 1234 | return NULL; |
| 1235 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1236 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1237 | char_size = 4; |
| 1238 | if (sizeof(wchar_t) == 4) |
| 1239 | is_sharing = 1; |
| 1240 | } |
| 1241 | |
| 1242 | /* Ensure we won't overflow the size. */ |
| 1243 | if (size < 0) { |
| 1244 | PyErr_SetString(PyExc_SystemError, |
| 1245 | "Negative size passed to PyUnicode_New"); |
| 1246 | return NULL; |
| 1247 | } |
| 1248 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1249 | return PyErr_NoMemory(); |
| 1250 | |
| 1251 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1252 | * PyObject_New() so we are able to allocate space for the object and |
| 1253 | * it's data buffer. |
| 1254 | */ |
| 1255 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1256 | if (obj == NULL) |
| 1257 | return PyErr_NoMemory(); |
| 1258 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1259 | if (obj == NULL) |
| 1260 | return NULL; |
| 1261 | |
| 1262 | unicode = (PyCompactUnicodeObject *)obj; |
| 1263 | if (is_ascii) |
| 1264 | data = ((PyASCIIObject*)obj) + 1; |
| 1265 | else |
| 1266 | data = unicode + 1; |
| 1267 | _PyUnicode_LENGTH(unicode) = size; |
| 1268 | _PyUnicode_HASH(unicode) = -1; |
| 1269 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1270 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1271 | _PyUnicode_STATE(unicode).compact = 1; |
| 1272 | _PyUnicode_STATE(unicode).ready = 1; |
| 1273 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1274 | if (is_ascii) { |
| 1275 | ((char*)data)[size] = 0; |
| 1276 | _PyUnicode_WSTR(unicode) = NULL; |
| 1277 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1278 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1279 | ((char*)data)[size] = 0; |
| 1280 | _PyUnicode_WSTR(unicode) = NULL; |
| 1281 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1282 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1283 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1284 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1285 | else { |
| 1286 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1287 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1288 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1289 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1290 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1291 | ((Py_UCS4*)data)[size] = 0; |
| 1292 | if (is_sharing) { |
| 1293 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1294 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1295 | } |
| 1296 | else { |
| 1297 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1298 | _PyUnicode_WSTR(unicode) = NULL; |
| 1299 | } |
| 1300 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1301 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1302 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1303 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1304 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1305 | return obj; |
| 1306 | } |
| 1307 | |
| 1308 | #if SIZEOF_WCHAR_T == 2 |
| 1309 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1310 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1311 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1312 | |
| 1313 | This function assumes that unicode can hold one more code point than wstr |
| 1314 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1315 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 | 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] | 1317 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1318 | { |
| 1319 | const wchar_t *iter; |
| 1320 | Py_UCS4 *ucs4_out; |
| 1321 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1322 | assert(unicode != NULL); |
| 1323 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1324 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1325 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1326 | |
| 1327 | for (iter = begin; iter < end; ) { |
| 1328 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1329 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1330 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1331 | && (iter+1) < end |
| 1332 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1333 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1334 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1335 | iter += 2; |
| 1336 | } |
| 1337 | else { |
| 1338 | *ucs4_out++ = *iter; |
| 1339 | iter++; |
| 1340 | } |
| 1341 | } |
| 1342 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1343 | _PyUnicode_GET_LENGTH(unicode))); |
| 1344 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1345 | } |
| 1346 | #endif |
| 1347 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1348 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1349 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1350 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1351 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1352 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1353 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1354 | return -1; |
| 1355 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1356 | return 0; |
| 1357 | } |
| 1358 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1359 | static int |
| 1360 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1361 | PyObject *from, Py_ssize_t from_start, |
| 1362 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1363 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1364 | unsigned int from_kind, to_kind; |
| 1365 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1366 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1367 | assert(0 <= how_many); |
| 1368 | assert(0 <= from_start); |
| 1369 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1370 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1371 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1372 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1373 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1374 | assert(PyUnicode_Check(to)); |
| 1375 | assert(PyUnicode_IS_READY(to)); |
| 1376 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1377 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1378 | if (how_many == 0) |
| 1379 | return 0; |
| 1380 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1381 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1382 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1383 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1384 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1385 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1386 | #ifdef Py_DEBUG |
| 1387 | if (!check_maxchar |
| 1388 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1389 | { |
| 1390 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1391 | Py_UCS4 ch; |
| 1392 | Py_ssize_t i; |
| 1393 | for (i=0; i < how_many; i++) { |
| 1394 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1395 | assert(ch <= to_maxchar); |
| 1396 | } |
| 1397 | } |
| 1398 | #endif |
| 1399 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1400 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1401 | if (check_maxchar |
| 1402 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1403 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1404 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1405 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1406 | Py_UCS4 max_char; |
| 1407 | max_char = ucs1lib_find_max_char(from_data, |
| 1408 | (Py_UCS1*)from_data + how_many); |
| 1409 | if (max_char >= 128) |
| 1410 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1411 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1412 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1413 | (char*)from_data + from_kind * from_start, |
| 1414 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1415 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1416 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1417 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1418 | { |
| 1419 | _PyUnicode_CONVERT_BYTES( |
| 1420 | Py_UCS1, Py_UCS2, |
| 1421 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1422 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1423 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1424 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1425 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1426 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1427 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1428 | { |
| 1429 | _PyUnicode_CONVERT_BYTES( |
| 1430 | Py_UCS1, Py_UCS4, |
| 1431 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1432 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1433 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1434 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1435 | } |
| 1436 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1437 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1438 | { |
| 1439 | _PyUnicode_CONVERT_BYTES( |
| 1440 | Py_UCS2, Py_UCS4, |
| 1441 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1442 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1443 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1444 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1445 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1446 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1447 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1448 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1449 | if (!check_maxchar) { |
| 1450 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1451 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1452 | { |
| 1453 | _PyUnicode_CONVERT_BYTES( |
| 1454 | Py_UCS2, Py_UCS1, |
| 1455 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1456 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1457 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1458 | ); |
| 1459 | } |
| 1460 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1461 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1462 | { |
| 1463 | _PyUnicode_CONVERT_BYTES( |
| 1464 | Py_UCS4, Py_UCS1, |
| 1465 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1466 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1467 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1468 | ); |
| 1469 | } |
| 1470 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1471 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1472 | { |
| 1473 | _PyUnicode_CONVERT_BYTES( |
| 1474 | Py_UCS4, Py_UCS2, |
| 1475 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1476 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1477 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1478 | ); |
| 1479 | } |
| 1480 | else { |
| 1481 | assert(0); |
| 1482 | return -1; |
| 1483 | } |
| 1484 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1485 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1486 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1487 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1488 | Py_ssize_t i; |
| 1489 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1490 | for (i=0; i < how_many; i++) { |
| 1491 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1492 | if (ch > to_maxchar) |
| 1493 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1494 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1495 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1496 | } |
| 1497 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
| 1500 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1501 | void |
| 1502 | _PyUnicode_FastCopyCharacters( |
| 1503 | PyObject *to, Py_ssize_t to_start, |
| 1504 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1505 | { |
| 1506 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1507 | } |
| 1508 | |
| 1509 | Py_ssize_t |
| 1510 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1511 | PyObject *from, Py_ssize_t from_start, |
| 1512 | Py_ssize_t how_many) |
| 1513 | { |
| 1514 | int err; |
| 1515 | |
| 1516 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1517 | PyErr_BadInternalCall(); |
| 1518 | return -1; |
| 1519 | } |
| 1520 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1521 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1522 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1523 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1524 | return -1; |
| 1525 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1526 | if (from_start < 0) { |
| 1527 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1528 | return -1; |
| 1529 | } |
| 1530 | if (to_start < 0) { |
| 1531 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1532 | return -1; |
| 1533 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1534 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1535 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1536 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 1537 | "Cannot write %zi characters at %zi " |
| 1538 | "in a string of %zi characters", |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1539 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1540 | return -1; |
| 1541 | } |
| 1542 | |
| 1543 | if (how_many == 0) |
| 1544 | return 0; |
| 1545 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1546 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1547 | return -1; |
| 1548 | |
| 1549 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1550 | if (err) { |
| 1551 | PyErr_Format(PyExc_SystemError, |
| 1552 | "Cannot copy %s characters " |
| 1553 | "into a string of %s characters", |
| 1554 | unicode_kind_name(from), |
| 1555 | unicode_kind_name(to)); |
| 1556 | return -1; |
| 1557 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1558 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1561 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1562 | correct string length can be computed before converting a string to UCS4. |
| 1563 | This function counts single surrogates as a character and not as a pair. |
| 1564 | |
| 1565 | Return 0 on success, or -1 on error. */ |
| 1566 | static int |
| 1567 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1568 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1569 | { |
| 1570 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1571 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1572 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1573 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1574 | *num_surrogates = 0; |
| 1575 | *maxchar = 0; |
| 1576 | |
| 1577 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1578 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1579 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1580 | && (iter+1) < end |
| 1581 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1582 | { |
| 1583 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1584 | ++(*num_surrogates); |
| 1585 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1586 | } |
| 1587 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1588 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1589 | { |
| 1590 | ch = *iter; |
| 1591 | iter++; |
| 1592 | } |
| 1593 | if (ch > *maxchar) { |
| 1594 | *maxchar = ch; |
| 1595 | if (*maxchar > MAX_UNICODE) { |
| 1596 | PyErr_Format(PyExc_ValueError, |
| 1597 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1598 | ch); |
| 1599 | return -1; |
| 1600 | } |
| 1601 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1602 | } |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1606 | int |
| 1607 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1608 | { |
| 1609 | wchar_t *end; |
| 1610 | Py_UCS4 maxchar = 0; |
| 1611 | Py_ssize_t num_surrogates; |
| 1612 | #if SIZEOF_WCHAR_T == 2 |
| 1613 | Py_ssize_t length_wo_surrogates; |
| 1614 | #endif |
| 1615 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1616 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1617 | strings were created using _PyObject_New() and where no canonical |
| 1618 | representation (the str field) has been set yet aka strings |
| 1619 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1620 | assert(_PyUnicode_CHECK(unicode)); |
| 1621 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1622 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1623 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1624 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1625 | /* Actually, it should neither be interned nor be anything else: */ |
| 1626 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1627 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1628 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1629 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1630 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1631 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1632 | |
| 1633 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1634 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1635 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1636 | PyErr_NoMemory(); |
| 1637 | return -1; |
| 1638 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1639 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1640 | _PyUnicode_WSTR(unicode), end, |
| 1641 | PyUnicode_1BYTE_DATA(unicode)); |
| 1642 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1643 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1644 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1645 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1646 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1647 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1648 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1649 | } |
| 1650 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1651 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1652 | _PyUnicode_UTF8(unicode) = NULL; |
| 1653 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1654 | } |
| 1655 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1656 | _PyUnicode_WSTR(unicode) = NULL; |
| 1657 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1658 | } |
| 1659 | /* In this case we might have to convert down from 4-byte native |
| 1660 | wchar_t to 2-byte unicode. */ |
| 1661 | else if (maxchar < 65536) { |
| 1662 | assert(num_surrogates == 0 && |
| 1663 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1664 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1665 | #if SIZEOF_WCHAR_T == 2 |
| 1666 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1667 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1668 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1669 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1670 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1671 | _PyUnicode_UTF8(unicode) = NULL; |
| 1672 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1673 | #else |
| 1674 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1675 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1676 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1677 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1678 | PyErr_NoMemory(); |
| 1679 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1680 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1681 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1682 | _PyUnicode_WSTR(unicode), end, |
| 1683 | PyUnicode_2BYTE_DATA(unicode)); |
| 1684 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1685 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1686 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1687 | _PyUnicode_UTF8(unicode) = NULL; |
| 1688 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1689 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1690 | _PyUnicode_WSTR(unicode) = NULL; |
| 1691 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1692 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1693 | } |
| 1694 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1695 | else { |
| 1696 | #if SIZEOF_WCHAR_T == 2 |
| 1697 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1698 | new normalized 4-byte version. */ |
| 1699 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 1700 | if (length_wo_surrogates > PY_SSIZE_T_MAX / 4 - 1) { |
| 1701 | PyErr_NoMemory(); |
| 1702 | return -1; |
| 1703 | } |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1704 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1705 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1706 | PyErr_NoMemory(); |
| 1707 | return -1; |
| 1708 | } |
| 1709 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1710 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1711 | _PyUnicode_UTF8(unicode) = NULL; |
| 1712 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1713 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1714 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1715 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1716 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1717 | _PyUnicode_WSTR(unicode) = NULL; |
| 1718 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1719 | #else |
| 1720 | assert(num_surrogates == 0); |
| 1721 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1722 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1723 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1724 | _PyUnicode_UTF8(unicode) = NULL; |
| 1725 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1726 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1727 | #endif |
| 1728 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1729 | } |
| 1730 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1731 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1732 | return 0; |
| 1733 | } |
| 1734 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1735 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1736 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1737 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1738 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1739 | case SSTATE_NOT_INTERNED: |
| 1740 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1741 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1742 | case SSTATE_INTERNED_MORTAL: |
| 1743 | /* revive dead object temporarily for DelItem */ |
| 1744 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1745 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1746 | Py_FatalError( |
| 1747 | "deletion of interned string failed"); |
| 1748 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1749 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1750 | case SSTATE_INTERNED_IMMORTAL: |
| 1751 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1752 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1753 | default: |
| 1754 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1757 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1759 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1760 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1761 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1762 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1763 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1764 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1767 | #ifdef Py_DEBUG |
| 1768 | static int |
| 1769 | unicode_is_singleton(PyObject *unicode) |
| 1770 | { |
| 1771 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1772 | if (unicode == unicode_empty) |
| 1773 | return 1; |
| 1774 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1775 | { |
| 1776 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1777 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1778 | return 1; |
| 1779 | } |
| 1780 | return 0; |
| 1781 | } |
| 1782 | #endif |
| 1783 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1784 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1785 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1786 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1787 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1788 | if (Py_REFCNT(unicode) != 1) |
| 1789 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1790 | if (_PyUnicode_HASH(unicode) != -1) |
| 1791 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1792 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1793 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1794 | if (!PyUnicode_CheckExact(unicode)) |
| 1795 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1796 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1797 | /* singleton refcount is greater than 1 */ |
| 1798 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1799 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1800 | return 1; |
| 1801 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1802 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1803 | static int |
| 1804 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1805 | { |
| 1806 | PyObject *unicode; |
| 1807 | Py_ssize_t old_length; |
| 1808 | |
| 1809 | assert(p_unicode != NULL); |
| 1810 | unicode = *p_unicode; |
| 1811 | |
| 1812 | assert(unicode != NULL); |
| 1813 | assert(PyUnicode_Check(unicode)); |
| 1814 | assert(0 <= length); |
| 1815 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1816 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1817 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1818 | else |
| 1819 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1820 | if (old_length == length) |
| 1821 | return 0; |
| 1822 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1823 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1824 | _Py_INCREF_UNICODE_EMPTY(); |
| 1825 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1826 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1827 | Py_DECREF(*p_unicode); |
| 1828 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1829 | return 0; |
| 1830 | } |
| 1831 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1832 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1833 | PyObject *copy = resize_copy(unicode, length); |
| 1834 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1835 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1836 | Py_DECREF(*p_unicode); |
| 1837 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1838 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1841 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1842 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1843 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1844 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1845 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1846 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1847 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1848 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1851 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1852 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1853 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1854 | PyObject *unicode; |
| 1855 | if (p_unicode == NULL) { |
| 1856 | PyErr_BadInternalCall(); |
| 1857 | return -1; |
| 1858 | } |
| 1859 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1860 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1861 | { |
| 1862 | PyErr_BadInternalCall(); |
| 1863 | return -1; |
| 1864 | } |
| 1865 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1866 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1867 | |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 1868 | /* Copy an ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1869 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1870 | WARNING: The function doesn't copy the terminating null character and |
| 1871 | doesn't check the maximum character (may write a latin1 character in an |
| 1872 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1873 | static void |
| 1874 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1875 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1876 | { |
| 1877 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1878 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1879 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1880 | |
| 1881 | switch (kind) { |
| 1882 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1883 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1884 | #ifdef Py_DEBUG |
| 1885 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1886 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1887 | (const Py_UCS1*)str, |
| 1888 | (const Py_UCS1*)str + len); |
| 1889 | assert(maxchar < 128); |
| 1890 | } |
| 1891 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1892 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1893 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1894 | } |
| 1895 | case PyUnicode_2BYTE_KIND: { |
| 1896 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1897 | Py_UCS2 *ucs2 = start; |
| 1898 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1899 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1900 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1901 | *ucs2 = (Py_UCS2)*str; |
| 1902 | |
| 1903 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1904 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1905 | } |
| 1906 | default: { |
| 1907 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1908 | Py_UCS4 *ucs4 = start; |
| 1909 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1910 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1911 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1912 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1913 | *ucs4 = (Py_UCS4)*str; |
| 1914 | |
| 1915 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1920 | static PyObject* |
| 1921 | get_latin1_char(unsigned char ch) |
| 1922 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1923 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1924 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1925 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1926 | if (!unicode) |
| 1927 | return NULL; |
| 1928 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1929 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1930 | unicode_latin1[ch] = unicode; |
| 1931 | } |
| 1932 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1933 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1936 | static PyObject* |
| 1937 | unicode_char(Py_UCS4 ch) |
| 1938 | { |
| 1939 | PyObject *unicode; |
| 1940 | |
| 1941 | assert(ch <= MAX_UNICODE); |
| 1942 | |
Victor Stinner | f3b46b4 | 2014-01-03 13:16:00 +0100 | [diff] [blame] | 1943 | if (ch < 256) |
| 1944 | return get_latin1_char(ch); |
| 1945 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1946 | unicode = PyUnicode_New(1, ch); |
| 1947 | if (unicode == NULL) |
| 1948 | return NULL; |
| 1949 | switch (PyUnicode_KIND(unicode)) { |
| 1950 | case PyUnicode_1BYTE_KIND: |
| 1951 | PyUnicode_1BYTE_DATA(unicode)[0] = (Py_UCS1)ch; |
| 1952 | break; |
| 1953 | case PyUnicode_2BYTE_KIND: |
| 1954 | PyUnicode_2BYTE_DATA(unicode)[0] = (Py_UCS2)ch; |
| 1955 | break; |
| 1956 | default: |
| 1957 | assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1958 | PyUnicode_4BYTE_DATA(unicode)[0] = ch; |
| 1959 | } |
| 1960 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1961 | return unicode; |
| 1962 | } |
| 1963 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1964 | PyObject * |
| 1965 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1966 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1967 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1968 | Py_UCS4 maxchar = 0; |
| 1969 | Py_ssize_t num_surrogates; |
| 1970 | |
| 1971 | if (u == NULL) |
| 1972 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1973 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1974 | /* If the Unicode data is known at construction time, we can apply |
| 1975 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1976 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1977 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1978 | if (size == 0) |
| 1979 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1980 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 | /* Single character Unicode objects in the Latin-1 range are |
| 1982 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1983 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1984 | return get_latin1_char((unsigned char)*u); |
| 1985 | |
| 1986 | /* If not empty and not single character, copy the Unicode data |
| 1987 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1988 | if (find_maxchar_surrogates(u, u + size, |
| 1989 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1990 | return NULL; |
| 1991 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1992 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1993 | if (!unicode) |
| 1994 | return NULL; |
| 1995 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1996 | switch (PyUnicode_KIND(unicode)) { |
| 1997 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1998 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 2000 | break; |
| 2001 | case PyUnicode_2BYTE_KIND: |
| 2002 | #if Py_UNICODE_SIZE == 2 |
| 2003 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 2004 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 2005 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2006 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 2007 | #endif |
| 2008 | break; |
| 2009 | case PyUnicode_4BYTE_KIND: |
| 2010 | #if SIZEOF_WCHAR_T == 2 |
| 2011 | /* This is the only case which has to process surrogates, thus |
| 2012 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 2013 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2014 | #else |
| 2015 | assert(num_surrogates == 0); |
| 2016 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 2017 | #endif |
| 2018 | break; |
| 2019 | default: |
| 2020 | assert(0 && "Impossible state"); |
| 2021 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2022 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2023 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2026 | PyObject * |
| 2027 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 2028 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2029 | if (size < 0) { |
| 2030 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2031 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2032 | return NULL; |
| 2033 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2034 | if (u != NULL) |
| 2035 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 2036 | else |
| 2037 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2040 | PyObject * |
| 2041 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2042 | { |
| 2043 | size_t size = strlen(u); |
| 2044 | if (size > PY_SSIZE_T_MAX) { |
| 2045 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 2046 | return NULL; |
| 2047 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2048 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2051 | PyObject * |
| 2052 | _PyUnicode_FromId(_Py_Identifier *id) |
| 2053 | { |
| 2054 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 2055 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 2056 | strlen(id->string), |
| 2057 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2058 | if (!id->object) |
| 2059 | return NULL; |
| 2060 | PyUnicode_InternInPlace(&id->object); |
| 2061 | assert(!id->next); |
| 2062 | id->next = static_strings; |
| 2063 | static_strings = id; |
| 2064 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2065 | return id->object; |
| 2066 | } |
| 2067 | |
| 2068 | void |
| 2069 | _PyUnicode_ClearStaticStrings() |
| 2070 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2071 | _Py_Identifier *tmp, *s = static_strings; |
| 2072 | while (s) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 2073 | Py_CLEAR(s->object); |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2074 | tmp = s->next; |
| 2075 | s->next = NULL; |
| 2076 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2077 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2078 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 2081 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2082 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2083 | PyObject* |
| 2084 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2085 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2086 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2087 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 2088 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 2089 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2090 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 2091 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 2092 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 2093 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2094 | unicode = PyUnicode_New(size, 127); |
| 2095 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2096 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2097 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 2098 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 2099 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2102 | static Py_UCS4 |
| 2103 | kind_maxchar_limit(unsigned int kind) |
| 2104 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2105 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2106 | case PyUnicode_1BYTE_KIND: |
| 2107 | return 0x80; |
| 2108 | case PyUnicode_2BYTE_KIND: |
| 2109 | return 0x100; |
| 2110 | case PyUnicode_4BYTE_KIND: |
| 2111 | return 0x10000; |
| 2112 | default: |
| 2113 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2114 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2118 | Py_LOCAL_INLINE(Py_UCS4) |
| 2119 | align_maxchar(Py_UCS4 maxchar) |
| 2120 | { |
| 2121 | if (maxchar <= 127) |
| 2122 | return 127; |
| 2123 | else if (maxchar <= 255) |
| 2124 | return 255; |
| 2125 | else if (maxchar <= 65535) |
| 2126 | return 65535; |
| 2127 | else |
| 2128 | return MAX_UNICODE; |
| 2129 | } |
| 2130 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2131 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2132 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2133 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2134 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2135 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2136 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2137 | if (size == 0) |
| 2138 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2139 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 2140 | if (size == 1) |
| 2141 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2142 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2143 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2144 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2145 | if (!res) |
| 2146 | return NULL; |
| 2147 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2148 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2149 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2152 | static PyObject* |
| 2153 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2154 | { |
| 2155 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2156 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2157 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2158 | if (size == 0) |
| 2159 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2160 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2161 | if (size == 1) |
| 2162 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2163 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2164 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2165 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2166 | if (!res) |
| 2167 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2168 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2169 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2170 | else { |
| 2171 | _PyUnicode_CONVERT_BYTES( |
| 2172 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 2173 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2174 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2175 | return res; |
| 2176 | } |
| 2177 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2178 | static PyObject* |
| 2179 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2180 | { |
| 2181 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2182 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2183 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2184 | if (size == 0) |
| 2185 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2186 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2187 | if (size == 1) |
| 2188 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2189 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2190 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2191 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2192 | if (!res) |
| 2193 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2194 | if (max_char < 256) |
| 2195 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2196 | PyUnicode_1BYTE_DATA(res)); |
| 2197 | else if (max_char < 0x10000) |
| 2198 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2199 | PyUnicode_2BYTE_DATA(res)); |
| 2200 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2201 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2202 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2203 | return res; |
| 2204 | } |
| 2205 | |
| 2206 | PyObject* |
| 2207 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2208 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2209 | if (size < 0) { |
| 2210 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2211 | return NULL; |
| 2212 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2213 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2214 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2215 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2216 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2217 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2218 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2219 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2220 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2221 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2222 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2223 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2224 | } |
| 2225 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2226 | Py_UCS4 |
| 2227 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2228 | { |
| 2229 | enum PyUnicode_Kind kind; |
| 2230 | void *startptr, *endptr; |
| 2231 | |
| 2232 | assert(PyUnicode_IS_READY(unicode)); |
| 2233 | assert(0 <= start); |
| 2234 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2235 | assert(start <= end); |
| 2236 | |
| 2237 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2238 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2239 | |
| 2240 | if (start == end) |
| 2241 | return 127; |
| 2242 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2243 | if (PyUnicode_IS_ASCII(unicode)) |
| 2244 | return 127; |
| 2245 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2246 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2247 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2248 | endptr = (char *)startptr + end * kind; |
| 2249 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2250 | switch(kind) { |
| 2251 | case PyUnicode_1BYTE_KIND: |
| 2252 | return ucs1lib_find_max_char(startptr, endptr); |
| 2253 | case PyUnicode_2BYTE_KIND: |
| 2254 | return ucs2lib_find_max_char(startptr, endptr); |
| 2255 | case PyUnicode_4BYTE_KIND: |
| 2256 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2257 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2258 | assert(0); |
| 2259 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2263 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2264 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2265 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2266 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2267 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2268 | { |
| 2269 | PyObject *unicode, *copy; |
| 2270 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2271 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2272 | unsigned int kind; |
| 2273 | |
| 2274 | assert(p_unicode != NULL); |
| 2275 | unicode = *p_unicode; |
| 2276 | assert(PyUnicode_IS_READY(unicode)); |
| 2277 | if (PyUnicode_IS_ASCII(unicode)) |
| 2278 | return; |
| 2279 | |
| 2280 | len = PyUnicode_GET_LENGTH(unicode); |
| 2281 | kind = PyUnicode_KIND(unicode); |
| 2282 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2283 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2284 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2285 | if (max_char >= 128) |
| 2286 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2287 | } |
| 2288 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2289 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2290 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2291 | if (max_char >= 256) |
| 2292 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2293 | } |
| 2294 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2295 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2296 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2297 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2298 | if (max_char >= 0x10000) |
| 2299 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2300 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2301 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2302 | if (copy != NULL) |
| 2303 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2304 | Py_DECREF(unicode); |
| 2305 | *p_unicode = copy; |
| 2306 | } |
| 2307 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2308 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2309 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2310 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2311 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2312 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2313 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2314 | if (!PyUnicode_Check(unicode)) { |
| 2315 | PyErr_BadInternalCall(); |
| 2316 | return NULL; |
| 2317 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2318 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2319 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2320 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2321 | length = PyUnicode_GET_LENGTH(unicode); |
| 2322 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2323 | if (!copy) |
| 2324 | return NULL; |
| 2325 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2326 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2327 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2328 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2329 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2330 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2331 | } |
| 2332 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2333 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2334 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2335 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2336 | |
| 2337 | void* |
| 2338 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2339 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2340 | Py_ssize_t len; |
| 2341 | void *result; |
| 2342 | unsigned int skind; |
| 2343 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2344 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2345 | return NULL; |
| 2346 | |
| 2347 | len = PyUnicode_GET_LENGTH(s); |
| 2348 | skind = PyUnicode_KIND(s); |
| 2349 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2350 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2351 | return NULL; |
| 2352 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2353 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2354 | case PyUnicode_2BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2355 | result = PyMem_New(Py_UCS2, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2356 | if (!result) |
| 2357 | return PyErr_NoMemory(); |
| 2358 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2359 | _PyUnicode_CONVERT_BYTES( |
| 2360 | Py_UCS1, Py_UCS2, |
| 2361 | PyUnicode_1BYTE_DATA(s), |
| 2362 | PyUnicode_1BYTE_DATA(s) + len, |
| 2363 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2364 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2365 | case PyUnicode_4BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2366 | result = PyMem_New(Py_UCS4, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2367 | if (!result) |
| 2368 | return PyErr_NoMemory(); |
| 2369 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2370 | _PyUnicode_CONVERT_BYTES( |
| 2371 | Py_UCS2, Py_UCS4, |
| 2372 | PyUnicode_2BYTE_DATA(s), |
| 2373 | PyUnicode_2BYTE_DATA(s) + len, |
| 2374 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2375 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2376 | else { |
| 2377 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2378 | _PyUnicode_CONVERT_BYTES( |
| 2379 | Py_UCS1, Py_UCS4, |
| 2380 | PyUnicode_1BYTE_DATA(s), |
| 2381 | PyUnicode_1BYTE_DATA(s) + len, |
| 2382 | result); |
| 2383 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2384 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2385 | default: |
| 2386 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2387 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2388 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2389 | return NULL; |
| 2390 | } |
| 2391 | |
| 2392 | static Py_UCS4* |
| 2393 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2394 | int copy_null) |
| 2395 | { |
| 2396 | int kind; |
| 2397 | void *data; |
| 2398 | Py_ssize_t len, targetlen; |
| 2399 | if (PyUnicode_READY(string) == -1) |
| 2400 | return NULL; |
| 2401 | kind = PyUnicode_KIND(string); |
| 2402 | data = PyUnicode_DATA(string); |
| 2403 | len = PyUnicode_GET_LENGTH(string); |
| 2404 | targetlen = len; |
| 2405 | if (copy_null) |
| 2406 | targetlen++; |
| 2407 | if (!target) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2408 | target = PyMem_New(Py_UCS4, targetlen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2409 | if (!target) { |
| 2410 | PyErr_NoMemory(); |
| 2411 | return NULL; |
| 2412 | } |
| 2413 | } |
| 2414 | else { |
| 2415 | if (targetsize < targetlen) { |
| 2416 | PyErr_Format(PyExc_SystemError, |
| 2417 | "string is longer than the buffer"); |
| 2418 | if (copy_null && 0 < targetsize) |
| 2419 | target[0] = 0; |
| 2420 | return NULL; |
| 2421 | } |
| 2422 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2423 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2424 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2425 | _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] | 2426 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2427 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2428 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2429 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2430 | } |
| 2431 | else { |
| 2432 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2433 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2434 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2435 | if (copy_null) |
| 2436 | target[len] = 0; |
| 2437 | return target; |
| 2438 | } |
| 2439 | |
| 2440 | Py_UCS4* |
| 2441 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2442 | int copy_null) |
| 2443 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2444 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2445 | PyErr_BadInternalCall(); |
| 2446 | return NULL; |
| 2447 | } |
| 2448 | return as_ucs4(string, target, targetsize, copy_null); |
| 2449 | } |
| 2450 | |
| 2451 | Py_UCS4* |
| 2452 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2453 | { |
| 2454 | return as_ucs4(string, NULL, 0, 1); |
| 2455 | } |
| 2456 | |
| 2457 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2458 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2459 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2460 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2461 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2462 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2463 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2464 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2465 | PyErr_BadInternalCall(); |
| 2466 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2469 | if (size == -1) { |
| 2470 | size = wcslen(w); |
| 2471 | } |
| 2472 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2473 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2476 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2477 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2478 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2479 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2480 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2481 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2482 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2483 | static int |
| 2484 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2485 | Py_ssize_t width, Py_ssize_t precision) |
| 2486 | { |
| 2487 | Py_ssize_t length, fill, arglen; |
| 2488 | Py_UCS4 maxchar; |
| 2489 | |
| 2490 | if (PyUnicode_READY(str) == -1) |
| 2491 | return -1; |
| 2492 | |
| 2493 | length = PyUnicode_GET_LENGTH(str); |
| 2494 | if ((precision == -1 || precision >= length) |
| 2495 | && width <= length) |
| 2496 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2497 | |
| 2498 | if (precision != -1) |
| 2499 | length = Py_MIN(precision, length); |
| 2500 | |
| 2501 | arglen = Py_MAX(length, width); |
| 2502 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2503 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2504 | else |
| 2505 | maxchar = writer->maxchar; |
| 2506 | |
| 2507 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2508 | return -1; |
| 2509 | |
| 2510 | if (width > length) { |
| 2511 | fill = width - length; |
| 2512 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2513 | return -1; |
| 2514 | writer->pos += fill; |
| 2515 | } |
| 2516 | |
| 2517 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2518 | str, 0, length); |
| 2519 | writer->pos += length; |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
| 2523 | static int |
| 2524 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2525 | Py_ssize_t width, Py_ssize_t precision) |
| 2526 | { |
| 2527 | /* UTF-8 */ |
| 2528 | Py_ssize_t length; |
| 2529 | PyObject *unicode; |
| 2530 | int res; |
| 2531 | |
| 2532 | length = strlen(str); |
| 2533 | if (precision != -1) |
| 2534 | length = Py_MIN(length, precision); |
| 2535 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2536 | if (unicode == NULL) |
| 2537 | return -1; |
| 2538 | |
| 2539 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2540 | Py_DECREF(unicode); |
| 2541 | return res; |
| 2542 | } |
| 2543 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2544 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2545 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2546 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2547 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2548 | const char *p; |
| 2549 | Py_ssize_t len; |
| 2550 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2551 | Py_ssize_t width; |
| 2552 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2553 | int longflag; |
| 2554 | int longlongflag; |
| 2555 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2556 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2557 | |
| 2558 | p = f; |
| 2559 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2560 | zeropad = 0; |
| 2561 | if (*f == '0') { |
| 2562 | zeropad = 1; |
| 2563 | f++; |
| 2564 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2565 | |
| 2566 | /* 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] | 2567 | width = -1; |
| 2568 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2569 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2570 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2571 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2572 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2573 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2574 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2575 | return NULL; |
| 2576 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2577 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2578 | f++; |
| 2579 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2580 | } |
| 2581 | precision = -1; |
| 2582 | if (*f == '.') { |
| 2583 | f++; |
| 2584 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2585 | precision = (*f - '0'); |
| 2586 | f++; |
| 2587 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2588 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2589 | PyErr_SetString(PyExc_ValueError, |
| 2590 | "precision too big"); |
| 2591 | return NULL; |
| 2592 | } |
| 2593 | precision = (precision * 10) + (*f - '0'); |
| 2594 | f++; |
| 2595 | } |
| 2596 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2597 | if (*f == '%') { |
| 2598 | /* "%.3%s" => f points to "3" */ |
| 2599 | f--; |
| 2600 | } |
| 2601 | } |
| 2602 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2603 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2604 | f--; |
| 2605 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2606 | |
| 2607 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2608 | longflag = 0; |
| 2609 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2610 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2611 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2612 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2613 | longflag = 1; |
| 2614 | ++f; |
| 2615 | } |
| 2616 | #ifdef HAVE_LONG_LONG |
| 2617 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2618 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2619 | longlongflag = 1; |
| 2620 | f += 2; |
| 2621 | } |
| 2622 | #endif |
| 2623 | } |
| 2624 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2625 | 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] | 2626 | size_tflag = 1; |
| 2627 | ++f; |
| 2628 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2629 | |
| 2630 | if (f[1] == '\0') |
| 2631 | writer->overallocate = 0; |
| 2632 | |
| 2633 | switch (*f) { |
| 2634 | case 'c': |
| 2635 | { |
| 2636 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2637 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2638 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2639 | "character argument not in range(0x110000)"); |
| 2640 | return NULL; |
| 2641 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2642 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2643 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2644 | break; |
| 2645 | } |
| 2646 | |
| 2647 | case 'i': |
| 2648 | case 'd': |
| 2649 | case 'u': |
| 2650 | case 'x': |
| 2651 | { |
| 2652 | /* used by sprintf */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2653 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2654 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2655 | |
| 2656 | if (*f == 'u') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2657 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2658 | len = sprintf(buffer, "%lu", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2659 | va_arg(*vargs, unsigned long)); |
| 2660 | #ifdef HAVE_LONG_LONG |
| 2661 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2662 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2663 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2664 | #endif |
| 2665 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2666 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2667 | va_arg(*vargs, size_t)); |
| 2668 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2669 | len = sprintf(buffer, "%u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2670 | va_arg(*vargs, unsigned int)); |
| 2671 | } |
| 2672 | else if (*f == 'x') { |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2673 | len = sprintf(buffer, "%x", va_arg(*vargs, int)); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2674 | } |
| 2675 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2676 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2677 | len = sprintf(buffer, "%li", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2678 | va_arg(*vargs, long)); |
| 2679 | #ifdef HAVE_LONG_LONG |
| 2680 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2681 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2682 | va_arg(*vargs, PY_LONG_LONG)); |
| 2683 | #endif |
| 2684 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2685 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2686 | va_arg(*vargs, Py_ssize_t)); |
| 2687 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2688 | len = sprintf(buffer, "%i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2689 | va_arg(*vargs, int)); |
| 2690 | } |
| 2691 | assert(len >= 0); |
| 2692 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2693 | if (precision < len) |
| 2694 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2695 | |
| 2696 | arglen = Py_MAX(precision, width); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2697 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2698 | return NULL; |
| 2699 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2700 | if (width > precision) { |
| 2701 | Py_UCS4 fillchar; |
| 2702 | fill = width - precision; |
| 2703 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2704 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2705 | return NULL; |
| 2706 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2707 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2708 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2709 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2710 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2711 | return NULL; |
| 2712 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2713 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2714 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2715 | if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) |
| 2716 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2717 | break; |
| 2718 | } |
| 2719 | |
| 2720 | case 'p': |
| 2721 | { |
| 2722 | char number[MAX_LONG_LONG_CHARS]; |
| 2723 | |
| 2724 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2725 | assert(len >= 0); |
| 2726 | |
| 2727 | /* %p is ill-defined: ensure leading 0x. */ |
| 2728 | if (number[1] == 'X') |
| 2729 | number[1] = 'x'; |
| 2730 | else if (number[1] != 'x') { |
| 2731 | memmove(number + 2, number, |
| 2732 | strlen(number) + 1); |
| 2733 | number[0] = '0'; |
| 2734 | number[1] = 'x'; |
| 2735 | len += 2; |
| 2736 | } |
| 2737 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2738 | if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2739 | return NULL; |
| 2740 | break; |
| 2741 | } |
| 2742 | |
| 2743 | case 's': |
| 2744 | { |
| 2745 | /* UTF-8 */ |
| 2746 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2747 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2748 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2749 | break; |
| 2750 | } |
| 2751 | |
| 2752 | case 'U': |
| 2753 | { |
| 2754 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2755 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2756 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2757 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2758 | return NULL; |
| 2759 | break; |
| 2760 | } |
| 2761 | |
| 2762 | case 'V': |
| 2763 | { |
| 2764 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2765 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2766 | if (obj) { |
| 2767 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2768 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2769 | return NULL; |
| 2770 | } |
| 2771 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2772 | assert(str != NULL); |
| 2773 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2774 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2775 | } |
| 2776 | break; |
| 2777 | } |
| 2778 | |
| 2779 | case 'S': |
| 2780 | { |
| 2781 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2782 | PyObject *str; |
| 2783 | assert(obj); |
| 2784 | str = PyObject_Str(obj); |
| 2785 | if (!str) |
| 2786 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2787 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2788 | Py_DECREF(str); |
| 2789 | return NULL; |
| 2790 | } |
| 2791 | Py_DECREF(str); |
| 2792 | break; |
| 2793 | } |
| 2794 | |
| 2795 | case 'R': |
| 2796 | { |
| 2797 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2798 | PyObject *repr; |
| 2799 | assert(obj); |
| 2800 | repr = PyObject_Repr(obj); |
| 2801 | if (!repr) |
| 2802 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2803 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2804 | Py_DECREF(repr); |
| 2805 | return NULL; |
| 2806 | } |
| 2807 | Py_DECREF(repr); |
| 2808 | break; |
| 2809 | } |
| 2810 | |
| 2811 | case 'A': |
| 2812 | { |
| 2813 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2814 | PyObject *ascii; |
| 2815 | assert(obj); |
| 2816 | ascii = PyObject_ASCII(obj); |
| 2817 | if (!ascii) |
| 2818 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2819 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2820 | Py_DECREF(ascii); |
| 2821 | return NULL; |
| 2822 | } |
| 2823 | Py_DECREF(ascii); |
| 2824 | break; |
| 2825 | } |
| 2826 | |
| 2827 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2828 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2829 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2830 | break; |
| 2831 | |
| 2832 | default: |
| 2833 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2834 | of the format string to the output string. (we cannot just |
| 2835 | skip the code, since there's no way to know what's in the |
| 2836 | argument list) */ |
| 2837 | len = strlen(p); |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2838 | if (_PyUnicodeWriter_WriteLatin1String(writer, p, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2839 | return NULL; |
| 2840 | f = p+len; |
| 2841 | return f; |
| 2842 | } |
| 2843 | |
| 2844 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2845 | return f; |
| 2846 | } |
| 2847 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2848 | PyObject * |
| 2849 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2850 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2851 | va_list vargs2; |
| 2852 | const char *f; |
| 2853 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2854 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2855 | _PyUnicodeWriter_Init(&writer); |
| 2856 | writer.min_length = strlen(format) + 100; |
| 2857 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2858 | |
| 2859 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2860 | Copy it to be able to pass a reference to a subfunction. */ |
| 2861 | Py_VA_COPY(vargs2, vargs); |
| 2862 | |
| 2863 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2864 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2865 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2866 | if (f == NULL) |
| 2867 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2868 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2869 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2870 | const char *p; |
| 2871 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2872 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2873 | p = f; |
| 2874 | do |
| 2875 | { |
| 2876 | if ((unsigned char)*p > 127) { |
| 2877 | PyErr_Format(PyExc_ValueError, |
| 2878 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2879 | "string, got a non-ASCII byte: 0x%02x", |
| 2880 | (unsigned char)*p); |
| 2881 | return NULL; |
| 2882 | } |
| 2883 | p++; |
| 2884 | } |
| 2885 | while (*p != '\0' && *p != '%'); |
| 2886 | len = p - f; |
| 2887 | |
| 2888 | if (*p == '\0') |
| 2889 | writer.overallocate = 0; |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2890 | |
| 2891 | if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2892 | goto fail; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2893 | |
| 2894 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2895 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2896 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2897 | return _PyUnicodeWriter_Finish(&writer); |
| 2898 | |
| 2899 | fail: |
| 2900 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2901 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2904 | PyObject * |
| 2905 | PyUnicode_FromFormat(const char *format, ...) |
| 2906 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2907 | PyObject* ret; |
| 2908 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2909 | |
| 2910 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2911 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2912 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2913 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2914 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2915 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2916 | va_end(vargs); |
| 2917 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2920 | #ifdef HAVE_WCHAR_H |
| 2921 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2922 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2923 | convert a Unicode object to a wide character string. |
| 2924 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2925 | - 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] | 2926 | character) required to convert the unicode object. Ignore size argument. |
| 2927 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2928 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2929 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2930 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2931 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2932 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2933 | wchar_t *w, |
| 2934 | Py_ssize_t size) |
| 2935 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2936 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2937 | const wchar_t *wstr; |
| 2938 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2939 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2940 | if (wstr == NULL) |
| 2941 | return -1; |
| 2942 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2943 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2944 | if (size > res) |
| 2945 | size = res + 1; |
| 2946 | else |
| 2947 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2948 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2949 | return res; |
| 2950 | } |
| 2951 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2952 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2956 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2957 | wchar_t *w, |
| 2958 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2959 | { |
| 2960 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2961 | PyErr_BadInternalCall(); |
| 2962 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2963 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2964 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2967 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2968 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2969 | Py_ssize_t *size) |
| 2970 | { |
| 2971 | wchar_t* buffer; |
| 2972 | Py_ssize_t buflen; |
| 2973 | |
| 2974 | if (unicode == NULL) { |
| 2975 | PyErr_BadInternalCall(); |
| 2976 | return NULL; |
| 2977 | } |
| 2978 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2979 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2980 | if (buflen == -1) |
| 2981 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2982 | buffer = PyMem_NEW(wchar_t, buflen); |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2983 | if (buffer == NULL) { |
| 2984 | PyErr_NoMemory(); |
| 2985 | return NULL; |
| 2986 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2987 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2988 | if (buflen == -1) { |
| 2989 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2990 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2991 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2992 | if (size != NULL) |
| 2993 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2994 | return buffer; |
| 2995 | } |
| 2996 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2997 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2999 | PyObject * |
| 3000 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3001 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3002 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3003 | PyErr_SetString(PyExc_ValueError, |
| 3004 | "chr() arg not in range(0x110000)"); |
| 3005 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3006 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 3007 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 3008 | return unicode_char((Py_UCS4)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3011 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3012 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3013 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3014 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3015 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3016 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3017 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 3018 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3019 | Py_INCREF(obj); |
| 3020 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3021 | } |
| 3022 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3023 | /* For a Unicode subtype that's not a Unicode object, |
| 3024 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 3025 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3026 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3027 | PyErr_Format(PyExc_TypeError, |
| 3028 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3029 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3030 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3033 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3034 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3035 | const char *encoding, |
| 3036 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3037 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3038 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3039 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3040 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3041 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3042 | PyErr_BadInternalCall(); |
| 3043 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3044 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3045 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3046 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3047 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3048 | if (PyBytes_GET_SIZE(obj) == 0) |
| 3049 | _Py_RETURN_UNICODE_EMPTY(); |
| 3050 | v = PyUnicode_Decode( |
| 3051 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3052 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3053 | return v; |
| 3054 | } |
| 3055 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3056 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3057 | PyErr_SetString(PyExc_TypeError, |
| 3058 | "decoding str is not supported"); |
| 3059 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3060 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3061 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3062 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3063 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3064 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3065 | "coercing to str: need a bytes-like object, %.80s found", |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3066 | Py_TYPE(obj)->tp_name); |
| 3067 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3068 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3069 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3070 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3071 | PyBuffer_Release(&buffer); |
| 3072 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3073 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3074 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3075 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3076 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3077 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3080 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3081 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3082 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3083 | int |
| 3084 | _Py_normalize_encoding(const char *encoding, |
| 3085 | char *lower, |
| 3086 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3087 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3088 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3089 | char *l; |
| 3090 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3091 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3092 | if (encoding == NULL) { |
Victor Stinner | 66b3270 | 2013-11-07 23:12:23 +0100 | [diff] [blame] | 3093 | /* 6 == strlen("utf-8") + 1 */ |
Victor Stinner | df23e30 | 2013-11-07 13:33:36 +0100 | [diff] [blame] | 3094 | if (lower_len < 6) |
| 3095 | return 0; |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3096 | strcpy(lower, "utf-8"); |
| 3097 | return 1; |
| 3098 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3099 | e = encoding; |
| 3100 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3101 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3102 | while (*e) { |
| 3103 | if (l == l_end) |
| 3104 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3105 | if (Py_ISUPPER(*e)) { |
| 3106 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3107 | } |
| 3108 | else if (*e == '_') { |
| 3109 | *l++ = '-'; |
| 3110 | e++; |
| 3111 | } |
| 3112 | else { |
| 3113 | *l++ = *e++; |
| 3114 | } |
| 3115 | } |
| 3116 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3117 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3120 | PyObject * |
| 3121 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3122 | Py_ssize_t size, |
| 3123 | const char *encoding, |
| 3124 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3125 | { |
| 3126 | PyObject *buffer = NULL, *unicode; |
| 3127 | Py_buffer info; |
| 3128 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3129 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3130 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3131 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3132 | if ((strcmp(lower, "utf-8") == 0) || |
| 3133 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3134 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3135 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3136 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3137 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3138 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3139 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3140 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3141 | else if (strcmp(lower, "mbcs") == 0) |
| 3142 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3143 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3144 | else if (strcmp(lower, "ascii") == 0) |
| 3145 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3146 | else if (strcmp(lower, "utf-16") == 0) |
| 3147 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3148 | else if (strcmp(lower, "utf-32") == 0) |
| 3149 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3150 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3151 | |
| 3152 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3153 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3154 | 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] | 3155 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3156 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | if (buffer == NULL) |
| 3158 | goto onError; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3159 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3160 | if (unicode == NULL) |
| 3161 | goto onError; |
| 3162 | if (!PyUnicode_Check(unicode)) { |
| 3163 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3164 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3165 | "use codecs.decode() to decode to arbitrary types", |
| 3166 | encoding, |
| 3167 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3168 | Py_DECREF(unicode); |
| 3169 | goto onError; |
| 3170 | } |
| 3171 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3172 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3173 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3174 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3175 | Py_XDECREF(buffer); |
| 3176 | return NULL; |
| 3177 | } |
| 3178 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3179 | PyObject * |
| 3180 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3181 | const char *encoding, |
| 3182 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3183 | { |
| 3184 | PyObject *v; |
| 3185 | |
| 3186 | if (!PyUnicode_Check(unicode)) { |
| 3187 | PyErr_BadArgument(); |
| 3188 | goto onError; |
| 3189 | } |
| 3190 | |
| 3191 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3192 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3193 | |
| 3194 | /* Decode via the codec registry */ |
| 3195 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3196 | if (v == NULL) |
| 3197 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3198 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3199 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3200 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3201 | return NULL; |
| 3202 | } |
| 3203 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3204 | PyObject * |
| 3205 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3206 | const char *encoding, |
| 3207 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3208 | { |
| 3209 | PyObject *v; |
| 3210 | |
| 3211 | if (!PyUnicode_Check(unicode)) { |
| 3212 | PyErr_BadArgument(); |
| 3213 | goto onError; |
| 3214 | } |
| 3215 | |
| 3216 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3217 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3218 | |
| 3219 | /* Decode via the codec registry */ |
| 3220 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3221 | if (v == NULL) |
| 3222 | goto onError; |
| 3223 | if (!PyUnicode_Check(v)) { |
| 3224 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3225 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3226 | "use codecs.decode() to decode to arbitrary types", |
| 3227 | encoding, |
| 3228 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3229 | Py_DECREF(v); |
| 3230 | goto onError; |
| 3231 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3232 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3233 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3234 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3235 | return NULL; |
| 3236 | } |
| 3237 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3238 | PyObject * |
| 3239 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3240 | Py_ssize_t size, |
| 3241 | const char *encoding, |
| 3242 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3243 | { |
| 3244 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3246 | unicode = PyUnicode_FromUnicode(s, size); |
| 3247 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3248 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3249 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3250 | Py_DECREF(unicode); |
| 3251 | return v; |
| 3252 | } |
| 3253 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3254 | PyObject * |
| 3255 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3256 | const char *encoding, |
| 3257 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3258 | { |
| 3259 | PyObject *v; |
| 3260 | |
| 3261 | if (!PyUnicode_Check(unicode)) { |
| 3262 | PyErr_BadArgument(); |
| 3263 | goto onError; |
| 3264 | } |
| 3265 | |
| 3266 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3267 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3268 | |
| 3269 | /* Encode via the codec registry */ |
| 3270 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3271 | if (v == NULL) |
| 3272 | goto onError; |
| 3273 | return v; |
| 3274 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3275 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3276 | return NULL; |
| 3277 | } |
| 3278 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3279 | static size_t |
| 3280 | wcstombs_errorpos(const wchar_t *wstr) |
| 3281 | { |
| 3282 | size_t len; |
| 3283 | #if SIZEOF_WCHAR_T == 2 |
| 3284 | wchar_t buf[3]; |
| 3285 | #else |
| 3286 | wchar_t buf[2]; |
| 3287 | #endif |
| 3288 | char outbuf[MB_LEN_MAX]; |
| 3289 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3290 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3291 | #if SIZEOF_WCHAR_T == 2 |
| 3292 | buf[2] = 0; |
| 3293 | #else |
| 3294 | buf[1] = 0; |
| 3295 | #endif |
| 3296 | start = wstr; |
| 3297 | while (*wstr != L'\0') |
| 3298 | { |
| 3299 | previous = wstr; |
| 3300 | #if SIZEOF_WCHAR_T == 2 |
| 3301 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3302 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3303 | { |
| 3304 | buf[0] = wstr[0]; |
| 3305 | buf[1] = wstr[1]; |
| 3306 | wstr += 2; |
| 3307 | } |
| 3308 | else { |
| 3309 | buf[0] = *wstr; |
| 3310 | buf[1] = 0; |
| 3311 | wstr++; |
| 3312 | } |
| 3313 | #else |
| 3314 | buf[0] = *wstr; |
| 3315 | wstr++; |
| 3316 | #endif |
| 3317 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3318 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3319 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3323 | return 0; |
| 3324 | } |
| 3325 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3326 | static int |
| 3327 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3328 | { |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3329 | _Py_error_handler error_handler = get_error_handler(errors); |
| 3330 | switch (error_handler) |
| 3331 | { |
| 3332 | case _Py_ERROR_STRICT: |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3333 | *surrogateescape = 0; |
| 3334 | return 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3335 | case _Py_ERROR_SURROGATEESCAPE: |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3336 | *surrogateescape = 1; |
| 3337 | return 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3338 | default: |
| 3339 | PyErr_Format(PyExc_ValueError, |
| 3340 | "only 'strict' and 'surrogateescape' error handlers " |
| 3341 | "are supported, not '%s'", |
| 3342 | errors); |
| 3343 | return -1; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3344 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3345 | } |
| 3346 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3347 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3348 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3349 | { |
| 3350 | Py_ssize_t wlen, wlen2; |
| 3351 | wchar_t *wstr; |
| 3352 | PyObject *bytes = NULL; |
| 3353 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3354 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3355 | PyObject *exc; |
| 3356 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3357 | int surrogateescape; |
| 3358 | |
| 3359 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3360 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3361 | |
| 3362 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3363 | if (wstr == NULL) |
| 3364 | return NULL; |
| 3365 | |
| 3366 | wlen2 = wcslen(wstr); |
| 3367 | if (wlen2 != wlen) { |
| 3368 | PyMem_Free(wstr); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3369 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3370 | return NULL; |
| 3371 | } |
| 3372 | |
| 3373 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3374 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3375 | char *str; |
| 3376 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3377 | str = Py_EncodeLocale(wstr, &error_pos); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3378 | if (str == NULL) { |
| 3379 | if (error_pos == (size_t)-1) { |
| 3380 | PyErr_NoMemory(); |
| 3381 | PyMem_Free(wstr); |
| 3382 | return NULL; |
| 3383 | } |
| 3384 | else { |
| 3385 | goto encode_error; |
| 3386 | } |
| 3387 | } |
| 3388 | PyMem_Free(wstr); |
| 3389 | |
| 3390 | bytes = PyBytes_FromString(str); |
| 3391 | PyMem_Free(str); |
| 3392 | } |
| 3393 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3394 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3395 | size_t len, len2; |
| 3396 | |
| 3397 | len = wcstombs(NULL, wstr, 0); |
| 3398 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3399 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3400 | goto encode_error; |
| 3401 | } |
| 3402 | |
| 3403 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3404 | if (bytes == NULL) { |
| 3405 | PyMem_Free(wstr); |
| 3406 | return NULL; |
| 3407 | } |
| 3408 | |
| 3409 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3410 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3411 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3412 | goto encode_error; |
| 3413 | } |
| 3414 | PyMem_Free(wstr); |
| 3415 | } |
| 3416 | return bytes; |
| 3417 | |
| 3418 | encode_error: |
| 3419 | errmsg = strerror(errno); |
| 3420 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3421 | |
| 3422 | if (error_pos == (size_t)-1) |
| 3423 | error_pos = wcstombs_errorpos(wstr); |
| 3424 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3425 | PyMem_Free(wstr); |
| 3426 | Py_XDECREF(bytes); |
| 3427 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3428 | if (errmsg != NULL) { |
| 3429 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3430 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3431 | if (wstr != NULL) { |
| 3432 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3433 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3434 | } else |
| 3435 | errmsg = NULL; |
| 3436 | } |
| 3437 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3438 | reason = PyUnicode_FromString( |
| 3439 | "wcstombs() encountered an unencodable " |
| 3440 | "wide character"); |
| 3441 | if (reason == NULL) |
| 3442 | return NULL; |
| 3443 | |
| 3444 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3445 | "locale", unicode, |
| 3446 | (Py_ssize_t)error_pos, |
| 3447 | (Py_ssize_t)(error_pos+1), |
| 3448 | reason); |
| 3449 | Py_DECREF(reason); |
| 3450 | if (exc != NULL) { |
| 3451 | PyCodec_StrictErrors(exc); |
| 3452 | Py_XDECREF(exc); |
| 3453 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3454 | return NULL; |
| 3455 | } |
| 3456 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3457 | PyObject * |
| 3458 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3459 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3460 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3461 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3462 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3463 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3464 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3465 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3466 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3467 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3468 | the Python codec requires to encode at least its own filename. Use the C |
| 3469 | version of the locale codec until the codec registry is initialized and |
| 3470 | the Python codec is loaded. |
| 3471 | |
| 3472 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3473 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3474 | subinterpreters. */ |
| 3475 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3476 | return PyUnicode_AsEncodedString(unicode, |
| 3477 | Py_FileSystemDefaultEncoding, |
| 3478 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3479 | } |
| 3480 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3481 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3482 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3483 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3486 | PyObject * |
| 3487 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3488 | const char *encoding, |
| 3489 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3490 | { |
| 3491 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3492 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3493 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3494 | if (!PyUnicode_Check(unicode)) { |
| 3495 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3496 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3497 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3498 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3499 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3500 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3501 | if ((strcmp(lower, "utf-8") == 0) || |
| 3502 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3503 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3504 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3505 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3506 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3507 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3508 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3509 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3510 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3511 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3512 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3513 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3514 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3515 | else if (strcmp(lower, "mbcs") == 0) |
| 3516 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3517 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3518 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3519 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3520 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3521 | |
| 3522 | /* Encode via the codec registry */ |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3523 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3524 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3525 | return NULL; |
| 3526 | |
| 3527 | /* The normal path */ |
| 3528 | if (PyBytes_Check(v)) |
| 3529 | return v; |
| 3530 | |
| 3531 | /* 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] | 3532 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3533 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3534 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3535 | |
| 3536 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3537 | "encoder %s returned bytearray instead of bytes; " |
| 3538 | "use codecs.encode() to encode to arbitrary types", |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3539 | encoding); |
| 3540 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3541 | Py_DECREF(v); |
| 3542 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3543 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3544 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3545 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3546 | Py_DECREF(v); |
| 3547 | return b; |
| 3548 | } |
| 3549 | |
| 3550 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3551 | "'%.400s' encoder returned '%.400s' instead of 'bytes'; " |
| 3552 | "use codecs.encode() to encode to arbitrary types", |
| 3553 | encoding, |
| 3554 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3555 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3556 | return NULL; |
| 3557 | } |
| 3558 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3559 | PyObject * |
| 3560 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3561 | const char *encoding, |
| 3562 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3563 | { |
| 3564 | PyObject *v; |
| 3565 | |
| 3566 | if (!PyUnicode_Check(unicode)) { |
| 3567 | PyErr_BadArgument(); |
| 3568 | goto onError; |
| 3569 | } |
| 3570 | |
| 3571 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3572 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3573 | |
| 3574 | /* Encode via the codec registry */ |
| 3575 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3576 | if (v == NULL) |
| 3577 | goto onError; |
| 3578 | if (!PyUnicode_Check(v)) { |
| 3579 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3580 | "'%.400s' encoder returned '%.400s' instead of 'str'; " |
| 3581 | "use codecs.encode() to encode to arbitrary types", |
| 3582 | encoding, |
| 3583 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3584 | Py_DECREF(v); |
| 3585 | goto onError; |
| 3586 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3587 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3588 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3589 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3590 | return NULL; |
| 3591 | } |
| 3592 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3593 | static size_t |
| 3594 | mbstowcs_errorpos(const char *str, size_t len) |
| 3595 | { |
| 3596 | #ifdef HAVE_MBRTOWC |
| 3597 | const char *start = str; |
| 3598 | mbstate_t mbs; |
| 3599 | size_t converted; |
| 3600 | wchar_t ch; |
| 3601 | |
| 3602 | memset(&mbs, 0, sizeof mbs); |
| 3603 | while (len) |
| 3604 | { |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3605 | converted = mbrtowc(&ch, str, len, &mbs); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3606 | if (converted == 0) |
| 3607 | /* Reached end of string */ |
| 3608 | break; |
| 3609 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3610 | /* Conversion error or incomplete character */ |
| 3611 | return str - start; |
| 3612 | } |
| 3613 | else { |
| 3614 | str += converted; |
| 3615 | len -= converted; |
| 3616 | } |
| 3617 | } |
| 3618 | /* failed to find the undecodable byte sequence */ |
| 3619 | return 0; |
| 3620 | #endif |
| 3621 | return 0; |
| 3622 | } |
| 3623 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3624 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3625 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3626 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3627 | { |
| 3628 | wchar_t smallbuf[256]; |
| 3629 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3630 | wchar_t *wstr; |
| 3631 | size_t wlen, wlen2; |
| 3632 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3633 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3634 | size_t error_pos; |
| 3635 | char *errmsg; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 3636 | PyObject *reason = NULL; /* initialize to prevent gcc warning */ |
| 3637 | PyObject *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3638 | |
| 3639 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3640 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3641 | |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3642 | if (str[len] != '\0' || (size_t)len != strlen(str)) { |
| 3643 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3644 | return NULL; |
| 3645 | } |
| 3646 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3647 | if (surrogateescape) { |
| 3648 | /* "surrogateescape" error handler */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3649 | wstr = Py_DecodeLocale(str, &wlen); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3650 | if (wstr == NULL) { |
| 3651 | if (wlen == (size_t)-1) |
| 3652 | PyErr_NoMemory(); |
| 3653 | else |
| 3654 | PyErr_SetFromErrno(PyExc_OSError); |
| 3655 | return NULL; |
| 3656 | } |
| 3657 | |
| 3658 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3659 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3660 | } |
| 3661 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3662 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3663 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3664 | wlen = mbstowcs(NULL, str, 0); |
| 3665 | #else |
| 3666 | wlen = len; |
| 3667 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3668 | if (wlen == (size_t)-1) |
| 3669 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3670 | if (wlen+1 <= smallbuf_len) { |
| 3671 | wstr = smallbuf; |
| 3672 | } |
| 3673 | else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3674 | wstr = PyMem_New(wchar_t, wlen+1); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3675 | if (!wstr) |
| 3676 | return PyErr_NoMemory(); |
| 3677 | } |
| 3678 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3679 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3680 | if (wlen2 == (size_t)-1) { |
| 3681 | if (wstr != smallbuf) |
| 3682 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3683 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3684 | } |
| 3685 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3686 | assert(wlen2 == wlen); |
| 3687 | #endif |
| 3688 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3689 | if (wstr != smallbuf) |
| 3690 | PyMem_Free(wstr); |
| 3691 | } |
| 3692 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3693 | |
| 3694 | decode_error: |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3695 | reason = NULL; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3696 | errmsg = strerror(errno); |
| 3697 | assert(errmsg != NULL); |
| 3698 | |
| 3699 | error_pos = mbstowcs_errorpos(str, len); |
| 3700 | if (errmsg != NULL) { |
| 3701 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3702 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3703 | if (wstr != NULL) { |
| 3704 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3705 | PyMem_RawFree(wstr); |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3706 | } |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3707 | } |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3708 | if (reason == NULL) |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3709 | reason = PyUnicode_FromString( |
| 3710 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3711 | if (reason == NULL) |
| 3712 | return NULL; |
| 3713 | |
| 3714 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3715 | "locale", str, len, |
| 3716 | (Py_ssize_t)error_pos, |
| 3717 | (Py_ssize_t)(error_pos+1), |
| 3718 | reason); |
| 3719 | Py_DECREF(reason); |
| 3720 | if (exc != NULL) { |
| 3721 | PyCodec_StrictErrors(exc); |
| 3722 | Py_XDECREF(exc); |
| 3723 | } |
| 3724 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3728 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3729 | { |
| 3730 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3731 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | |
| 3735 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3736 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3737 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3738 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3739 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3740 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3741 | PyObject* |
| 3742 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3743 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3744 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3745 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3746 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3747 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3748 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3749 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3750 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3751 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3752 | the Python codec requires to encode at least its own filename. Use the C |
| 3753 | version of the locale codec until the codec registry is initialized and |
| 3754 | the Python codec is loaded. |
| 3755 | |
| 3756 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3757 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3758 | subinterpreters. */ |
| 3759 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3760 | return PyUnicode_Decode(s, size, |
| 3761 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3762 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3763 | } |
| 3764 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3765 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3766 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3767 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3770 | |
| 3771 | int |
| 3772 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3773 | { |
| 3774 | PyObject *output = NULL; |
| 3775 | Py_ssize_t size; |
| 3776 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3777 | if (arg == NULL) { |
| 3778 | Py_DECREF(*(PyObject**)addr); |
| 3779 | return 1; |
| 3780 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3781 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3782 | output = arg; |
| 3783 | Py_INCREF(output); |
| 3784 | } |
| 3785 | else { |
| 3786 | arg = PyUnicode_FromObject(arg); |
| 3787 | if (!arg) |
| 3788 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3789 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3790 | Py_DECREF(arg); |
| 3791 | if (!output) |
| 3792 | return 0; |
| 3793 | if (!PyBytes_Check(output)) { |
| 3794 | Py_DECREF(output); |
| 3795 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3796 | return 0; |
| 3797 | } |
| 3798 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3799 | size = PyBytes_GET_SIZE(output); |
| 3800 | data = PyBytes_AS_STRING(output); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 3801 | if ((size_t)size != strlen(data)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3802 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3803 | Py_DECREF(output); |
| 3804 | return 0; |
| 3805 | } |
| 3806 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3807 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3811 | int |
| 3812 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3813 | { |
| 3814 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3815 | if (arg == NULL) { |
| 3816 | Py_DECREF(*(PyObject**)addr); |
| 3817 | return 1; |
| 3818 | } |
| 3819 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3820 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3821 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3822 | output = arg; |
| 3823 | Py_INCREF(output); |
| 3824 | } |
| 3825 | else { |
| 3826 | arg = PyBytes_FromObject(arg); |
| 3827 | if (!arg) |
| 3828 | return 0; |
| 3829 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3830 | PyBytes_GET_SIZE(arg)); |
| 3831 | Py_DECREF(arg); |
| 3832 | if (!output) |
| 3833 | return 0; |
| 3834 | if (!PyUnicode_Check(output)) { |
| 3835 | Py_DECREF(output); |
| 3836 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3837 | return 0; |
| 3838 | } |
| 3839 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3840 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3841 | Py_DECREF(output); |
| 3842 | return 0; |
| 3843 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3844 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3845 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3846 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3847 | Py_DECREF(output); |
| 3848 | return 0; |
| 3849 | } |
| 3850 | *(PyObject**)addr = output; |
| 3851 | return Py_CLEANUP_SUPPORTED; |
| 3852 | } |
| 3853 | |
| 3854 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3855 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3856 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3857 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3858 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3860 | if (!PyUnicode_Check(unicode)) { |
| 3861 | PyErr_BadArgument(); |
| 3862 | return NULL; |
| 3863 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3864 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3865 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3866 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3867 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3868 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3869 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3870 | if (bytes == NULL) |
| 3871 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3872 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3873 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3874 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3875 | Py_DECREF(bytes); |
| 3876 | return NULL; |
| 3877 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3878 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3879 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3880 | PyBytes_AS_STRING(bytes), |
| 3881 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3882 | Py_DECREF(bytes); |
| 3883 | } |
| 3884 | |
| 3885 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3886 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3887 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3891 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3892 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3893 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3894 | } |
| 3895 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3896 | Py_UNICODE * |
| 3897 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3898 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3899 | const unsigned char *one_byte; |
| 3900 | #if SIZEOF_WCHAR_T == 4 |
| 3901 | const Py_UCS2 *two_bytes; |
| 3902 | #else |
| 3903 | const Py_UCS4 *four_bytes; |
| 3904 | const Py_UCS4 *ucs4_end; |
| 3905 | Py_ssize_t num_surrogates; |
| 3906 | #endif |
| 3907 | wchar_t *w; |
| 3908 | wchar_t *wchar_end; |
| 3909 | |
| 3910 | if (!PyUnicode_Check(unicode)) { |
| 3911 | PyErr_BadArgument(); |
| 3912 | return NULL; |
| 3913 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3914 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3915 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3916 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3917 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3918 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3919 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3920 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3921 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3922 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3923 | num_surrogates = 0; |
| 3924 | |
| 3925 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3926 | if (*four_bytes > 0xFFFF) |
| 3927 | ++num_surrogates; |
| 3928 | } |
| 3929 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3930 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3931 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3932 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3933 | PyErr_NoMemory(); |
| 3934 | return NULL; |
| 3935 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3936 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3937 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3938 | w = _PyUnicode_WSTR(unicode); |
| 3939 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3940 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3941 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3942 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3943 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3944 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3945 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3946 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3947 | } |
| 3948 | else |
| 3949 | *w = *four_bytes; |
| 3950 | |
| 3951 | if (w > wchar_end) { |
| 3952 | assert(0 && "Miscalculated string end"); |
| 3953 | } |
| 3954 | } |
| 3955 | *w = 0; |
| 3956 | #else |
| 3957 | /* sizeof(wchar_t) == 4 */ |
| 3958 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3959 | "should share memory already."); |
| 3960 | return NULL; |
| 3961 | #endif |
| 3962 | } |
| 3963 | else { |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 3964 | if ((size_t)_PyUnicode_LENGTH(unicode) > |
| 3965 | PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 3966 | PyErr_NoMemory(); |
| 3967 | return NULL; |
| 3968 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3969 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3970 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3971 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3972 | PyErr_NoMemory(); |
| 3973 | return NULL; |
| 3974 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3975 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3976 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3977 | w = _PyUnicode_WSTR(unicode); |
| 3978 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3979 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3980 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3981 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3982 | for (; w < wchar_end; ++one_byte, ++w) |
| 3983 | *w = *one_byte; |
| 3984 | /* null-terminate the wstr */ |
| 3985 | *w = 0; |
| 3986 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3987 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3988 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3989 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3990 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3991 | *w = *two_bytes; |
| 3992 | /* null-terminate the wstr */ |
| 3993 | *w = 0; |
| 3994 | #else |
| 3995 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3996 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3997 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3998 | Py_FatalError("Impossible unicode object state, wstr " |
| 3999 | "and str should share memory already."); |
| 4000 | return NULL; |
| 4001 | #endif |
| 4002 | } |
| 4003 | else { |
| 4004 | assert(0 && "This should never happen."); |
| 4005 | } |
| 4006 | } |
| 4007 | } |
| 4008 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 4009 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 4010 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4013 | Py_UNICODE * |
| 4014 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4015 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4016 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4020 | Py_ssize_t |
| 4021 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4022 | { |
| 4023 | if (!PyUnicode_Check(unicode)) { |
| 4024 | PyErr_BadArgument(); |
| 4025 | goto onError; |
| 4026 | } |
| 4027 | return PyUnicode_GET_SIZE(unicode); |
| 4028 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4029 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4030 | return -1; |
| 4031 | } |
| 4032 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4033 | Py_ssize_t |
| 4034 | PyUnicode_GetLength(PyObject *unicode) |
| 4035 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4036 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4037 | PyErr_BadArgument(); |
| 4038 | return -1; |
| 4039 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4040 | if (PyUnicode_READY(unicode) == -1) |
| 4041 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4042 | return PyUnicode_GET_LENGTH(unicode); |
| 4043 | } |
| 4044 | |
| 4045 | Py_UCS4 |
| 4046 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4047 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 4048 | void *data; |
| 4049 | int kind; |
| 4050 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4051 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4052 | PyErr_BadArgument(); |
| 4053 | return (Py_UCS4)-1; |
| 4054 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4055 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4056 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4057 | return (Py_UCS4)-1; |
| 4058 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 4059 | data = PyUnicode_DATA(unicode); |
| 4060 | kind = PyUnicode_KIND(unicode); |
| 4061 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | int |
| 4065 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4066 | { |
| 4067 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4068 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4069 | return -1; |
| 4070 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4071 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4072 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4073 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4074 | return -1; |
| 4075 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4076 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4077 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4078 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4079 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4080 | return -1; |
| 4081 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4082 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4083 | index, ch); |
| 4084 | return 0; |
| 4085 | } |
| 4086 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4087 | const char * |
| 4088 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4089 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4090 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4093 | /* create or adjust a UnicodeDecodeError */ |
| 4094 | static void |
| 4095 | make_decode_exception(PyObject **exceptionObject, |
| 4096 | const char *encoding, |
| 4097 | const char *input, Py_ssize_t length, |
| 4098 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4099 | const char *reason) |
| 4100 | { |
| 4101 | if (*exceptionObject == NULL) { |
| 4102 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4103 | encoding, input, length, startpos, endpos, reason); |
| 4104 | } |
| 4105 | else { |
| 4106 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4107 | goto onError; |
| 4108 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4109 | goto onError; |
| 4110 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4111 | goto onError; |
| 4112 | } |
| 4113 | return; |
| 4114 | |
| 4115 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4116 | Py_CLEAR(*exceptionObject); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4119 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4120 | /* error handling callback helper: |
| 4121 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4122 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4123 | and adjust various state variables. |
| 4124 | return 0 on success, -1 on error |
| 4125 | */ |
| 4126 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4127 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4128 | unicode_decode_call_errorhandler_wchar( |
| 4129 | const char *errors, PyObject **errorHandler, |
| 4130 | const char *encoding, const char *reason, |
| 4131 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4132 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4133 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4134 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4135 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4136 | |
| 4137 | PyObject *restuple = NULL; |
| 4138 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4139 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4140 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4141 | Py_ssize_t requiredsize; |
| 4142 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4143 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4144 | wchar_t *repwstr; |
| 4145 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4146 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4147 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 4148 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4149 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4150 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4151 | *errorHandler = PyCodec_LookupError(errors); |
| 4152 | if (*errorHandler == NULL) |
| 4153 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4156 | make_decode_exception(exceptionObject, |
| 4157 | encoding, |
| 4158 | *input, *inend - *input, |
| 4159 | *startinpos, *endinpos, |
| 4160 | reason); |
| 4161 | if (*exceptionObject == NULL) |
| 4162 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4163 | |
| 4164 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4165 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4166 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4167 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4168 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4169 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4170 | } |
| 4171 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4172 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4173 | |
| 4174 | /* Copy back the bytes variables, which might have been modified by the |
| 4175 | callback */ |
| 4176 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4177 | if (!inputobj) |
| 4178 | goto onError; |
| 4179 | if (!PyBytes_Check(inputobj)) { |
| 4180 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4181 | } |
| 4182 | *input = PyBytes_AS_STRING(inputobj); |
| 4183 | insize = PyBytes_GET_SIZE(inputobj); |
| 4184 | *inend = *input + insize; |
| 4185 | /* we can DECREF safely, as the exception has another reference, |
| 4186 | so the object won't go away. */ |
| 4187 | Py_DECREF(inputobj); |
| 4188 | |
| 4189 | if (newpos<0) |
| 4190 | newpos = insize+newpos; |
| 4191 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4192 | 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] | 4193 | goto onError; |
| 4194 | } |
| 4195 | |
| 4196 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4197 | if (repwstr == NULL) |
| 4198 | goto onError; |
| 4199 | /* need more space? (at least enough for what we |
| 4200 | have+the replacement+the rest of the string (starting |
| 4201 | at the new input position), so we won't have to check space |
| 4202 | when there are no errors in the rest of the string) */ |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4203 | requiredsize = *outpos; |
| 4204 | if (requiredsize > PY_SSIZE_T_MAX - repwlen) |
| 4205 | goto overflow; |
| 4206 | requiredsize += repwlen; |
| 4207 | if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) |
| 4208 | goto overflow; |
| 4209 | requiredsize += insize - newpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4210 | if (requiredsize > outsize) { |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4211 | if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4212 | requiredsize = 2*outsize; |
| 4213 | if (unicode_resize(output, requiredsize) < 0) |
| 4214 | goto onError; |
| 4215 | } |
| 4216 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4217 | *outpos += repwlen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4218 | *endinpos = newpos; |
| 4219 | *inptr = *input + newpos; |
| 4220 | |
| 4221 | /* we made it! */ |
| 4222 | Py_XDECREF(restuple); |
| 4223 | return 0; |
| 4224 | |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4225 | overflow: |
| 4226 | PyErr_SetString(PyExc_OverflowError, |
| 4227 | "decoded result is too long for a Python string"); |
| 4228 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4229 | onError: |
| 4230 | Py_XDECREF(restuple); |
| 4231 | return -1; |
| 4232 | } |
| 4233 | #endif /* HAVE_MBCS */ |
| 4234 | |
| 4235 | static int |
| 4236 | unicode_decode_call_errorhandler_writer( |
| 4237 | const char *errors, PyObject **errorHandler, |
| 4238 | const char *encoding, const char *reason, |
| 4239 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4240 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4241 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4242 | { |
| 4243 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4244 | |
| 4245 | PyObject *restuple = NULL; |
| 4246 | PyObject *repunicode = NULL; |
| 4247 | Py_ssize_t insize; |
| 4248 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4249 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4250 | PyObject *inputobj = NULL; |
| 4251 | |
| 4252 | if (*errorHandler == NULL) { |
| 4253 | *errorHandler = PyCodec_LookupError(errors); |
| 4254 | if (*errorHandler == NULL) |
| 4255 | goto onError; |
| 4256 | } |
| 4257 | |
| 4258 | make_decode_exception(exceptionObject, |
| 4259 | encoding, |
| 4260 | *input, *inend - *input, |
| 4261 | *startinpos, *endinpos, |
| 4262 | reason); |
| 4263 | if (*exceptionObject == NULL) |
| 4264 | goto onError; |
| 4265 | |
| 4266 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4267 | if (restuple == NULL) |
| 4268 | goto onError; |
| 4269 | if (!PyTuple_Check(restuple)) { |
| 4270 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4271 | goto onError; |
| 4272 | } |
| 4273 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4274 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4275 | |
| 4276 | /* Copy back the bytes variables, which might have been modified by the |
| 4277 | callback */ |
| 4278 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4279 | if (!inputobj) |
| 4280 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4281 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4282 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4283 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4284 | *input = PyBytes_AS_STRING(inputobj); |
| 4285 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4286 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4287 | /* we can DECREF safely, as the exception has another reference, |
| 4288 | so the object won't go away. */ |
| 4289 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4290 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4291 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4292 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4293 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4294 | 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] | 4295 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4296 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4297 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4298 | if (PyUnicode_READY(repunicode) < 0) |
| 4299 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4300 | replen = PyUnicode_GET_LENGTH(repunicode); |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4301 | if (replen > 1) { |
| 4302 | writer->min_length += replen - 1; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4303 | writer->overallocate = 1; |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4304 | if (_PyUnicodeWriter_Prepare(writer, writer->min_length, |
| 4305 | PyUnicode_MAX_CHAR_VALUE(repunicode)) == -1) |
| 4306 | goto onError; |
| 4307 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4308 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4309 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4310 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4311 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4312 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4313 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4314 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4315 | Py_XDECREF(restuple); |
| 4316 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4317 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4318 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4319 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4320 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4323 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4324 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4325 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4326 | |
| 4327 | /* Three simple macros defining base-64. */ |
| 4328 | |
| 4329 | /* Is c a base-64 character? */ |
| 4330 | |
| 4331 | #define IS_BASE64(c) \ |
| 4332 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4333 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4334 | ((c) >= '0' && (c) <= '9') || \ |
| 4335 | (c) == '+' || (c) == '/') |
| 4336 | |
| 4337 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4338 | |
| 4339 | #define FROM_BASE64(c) \ |
| 4340 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4341 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4342 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4343 | (c) == '+' ? 62 : 63) |
| 4344 | |
| 4345 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4346 | |
| 4347 | #define TO_BASE64(n) \ |
| 4348 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4349 | |
| 4350 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4351 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4352 | * byte not decoding to itself is the + which begins a base64 |
| 4353 | * string. */ |
| 4354 | |
| 4355 | #define DECODE_DIRECT(c) \ |
| 4356 | ((c) <= 127 && (c) != '+') |
| 4357 | |
| 4358 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4359 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4360 | * the above). See RFC2152. This array identifies these different |
| 4361 | * sets: |
| 4362 | * 0 : "Set D" |
| 4363 | * alphanumeric and '(),-./:? |
| 4364 | * 1 : "Set O" |
| 4365 | * !"#$%&*;<=>@[]^_`{|} |
| 4366 | * 2 : "whitespace" |
| 4367 | * ht nl cr sp |
| 4368 | * 3 : special (must be base64 encoded) |
| 4369 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4370 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4371 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4372 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4373 | char utf7_category[128] = { |
| 4374 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4375 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4376 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4377 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4378 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4379 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4380 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4381 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4382 | /* @ A B C D E F G H I J K L M N O */ |
| 4383 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4384 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4385 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4386 | /* ` a b c d e f g h i j k l m n o */ |
| 4387 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4388 | /* p q r s t u v w x y z { | } ~ del */ |
| 4389 | 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] | 4390 | }; |
| 4391 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4392 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4393 | * answer depends on whether we are encoding set O as itself, and also |
| 4394 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4395 | * clear that the answers to these questions vary between |
| 4396 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4397 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4398 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4399 | ((c) < 128 && (c) > 0 && \ |
| 4400 | ((utf7_category[(c)] == 0) || \ |
| 4401 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4402 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4403 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4404 | PyObject * |
| 4405 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4406 | Py_ssize_t size, |
| 4407 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4408 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4409 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4410 | } |
| 4411 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4412 | /* The decoder. The only state we preserve is our read position, |
| 4413 | * i.e. how many characters we have consumed. So if we end in the |
| 4414 | * middle of a shift sequence we have to back off the read position |
| 4415 | * and the output to the beginning of the sequence, otherwise we lose |
| 4416 | * all the shift state (seen bits, number of bits seen, high |
| 4417 | * surrogate). */ |
| 4418 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4419 | PyObject * |
| 4420 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4421 | Py_ssize_t size, |
| 4422 | const char *errors, |
| 4423 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4424 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4425 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4426 | Py_ssize_t startinpos; |
| 4427 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4428 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4429 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4430 | const char *errmsg = ""; |
| 4431 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4432 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | unsigned int base64bits = 0; |
| 4434 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4435 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4436 | PyObject *errorHandler = NULL; |
| 4437 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4439 | if (size == 0) { |
| 4440 | if (consumed) |
| 4441 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4442 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4443 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4444 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4445 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4446 | _PyUnicodeWriter_Init(&writer); |
| 4447 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4448 | |
| 4449 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4450 | e = s + size; |
| 4451 | |
| 4452 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4453 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4454 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4455 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4456 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4457 | if (inShift) { /* in a base-64 section */ |
| 4458 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4459 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4460 | base64bits += 6; |
| 4461 | s++; |
| 4462 | if (base64bits >= 16) { |
| 4463 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4464 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4465 | base64bits -= 16; |
| 4466 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4467 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4468 | if (surrogate) { |
| 4469 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4470 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4471 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4472 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4473 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4474 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4475 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4476 | } |
| 4477 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4478 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4479 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4480 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4481 | } |
| 4482 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4483 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4484 | /* first surrogate */ |
| 4485 | surrogate = outCh; |
| 4486 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4487 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4488 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4489 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4490 | } |
| 4491 | } |
| 4492 | } |
| 4493 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4494 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4495 | if (base64bits > 0) { /* left-over bits */ |
| 4496 | if (base64bits >= 6) { |
| 4497 | /* We've seen at least one base-64 character */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4498 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4499 | errmsg = "partial character in shift sequence"; |
| 4500 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4501 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4502 | else { |
| 4503 | /* Some bits remain; they should be zero */ |
| 4504 | if (base64buffer != 0) { |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4505 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4506 | errmsg = "non-zero padding bits in shift sequence"; |
| 4507 | goto utf7Error; |
| 4508 | } |
| 4509 | } |
| 4510 | } |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4511 | if (surrogate && DECODE_DIRECT(ch)) { |
| 4512 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
| 4513 | goto onError; |
| 4514 | } |
| 4515 | surrogate = 0; |
| 4516 | if (ch == '-') { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4517 | /* '-' is absorbed; other terminating |
| 4518 | characters are preserved */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4519 | s++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4520 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4524 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4525 | s++; /* consume '+' */ |
| 4526 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4527 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4528 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4529 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4530 | } |
| 4531 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4532 | inShift = 1; |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4533 | surrogate = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4534 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4535 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4536 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4537 | } |
| 4538 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4539 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4540 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4541 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4542 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4543 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4544 | else { |
| 4545 | startinpos = s-starts; |
| 4546 | s++; |
| 4547 | errmsg = "unexpected special character"; |
| 4548 | goto utf7Error; |
| 4549 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4550 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4551 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4552 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4553 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4554 | errors, &errorHandler, |
| 4555 | "utf7", errmsg, |
| 4556 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4557 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4558 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4561 | /* end of string */ |
| 4562 | |
| 4563 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4564 | /* if we're in an inconsistent state, that's an error */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4565 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4566 | if (surrogate || |
| 4567 | (base64bits >= 6) || |
| 4568 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4569 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4570 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4571 | errors, &errorHandler, |
| 4572 | "utf7", "unterminated shift sequence", |
| 4573 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4574 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4575 | goto onError; |
| 4576 | if (s < e) |
| 4577 | goto restart; |
| 4578 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4579 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4580 | |
| 4581 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4582 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4583 | if (inShift) { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4584 | *consumed = startinpos; |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4585 | if (writer.pos != shiftOutStart && writer.maxchar > 127) { |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4586 | PyObject *result = PyUnicode_FromKindAndData( |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4587 | writer.kind, writer.data, shiftOutStart); |
| 4588 | Py_XDECREF(errorHandler); |
| 4589 | Py_XDECREF(exc); |
| 4590 | _PyUnicodeWriter_Dealloc(&writer); |
| 4591 | return result; |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4592 | } |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4593 | writer.pos = shiftOutStart; /* back off output */ |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4594 | } |
| 4595 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4596 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4597 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4598 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4599 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4600 | Py_XDECREF(errorHandler); |
| 4601 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4602 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4603 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4604 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4605 | Py_XDECREF(errorHandler); |
| 4606 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4607 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4608 | return NULL; |
| 4609 | } |
| 4610 | |
| 4611 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4612 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4613 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4614 | int base64SetO, |
| 4615 | int base64WhiteSpace, |
| 4616 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4617 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4618 | int kind; |
| 4619 | void *data; |
| 4620 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4621 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4622 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4623 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4624 | unsigned int base64bits = 0; |
| 4625 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4626 | char * out; |
| 4627 | char * start; |
| 4628 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4629 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4630 | return NULL; |
| 4631 | kind = PyUnicode_KIND(str); |
| 4632 | data = PyUnicode_DATA(str); |
| 4633 | len = PyUnicode_GET_LENGTH(str); |
| 4634 | |
| 4635 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4636 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4637 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4638 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4639 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4640 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4641 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4642 | if (v == NULL) |
| 4643 | return NULL; |
| 4644 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4645 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4646 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4647 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4648 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4649 | if (inShift) { |
| 4650 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4651 | /* shifting out */ |
| 4652 | if (base64bits) { /* output remaining bits */ |
| 4653 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4654 | base64buffer = 0; |
| 4655 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4656 | } |
| 4657 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4658 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4659 | so no '-' is required, except if the character is itself a '-' */ |
| 4660 | if (IS_BASE64(ch) || ch == '-') { |
| 4661 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4662 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4663 | *out++ = (char) ch; |
| 4664 | } |
| 4665 | else { |
| 4666 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4667 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4668 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4669 | else { /* not in a shift sequence */ |
| 4670 | if (ch == '+') { |
| 4671 | *out++ = '+'; |
| 4672 | *out++ = '-'; |
| 4673 | } |
| 4674 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4675 | *out++ = (char) ch; |
| 4676 | } |
| 4677 | else { |
| 4678 | *out++ = '+'; |
| 4679 | inShift = 1; |
| 4680 | goto encode_char; |
| 4681 | } |
| 4682 | } |
| 4683 | continue; |
| 4684 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4685 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4686 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4687 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4688 | /* code first surrogate */ |
| 4689 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4690 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4691 | while (base64bits >= 6) { |
| 4692 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4693 | base64bits -= 6; |
| 4694 | } |
| 4695 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4696 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4697 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4698 | base64bits += 16; |
| 4699 | base64buffer = (base64buffer << 16) | ch; |
| 4700 | while (base64bits >= 6) { |
| 4701 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4702 | base64bits -= 6; |
| 4703 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4704 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4705 | if (base64bits) |
| 4706 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4707 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4708 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4709 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4710 | return NULL; |
| 4711 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4712 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4713 | PyObject * |
| 4714 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4715 | Py_ssize_t size, |
| 4716 | int base64SetO, |
| 4717 | int base64WhiteSpace, |
| 4718 | const char *errors) |
| 4719 | { |
| 4720 | PyObject *result; |
| 4721 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4722 | if (tmp == NULL) |
| 4723 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4724 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4725 | base64WhiteSpace, errors); |
| 4726 | Py_DECREF(tmp); |
| 4727 | return result; |
| 4728 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4729 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4730 | #undef IS_BASE64 |
| 4731 | #undef FROM_BASE64 |
| 4732 | #undef TO_BASE64 |
| 4733 | #undef DECODE_DIRECT |
| 4734 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4736 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4737 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4738 | PyObject * |
| 4739 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4740 | Py_ssize_t size, |
| 4741 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4742 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4743 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4744 | } |
| 4745 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4746 | #include "stringlib/asciilib.h" |
| 4747 | #include "stringlib/codecs.h" |
| 4748 | #include "stringlib/undef.h" |
| 4749 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4750 | #include "stringlib/ucs1lib.h" |
| 4751 | #include "stringlib/codecs.h" |
| 4752 | #include "stringlib/undef.h" |
| 4753 | |
| 4754 | #include "stringlib/ucs2lib.h" |
| 4755 | #include "stringlib/codecs.h" |
| 4756 | #include "stringlib/undef.h" |
| 4757 | |
| 4758 | #include "stringlib/ucs4lib.h" |
| 4759 | #include "stringlib/codecs.h" |
| 4760 | #include "stringlib/undef.h" |
| 4761 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4762 | /* Mask to quickly check whether a C 'long' contains a |
| 4763 | non-ASCII, UTF8-encoded char. */ |
| 4764 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4765 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4766 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4767 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4768 | #else |
| 4769 | # error C 'long' size should be either 4 or 8! |
| 4770 | #endif |
| 4771 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4772 | static Py_ssize_t |
| 4773 | 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] | 4774 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4775 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4776 | 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] | 4777 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4778 | /* |
| 4779 | * Issue #17237: m68k is a bit different from most architectures in |
| 4780 | * that objects do not use "natural alignment" - for example, int and |
| 4781 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4782 | * won't work; also, tests have shown that skipping the "optimised |
| 4783 | * version" will even speed up m68k. |
| 4784 | */ |
| 4785 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4786 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4787 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4788 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4789 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4790 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4791 | /* Help allocation */ |
| 4792 | const char *_p = p; |
| 4793 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4794 | while (_p < aligned_end) { |
| 4795 | unsigned long value = *(const unsigned long *) _p; |
| 4796 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4797 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4798 | *((unsigned long *)q) = value; |
| 4799 | _p += SIZEOF_LONG; |
| 4800 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4801 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4802 | p = _p; |
| 4803 | while (p < end) { |
| 4804 | if ((unsigned char)*p & 0x80) |
| 4805 | break; |
| 4806 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4807 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4808 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4809 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4810 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4811 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4812 | while (p < end) { |
| 4813 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4814 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4815 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4816 | /* Help allocation */ |
| 4817 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4818 | while (_p < aligned_end) { |
| 4819 | unsigned long value = *(unsigned long *) _p; |
| 4820 | if (value & ASCII_CHAR_MASK) |
| 4821 | break; |
| 4822 | _p += SIZEOF_LONG; |
| 4823 | } |
| 4824 | p = _p; |
| 4825 | if (_p == end) |
| 4826 | break; |
| 4827 | } |
| 4828 | if ((unsigned char)*p & 0x80) |
| 4829 | break; |
| 4830 | ++p; |
| 4831 | } |
| 4832 | memcpy(dest, start, p - start); |
| 4833 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4834 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4835 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4836 | PyObject * |
| 4837 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4838 | Py_ssize_t size, |
| 4839 | const char *errors, |
| 4840 | Py_ssize_t *consumed) |
| 4841 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4842 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4843 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4844 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4845 | |
| 4846 | Py_ssize_t startinpos; |
| 4847 | Py_ssize_t endinpos; |
| 4848 | const char *errmsg = ""; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4849 | PyObject *error_handler_obj = NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4850 | PyObject *exc = NULL; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4851 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4852 | |
| 4853 | if (size == 0) { |
| 4854 | if (consumed) |
| 4855 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4856 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4857 | } |
| 4858 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4859 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4860 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4861 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4862 | *consumed = 1; |
| 4863 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4864 | } |
| 4865 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4866 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4867 | writer.min_length = size; |
| 4868 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4869 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4870 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4871 | writer.pos = ascii_decode(s, end, writer.data); |
| 4872 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4873 | while (s < end) { |
| 4874 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4875 | int kind = writer.kind; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4876 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4877 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4878 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4879 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4880 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4881 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4882 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4883 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4884 | } else { |
| 4885 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4886 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | switch (ch) { |
| 4890 | case 0: |
| 4891 | if (s == end || consumed) |
| 4892 | goto End; |
| 4893 | errmsg = "unexpected end of data"; |
| 4894 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4895 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4896 | break; |
| 4897 | case 1: |
| 4898 | errmsg = "invalid start byte"; |
| 4899 | startinpos = s - starts; |
| 4900 | endinpos = startinpos + 1; |
| 4901 | break; |
| 4902 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4903 | case 3: |
| 4904 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4905 | errmsg = "invalid continuation byte"; |
| 4906 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4907 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4908 | break; |
| 4909 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4910 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4911 | goto onError; |
| 4912 | continue; |
| 4913 | } |
| 4914 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4915 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 4916 | error_handler = get_error_handler(errors); |
| 4917 | |
| 4918 | switch (error_handler) { |
| 4919 | case _Py_ERROR_IGNORE: |
| 4920 | s += (endinpos - startinpos); |
| 4921 | break; |
| 4922 | |
| 4923 | case _Py_ERROR_REPLACE: |
| 4924 | if (_PyUnicodeWriter_WriteCharInline(&writer, 0xfffd) < 0) |
| 4925 | goto onError; |
| 4926 | s += (endinpos - startinpos); |
| 4927 | break; |
| 4928 | |
| 4929 | case _Py_ERROR_SURROGATEESCAPE: |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4930 | { |
| 4931 | Py_ssize_t i; |
| 4932 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4933 | if (_PyUnicodeWriter_PrepareKind(&writer, PyUnicode_2BYTE_KIND) < 0) |
| 4934 | goto onError; |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4935 | for (i=startinpos; i<endinpos; i++) { |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4936 | ch = (Py_UCS4)(unsigned char)(starts[i]); |
| 4937 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, |
| 4938 | ch + 0xdc00); |
| 4939 | writer.pos++; |
| 4940 | } |
| 4941 | s += (endinpos - startinpos); |
| 4942 | break; |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4943 | } |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4944 | |
| 4945 | default: |
| 4946 | if (unicode_decode_call_errorhandler_writer( |
| 4947 | errors, &error_handler_obj, |
| 4948 | "utf-8", errmsg, |
| 4949 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4950 | &writer)) |
| 4951 | goto onError; |
| 4952 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4953 | } |
| 4954 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4955 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4956 | if (consumed) |
| 4957 | *consumed = s - starts; |
| 4958 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4959 | Py_XDECREF(error_handler_obj); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4960 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4961 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4962 | |
| 4963 | onError: |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4964 | Py_XDECREF(error_handler_obj); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4965 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4966 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4967 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4968 | } |
| 4969 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4970 | #ifdef __APPLE__ |
| 4971 | |
| 4972 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4973 | used to decode the command line arguments on Mac OS X. |
| 4974 | |
| 4975 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4976 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4977 | |
| 4978 | wchar_t* |
| 4979 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4980 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4981 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4982 | wchar_t *unicode; |
| 4983 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4984 | |
| 4985 | /* Note: size will always be longer than the resulting Unicode |
| 4986 | character count */ |
Victor Stinner | f50e187 | 2015-03-20 11:32:24 +0100 | [diff] [blame] | 4987 | 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] | 4988 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4989 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4990 | if (!unicode) |
| 4991 | return NULL; |
| 4992 | |
| 4993 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4994 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4995 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4996 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4997 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4998 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4999 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5000 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5001 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5002 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5003 | if (ch > 0xFF) { |
| 5004 | #if SIZEOF_WCHAR_T == 4 |
| 5005 | assert(0); |
| 5006 | #else |
| 5007 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 5008 | /* compute and append the two surrogates: */ |
| 5009 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5010 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 5011 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5012 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5013 | else { |
| 5014 | if (!ch && s == e) |
| 5015 | break; |
| 5016 | /* surrogateescape */ |
| 5017 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 5018 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5019 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5020 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5021 | return unicode; |
| 5022 | } |
| 5023 | |
| 5024 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5025 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5026 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5027 | |
| 5028 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5029 | and allocate exactly as much space needed at the end. Else allocate the |
| 5030 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5031 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5032 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5033 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5034 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5035 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5036 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5037 | void *data; |
| 5038 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5039 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5040 | if (!PyUnicode_Check(unicode)) { |
| 5041 | PyErr_BadArgument(); |
| 5042 | return NULL; |
| 5043 | } |
| 5044 | |
| 5045 | if (PyUnicode_READY(unicode) == -1) |
| 5046 | return NULL; |
| 5047 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5048 | if (PyUnicode_UTF8(unicode)) |
| 5049 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5050 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5051 | |
| 5052 | kind = PyUnicode_KIND(unicode); |
| 5053 | data = PyUnicode_DATA(unicode); |
| 5054 | size = PyUnicode_GET_LENGTH(unicode); |
| 5055 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5056 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5057 | default: |
| 5058 | assert(0); |
| 5059 | case PyUnicode_1BYTE_KIND: |
| 5060 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5061 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5062 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5063 | case PyUnicode_2BYTE_KIND: |
| 5064 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5065 | case PyUnicode_4BYTE_KIND: |
| 5066 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5067 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5070 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5071 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5072 | Py_ssize_t size, |
| 5073 | const char *errors) |
| 5074 | { |
| 5075 | PyObject *v, *unicode; |
| 5076 | |
| 5077 | unicode = PyUnicode_FromUnicode(s, size); |
| 5078 | if (unicode == NULL) |
| 5079 | return NULL; |
| 5080 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5081 | Py_DECREF(unicode); |
| 5082 | return v; |
| 5083 | } |
| 5084 | |
| 5085 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5086 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5087 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5088 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5089 | } |
| 5090 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5092 | |
| 5093 | PyObject * |
| 5094 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | Py_ssize_t size, |
| 5096 | const char *errors, |
| 5097 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5098 | { |
| 5099 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5100 | } |
| 5101 | |
| 5102 | PyObject * |
| 5103 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5104 | Py_ssize_t size, |
| 5105 | const char *errors, |
| 5106 | int *byteorder, |
| 5107 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5108 | { |
| 5109 | const char *starts = s; |
| 5110 | Py_ssize_t startinpos; |
| 5111 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5112 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5113 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5114 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5115 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5116 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5117 | PyObject *errorHandler = NULL; |
| 5118 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5119 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5120 | q = (unsigned char *)s; |
| 5121 | e = q + size; |
| 5122 | |
| 5123 | if (byteorder) |
| 5124 | bo = *byteorder; |
| 5125 | |
| 5126 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5127 | byte order setting accordingly. In native mode, the leading BOM |
| 5128 | mark is skipped, in all other modes, it is copied to the output |
| 5129 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5130 | if (bo == 0 && size >= 4) { |
| 5131 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5132 | if (bom == 0x0000FEFF) { |
| 5133 | bo = -1; |
| 5134 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5135 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5136 | else if (bom == 0xFFFE0000) { |
| 5137 | bo = 1; |
| 5138 | q += 4; |
| 5139 | } |
| 5140 | if (byteorder) |
| 5141 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5144 | if (q == e) { |
| 5145 | if (consumed) |
| 5146 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5147 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5150 | #ifdef WORDS_BIGENDIAN |
| 5151 | le = bo < 0; |
| 5152 | #else |
| 5153 | le = bo <= 0; |
| 5154 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5155 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5156 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5157 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5158 | writer.min_length = (e - q + 3) / 4; |
| 5159 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5160 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5161 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5162 | while (1) { |
| 5163 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5164 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5165 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5166 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5167 | enum PyUnicode_Kind kind = writer.kind; |
| 5168 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5169 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5170 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5171 | if (le) { |
| 5172 | do { |
| 5173 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5174 | if (ch > maxch) |
| 5175 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5176 | if (kind != PyUnicode_1BYTE_KIND && |
| 5177 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5178 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5179 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5180 | q += 4; |
| 5181 | } while (q <= last); |
| 5182 | } |
| 5183 | else { |
| 5184 | do { |
| 5185 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5186 | if (ch > maxch) |
| 5187 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5188 | if (kind != PyUnicode_1BYTE_KIND && |
| 5189 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5190 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5191 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5192 | q += 4; |
| 5193 | } while (q <= last); |
| 5194 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5195 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5196 | } |
| 5197 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5198 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5199 | errmsg = "code point in surrogate code point range(0xd800, 0xe000)"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5200 | startinpos = ((const char *)q) - starts; |
| 5201 | endinpos = startinpos + 4; |
| 5202 | } |
| 5203 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5204 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5205 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5206 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5207 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5208 | startinpos = ((const char *)q) - starts; |
| 5209 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5210 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5211 | else { |
| 5212 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5213 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5214 | goto onError; |
| 5215 | q += 4; |
| 5216 | continue; |
| 5217 | } |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5218 | errmsg = "code point not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5219 | startinpos = ((const char *)q) - starts; |
| 5220 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5221 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5222 | |
| 5223 | /* The remaining input chars are ignored if the callback |
| 5224 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5225 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5226 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5227 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5228 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5229 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5230 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5231 | } |
| 5232 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5233 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5234 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5235 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5236 | Py_XDECREF(errorHandler); |
| 5237 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5238 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5239 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5240 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5241 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5242 | Py_XDECREF(errorHandler); |
| 5243 | Py_XDECREF(exc); |
| 5244 | return NULL; |
| 5245 | } |
| 5246 | |
| 5247 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5248 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5249 | const char *errors, |
| 5250 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5251 | { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5252 | enum PyUnicode_Kind kind; |
| 5253 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5254 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5255 | PyObject *v; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5256 | PY_UINT32_T *out; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5257 | #if PY_LITTLE_ENDIAN |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5258 | int native_ordering = byteorder <= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5259 | #else |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5260 | int native_ordering = byteorder >= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5261 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5262 | const char *encoding; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5263 | Py_ssize_t nsize, pos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5264 | PyObject *errorHandler = NULL; |
| 5265 | PyObject *exc = NULL; |
| 5266 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5267 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5268 | if (!PyUnicode_Check(str)) { |
| 5269 | PyErr_BadArgument(); |
| 5270 | return NULL; |
| 5271 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5272 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5273 | return NULL; |
| 5274 | kind = PyUnicode_KIND(str); |
| 5275 | data = PyUnicode_DATA(str); |
| 5276 | len = PyUnicode_GET_LENGTH(str); |
| 5277 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5278 | if (len > PY_SSIZE_T_MAX / 4 - (byteorder == 0)) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5279 | return PyErr_NoMemory(); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5280 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5281 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5282 | if (v == NULL) |
| 5283 | return NULL; |
| 5284 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5285 | /* output buffer is 4-bytes aligned */ |
| 5286 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 4)); |
| 5287 | out = (PY_UINT32_T *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5288 | if (byteorder == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5289 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5290 | if (len == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5291 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5292 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5293 | if (byteorder == -1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5294 | encoding = "utf-32-le"; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5295 | else if (byteorder == 1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5296 | encoding = "utf-32-be"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5297 | else |
| 5298 | encoding = "utf-32"; |
| 5299 | |
| 5300 | if (kind == PyUnicode_1BYTE_KIND) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5301 | ucs1lib_utf32_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5302 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5305 | pos = 0; |
| 5306 | while (pos < len) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5307 | Py_ssize_t repsize, moreunits; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5308 | |
| 5309 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5310 | pos += ucs2lib_utf32_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5311 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5312 | } |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5313 | else { |
| 5314 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5315 | pos += ucs4lib_utf32_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5316 | &out, native_ordering); |
| 5317 | } |
| 5318 | if (pos == len) |
| 5319 | break; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5320 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5321 | rep = unicode_encode_call_errorhandler( |
| 5322 | errors, &errorHandler, |
| 5323 | encoding, "surrogates not allowed", |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5324 | str, &exc, pos, pos + 1, &pos); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5325 | if (!rep) |
| 5326 | goto error; |
| 5327 | |
| 5328 | if (PyBytes_Check(rep)) { |
| 5329 | repsize = PyBytes_GET_SIZE(rep); |
| 5330 | if (repsize & 3) { |
| 5331 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5332 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5333 | "surrogates not allowed"); |
| 5334 | goto error; |
| 5335 | } |
| 5336 | moreunits = repsize / 4; |
| 5337 | } |
| 5338 | else { |
| 5339 | assert(PyUnicode_Check(rep)); |
| 5340 | if (PyUnicode_READY(rep) < 0) |
| 5341 | goto error; |
| 5342 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5343 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5344 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5345 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5346 | "surrogates not allowed"); |
| 5347 | goto error; |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | /* four bytes are reserved for each surrogate */ |
| 5352 | if (moreunits > 1) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5353 | Py_ssize_t outpos = out - (PY_UINT32_T*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5354 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5355 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5356 | /* integer overflow */ |
| 5357 | PyErr_NoMemory(); |
| 5358 | goto error; |
| 5359 | } |
| 5360 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5361 | goto error; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5362 | out = (PY_UINT32_T*) PyBytes_AS_STRING(v) + outpos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | if (PyBytes_Check(rep)) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5366 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5367 | out += moreunits; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5368 | } else /* rep is unicode */ { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5369 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5370 | ucs1lib_utf32_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5371 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | Py_CLEAR(rep); |
| 5375 | } |
| 5376 | |
| 5377 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5378 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5379 | handler is used. */ |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5380 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5381 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5382 | _PyBytes_Resize(&v, nsize); |
| 5383 | Py_XDECREF(errorHandler); |
| 5384 | Py_XDECREF(exc); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5385 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5386 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5387 | error: |
| 5388 | Py_XDECREF(rep); |
| 5389 | Py_XDECREF(errorHandler); |
| 5390 | Py_XDECREF(exc); |
| 5391 | Py_XDECREF(v); |
| 5392 | return NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5393 | } |
| 5394 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5395 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5396 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5397 | Py_ssize_t size, |
| 5398 | const char *errors, |
| 5399 | int byteorder) |
| 5400 | { |
| 5401 | PyObject *result; |
| 5402 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5403 | if (tmp == NULL) |
| 5404 | return NULL; |
| 5405 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5406 | Py_DECREF(tmp); |
| 5407 | return result; |
| 5408 | } |
| 5409 | |
| 5410 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5411 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5412 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5413 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5414 | } |
| 5415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5416 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5417 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5418 | PyObject * |
| 5419 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5420 | Py_ssize_t size, |
| 5421 | const char *errors, |
| 5422 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5424 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5425 | } |
| 5426 | |
| 5427 | PyObject * |
| 5428 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5429 | Py_ssize_t size, |
| 5430 | const char *errors, |
| 5431 | int *byteorder, |
| 5432 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5433 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5434 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5435 | Py_ssize_t startinpos; |
| 5436 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5437 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5438 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5439 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5440 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5441 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5442 | PyObject *errorHandler = NULL; |
| 5443 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5444 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5445 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5446 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5447 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | |
| 5449 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5450 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5451 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5452 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5453 | byte order setting accordingly. In native mode, the leading BOM |
| 5454 | mark is skipped, in all other modes, it is copied to the output |
| 5455 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5456 | if (bo == 0 && size >= 2) { |
| 5457 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5458 | if (bom == 0xFEFF) { |
| 5459 | q += 2; |
| 5460 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5461 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5462 | else if (bom == 0xFFFE) { |
| 5463 | q += 2; |
| 5464 | bo = 1; |
| 5465 | } |
| 5466 | if (byteorder) |
| 5467 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5468 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5470 | if (q == e) { |
| 5471 | if (consumed) |
| 5472 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5473 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5474 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5475 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5476 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5477 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5478 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5479 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5480 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5481 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5482 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5483 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5484 | /* Note: size will always be longer than the resulting Unicode |
| 5485 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5486 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5487 | writer.min_length = (e - q + 1) / 2; |
| 5488 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5489 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5490 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5491 | while (1) { |
| 5492 | Py_UCS4 ch = 0; |
| 5493 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5494 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5495 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5496 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5497 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5498 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5499 | native_ordering); |
| 5500 | else |
| 5501 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5502 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5503 | native_ordering); |
| 5504 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5505 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5506 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5507 | native_ordering); |
| 5508 | } else { |
| 5509 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5510 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5511 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5512 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5513 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5514 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5515 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5516 | switch (ch) |
| 5517 | { |
| 5518 | case 0: |
| 5519 | /* remaining byte at the end? (size should be even) */ |
| 5520 | if (q == e || consumed) |
| 5521 | goto End; |
| 5522 | errmsg = "truncated data"; |
| 5523 | startinpos = ((const char *)q) - starts; |
| 5524 | endinpos = ((const char *)e) - starts; |
| 5525 | break; |
| 5526 | /* The remaining input chars are ignored if the callback |
| 5527 | chooses to skip the input */ |
| 5528 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5529 | q -= 2; |
| 5530 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5531 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5532 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5533 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5534 | endinpos = ((const char *)e) - starts; |
| 5535 | break; |
| 5536 | case 2: |
| 5537 | errmsg = "illegal encoding"; |
| 5538 | startinpos = ((const char *)q) - 2 - starts; |
| 5539 | endinpos = startinpos + 2; |
| 5540 | break; |
| 5541 | case 3: |
| 5542 | errmsg = "illegal UTF-16 surrogate"; |
| 5543 | startinpos = ((const char *)q) - 4 - starts; |
| 5544 | endinpos = startinpos + 2; |
| 5545 | break; |
| 5546 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5547 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5548 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5549 | continue; |
| 5550 | } |
| 5551 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5552 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5553 | errors, |
| 5554 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5555 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5556 | &starts, |
| 5557 | (const char **)&e, |
| 5558 | &startinpos, |
| 5559 | &endinpos, |
| 5560 | &exc, |
| 5561 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5562 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5563 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5566 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5567 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5568 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5569 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5570 | Py_XDECREF(errorHandler); |
| 5571 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5572 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5573 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5574 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5575 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5576 | Py_XDECREF(errorHandler); |
| 5577 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | return NULL; |
| 5579 | } |
| 5580 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5581 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5582 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5583 | const char *errors, |
| 5584 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5585 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5586 | enum PyUnicode_Kind kind; |
| 5587 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5588 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5589 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5590 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5591 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5592 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5593 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5594 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5595 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5596 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5597 | const char *encoding; |
| 5598 | Py_ssize_t nsize, pos; |
| 5599 | PyObject *errorHandler = NULL; |
| 5600 | PyObject *exc = NULL; |
| 5601 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5602 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5603 | if (!PyUnicode_Check(str)) { |
| 5604 | PyErr_BadArgument(); |
| 5605 | return NULL; |
| 5606 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5607 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5608 | return NULL; |
| 5609 | kind = PyUnicode_KIND(str); |
| 5610 | data = PyUnicode_DATA(str); |
| 5611 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5612 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5613 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5614 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5615 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5616 | const Py_UCS4 *end = in + len; |
| 5617 | while (in < end) |
| 5618 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5619 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5620 | } |
| 5621 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5622 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5623 | nsize = len + pairs + (byteorder == 0); |
| 5624 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | if (v == NULL) |
| 5626 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5628 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5629 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5630 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5632 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5633 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5634 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5635 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5636 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5637 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5638 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5639 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5640 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5641 | if (byteorder < 0) |
| 5642 | encoding = "utf-16-le"; |
| 5643 | else if (byteorder > 0) |
| 5644 | encoding = "utf-16-be"; |
| 5645 | else |
| 5646 | encoding = "utf-16"; |
| 5647 | |
| 5648 | pos = 0; |
| 5649 | while (pos < len) { |
| 5650 | Py_ssize_t repsize, moreunits; |
| 5651 | |
| 5652 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5653 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5654 | &out, native_ordering); |
| 5655 | } |
| 5656 | else { |
| 5657 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5658 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5659 | &out, native_ordering); |
| 5660 | } |
| 5661 | if (pos == len) |
| 5662 | break; |
| 5663 | |
| 5664 | rep = unicode_encode_call_errorhandler( |
| 5665 | errors, &errorHandler, |
| 5666 | encoding, "surrogates not allowed", |
| 5667 | str, &exc, pos, pos + 1, &pos); |
| 5668 | if (!rep) |
| 5669 | goto error; |
| 5670 | |
| 5671 | if (PyBytes_Check(rep)) { |
| 5672 | repsize = PyBytes_GET_SIZE(rep); |
| 5673 | if (repsize & 1) { |
| 5674 | raise_encode_exception(&exc, encoding, |
| 5675 | str, pos - 1, pos, |
| 5676 | "surrogates not allowed"); |
| 5677 | goto error; |
| 5678 | } |
| 5679 | moreunits = repsize / 2; |
| 5680 | } |
| 5681 | else { |
| 5682 | assert(PyUnicode_Check(rep)); |
| 5683 | if (PyUnicode_READY(rep) < 0) |
| 5684 | goto error; |
| 5685 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5686 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5687 | raise_encode_exception(&exc, encoding, |
| 5688 | str, pos - 1, pos, |
| 5689 | "surrogates not allowed"); |
| 5690 | goto error; |
| 5691 | } |
| 5692 | } |
| 5693 | |
| 5694 | /* two bytes are reserved for each surrogate */ |
| 5695 | if (moreunits > 1) { |
| 5696 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5697 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5698 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5699 | /* integer overflow */ |
| 5700 | PyErr_NoMemory(); |
| 5701 | goto error; |
| 5702 | } |
| 5703 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5704 | goto error; |
| 5705 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5706 | } |
| 5707 | |
| 5708 | if (PyBytes_Check(rep)) { |
| 5709 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5710 | out += moreunits; |
| 5711 | } else /* rep is unicode */ { |
| 5712 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5713 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5714 | &out, native_ordering); |
| 5715 | } |
| 5716 | |
| 5717 | Py_CLEAR(rep); |
| 5718 | } |
| 5719 | |
| 5720 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5721 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5722 | is used. */ |
| 5723 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5724 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5725 | _PyBytes_Resize(&v, nsize); |
| 5726 | Py_XDECREF(errorHandler); |
| 5727 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5728 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5729 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5730 | error: |
| 5731 | Py_XDECREF(rep); |
| 5732 | Py_XDECREF(errorHandler); |
| 5733 | Py_XDECREF(exc); |
| 5734 | Py_XDECREF(v); |
| 5735 | return NULL; |
| 5736 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | } |
| 5738 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5739 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5740 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5741 | Py_ssize_t size, |
| 5742 | const char *errors, |
| 5743 | int byteorder) |
| 5744 | { |
| 5745 | PyObject *result; |
| 5746 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5747 | if (tmp == NULL) |
| 5748 | return NULL; |
| 5749 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5750 | Py_DECREF(tmp); |
| 5751 | return result; |
| 5752 | } |
| 5753 | |
| 5754 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5755 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5756 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5757 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5761 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5762 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5763 | if all the escapes in the string make it still a valid ASCII string. |
| 5764 | Returns -1 if any escapes were found which cause the string to |
| 5765 | pop out of ASCII range. Otherwise returns the length of the |
| 5766 | required buffer to hold the string. |
| 5767 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5768 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5769 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5770 | { |
| 5771 | const unsigned char *p = (const unsigned char *)s; |
| 5772 | const unsigned char *end = p + size; |
| 5773 | Py_ssize_t length = 0; |
| 5774 | |
| 5775 | if (size < 0) |
| 5776 | return -1; |
| 5777 | |
| 5778 | for (; p < end; ++p) { |
| 5779 | if (*p > 127) { |
| 5780 | /* Non-ASCII */ |
| 5781 | return -1; |
| 5782 | } |
| 5783 | else if (*p != '\\') { |
| 5784 | /* Normal character */ |
| 5785 | ++length; |
| 5786 | } |
| 5787 | else { |
| 5788 | /* Backslash-escape, check next char */ |
| 5789 | ++p; |
| 5790 | /* Escape sequence reaches till end of string or |
| 5791 | non-ASCII follow-up. */ |
| 5792 | if (p >= end || *p > 127) |
| 5793 | return -1; |
| 5794 | switch (*p) { |
| 5795 | case '\n': |
| 5796 | /* backslash + \n result in zero characters */ |
| 5797 | break; |
| 5798 | case '\\': case '\'': case '\"': |
| 5799 | case 'b': case 'f': case 't': |
| 5800 | case 'n': case 'r': case 'v': case 'a': |
| 5801 | ++length; |
| 5802 | break; |
| 5803 | case '0': case '1': case '2': case '3': |
| 5804 | case '4': case '5': case '6': case '7': |
| 5805 | case 'x': case 'u': case 'U': case 'N': |
| 5806 | /* these do not guarantee ASCII characters */ |
| 5807 | return -1; |
| 5808 | default: |
| 5809 | /* count the backslash + the other character */ |
| 5810 | length += 2; |
| 5811 | } |
| 5812 | } |
| 5813 | } |
| 5814 | return length; |
| 5815 | } |
| 5816 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5817 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5818 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5819 | PyObject * |
| 5820 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5821 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5822 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5823 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5824 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5825 | Py_ssize_t startinpos; |
| 5826 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5827 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5829 | char* message; |
| 5830 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5831 | PyObject *errorHandler = NULL; |
| 5832 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5833 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5834 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5835 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5836 | if (len == 0) |
| 5837 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5838 | |
| 5839 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5840 | either the string is pure ASCII with named escapes like \n, etc. |
| 5841 | and we determined it's exact size (common case) |
| 5842 | or it contains \x, \u, ... escape sequences. then we create a |
| 5843 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5844 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5845 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5846 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5847 | } |
| 5848 | else { |
| 5849 | /* Escaped strings will always be longer than the resulting |
| 5850 | Unicode string, so we start with size here and then reduce the |
| 5851 | length after conversion to the true value. |
| 5852 | (but if the error callback returns a long replacement string |
| 5853 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5854 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5855 | } |
| 5856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5857 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5858 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5859 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5860 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | while (s < end) { |
| 5862 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5863 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5864 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | |
| 5866 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5867 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5868 | x = (unsigned char)*s; |
| 5869 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5870 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5871 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | continue; |
| 5873 | } |
| 5874 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5875 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | /* \ - Escapes */ |
| 5877 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5878 | c = *s++; |
| 5879 | if (s > end) |
| 5880 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5881 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5882 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5884 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5885 | #define WRITECHAR(ch) \ |
| 5886 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5887 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5888 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5889 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5890 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5892 | case '\\': WRITECHAR('\\'); break; |
| 5893 | case '\'': WRITECHAR('\''); break; |
| 5894 | case '\"': WRITECHAR('\"'); break; |
| 5895 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5896 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5897 | case 'f': WRITECHAR('\014'); break; |
| 5898 | case 't': WRITECHAR('\t'); break; |
| 5899 | case 'n': WRITECHAR('\n'); break; |
| 5900 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5901 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5902 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5903 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5904 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5906 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | case '0': case '1': case '2': case '3': |
| 5908 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5909 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5910 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5911 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5912 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5913 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5915 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5916 | break; |
| 5917 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5918 | /* hex escapes */ |
| 5919 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5921 | digits = 2; |
| 5922 | message = "truncated \\xXX escape"; |
| 5923 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5927 | digits = 4; |
| 5928 | message = "truncated \\uXXXX escape"; |
| 5929 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5931 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5932 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5933 | digits = 8; |
| 5934 | message = "truncated \\UXXXXXXXX escape"; |
| 5935 | hexescape: |
| 5936 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5937 | if (end - s < digits) { |
| 5938 | /* count only hex digits */ |
| 5939 | for (; s < end; ++s) { |
| 5940 | c = (unsigned char)*s; |
| 5941 | if (!Py_ISXDIGIT(c)) |
| 5942 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5943 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5944 | goto error; |
| 5945 | } |
| 5946 | for (; digits--; ++s) { |
| 5947 | c = (unsigned char)*s; |
| 5948 | if (!Py_ISXDIGIT(c)) |
| 5949 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5950 | chr = (chr<<4) & ~0xF; |
| 5951 | if (c >= '0' && c <= '9') |
| 5952 | chr += c - '0'; |
| 5953 | else if (c >= 'a' && c <= 'f') |
| 5954 | chr += 10 + c - 'a'; |
| 5955 | else |
| 5956 | chr += 10 + c - 'A'; |
| 5957 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5958 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5959 | /* _decoding_error will have already written into the |
| 5960 | target buffer. */ |
| 5961 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5962 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5963 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5964 | message = "illegal Unicode character"; |
| 5965 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5966 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5967 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5968 | break; |
| 5969 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5970 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5971 | case 'N': |
| 5972 | message = "malformed \\N character escape"; |
| 5973 | if (ucnhash_CAPI == NULL) { |
| 5974 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5975 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5976 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5977 | if (ucnhash_CAPI == NULL) |
| 5978 | goto ucnhashError; |
| 5979 | } |
| 5980 | if (*s == '{') { |
| 5981 | const char *start = s+1; |
| 5982 | /* look for the closing brace */ |
| 5983 | while (*s != '}' && s < end) |
| 5984 | s++; |
| 5985 | if (s > start && s < end && *s == '}') { |
| 5986 | /* found a name. look it up in the unicode database */ |
| 5987 | message = "unknown Unicode character name"; |
| 5988 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5989 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5990 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5991 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5992 | goto store; |
| 5993 | } |
| 5994 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5995 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5996 | |
| 5997 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5998 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5999 | message = "\\ at end of string"; |
| 6000 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6001 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6002 | } |
| 6003 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6004 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 6005 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6006 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6007 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6009 | continue; |
| 6010 | |
| 6011 | error: |
| 6012 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 6013 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6014 | errors, &errorHandler, |
| 6015 | "unicodeescape", message, |
| 6016 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 6017 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6018 | goto onError; |
| 6019 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6020 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6021 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6022 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6023 | Py_XDECREF(errorHandler); |
| 6024 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6025 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6026 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6027 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6028 | PyErr_SetString( |
| 6029 | PyExc_UnicodeError, |
| 6030 | "\\N escapes not supported (can't load unicodedata module)" |
| 6031 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6032 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6033 | Py_XDECREF(errorHandler); |
| 6034 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6035 | return NULL; |
| 6036 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6037 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6038 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6039 | Py_XDECREF(errorHandler); |
| 6040 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | return NULL; |
| 6042 | } |
| 6043 | |
| 6044 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6045 | |
| 6046 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6047 | appropriate. |
| 6048 | |
| 6049 | */ |
| 6050 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6051 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6052 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6054 | Py_ssize_t i, len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6056 | int kind; |
| 6057 | void *data; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6058 | _PyBytesWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 6060 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6061 | escape. |
| 6062 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 6063 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 6064 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 6065 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6066 | */ |
| 6067 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6068 | if (!PyUnicode_Check(unicode)) { |
| 6069 | PyErr_BadArgument(); |
| 6070 | return NULL; |
| 6071 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6072 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6073 | return NULL; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6074 | |
| 6075 | _PyBytesWriter_Init(&writer); |
| 6076 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6077 | len = PyUnicode_GET_LENGTH(unicode); |
| 6078 | kind = PyUnicode_KIND(unicode); |
| 6079 | data = PyUnicode_DATA(unicode); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6080 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6081 | p = _PyBytesWriter_Alloc(&writer, len); |
| 6082 | if (p == NULL) |
| 6083 | goto error; |
| 6084 | writer.overallocate = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6086 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6087 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6088 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6089 | /* Escape backslashes */ |
| 6090 | if (ch == '\\') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6091 | /* -1: substract 1 preallocated byte */ |
| 6092 | p = _PyBytesWriter_Prepare(&writer, p, 2-1); |
| 6093 | if (p == NULL) |
| 6094 | goto error; |
| 6095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | *p++ = '\\'; |
| 6097 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6098 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6099 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6100 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6101 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6102 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6103 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6104 | |
| 6105 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 6106 | if (p == NULL) |
| 6107 | goto error; |
| 6108 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6109 | *p++ = '\\'; |
| 6110 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6111 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6112 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6113 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6114 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6115 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6116 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6117 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6118 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6119 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6120 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6123 | if (ch >= 256) { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6124 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 6125 | if (p == NULL) |
| 6126 | goto error; |
| 6127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6128 | *p++ = '\\'; |
| 6129 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6130 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6131 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6132 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6133 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6134 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6135 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6136 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6137 | else if (ch == '\t') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6138 | p = _PyBytesWriter_Prepare(&writer, p, 2-1); |
| 6139 | if (p == NULL) |
| 6140 | goto error; |
| 6141 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6142 | *p++ = '\\'; |
| 6143 | *p++ = 't'; |
| 6144 | } |
| 6145 | else if (ch == '\n') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6146 | p = _PyBytesWriter_Prepare(&writer, p, 2-1); |
| 6147 | if (p == NULL) |
| 6148 | goto error; |
| 6149 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6150 | *p++ = '\\'; |
| 6151 | *p++ = 'n'; |
| 6152 | } |
| 6153 | else if (ch == '\r') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6154 | p = _PyBytesWriter_Prepare(&writer, p, 2-1); |
| 6155 | if (p == NULL) |
| 6156 | goto error; |
| 6157 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6158 | *p++ = '\\'; |
| 6159 | *p++ = 'r'; |
| 6160 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6161 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6162 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6163 | else if (ch < ' ' || ch >= 0x7F) { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6164 | /* -1: substract 1 preallocated byte */ |
| 6165 | p = _PyBytesWriter_Prepare(&writer, p, 4-1); |
| 6166 | if (p == NULL) |
| 6167 | goto error; |
| 6168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6170 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6171 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6172 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6173 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | /* Copy everything else as-is */ |
| 6176 | else |
| 6177 | *p++ = (char) ch; |
| 6178 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6180 | return _PyBytesWriter_Finish(&writer, p); |
| 6181 | |
| 6182 | error: |
| 6183 | _PyBytesWriter_Dealloc(&writer); |
| 6184 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6187 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6188 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6189 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6190 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6191 | PyObject *result; |
| 6192 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6193 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6195 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6196 | Py_DECREF(tmp); |
| 6197 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6201 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6202 | PyObject * |
| 6203 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6204 | Py_ssize_t size, |
| 6205 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6207 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6208 | Py_ssize_t startinpos; |
| 6209 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6210 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6211 | const char *end; |
| 6212 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6213 | PyObject *errorHandler = NULL; |
| 6214 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6215 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6216 | if (size == 0) |
| 6217 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6219 | /* Escaped strings will always be longer than the resulting |
| 6220 | 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] | 6221 | length after conversion to the true value. (But decoding error |
| 6222 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6223 | _PyUnicodeWriter_Init(&writer); |
| 6224 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6226 | end = s + size; |
| 6227 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6228 | unsigned char c; |
| 6229 | Py_UCS4 x; |
| 6230 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6231 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6233 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6234 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6235 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6236 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6237 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6238 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6239 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6240 | startinpos = s-starts; |
| 6241 | |
| 6242 | /* \u-escapes are only interpreted iff the number of leading |
| 6243 | backslashes if odd */ |
| 6244 | bs = s; |
| 6245 | for (;s < end;) { |
| 6246 | if (*s != '\\') |
| 6247 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6248 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6249 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6250 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6251 | } |
| 6252 | if (((s - bs) & 1) == 0 || |
| 6253 | s >= end || |
| 6254 | (*s != 'u' && *s != 'U')) { |
| 6255 | continue; |
| 6256 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6257 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6258 | count = *s=='u' ? 4 : 8; |
| 6259 | s++; |
| 6260 | |
| 6261 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6262 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6263 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6264 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6265 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6266 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6267 | errors, &errorHandler, |
| 6268 | "rawunicodeescape", "truncated \\uXXXX", |
| 6269 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6270 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6271 | goto onError; |
| 6272 | goto nextByte; |
| 6273 | } |
| 6274 | x = (x<<4) & ~0xF; |
| 6275 | if (c >= '0' && c <= '9') |
| 6276 | x += c - '0'; |
| 6277 | else if (c >= 'a' && c <= 'f') |
| 6278 | x += 10 + c - 'a'; |
| 6279 | else |
| 6280 | x += 10 + c - 'A'; |
| 6281 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6282 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6283 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6284 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6285 | } |
| 6286 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6287 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6288 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6289 | errors, &errorHandler, |
| 6290 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6292 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6293 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6294 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6295 | nextByte: |
| 6296 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6297 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6298 | Py_XDECREF(errorHandler); |
| 6299 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6300 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6301 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6302 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6303 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6304 | Py_XDECREF(errorHandler); |
| 6305 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6306 | return NULL; |
| 6307 | } |
| 6308 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6309 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6310 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6311 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6312 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | char *p; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6314 | Py_ssize_t pos; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6315 | int kind; |
| 6316 | void *data; |
| 6317 | Py_ssize_t len; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6318 | _PyBytesWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6319 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6320 | if (!PyUnicode_Check(unicode)) { |
| 6321 | PyErr_BadArgument(); |
| 6322 | return NULL; |
| 6323 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6324 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6325 | return NULL; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6326 | |
| 6327 | _PyBytesWriter_Init(&writer); |
| 6328 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6329 | kind = PyUnicode_KIND(unicode); |
| 6330 | data = PyUnicode_DATA(unicode); |
| 6331 | len = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6332 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6333 | p = _PyBytesWriter_Alloc(&writer, len); |
| 6334 | if (p == NULL) |
| 6335 | goto error; |
| 6336 | writer.overallocate = 1; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6337 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6338 | for (pos = 0; pos < len; pos++) { |
| 6339 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6340 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6341 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6342 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6343 | |
| 6344 | /* -1: substract 1 preallocated byte */ |
| 6345 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 6346 | if (p == NULL) |
| 6347 | goto error; |
| 6348 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6349 | *p++ = '\\'; |
| 6350 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6351 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6352 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6353 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6354 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6355 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6356 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6357 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6358 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6359 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6361 | else if (ch >= 256) { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6362 | /* -1: substract 1 preallocated byte */ |
| 6363 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 6364 | if (p == NULL) |
| 6365 | goto error; |
| 6366 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6367 | *p++ = '\\'; |
| 6368 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6369 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6370 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6371 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6372 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6374 | /* Copy everything else as-is */ |
| 6375 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6376 | *p++ = (char) ch; |
| 6377 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6378 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 6379 | return _PyBytesWriter_Finish(&writer, p); |
| 6380 | |
| 6381 | error: |
| 6382 | _PyBytesWriter_Dealloc(&writer); |
| 6383 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6386 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6387 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6388 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6389 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6390 | PyObject *result; |
| 6391 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6392 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6393 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6394 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6395 | Py_DECREF(tmp); |
| 6396 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6399 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6400 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6401 | PyObject * |
| 6402 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6403 | Py_ssize_t size, |
| 6404 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6405 | { |
| 6406 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6407 | Py_ssize_t startinpos; |
| 6408 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6409 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6410 | const char *end; |
| 6411 | const char *reason; |
| 6412 | PyObject *errorHandler = NULL; |
| 6413 | PyObject *exc = NULL; |
| 6414 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6415 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6416 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6417 | 1)) |
| 6418 | return NULL; |
| 6419 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6420 | if (size == 0) |
| 6421 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6422 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6423 | _PyUnicodeWriter_Init(&writer); |
| 6424 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6425 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6426 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6427 | } |
| 6428 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6429 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6430 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6431 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6432 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6433 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6434 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6435 | endinpos = end-starts; |
| 6436 | reason = "truncated input"; |
| 6437 | goto error; |
| 6438 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6439 | /* We copy the raw representation one byte at a time because the |
| 6440 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6441 | ((char *) &uch)[0] = s[0]; |
| 6442 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6443 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6444 | ((char *) &uch)[2] = s[2]; |
| 6445 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6446 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6447 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6448 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6449 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6450 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6451 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6452 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6453 | reason = "illegal code point (> 0x10FFFF)"; |
| 6454 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6455 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6456 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6457 | s += Py_UNICODE_SIZE; |
| 6458 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6459 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6460 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6461 | Py_UNICODE uch2; |
| 6462 | ((char *) &uch2)[0] = s[0]; |
| 6463 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6464 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6465 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6466 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6467 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6468 | } |
| 6469 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6470 | #endif |
| 6471 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6472 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6473 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6474 | continue; |
| 6475 | |
| 6476 | error: |
| 6477 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6478 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6479 | errors, &errorHandler, |
| 6480 | "unicode_internal", reason, |
| 6481 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6482 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6483 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6486 | Py_XDECREF(errorHandler); |
| 6487 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6488 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6491 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6492 | Py_XDECREF(errorHandler); |
| 6493 | Py_XDECREF(exc); |
| 6494 | return NULL; |
| 6495 | } |
| 6496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6497 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6498 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6499 | PyObject * |
| 6500 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6501 | Py_ssize_t size, |
| 6502 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6505 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | } |
| 6507 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6508 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6509 | static void |
| 6510 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6511 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6512 | PyObject *unicode, |
| 6513 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6514 | const char *reason) |
| 6515 | { |
| 6516 | if (*exceptionObject == NULL) { |
| 6517 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6518 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6519 | encoding, unicode, startpos, endpos, reason); |
| 6520 | } |
| 6521 | else { |
| 6522 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6523 | goto onError; |
| 6524 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6525 | goto onError; |
| 6526 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6527 | goto onError; |
| 6528 | return; |
| 6529 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 6530 | Py_CLEAR(*exceptionObject); |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6531 | } |
| 6532 | } |
| 6533 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6534 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6535 | static void |
| 6536 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6537 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6538 | PyObject *unicode, |
| 6539 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6540 | const char *reason) |
| 6541 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6542 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6543 | encoding, unicode, startpos, endpos, reason); |
| 6544 | if (*exceptionObject != NULL) |
| 6545 | PyCodec_StrictErrors(*exceptionObject); |
| 6546 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6547 | |
| 6548 | /* error handling callback helper: |
| 6549 | build arguments, call the callback and check the arguments, |
| 6550 | put the result into newpos and return the replacement string, which |
| 6551 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6552 | static PyObject * |
| 6553 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6554 | PyObject **errorHandler, |
| 6555 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6556 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6557 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6558 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6559 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6560 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6561 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6562 | PyObject *restuple; |
| 6563 | PyObject *resunicode; |
| 6564 | |
| 6565 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6567 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6568 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6569 | } |
| 6570 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6571 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6572 | return NULL; |
| 6573 | len = PyUnicode_GET_LENGTH(unicode); |
| 6574 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6575 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6576 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6577 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6578 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6579 | |
| 6580 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6582 | if (restuple == 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 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6585 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6586 | Py_DECREF(restuple); |
| 6587 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6588 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6589 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | &resunicode, newpos)) { |
| 6591 | Py_DECREF(restuple); |
| 6592 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6593 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6594 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6595 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6596 | Py_DECREF(restuple); |
| 6597 | return NULL; |
| 6598 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6599 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6600 | *newpos = len + *newpos; |
| 6601 | if (*newpos<0 || *newpos>len) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 6602 | 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] | 6603 | Py_DECREF(restuple); |
| 6604 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6605 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6606 | Py_INCREF(resunicode); |
| 6607 | Py_DECREF(restuple); |
| 6608 | return resunicode; |
| 6609 | } |
| 6610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6611 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6612 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6613 | const char *errors, |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6614 | const Py_UCS4 limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6615 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6616 | /* input state */ |
| 6617 | Py_ssize_t pos=0, size; |
| 6618 | int kind; |
| 6619 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6620 | /* pointer into the output */ |
| 6621 | char *str; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6622 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6623 | 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] | 6624 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6625 | PyObject *exc = NULL; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6626 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6627 | PyObject *rep = NULL; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6628 | /* output object */ |
| 6629 | _PyBytesWriter writer; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6630 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6631 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6632 | return NULL; |
| 6633 | size = PyUnicode_GET_LENGTH(unicode); |
| 6634 | kind = PyUnicode_KIND(unicode); |
| 6635 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6636 | /* allocate enough for a simple encoding without |
| 6637 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6638 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6639 | return PyBytes_FromStringAndSize(NULL, 0); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6640 | |
| 6641 | _PyBytesWriter_Init(&writer); |
| 6642 | str = _PyBytesWriter_Alloc(&writer, size); |
| 6643 | if (str == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6644 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6645 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6646 | while (pos < size) { |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6647 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6648 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6649 | /* can we encode this? */ |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6650 | if (ch < limit) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6651 | /* no overflow check, because we know that the space is enough */ |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6652 | *str++ = (char)ch; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6653 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6654 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6655 | else { |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6656 | Py_ssize_t newpos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6657 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6658 | Py_ssize_t collstart = pos; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6659 | Py_ssize_t collend = collstart + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6660 | /* find all unecodable characters */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6661 | |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6662 | while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6663 | ++collend; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6664 | |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6665 | /* Only overallocate the buffer if it's not the last write */ |
| 6666 | writer.overallocate = (collend < size); |
| 6667 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6668 | /* 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] | 6669 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 6670 | error_handler = get_error_handler(errors); |
| 6671 | |
| 6672 | switch (error_handler) { |
| 6673 | case _Py_ERROR_STRICT: |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6674 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6675 | goto onError; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6676 | |
| 6677 | case _Py_ERROR_REPLACE: |
Victor Stinner | 01ada39 | 2015-10-01 21:54:51 +0200 | [diff] [blame] | 6678 | memset(str, '?', collend - collstart); |
| 6679 | str += (collend - collstart); |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6680 | /* fall through ignore error handler */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6681 | case _Py_ERROR_IGNORE: |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6682 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6683 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6684 | |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6685 | case _Py_ERROR_BACKSLASHREPLACE: |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 6686 | /* substract preallocated bytes */ |
| 6687 | writer.min_size -= (collend - collstart); |
| 6688 | str = backslashreplace(&writer, str, |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6689 | unicode, collstart, collend); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6690 | if (str == NULL) |
| 6691 | goto onError; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6692 | pos = collend; |
| 6693 | break; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6694 | |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6695 | case _Py_ERROR_XMLCHARREFREPLACE: |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 6696 | /* substract preallocated bytes */ |
| 6697 | writer.min_size -= (collend - collstart); |
| 6698 | str = xmlcharrefreplace(&writer, str, |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6699 | unicode, collstart, collend); |
| 6700 | if (str == NULL) |
| 6701 | goto onError; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6702 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6703 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6704 | |
Victor Stinner | c3713e9 | 2015-09-29 12:32:13 +0200 | [diff] [blame] | 6705 | case _Py_ERROR_SURROGATEESCAPE: |
| 6706 | for (i = collstart; i < collend; ++i) { |
| 6707 | ch = PyUnicode_READ(kind, data, i); |
| 6708 | if (ch < 0xdc80 || 0xdcff < ch) { |
| 6709 | /* Not a UTF-8b surrogate */ |
| 6710 | break; |
| 6711 | } |
| 6712 | *str++ = (char)(ch - 0xdc00); |
| 6713 | ++pos; |
| 6714 | } |
| 6715 | if (i >= collend) |
| 6716 | break; |
| 6717 | collstart = pos; |
| 6718 | assert(collstart != collend); |
| 6719 | /* fallback to general error handling */ |
| 6720 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6721 | default: |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6722 | rep = unicode_encode_call_errorhandler(errors, &error_handler_obj, |
| 6723 | encoding, reason, unicode, &exc, |
| 6724 | collstart, collend, &newpos); |
| 6725 | if (rep == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6726 | goto onError; |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6727 | |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 6728 | /* substract preallocated bytes */ |
| 6729 | writer.min_size -= 1; |
| 6730 | |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6731 | if (PyBytes_Check(rep)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6732 | /* Directly copy bytes result to output. */ |
Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 6733 | str = _PyBytesWriter_WriteBytes(&writer, str, |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6734 | PyBytes_AS_STRING(rep), |
| 6735 | PyBytes_GET_SIZE(rep)); |
Victor Stinner | ad77158 | 2015-10-09 12:38:53 +0200 | [diff] [blame] | 6736 | if (str == NULL) |
| 6737 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6738 | } |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6739 | else { |
| 6740 | assert(PyUnicode_Check(rep)); |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6741 | |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6742 | if (PyUnicode_READY(rep) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6743 | goto onError; |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6744 | |
| 6745 | if (PyUnicode_IS_ASCII(rep)) { |
| 6746 | /* Fast path: all characters are smaller than limit */ |
| 6747 | assert(limit >= 128); |
| 6748 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 6749 | str = _PyBytesWriter_WriteBytes(&writer, str, |
| 6750 | PyUnicode_DATA(rep), |
| 6751 | PyUnicode_GET_LENGTH(rep)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6752 | } |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6753 | else { |
| 6754 | Py_ssize_t repsize = PyUnicode_GET_LENGTH(rep); |
| 6755 | |
| 6756 | str = _PyBytesWriter_Prepare(&writer, str, repsize); |
| 6757 | if (str == NULL) |
| 6758 | goto onError; |
| 6759 | |
| 6760 | /* check if there is anything unencodable in the |
| 6761 | replacement and copy it to the output */ |
| 6762 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6763 | ch = PyUnicode_READ_CHAR(rep, i); |
| 6764 | if (ch >= limit) { |
| 6765 | raise_encode_exception(&exc, encoding, unicode, |
| 6766 | pos, pos+1, reason); |
| 6767 | goto onError; |
| 6768 | } |
| 6769 | *str = (char)ch; |
| 6770 | } |
| 6771 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6772 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6773 | pos = newpos; |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6774 | Py_CLEAR(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6775 | } |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6776 | |
| 6777 | /* If overallocation was disabled, ensure that it was the last |
| 6778 | write. Otherwise, we missed an optimization */ |
| 6779 | assert(writer.overallocate || pos == size); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6780 | } |
| 6781 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6782 | |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6783 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6784 | Py_XDECREF(exc); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6785 | return _PyBytesWriter_Finish(&writer, str); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6786 | |
| 6787 | onError: |
Victor Stinner | 6bd525b | 2015-10-09 13:10:05 +0200 | [diff] [blame] | 6788 | Py_XDECREF(rep); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6789 | _PyBytesWriter_Dealloc(&writer); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6790 | Py_XDECREF(error_handler_obj); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6791 | Py_XDECREF(exc); |
| 6792 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6795 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6796 | PyObject * |
| 6797 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6798 | Py_ssize_t size, |
| 6799 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6800 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6801 | PyObject *result; |
| 6802 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6803 | if (unicode == NULL) |
| 6804 | return NULL; |
| 6805 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6806 | Py_DECREF(unicode); |
| 6807 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6810 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6811 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | { |
| 6813 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6814 | PyErr_BadArgument(); |
| 6815 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6817 | if (PyUnicode_READY(unicode) == -1) |
| 6818 | return NULL; |
| 6819 | /* Fast path: if it is a one-byte string, construct |
| 6820 | bytes object directly. */ |
| 6821 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6822 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6823 | PyUnicode_GET_LENGTH(unicode)); |
| 6824 | /* Non-Latin-1 characters present. Defer to above function to |
| 6825 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6826 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6827 | } |
| 6828 | |
| 6829 | PyObject* |
| 6830 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6831 | { |
| 6832 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6836 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6837 | PyObject * |
| 6838 | PyUnicode_DecodeASCII(const char *s, |
| 6839 | Py_ssize_t size, |
| 6840 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6842 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6843 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6844 | int kind; |
| 6845 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6846 | Py_ssize_t startinpos; |
| 6847 | Py_ssize_t endinpos; |
| 6848 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6849 | const char *e; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6850 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6851 | PyObject *exc = NULL; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6852 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6855 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6857 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6858 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6859 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6860 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6861 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6862 | writer.min_length = size; |
| 6863 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6864 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6865 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6866 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6867 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6868 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6869 | writer.pos = outpos; |
| 6870 | if (writer.pos == size) |
| 6871 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6872 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6873 | s += writer.pos; |
| 6874 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6875 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6876 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6877 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6878 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6879 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6880 | ++s; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6881 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6882 | } |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6883 | |
| 6884 | /* byte outsize range 0x00..0x7f: call the error handler */ |
| 6885 | |
| 6886 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 6887 | error_handler = get_error_handler(errors); |
| 6888 | |
| 6889 | switch (error_handler) |
| 6890 | { |
| 6891 | case _Py_ERROR_REPLACE: |
| 6892 | case _Py_ERROR_SURROGATEESCAPE: |
| 6893 | /* Fast-path: the error handler only writes one character, |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 6894 | but we may switch to UCS2 at the first write */ |
| 6895 | if (_PyUnicodeWriter_PrepareKind(&writer, PyUnicode_2BYTE_KIND) < 0) |
| 6896 | goto onError; |
| 6897 | kind = writer.kind; |
| 6898 | data = writer.data; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6899 | |
| 6900 | if (error_handler == _Py_ERROR_REPLACE) |
| 6901 | PyUnicode_WRITE(kind, data, writer.pos, 0xfffd); |
| 6902 | else |
| 6903 | PyUnicode_WRITE(kind, data, writer.pos, c + 0xdc00); |
| 6904 | writer.pos++; |
| 6905 | ++s; |
| 6906 | break; |
| 6907 | |
| 6908 | case _Py_ERROR_IGNORE: |
| 6909 | ++s; |
| 6910 | break; |
| 6911 | |
| 6912 | default: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6913 | startinpos = s-starts; |
| 6914 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6915 | if (unicode_decode_call_errorhandler_writer( |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6916 | errors, &error_handler_obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6917 | "ascii", "ordinal not in range(128)", |
| 6918 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6919 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6920 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6921 | kind = writer.kind; |
| 6922 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6923 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 | } |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6925 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6926 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6927 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6928 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6929 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6930 | _PyUnicodeWriter_Dealloc(&writer); |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6931 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6932 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6933 | return NULL; |
| 6934 | } |
| 6935 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6936 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6937 | PyObject * |
| 6938 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6939 | Py_ssize_t size, |
| 6940 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6941 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6942 | PyObject *result; |
| 6943 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6944 | if (unicode == NULL) |
| 6945 | return NULL; |
| 6946 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6947 | Py_DECREF(unicode); |
| 6948 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | } |
| 6950 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6951 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6952 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | { |
| 6954 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6955 | PyErr_BadArgument(); |
| 6956 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6958 | if (PyUnicode_READY(unicode) == -1) |
| 6959 | return NULL; |
| 6960 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6961 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6962 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6963 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6964 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6965 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6966 | } |
| 6967 | |
| 6968 | PyObject * |
| 6969 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6970 | { |
| 6971 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | } |
| 6973 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6974 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6975 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6976 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6977 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6978 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6979 | #define NEED_RETRY |
| 6980 | #endif |
| 6981 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6982 | #ifndef WC_ERR_INVALID_CHARS |
| 6983 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6984 | #endif |
| 6985 | |
| 6986 | static char* |
| 6987 | code_page_name(UINT code_page, PyObject **obj) |
| 6988 | { |
| 6989 | *obj = NULL; |
| 6990 | if (code_page == CP_ACP) |
| 6991 | return "mbcs"; |
| 6992 | if (code_page == CP_UTF7) |
| 6993 | return "CP_UTF7"; |
| 6994 | if (code_page == CP_UTF8) |
| 6995 | return "CP_UTF8"; |
| 6996 | |
| 6997 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6998 | if (*obj == NULL) |
| 6999 | return NULL; |
| 7000 | return PyBytes_AS_STRING(*obj); |
| 7001 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7002 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7003 | static DWORD |
| 7004 | decode_code_page_flags(UINT code_page) |
| 7005 | { |
| 7006 | if (code_page == CP_UTF7) { |
| 7007 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 7008 | return 0; |
| 7009 | } |
| 7010 | else |
| 7011 | return MB_ERR_INVALID_CHARS; |
| 7012 | } |
| 7013 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7014 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7015 | * Decode a byte string from a Windows code page into unicode object in strict |
| 7016 | * mode. |
| 7017 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7018 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 7019 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7020 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7021 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7022 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7023 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7024 | const char *in, |
| 7025 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7026 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7027 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7028 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7029 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7030 | |
| 7031 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7032 | assert(insize > 0); |
| 7033 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7034 | if (outsize <= 0) |
| 7035 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7036 | |
| 7037 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7038 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7039 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7040 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7041 | if (*v == NULL) |
| 7042 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7043 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7044 | } |
| 7045 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7046 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7047 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7048 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7049 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7050 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7054 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7055 | if (outsize <= 0) |
| 7056 | goto error; |
| 7057 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7058 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7059 | error: |
| 7060 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7061 | return -2; |
| 7062 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7063 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7066 | /* |
| 7067 | * Decode a byte string from a code page into unicode object with an error |
| 7068 | * handler. |
| 7069 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7070 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7071 | * UnicodeDecodeError exception and returns -1 on error. |
| 7072 | */ |
| 7073 | static int |
| 7074 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7075 | PyObject **v, |
| 7076 | const char *in, const int size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7077 | const char *errors, int final) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7078 | { |
| 7079 | const char *startin = in; |
| 7080 | const char *endin = in + size; |
| 7081 | const DWORD flags = decode_code_page_flags(code_page); |
| 7082 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7083 | 2000 English version of the message. */ |
| 7084 | const char *reason = "No mapping for the Unicode character exists " |
| 7085 | "in the target code page."; |
| 7086 | /* each step cannot decode more than 1 character, but a character can be |
| 7087 | represented as a surrogate pair */ |
| 7088 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7089 | int insize; |
| 7090 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7091 | PyObject *errorHandler = NULL; |
| 7092 | PyObject *exc = NULL; |
| 7093 | PyObject *encoding_obj = NULL; |
| 7094 | char *encoding; |
| 7095 | DWORD err; |
| 7096 | int ret = -1; |
| 7097 | |
| 7098 | assert(size > 0); |
| 7099 | |
| 7100 | encoding = code_page_name(code_page, &encoding_obj); |
| 7101 | if (encoding == NULL) |
| 7102 | return -1; |
| 7103 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7104 | if ((errors == NULL || strcmp(errors, "strict") == 0) && final) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7105 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7106 | UnicodeDecodeError. */ |
| 7107 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7108 | if (exc != NULL) { |
| 7109 | PyCodec_StrictErrors(exc); |
| 7110 | Py_CLEAR(exc); |
| 7111 | } |
| 7112 | goto error; |
| 7113 | } |
| 7114 | |
| 7115 | if (*v == NULL) { |
| 7116 | /* Create unicode object */ |
| 7117 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7118 | PyErr_NoMemory(); |
| 7119 | goto error; |
| 7120 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7121 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7122 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7123 | if (*v == NULL) |
| 7124 | goto error; |
| 7125 | startout = PyUnicode_AS_UNICODE(*v); |
| 7126 | } |
| 7127 | else { |
| 7128 | /* Extend unicode object */ |
| 7129 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7130 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7131 | PyErr_NoMemory(); |
| 7132 | goto error; |
| 7133 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7134 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7135 | goto error; |
| 7136 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7137 | } |
| 7138 | |
| 7139 | /* Decode the byte string character per character */ |
| 7140 | out = startout; |
| 7141 | while (in < endin) |
| 7142 | { |
| 7143 | /* Decode a character */ |
| 7144 | insize = 1; |
| 7145 | do |
| 7146 | { |
| 7147 | outsize = MultiByteToWideChar(code_page, flags, |
| 7148 | in, insize, |
| 7149 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7150 | if (outsize > 0) |
| 7151 | break; |
| 7152 | err = GetLastError(); |
| 7153 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7154 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7155 | { |
| 7156 | PyErr_SetFromWindowsErr(0); |
| 7157 | goto error; |
| 7158 | } |
| 7159 | insize++; |
| 7160 | } |
| 7161 | /* 4=maximum length of a UTF-8 sequence */ |
| 7162 | while (insize <= 4 && (in + insize) <= endin); |
| 7163 | |
| 7164 | if (outsize <= 0) { |
| 7165 | Py_ssize_t startinpos, endinpos, outpos; |
| 7166 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7167 | /* last character in partial decode? */ |
| 7168 | if (in + insize >= endin && !final) |
| 7169 | break; |
| 7170 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7171 | startinpos = in - startin; |
| 7172 | endinpos = startinpos + 1; |
| 7173 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7174 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7175 | errors, &errorHandler, |
| 7176 | encoding, reason, |
| 7177 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7178 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7179 | { |
| 7180 | goto error; |
| 7181 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7182 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7183 | } |
| 7184 | else { |
| 7185 | in += insize; |
| 7186 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7187 | out += outsize; |
| 7188 | } |
| 7189 | } |
| 7190 | |
| 7191 | /* write a NUL character at the end */ |
| 7192 | *out = 0; |
| 7193 | |
| 7194 | /* Extend unicode object */ |
| 7195 | outsize = out - startout; |
| 7196 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7197 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7198 | goto error; |
Victor Stinner | e1f17c6 | 2014-07-25 14:03:03 +0200 | [diff] [blame] | 7199 | /* (in - startin) <= size and size is an int */ |
| 7200 | ret = Py_SAFE_DOWNCAST(in - startin, Py_ssize_t, int); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7201 | |
| 7202 | error: |
| 7203 | Py_XDECREF(encoding_obj); |
| 7204 | Py_XDECREF(errorHandler); |
| 7205 | Py_XDECREF(exc); |
| 7206 | return ret; |
| 7207 | } |
| 7208 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7209 | static PyObject * |
| 7210 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7211 | const char *s, Py_ssize_t size, |
| 7212 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7213 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7214 | PyObject *v = NULL; |
| 7215 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7216 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7217 | if (code_page < 0) { |
| 7218 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7219 | return NULL; |
| 7220 | } |
| 7221 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7222 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7223 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7224 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7225 | do |
| 7226 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7227 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7228 | if (size > INT_MAX) { |
| 7229 | chunk_size = INT_MAX; |
| 7230 | final = 0; |
| 7231 | done = 0; |
| 7232 | } |
| 7233 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7234 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7235 | { |
| 7236 | chunk_size = (int)size; |
| 7237 | final = (consumed == NULL); |
| 7238 | done = 1; |
| 7239 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7240 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7241 | if (chunk_size == 0 && done) { |
| 7242 | if (v != NULL) |
| 7243 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7244 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7245 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7246 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7247 | converted = decode_code_page_strict(code_page, &v, |
| 7248 | s, chunk_size); |
| 7249 | if (converted == -2) |
| 7250 | converted = decode_code_page_errors(code_page, &v, |
| 7251 | s, chunk_size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7252 | errors, final); |
| 7253 | assert(converted != 0 || done); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7254 | |
| 7255 | if (converted < 0) { |
| 7256 | Py_XDECREF(v); |
| 7257 | return NULL; |
| 7258 | } |
| 7259 | |
| 7260 | if (consumed) |
| 7261 | *consumed += converted; |
| 7262 | |
| 7263 | s += converted; |
| 7264 | size -= converted; |
| 7265 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7266 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7267 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7268 | } |
| 7269 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7270 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7271 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7272 | const char *s, |
| 7273 | Py_ssize_t size, |
| 7274 | const char *errors, |
| 7275 | Py_ssize_t *consumed) |
| 7276 | { |
| 7277 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7278 | } |
| 7279 | |
| 7280 | PyObject * |
| 7281 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7282 | Py_ssize_t size, |
| 7283 | const char *errors, |
| 7284 | Py_ssize_t *consumed) |
| 7285 | { |
| 7286 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7287 | } |
| 7288 | |
| 7289 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7290 | PyUnicode_DecodeMBCS(const char *s, |
| 7291 | Py_ssize_t size, |
| 7292 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7293 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7294 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7295 | } |
| 7296 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7297 | static DWORD |
| 7298 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7299 | { |
| 7300 | if (code_page == CP_UTF8) { |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 7301 | return WC_ERR_INVALID_CHARS; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7302 | } |
| 7303 | else if (code_page == CP_UTF7) { |
| 7304 | /* CP_UTF7 only supports flags=0 */ |
| 7305 | return 0; |
| 7306 | } |
| 7307 | else { |
| 7308 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7309 | return 0; |
| 7310 | else |
| 7311 | return WC_NO_BEST_FIT_CHARS; |
| 7312 | } |
| 7313 | } |
| 7314 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7315 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7316 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7317 | * mode. |
| 7318 | * |
| 7319 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7320 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7321 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7322 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7323 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7324 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7325 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7326 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7327 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7328 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7329 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7330 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7331 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7332 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7333 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7334 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7335 | /* Create a substring so that we can get the UTF-16 representation |
| 7336 | of just the slice under consideration. */ |
| 7337 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7338 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7339 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7340 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7341 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7342 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7343 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7344 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7345 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7346 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7347 | if (substring == NULL) |
| 7348 | return -1; |
| 7349 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7350 | if (p == NULL) { |
| 7351 | Py_DECREF(substring); |
| 7352 | return -1; |
| 7353 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7354 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7355 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7356 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7357 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7358 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7359 | NULL, 0, |
| 7360 | NULL, pusedDefaultChar); |
| 7361 | if (outsize <= 0) |
| 7362 | goto error; |
| 7363 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7364 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7365 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7366 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7367 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7368 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7369 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7370 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7371 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7372 | if (*outbytes == NULL) { |
| 7373 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7374 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7375 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7376 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7377 | } |
| 7378 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7379 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7380 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7381 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7382 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7383 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7384 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7385 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7386 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7387 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [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) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7391 | } |
| 7392 | |
| 7393 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7394 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7395 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7396 | out, outsize, |
| 7397 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7398 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7399 | if (outsize <= 0) |
| 7400 | goto error; |
| 7401 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7402 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7403 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7404 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7405 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7406 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7407 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7408 | return -2; |
| 7409 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7410 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7411 | } |
| 7412 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7413 | /* |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 7414 | * 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] | 7415 | * error handler. |
| 7416 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7417 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7418 | * -1 on other error. |
| 7419 | */ |
| 7420 | static int |
| 7421 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7422 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7423 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7424 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7425 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7426 | Py_ssize_t pos = unicode_offset; |
| 7427 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7428 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7429 | 2000 English version of the message. */ |
| 7430 | const char *reason = "invalid character"; |
| 7431 | /* 4=maximum length of a UTF-8 sequence */ |
| 7432 | char buffer[4]; |
| 7433 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7434 | Py_ssize_t outsize; |
| 7435 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7436 | PyObject *errorHandler = NULL; |
| 7437 | PyObject *exc = NULL; |
| 7438 | PyObject *encoding_obj = NULL; |
| 7439 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7440 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7441 | PyObject *rep; |
| 7442 | int ret = -1; |
| 7443 | |
| 7444 | assert(insize > 0); |
| 7445 | |
| 7446 | encoding = code_page_name(code_page, &encoding_obj); |
| 7447 | if (encoding == NULL) |
| 7448 | return -1; |
| 7449 | |
| 7450 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7451 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7452 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7453 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7454 | if (exc != NULL) { |
| 7455 | PyCodec_StrictErrors(exc); |
| 7456 | Py_DECREF(exc); |
| 7457 | } |
| 7458 | Py_XDECREF(encoding_obj); |
| 7459 | return -1; |
| 7460 | } |
| 7461 | |
| 7462 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7463 | pusedDefaultChar = &usedDefaultChar; |
| 7464 | else |
| 7465 | pusedDefaultChar = NULL; |
| 7466 | |
| 7467 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7468 | PyErr_NoMemory(); |
| 7469 | goto error; |
| 7470 | } |
| 7471 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7472 | |
| 7473 | if (*outbytes == NULL) { |
| 7474 | /* Create string object */ |
| 7475 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7476 | if (*outbytes == NULL) |
| 7477 | goto error; |
| 7478 | out = PyBytes_AS_STRING(*outbytes); |
| 7479 | } |
| 7480 | else { |
| 7481 | /* Extend string object */ |
| 7482 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7483 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7484 | PyErr_NoMemory(); |
| 7485 | goto error; |
| 7486 | } |
| 7487 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7488 | goto error; |
| 7489 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7490 | } |
| 7491 | |
| 7492 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7493 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7494 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7495 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7496 | wchar_t chars[2]; |
| 7497 | int charsize; |
| 7498 | if (ch < 0x10000) { |
| 7499 | chars[0] = (wchar_t)ch; |
| 7500 | charsize = 1; |
| 7501 | } |
| 7502 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7503 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7504 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7505 | charsize = 2; |
| 7506 | } |
| 7507 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7508 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7509 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7510 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7511 | NULL, pusedDefaultChar); |
| 7512 | if (outsize > 0) { |
| 7513 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7514 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7515 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7516 | memcpy(out, buffer, outsize); |
| 7517 | out += outsize; |
| 7518 | continue; |
| 7519 | } |
| 7520 | } |
| 7521 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7522 | PyErr_SetFromWindowsErr(0); |
| 7523 | goto error; |
| 7524 | } |
| 7525 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7526 | rep = unicode_encode_call_errorhandler( |
| 7527 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7528 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7529 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7530 | if (rep == NULL) |
| 7531 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7532 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7533 | |
| 7534 | if (PyBytes_Check(rep)) { |
| 7535 | outsize = PyBytes_GET_SIZE(rep); |
| 7536 | if (outsize != 1) { |
| 7537 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7538 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7539 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7540 | Py_DECREF(rep); |
| 7541 | goto error; |
| 7542 | } |
| 7543 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7544 | } |
| 7545 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7546 | out += outsize; |
| 7547 | } |
| 7548 | else { |
| 7549 | Py_ssize_t i; |
| 7550 | enum PyUnicode_Kind kind; |
| 7551 | void *data; |
| 7552 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7553 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7554 | Py_DECREF(rep); |
| 7555 | goto error; |
| 7556 | } |
| 7557 | |
| 7558 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7559 | if (outsize != 1) { |
| 7560 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7561 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7562 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7563 | Py_DECREF(rep); |
| 7564 | goto error; |
| 7565 | } |
| 7566 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7567 | } |
| 7568 | kind = PyUnicode_KIND(rep); |
| 7569 | data = PyUnicode_DATA(rep); |
| 7570 | for (i=0; i < outsize; i++) { |
| 7571 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7572 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7573 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7574 | encoding, unicode, |
| 7575 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7576 | "unable to encode error handler result to ASCII"); |
| 7577 | Py_DECREF(rep); |
| 7578 | goto error; |
| 7579 | } |
| 7580 | *out = (unsigned char)ch; |
| 7581 | out++; |
| 7582 | } |
| 7583 | } |
| 7584 | Py_DECREF(rep); |
| 7585 | } |
| 7586 | /* write a NUL byte */ |
| 7587 | *out = 0; |
| 7588 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7589 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7590 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7591 | goto error; |
| 7592 | ret = 0; |
| 7593 | |
| 7594 | error: |
| 7595 | Py_XDECREF(encoding_obj); |
| 7596 | Py_XDECREF(errorHandler); |
| 7597 | Py_XDECREF(exc); |
| 7598 | return ret; |
| 7599 | } |
| 7600 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7601 | static PyObject * |
| 7602 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7603 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7604 | const char *errors) |
| 7605 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7606 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7607 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7608 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7609 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7610 | |
Victor Stinner | 29dacf2 | 2015-01-26 16:41:32 +0100 | [diff] [blame] | 7611 | if (!PyUnicode_Check(unicode)) { |
| 7612 | PyErr_BadArgument(); |
| 7613 | return NULL; |
| 7614 | } |
| 7615 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7616 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7617 | return NULL; |
| 7618 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7619 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7620 | if (code_page < 0) { |
| 7621 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7622 | return NULL; |
| 7623 | } |
| 7624 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7625 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7626 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7627 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7628 | offset = 0; |
| 7629 | do |
| 7630 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7631 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7632 | /* 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] | 7633 | chunks. */ |
| 7634 | if (len > INT_MAX/2) { |
| 7635 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7636 | done = 0; |
| 7637 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7638 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7639 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7640 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7641 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7642 | done = 1; |
| 7643 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7644 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7645 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7646 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7647 | errors); |
| 7648 | if (ret == -2) |
| 7649 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7650 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7651 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7652 | if (ret < 0) { |
| 7653 | Py_XDECREF(outbytes); |
| 7654 | return NULL; |
| 7655 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7656 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7657 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7658 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7659 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7660 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7661 | return outbytes; |
| 7662 | } |
| 7663 | |
| 7664 | PyObject * |
| 7665 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7666 | Py_ssize_t size, |
| 7667 | const char *errors) |
| 7668 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7669 | PyObject *unicode, *res; |
| 7670 | unicode = PyUnicode_FromUnicode(p, size); |
| 7671 | if (unicode == NULL) |
| 7672 | return NULL; |
| 7673 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7674 | Py_DECREF(unicode); |
| 7675 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7676 | } |
| 7677 | |
| 7678 | PyObject * |
| 7679 | PyUnicode_EncodeCodePage(int code_page, |
| 7680 | PyObject *unicode, |
| 7681 | const char *errors) |
| 7682 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7683 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7684 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7685 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7686 | PyObject * |
| 7687 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7688 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7689 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7690 | } |
| 7691 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7692 | #undef NEED_RETRY |
| 7693 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7694 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7695 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7696 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7697 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7698 | static int |
| 7699 | charmap_decode_string(const char *s, |
| 7700 | Py_ssize_t size, |
| 7701 | PyObject *mapping, |
| 7702 | const char *errors, |
| 7703 | _PyUnicodeWriter *writer) |
| 7704 | { |
| 7705 | const char *starts = s; |
| 7706 | const char *e; |
| 7707 | Py_ssize_t startinpos, endinpos; |
| 7708 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7709 | Py_ssize_t maplen; |
| 7710 | enum PyUnicode_Kind mapkind; |
| 7711 | void *mapdata; |
| 7712 | Py_UCS4 x; |
| 7713 | unsigned char ch; |
| 7714 | |
| 7715 | if (PyUnicode_READY(mapping) == -1) |
| 7716 | return -1; |
| 7717 | |
| 7718 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7719 | mapdata = PyUnicode_DATA(mapping); |
| 7720 | mapkind = PyUnicode_KIND(mapping); |
| 7721 | |
| 7722 | e = s + size; |
| 7723 | |
| 7724 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7725 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7726 | * is disabled in encoding aliases, latin1 is preferred because |
| 7727 | * its implementation is faster. */ |
| 7728 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7729 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7730 | Py_UCS4 maxchar = writer->maxchar; |
| 7731 | |
| 7732 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7733 | while (s < e) { |
| 7734 | ch = *s; |
| 7735 | x = mapdata_ucs1[ch]; |
| 7736 | if (x > maxchar) { |
| 7737 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7738 | goto onError; |
| 7739 | maxchar = writer->maxchar; |
| 7740 | outdata = (Py_UCS1 *)writer->data; |
| 7741 | } |
| 7742 | outdata[writer->pos] = x; |
| 7743 | writer->pos++; |
| 7744 | ++s; |
| 7745 | } |
| 7746 | return 0; |
| 7747 | } |
| 7748 | |
| 7749 | while (s < e) { |
| 7750 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7751 | enum PyUnicode_Kind outkind = writer->kind; |
| 7752 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7753 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7754 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7755 | Py_UCS4 maxchar = writer->maxchar; |
| 7756 | while (s < e) { |
| 7757 | ch = *s; |
| 7758 | x = mapdata_ucs2[ch]; |
| 7759 | if (x > maxchar) |
| 7760 | goto Error; |
| 7761 | outdata[writer->pos] = x; |
| 7762 | writer->pos++; |
| 7763 | ++s; |
| 7764 | } |
| 7765 | break; |
| 7766 | } |
| 7767 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7768 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7769 | while (s < e) { |
| 7770 | ch = *s; |
| 7771 | x = mapdata_ucs2[ch]; |
| 7772 | if (x == 0xFFFE) |
| 7773 | goto Error; |
| 7774 | outdata[writer->pos] = x; |
| 7775 | writer->pos++; |
| 7776 | ++s; |
| 7777 | } |
| 7778 | break; |
| 7779 | } |
| 7780 | } |
| 7781 | ch = *s; |
| 7782 | |
| 7783 | if (ch < maplen) |
| 7784 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7785 | else |
| 7786 | x = 0xfffe; /* invalid value */ |
| 7787 | Error: |
| 7788 | if (x == 0xfffe) |
| 7789 | { |
| 7790 | /* undefined mapping */ |
| 7791 | startinpos = s-starts; |
| 7792 | endinpos = startinpos+1; |
| 7793 | if (unicode_decode_call_errorhandler_writer( |
| 7794 | errors, &errorHandler, |
| 7795 | "charmap", "character maps to <undefined>", |
| 7796 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7797 | writer)) { |
| 7798 | goto onError; |
| 7799 | } |
| 7800 | continue; |
| 7801 | } |
| 7802 | |
| 7803 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7804 | goto onError; |
| 7805 | ++s; |
| 7806 | } |
| 7807 | Py_XDECREF(errorHandler); |
| 7808 | Py_XDECREF(exc); |
| 7809 | return 0; |
| 7810 | |
| 7811 | onError: |
| 7812 | Py_XDECREF(errorHandler); |
| 7813 | Py_XDECREF(exc); |
| 7814 | return -1; |
| 7815 | } |
| 7816 | |
| 7817 | static int |
| 7818 | charmap_decode_mapping(const char *s, |
| 7819 | Py_ssize_t size, |
| 7820 | PyObject *mapping, |
| 7821 | const char *errors, |
| 7822 | _PyUnicodeWriter *writer) |
| 7823 | { |
| 7824 | const char *starts = s; |
| 7825 | const char *e; |
| 7826 | Py_ssize_t startinpos, endinpos; |
| 7827 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7828 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7829 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7830 | |
| 7831 | e = s + size; |
| 7832 | |
| 7833 | while (s < e) { |
| 7834 | ch = *s; |
| 7835 | |
| 7836 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7837 | key = PyLong_FromLong((long)ch); |
| 7838 | if (key == NULL) |
| 7839 | goto onError; |
| 7840 | |
| 7841 | item = PyObject_GetItem(mapping, key); |
| 7842 | Py_DECREF(key); |
| 7843 | if (item == NULL) { |
| 7844 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7845 | /* No mapping found means: mapping is undefined. */ |
| 7846 | PyErr_Clear(); |
| 7847 | goto Undefined; |
| 7848 | } else |
| 7849 | goto onError; |
| 7850 | } |
| 7851 | |
| 7852 | /* Apply mapping */ |
| 7853 | if (item == Py_None) |
| 7854 | goto Undefined; |
| 7855 | if (PyLong_Check(item)) { |
| 7856 | long value = PyLong_AS_LONG(item); |
| 7857 | if (value == 0xFFFE) |
| 7858 | goto Undefined; |
| 7859 | if (value < 0 || value > MAX_UNICODE) { |
| 7860 | PyErr_Format(PyExc_TypeError, |
| 7861 | "character mapping must be in range(0x%lx)", |
| 7862 | (unsigned long)MAX_UNICODE + 1); |
| 7863 | goto onError; |
| 7864 | } |
| 7865 | |
| 7866 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7867 | goto onError; |
| 7868 | } |
| 7869 | else if (PyUnicode_Check(item)) { |
| 7870 | if (PyUnicode_READY(item) == -1) |
| 7871 | goto onError; |
| 7872 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7873 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7874 | if (value == 0xFFFE) |
| 7875 | goto Undefined; |
| 7876 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7877 | goto onError; |
| 7878 | } |
| 7879 | else { |
| 7880 | writer->overallocate = 1; |
| 7881 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7882 | goto onError; |
| 7883 | } |
| 7884 | } |
| 7885 | else { |
| 7886 | /* wrong return value */ |
| 7887 | PyErr_SetString(PyExc_TypeError, |
| 7888 | "character mapping must return integer, None or str"); |
| 7889 | goto onError; |
| 7890 | } |
| 7891 | Py_CLEAR(item); |
| 7892 | ++s; |
| 7893 | continue; |
| 7894 | |
| 7895 | Undefined: |
| 7896 | /* undefined mapping */ |
| 7897 | Py_CLEAR(item); |
| 7898 | startinpos = s-starts; |
| 7899 | endinpos = startinpos+1; |
| 7900 | if (unicode_decode_call_errorhandler_writer( |
| 7901 | errors, &errorHandler, |
| 7902 | "charmap", "character maps to <undefined>", |
| 7903 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7904 | writer)) { |
| 7905 | goto onError; |
| 7906 | } |
| 7907 | } |
| 7908 | Py_XDECREF(errorHandler); |
| 7909 | Py_XDECREF(exc); |
| 7910 | return 0; |
| 7911 | |
| 7912 | onError: |
| 7913 | Py_XDECREF(item); |
| 7914 | Py_XDECREF(errorHandler); |
| 7915 | Py_XDECREF(exc); |
| 7916 | return -1; |
| 7917 | } |
| 7918 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7919 | PyObject * |
| 7920 | PyUnicode_DecodeCharmap(const char *s, |
| 7921 | Py_ssize_t size, |
| 7922 | PyObject *mapping, |
| 7923 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7924 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7925 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7926 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7927 | /* Default to Latin-1 */ |
| 7928 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7929 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7930 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7932 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7933 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7934 | writer.min_length = size; |
| 7935 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7936 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7937 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7938 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7939 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7940 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7941 | } |
| 7942 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7943 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7944 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7945 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7946 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7947 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7948 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7949 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7950 | return NULL; |
| 7951 | } |
| 7952 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7953 | /* Charmap encoding: the lookup table */ |
| 7954 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7955 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7956 | PyObject_HEAD |
| 7957 | unsigned char level1[32]; |
| 7958 | int count2, count3; |
| 7959 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7960 | }; |
| 7961 | |
| 7962 | static PyObject* |
| 7963 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7964 | { |
| 7965 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7966 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7967 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7968 | } |
| 7969 | |
| 7970 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7971 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7972 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7973 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7974 | }; |
| 7975 | |
| 7976 | static void |
| 7977 | encoding_map_dealloc(PyObject* o) |
| 7978 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7979 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7980 | } |
| 7981 | |
| 7982 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7983 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7984 | "EncodingMap", /*tp_name*/ |
| 7985 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7986 | 0, /*tp_itemsize*/ |
| 7987 | /* methods */ |
| 7988 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7989 | 0, /*tp_print*/ |
| 7990 | 0, /*tp_getattr*/ |
| 7991 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7992 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7993 | 0, /*tp_repr*/ |
| 7994 | 0, /*tp_as_number*/ |
| 7995 | 0, /*tp_as_sequence*/ |
| 7996 | 0, /*tp_as_mapping*/ |
| 7997 | 0, /*tp_hash*/ |
| 7998 | 0, /*tp_call*/ |
| 7999 | 0, /*tp_str*/ |
| 8000 | 0, /*tp_getattro*/ |
| 8001 | 0, /*tp_setattro*/ |
| 8002 | 0, /*tp_as_buffer*/ |
| 8003 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 8004 | 0, /*tp_doc*/ |
| 8005 | 0, /*tp_traverse*/ |
| 8006 | 0, /*tp_clear*/ |
| 8007 | 0, /*tp_richcompare*/ |
| 8008 | 0, /*tp_weaklistoffset*/ |
| 8009 | 0, /*tp_iter*/ |
| 8010 | 0, /*tp_iternext*/ |
| 8011 | encoding_map_methods, /*tp_methods*/ |
| 8012 | 0, /*tp_members*/ |
| 8013 | 0, /*tp_getset*/ |
| 8014 | 0, /*tp_base*/ |
| 8015 | 0, /*tp_dict*/ |
| 8016 | 0, /*tp_descr_get*/ |
| 8017 | 0, /*tp_descr_set*/ |
| 8018 | 0, /*tp_dictoffset*/ |
| 8019 | 0, /*tp_init*/ |
| 8020 | 0, /*tp_alloc*/ |
| 8021 | 0, /*tp_new*/ |
| 8022 | 0, /*tp_free*/ |
| 8023 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8024 | }; |
| 8025 | |
| 8026 | PyObject* |
| 8027 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 8028 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8029 | PyObject *result; |
| 8030 | struct encoding_map *mresult; |
| 8031 | int i; |
| 8032 | int need_dict = 0; |
| 8033 | unsigned char level1[32]; |
| 8034 | unsigned char level2[512]; |
| 8035 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8036 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8037 | int kind; |
| 8038 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8039 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8040 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8041 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8042 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8043 | PyErr_BadArgument(); |
| 8044 | return NULL; |
| 8045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8046 | kind = PyUnicode_KIND(string); |
| 8047 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8048 | length = PyUnicode_GET_LENGTH(string); |
| 8049 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8050 | memset(level1, 0xFF, sizeof level1); |
| 8051 | memset(level2, 0xFF, sizeof level2); |
| 8052 | |
| 8053 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8054 | or if there are non-BMP characters, we need to use |
| 8055 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8056 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8057 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8058 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8059 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8060 | ch = PyUnicode_READ(kind, data, i); |
| 8061 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8062 | need_dict = 1; |
| 8063 | break; |
| 8064 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8065 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8066 | /* unmapped character */ |
| 8067 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8068 | l1 = ch >> 11; |
| 8069 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8070 | if (level1[l1] == 0xFF) |
| 8071 | level1[l1] = count2++; |
| 8072 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8073 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8074 | } |
| 8075 | |
| 8076 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8077 | need_dict = 1; |
| 8078 | |
| 8079 | if (need_dict) { |
| 8080 | PyObject *result = PyDict_New(); |
| 8081 | PyObject *key, *value; |
| 8082 | if (!result) |
| 8083 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8084 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8085 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8086 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8087 | if (!key || !value) |
| 8088 | goto failed1; |
| 8089 | if (PyDict_SetItem(result, key, value) == -1) |
| 8090 | goto failed1; |
| 8091 | Py_DECREF(key); |
| 8092 | Py_DECREF(value); |
| 8093 | } |
| 8094 | return result; |
| 8095 | failed1: |
| 8096 | Py_XDECREF(key); |
| 8097 | Py_XDECREF(value); |
| 8098 | Py_DECREF(result); |
| 8099 | return NULL; |
| 8100 | } |
| 8101 | |
| 8102 | /* Create a three-level trie */ |
| 8103 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8104 | 16*count2 + 128*count3 - 1); |
| 8105 | if (!result) |
| 8106 | return PyErr_NoMemory(); |
| 8107 | PyObject_Init(result, &EncodingMapType); |
| 8108 | mresult = (struct encoding_map*)result; |
| 8109 | mresult->count2 = count2; |
| 8110 | mresult->count3 = count3; |
| 8111 | mlevel1 = mresult->level1; |
| 8112 | mlevel2 = mresult->level23; |
| 8113 | mlevel3 = mresult->level23 + 16*count2; |
| 8114 | memcpy(mlevel1, level1, 32); |
| 8115 | memset(mlevel2, 0xFF, 16*count2); |
| 8116 | memset(mlevel3, 0, 128*count3); |
| 8117 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8118 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8119 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8120 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8121 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8122 | /* unmapped character */ |
| 8123 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8124 | o1 = ch>>11; |
| 8125 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8126 | i2 = 16*mlevel1[o1] + o2; |
| 8127 | if (mlevel2[i2] == 0xFF) |
| 8128 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8129 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8130 | i3 = 128*mlevel2[i2] + o3; |
| 8131 | mlevel3[i3] = i; |
| 8132 | } |
| 8133 | return result; |
| 8134 | } |
| 8135 | |
| 8136 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8137 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8138 | { |
| 8139 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8140 | int l1 = c>>11; |
| 8141 | int l2 = (c>>7) & 0xF; |
| 8142 | int l3 = c & 0x7F; |
| 8143 | int i; |
| 8144 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8145 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8146 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8147 | if (c == 0) |
| 8148 | return 0; |
| 8149 | /* level 1*/ |
| 8150 | i = map->level1[l1]; |
| 8151 | if (i == 0xFF) { |
| 8152 | return -1; |
| 8153 | } |
| 8154 | /* level 2*/ |
| 8155 | i = map->level23[16*i+l2]; |
| 8156 | if (i == 0xFF) { |
| 8157 | return -1; |
| 8158 | } |
| 8159 | /* level 3 */ |
| 8160 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8161 | if (i == 0) { |
| 8162 | return -1; |
| 8163 | } |
| 8164 | return i; |
| 8165 | } |
| 8166 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8167 | /* Lookup the character ch in the mapping. If the character |
| 8168 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8169 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8170 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8171 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8173 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8174 | PyObject *x; |
| 8175 | |
| 8176 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8177 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8178 | x = PyObject_GetItem(mapping, w); |
| 8179 | Py_DECREF(w); |
| 8180 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8181 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8182 | /* No mapping found means: mapping is undefined. */ |
| 8183 | PyErr_Clear(); |
| 8184 | x = Py_None; |
| 8185 | Py_INCREF(x); |
| 8186 | return x; |
| 8187 | } else |
| 8188 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8189 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8190 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8191 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8192 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8193 | long value = PyLong_AS_LONG(x); |
| 8194 | if (value < 0 || value > 255) { |
| 8195 | PyErr_SetString(PyExc_TypeError, |
| 8196 | "character mapping must be in range(256)"); |
| 8197 | Py_DECREF(x); |
| 8198 | return NULL; |
| 8199 | } |
| 8200 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8201 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8202 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8203 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8204 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8205 | /* wrong return value */ |
| 8206 | PyErr_Format(PyExc_TypeError, |
| 8207 | "character mapping must return integer, bytes or None, not %.400s", |
| 8208 | x->ob_type->tp_name); |
| 8209 | Py_DECREF(x); |
| 8210 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8211 | } |
| 8212 | } |
| 8213 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8214 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8215 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8216 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8217 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8218 | /* exponentially overallocate to minimize reallocations */ |
| 8219 | if (requiredsize < 2*outsize) |
| 8220 | requiredsize = 2*outsize; |
| 8221 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8222 | return -1; |
| 8223 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8224 | } |
| 8225 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8226 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8227 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8228 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8229 | /* 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] | 8230 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8231 | space is available. Return a new reference to the object that |
| 8232 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8233 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8234 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8235 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8236 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8237 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8238 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8239 | PyObject *rep; |
| 8240 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8241 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8242 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8243 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8244 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8245 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8246 | if (res == -1) |
| 8247 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8248 | if (outsize<requiredsize) |
| 8249 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8250 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8251 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8252 | outstart[(*outpos)++] = (char)res; |
| 8253 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8254 | } |
| 8255 | |
| 8256 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8257 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8258 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8259 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8260 | Py_DECREF(rep); |
| 8261 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8262 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8263 | if (PyLong_Check(rep)) { |
| 8264 | Py_ssize_t requiredsize = *outpos+1; |
| 8265 | if (outsize<requiredsize) |
| 8266 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8267 | Py_DECREF(rep); |
| 8268 | return enc_EXCEPTION; |
| 8269 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8270 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8272 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8273 | else { |
| 8274 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8275 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8276 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8277 | if (outsize<requiredsize) |
| 8278 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8279 | Py_DECREF(rep); |
| 8280 | return enc_EXCEPTION; |
| 8281 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8282 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8283 | memcpy(outstart + *outpos, repchars, repsize); |
| 8284 | *outpos += repsize; |
| 8285 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8286 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8287 | Py_DECREF(rep); |
| 8288 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8289 | } |
| 8290 | |
| 8291 | /* handle an error in PyUnicode_EncodeCharmap |
| 8292 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8293 | static int |
| 8294 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8295 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8296 | PyObject **exceptionObject, |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8297 | _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] | 8298 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8299 | { |
| 8300 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8301 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8302 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8303 | enum PyUnicode_Kind kind; |
| 8304 | void *data; |
| 8305 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8306 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8307 | Py_ssize_t collstartpos = *inpos; |
| 8308 | Py_ssize_t collendpos = *inpos+1; |
| 8309 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8310 | char *encoding = "charmap"; |
| 8311 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8312 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8313 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8314 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8315 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8316 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8317 | return -1; |
| 8318 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8319 | /* find all unencodable characters */ |
| 8320 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8321 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8322 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8323 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8324 | val = encoding_map_lookup(ch, mapping); |
| 8325 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | break; |
| 8327 | ++collendpos; |
| 8328 | continue; |
| 8329 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8330 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8331 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8332 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | if (rep==NULL) |
| 8334 | return -1; |
| 8335 | else if (rep!=Py_None) { |
| 8336 | Py_DECREF(rep); |
| 8337 | break; |
| 8338 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8339 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8340 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8341 | } |
| 8342 | /* cache callback name lookup |
| 8343 | * (if not done yet, i.e. it's the first error) */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8344 | if (*error_handler == _Py_ERROR_UNKNOWN) |
| 8345 | *error_handler = get_error_handler(errors); |
| 8346 | |
| 8347 | switch (*error_handler) { |
| 8348 | case _Py_ERROR_STRICT: |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8349 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8350 | return -1; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8351 | |
| 8352 | case _Py_ERROR_REPLACE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8353 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8354 | x = charmapencode_output('?', mapping, res, respos); |
| 8355 | if (x==enc_EXCEPTION) { |
| 8356 | return -1; |
| 8357 | } |
| 8358 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8359 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8360 | return -1; |
| 8361 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8362 | } |
| 8363 | /* fall through */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8364 | case _Py_ERROR_IGNORE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8365 | *inpos = collendpos; |
| 8366 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8367 | |
| 8368 | case _Py_ERROR_XMLCHARREFREPLACE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8369 | /* generate replacement (temporarily (mis)uses p) */ |
| 8370 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8371 | char buffer[2+29+1+1]; |
| 8372 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8373 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8374 | for (cp = buffer; *cp; ++cp) { |
| 8375 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8376 | if (x==enc_EXCEPTION) |
| 8377 | return -1; |
| 8378 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8379 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | return -1; |
| 8381 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8382 | } |
| 8383 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8384 | *inpos = collendpos; |
| 8385 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8386 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8387 | default: |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8388 | repunicode = unicode_encode_call_errorhandler(errors, error_handler_obj, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8389 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8390 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8391 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8392 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8393 | if (PyBytes_Check(repunicode)) { |
| 8394 | /* Directly copy bytes result to output. */ |
| 8395 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8396 | Py_ssize_t requiredsize; |
| 8397 | repsize = PyBytes_Size(repunicode); |
| 8398 | requiredsize = *respos + repsize; |
| 8399 | if (requiredsize > outsize) |
| 8400 | /* Make room for all additional bytes. */ |
| 8401 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8402 | Py_DECREF(repunicode); |
| 8403 | return -1; |
| 8404 | } |
| 8405 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8406 | PyBytes_AsString(repunicode), repsize); |
| 8407 | *respos += repsize; |
| 8408 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8409 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8410 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8411 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8412 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8413 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8414 | Py_DECREF(repunicode); |
| 8415 | return -1; |
| 8416 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8417 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8418 | data = PyUnicode_DATA(repunicode); |
| 8419 | kind = PyUnicode_KIND(repunicode); |
| 8420 | for (index = 0; index < repsize; index++) { |
| 8421 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8422 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8423 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8424 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8425 | return -1; |
| 8426 | } |
| 8427 | else if (x==enc_FAILED) { |
| 8428 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8429 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8430 | return -1; |
| 8431 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8432 | } |
| 8433 | *inpos = newpos; |
| 8434 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8435 | } |
| 8436 | return 0; |
| 8437 | } |
| 8438 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8439 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8440 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8441 | PyObject *mapping, |
| 8442 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8443 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8444 | /* output object */ |
| 8445 | PyObject *res = NULL; |
| 8446 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8447 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8448 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8449 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8450 | Py_ssize_t respos = 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8451 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8452 | PyObject *exc = NULL; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8453 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8454 | void *data; |
| 8455 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8456 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8457 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8458 | return NULL; |
| 8459 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8460 | data = PyUnicode_DATA(unicode); |
| 8461 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8463 | /* Default to Latin-1 */ |
| 8464 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8465 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8466 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8467 | /* allocate enough for a simple encoding without |
| 8468 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8469 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8470 | if (res == NULL) |
| 8471 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8472 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8473 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8474 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8475 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8476 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8477 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8478 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8479 | if (x==enc_EXCEPTION) /* error */ |
| 8480 | goto onError; |
| 8481 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8482 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8483 | &exc, |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8484 | &error_handler, &error_handler_obj, errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8485 | &res, &respos)) { |
| 8486 | goto onError; |
| 8487 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8488 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8489 | else |
| 8490 | /* done with this character => adjust input position */ |
| 8491 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8492 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8494 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8495 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8496 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8497 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8498 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8499 | Py_XDECREF(exc); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8500 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8501 | return res; |
| 8502 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8503 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8504 | Py_XDECREF(res); |
| 8505 | Py_XDECREF(exc); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8506 | Py_XDECREF(error_handler_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8507 | return NULL; |
| 8508 | } |
| 8509 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8510 | /* Deprecated */ |
| 8511 | PyObject * |
| 8512 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8513 | Py_ssize_t size, |
| 8514 | PyObject *mapping, |
| 8515 | const char *errors) |
| 8516 | { |
| 8517 | PyObject *result; |
| 8518 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8519 | if (unicode == NULL) |
| 8520 | return NULL; |
| 8521 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8522 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8523 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8524 | } |
| 8525 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8526 | PyObject * |
| 8527 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8528 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8529 | { |
| 8530 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8531 | PyErr_BadArgument(); |
| 8532 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8533 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8534 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8535 | } |
| 8536 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8537 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8538 | static void |
| 8539 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8540 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8541 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8542 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8544 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8545 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8546 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8547 | } |
| 8548 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8549 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8550 | goto onError; |
| 8551 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8552 | goto onError; |
| 8553 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8554 | goto onError; |
| 8555 | return; |
| 8556 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 8557 | Py_CLEAR(*exceptionObject); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8558 | } |
| 8559 | } |
| 8560 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8561 | /* error handling callback helper: |
| 8562 | build arguments, call the callback and check the arguments, |
| 8563 | put the result into newpos and return the replacement string, which |
| 8564 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8565 | static PyObject * |
| 8566 | unicode_translate_call_errorhandler(const char *errors, |
| 8567 | PyObject **errorHandler, |
| 8568 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8569 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8570 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8571 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8572 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8573 | static char *argparse = "O!n;translating error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8574 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8575 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8576 | PyObject *restuple; |
| 8577 | PyObject *resunicode; |
| 8578 | |
| 8579 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8580 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8581 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8582 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8583 | } |
| 8584 | |
| 8585 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8586 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8587 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8588 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8589 | |
| 8590 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8591 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8592 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8593 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8594 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8595 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8596 | Py_DECREF(restuple); |
| 8597 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8598 | } |
| 8599 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8600 | &resunicode, &i_newpos)) { |
| 8601 | Py_DECREF(restuple); |
| 8602 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8603 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8604 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8605 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8606 | else |
| 8607 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8608 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 8609 | 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] | 8610 | Py_DECREF(restuple); |
| 8611 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8612 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8613 | Py_INCREF(resunicode); |
| 8614 | Py_DECREF(restuple); |
| 8615 | return resunicode; |
| 8616 | } |
| 8617 | |
| 8618 | /* Lookup the character ch in the mapping and put the result in result, |
| 8619 | which must be decrefed by the caller. |
| 8620 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8621 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8622 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8623 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8624 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8625 | PyObject *x; |
| 8626 | |
| 8627 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8628 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8629 | x = PyObject_GetItem(mapping, w); |
| 8630 | Py_DECREF(w); |
| 8631 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8632 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8633 | /* No mapping found means: use 1:1 mapping. */ |
| 8634 | PyErr_Clear(); |
| 8635 | *result = NULL; |
| 8636 | return 0; |
| 8637 | } else |
| 8638 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8639 | } |
| 8640 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8641 | *result = x; |
| 8642 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8643 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8644 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8645 | long value = PyLong_AS_LONG(x); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8646 | if (value < 0 || value > MAX_UNICODE) { |
| 8647 | PyErr_Format(PyExc_ValueError, |
| 8648 | "character mapping must be in range(0x%x)", |
| 8649 | MAX_UNICODE+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8650 | Py_DECREF(x); |
| 8651 | return -1; |
| 8652 | } |
| 8653 | *result = x; |
| 8654 | return 0; |
| 8655 | } |
| 8656 | else if (PyUnicode_Check(x)) { |
| 8657 | *result = x; |
| 8658 | return 0; |
| 8659 | } |
| 8660 | else { |
| 8661 | /* wrong return value */ |
| 8662 | PyErr_SetString(PyExc_TypeError, |
| 8663 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8664 | Py_DECREF(x); |
| 8665 | return -1; |
| 8666 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8667 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8668 | |
| 8669 | /* lookup the character, write the result into the writer. |
| 8670 | Return 1 if the result was written into the writer, return 0 if the mapping |
| 8671 | was undefined, raise an exception return -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8672 | static int |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8673 | charmaptranslate_output(Py_UCS4 ch, PyObject *mapping, |
| 8674 | _PyUnicodeWriter *writer) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8675 | { |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8676 | PyObject *item; |
| 8677 | |
| 8678 | if (charmaptranslate_lookup(ch, mapping, &item)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8679 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8680 | |
| 8681 | if (item == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8682 | /* not found => default to 1:1 mapping */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8683 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8684 | return -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8685 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8686 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8687 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8688 | |
| 8689 | if (item == Py_None) { |
| 8690 | Py_DECREF(item); |
| 8691 | return 0; |
| 8692 | } |
| 8693 | |
| 8694 | if (PyLong_Check(item)) { |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8695 | long ch = (Py_UCS4)PyLong_AS_LONG(item); |
| 8696 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8697 | used it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8698 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
| 8699 | Py_DECREF(item); |
| 8700 | return -1; |
| 8701 | } |
| 8702 | Py_DECREF(item); |
| 8703 | return 1; |
| 8704 | } |
| 8705 | |
| 8706 | if (!PyUnicode_Check(item)) { |
| 8707 | Py_DECREF(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8708 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8709 | } |
| 8710 | |
| 8711 | if (_PyUnicodeWriter_WriteStr(writer, item) < 0) { |
| 8712 | Py_DECREF(item); |
| 8713 | return -1; |
| 8714 | } |
| 8715 | |
| 8716 | Py_DECREF(item); |
| 8717 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8718 | } |
| 8719 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8720 | static int |
| 8721 | unicode_fast_translate_lookup(PyObject *mapping, Py_UCS1 ch, |
| 8722 | Py_UCS1 *translate) |
| 8723 | { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8724 | PyObject *item = NULL; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8725 | int ret = 0; |
| 8726 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8727 | if (charmaptranslate_lookup(ch, mapping, &item)) { |
| 8728 | return -1; |
| 8729 | } |
| 8730 | |
| 8731 | if (item == Py_None) { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8732 | /* deletion */ |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8733 | translate[ch] = 0xfe; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8734 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8735 | else if (item == NULL) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8736 | /* not found => default to 1:1 mapping */ |
| 8737 | translate[ch] = ch; |
| 8738 | return 1; |
| 8739 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8740 | else if (PyLong_Check(item)) { |
Victor Stinner | 4dd2525 | 2014-04-08 09:14:21 +0200 | [diff] [blame] | 8741 | long replace = PyLong_AS_LONG(item); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8742 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8743 | used it */ |
| 8744 | if (127 < replace) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8745 | /* invalid character or character outside ASCII: |
| 8746 | skip the fast translate */ |
| 8747 | goto exit; |
| 8748 | } |
| 8749 | translate[ch] = (Py_UCS1)replace; |
| 8750 | } |
| 8751 | else if (PyUnicode_Check(item)) { |
| 8752 | Py_UCS4 replace; |
| 8753 | |
| 8754 | if (PyUnicode_READY(item) == -1) { |
| 8755 | Py_DECREF(item); |
| 8756 | return -1; |
| 8757 | } |
| 8758 | if (PyUnicode_GET_LENGTH(item) != 1) |
| 8759 | goto exit; |
| 8760 | |
| 8761 | replace = PyUnicode_READ_CHAR(item, 0); |
| 8762 | if (replace > 127) |
| 8763 | goto exit; |
| 8764 | translate[ch] = (Py_UCS1)replace; |
| 8765 | } |
| 8766 | else { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8767 | /* not None, NULL, long or unicode */ |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8768 | goto exit; |
| 8769 | } |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8770 | ret = 1; |
| 8771 | |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8772 | exit: |
| 8773 | Py_DECREF(item); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8774 | return ret; |
| 8775 | } |
| 8776 | |
| 8777 | /* Fast path for ascii => ascii translation. Return 1 if the whole string |
| 8778 | was translated into writer, return 0 if the input string was partially |
| 8779 | translated into writer, raise an exception and return -1 on error. */ |
| 8780 | static int |
| 8781 | unicode_fast_translate(PyObject *input, PyObject *mapping, |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8782 | _PyUnicodeWriter *writer, int ignore) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8783 | { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8784 | Py_UCS1 ascii_table[128], ch, ch2; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8785 | Py_ssize_t len; |
| 8786 | Py_UCS1 *in, *end, *out; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8787 | int res = 0; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8788 | |
| 8789 | if (PyUnicode_READY(input) == -1) |
| 8790 | return -1; |
| 8791 | if (!PyUnicode_IS_ASCII(input)) |
| 8792 | return 0; |
| 8793 | len = PyUnicode_GET_LENGTH(input); |
| 8794 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8795 | memset(ascii_table, 0xff, 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8796 | |
| 8797 | in = PyUnicode_1BYTE_DATA(input); |
| 8798 | end = in + len; |
| 8799 | |
| 8800 | assert(PyUnicode_IS_ASCII(writer->buffer)); |
| 8801 | assert(PyUnicode_GET_LENGTH(writer->buffer) == len); |
| 8802 | out = PyUnicode_1BYTE_DATA(writer->buffer); |
| 8803 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8804 | for (; in < end; in++) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8805 | ch = *in; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8806 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8807 | if (ch2 == 0xff) { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8808 | int translate = unicode_fast_translate_lookup(mapping, ch, |
| 8809 | ascii_table); |
| 8810 | if (translate < 0) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8811 | return -1; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8812 | if (translate == 0) |
| 8813 | goto exit; |
| 8814 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8815 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8816 | if (ch2 == 0xfe) { |
| 8817 | if (ignore) |
| 8818 | continue; |
| 8819 | goto exit; |
| 8820 | } |
| 8821 | assert(ch2 < 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8822 | *out = ch2; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8823 | out++; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8824 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8825 | res = 1; |
| 8826 | |
| 8827 | exit: |
| 8828 | writer->pos = out - PyUnicode_1BYTE_DATA(writer->buffer); |
| 8829 | return res; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8830 | } |
| 8831 | |
Victor Stinner | 3222da2 | 2015-10-01 22:07:32 +0200 | [diff] [blame] | 8832 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8834 | PyObject *mapping, |
| 8835 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8836 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8837 | /* input object */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8838 | char *data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8839 | Py_ssize_t size, i; |
| 8840 | int kind; |
| 8841 | /* output buffer */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8842 | _PyUnicodeWriter writer; |
| 8843 | /* error handler */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8844 | char *reason = "character maps to <undefined>"; |
| 8845 | PyObject *errorHandler = NULL; |
| 8846 | PyObject *exc = NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8847 | int ignore; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8848 | int res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8849 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8850 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8851 | PyErr_BadArgument(); |
| 8852 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8853 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8854 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8855 | if (PyUnicode_READY(input) == -1) |
| 8856 | return NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8857 | data = (char*)PyUnicode_DATA(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8858 | kind = PyUnicode_KIND(input); |
| 8859 | size = PyUnicode_GET_LENGTH(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8860 | |
| 8861 | if (size == 0) { |
| 8862 | Py_INCREF(input); |
| 8863 | return input; |
| 8864 | } |
| 8865 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8866 | /* allocate enough for a simple 1:1 translation without |
| 8867 | replacements, if we need more, we'll resize */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8868 | _PyUnicodeWriter_Init(&writer); |
| 8869 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8870 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8871 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8872 | ignore = (errors != NULL && strcmp(errors, "ignore") == 0); |
| 8873 | |
| 8874 | res = unicode_fast_translate(input, mapping, &writer, ignore); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8875 | if (res < 0) { |
| 8876 | _PyUnicodeWriter_Dealloc(&writer); |
| 8877 | return NULL; |
| 8878 | } |
| 8879 | if (res == 1) |
| 8880 | return _PyUnicodeWriter_Finish(&writer); |
| 8881 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8882 | i = writer.pos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8883 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8884 | /* try to encode it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8885 | int translate; |
| 8886 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8887 | Py_ssize_t newpos; |
| 8888 | /* startpos for collecting untranslatable chars */ |
| 8889 | Py_ssize_t collstart; |
| 8890 | Py_ssize_t collend; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8891 | Py_UCS4 ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8892 | |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8893 | ch = PyUnicode_READ(kind, data, i); |
| 8894 | translate = charmaptranslate_output(ch, mapping, &writer); |
| 8895 | if (translate < 0) |
| 8896 | goto onError; |
| 8897 | |
| 8898 | if (translate != 0) { |
| 8899 | /* it worked => adjust input pointer */ |
| 8900 | ++i; |
| 8901 | continue; |
| 8902 | } |
| 8903 | |
| 8904 | /* untranslatable character */ |
| 8905 | collstart = i; |
| 8906 | collend = i+1; |
| 8907 | |
| 8908 | /* find all untranslatable characters */ |
| 8909 | while (collend < size) { |
| 8910 | PyObject *x; |
| 8911 | ch = PyUnicode_READ(kind, data, collend); |
| 8912 | if (charmaptranslate_lookup(ch, mapping, &x)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8913 | goto onError; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8914 | Py_XDECREF(x); |
| 8915 | if (x != Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8916 | break; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8917 | ++collend; |
| 8918 | } |
| 8919 | |
| 8920 | if (ignore) { |
| 8921 | i = collend; |
| 8922 | } |
| 8923 | else { |
| 8924 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 8925 | reason, input, &exc, |
| 8926 | collstart, collend, &newpos); |
| 8927 | if (repunicode == NULL) |
| 8928 | goto onError; |
| 8929 | if (_PyUnicodeWriter_WriteStr(&writer, repunicode) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8930 | Py_DECREF(repunicode); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8931 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8932 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8933 | Py_DECREF(repunicode); |
| 8934 | i = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8935 | } |
| 8936 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8937 | Py_XDECREF(exc); |
| 8938 | Py_XDECREF(errorHandler); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8939 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8940 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8941 | onError: |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8942 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8943 | Py_XDECREF(exc); |
| 8944 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8945 | return NULL; |
| 8946 | } |
| 8947 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8948 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8949 | PyObject * |
| 8950 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8951 | Py_ssize_t size, |
| 8952 | PyObject *mapping, |
| 8953 | const char *errors) |
| 8954 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8955 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8956 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8957 | if (!unicode) |
| 8958 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8959 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8960 | Py_DECREF(unicode); |
| 8961 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8962 | } |
| 8963 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8964 | PyObject * |
| 8965 | PyUnicode_Translate(PyObject *str, |
| 8966 | PyObject *mapping, |
| 8967 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8968 | { |
| 8969 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8971 | str = PyUnicode_FromObject(str); |
| 8972 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8973 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8974 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8975 | Py_DECREF(str); |
| 8976 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8977 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8979 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8980 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 | { |
| 8982 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8983 | called as a callback from fixup() which does it already. */ |
| 8984 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8985 | const int kind = PyUnicode_KIND(self); |
| 8986 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8987 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8988 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8989 | Py_ssize_t i; |
| 8990 | |
| 8991 | for (i = 0; i < len; ++i) { |
| 8992 | ch = PyUnicode_READ(kind, data, i); |
| 8993 | fixed = 0; |
| 8994 | if (ch > 127) { |
| 8995 | if (Py_UNICODE_ISSPACE(ch)) |
| 8996 | fixed = ' '; |
| 8997 | else { |
| 8998 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8999 | if (decimal >= 0) |
| 9000 | fixed = '0' + decimal; |
| 9001 | } |
| 9002 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9003 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9004 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9005 | PyUnicode_WRITE(kind, data, i, fixed); |
| 9006 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9007 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9008 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9009 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9010 | } |
| 9011 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 9012 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9013 | } |
| 9014 | |
| 9015 | PyObject * |
| 9016 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 9017 | { |
| 9018 | if (!PyUnicode_Check(unicode)) { |
| 9019 | PyErr_BadInternalCall(); |
| 9020 | return NULL; |
| 9021 | } |
| 9022 | if (PyUnicode_READY(unicode) == -1) |
| 9023 | return NULL; |
| 9024 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 9025 | /* If the string is already ASCII, just return the same string */ |
| 9026 | Py_INCREF(unicode); |
| 9027 | return unicode; |
| 9028 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9029 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 | } |
| 9031 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9032 | PyObject * |
| 9033 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 9034 | Py_ssize_t length) |
| 9035 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9036 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9037 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9038 | Py_UCS4 maxchar; |
| 9039 | enum PyUnicode_Kind kind; |
| 9040 | void *data; |
| 9041 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 9042 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9043 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 9044 | Py_UCS4 ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9045 | if (ch > 127) { |
| 9046 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9047 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9048 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9049 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9050 | } |
| 9051 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9052 | |
| 9053 | /* Copy to a new string */ |
| 9054 | decimal = PyUnicode_New(length, maxchar); |
| 9055 | if (decimal == NULL) |
| 9056 | return decimal; |
| 9057 | kind = PyUnicode_KIND(decimal); |
| 9058 | data = PyUnicode_DATA(decimal); |
| 9059 | /* Iterate over code points */ |
| 9060 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 9061 | Py_UCS4 ch = s[i]; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9062 | if (ch > 127) { |
| 9063 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9064 | if (decimal >= 0) |
| 9065 | ch = '0' + decimal; |
| 9066 | } |
| 9067 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9068 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9069 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9070 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9071 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9072 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9073 | int |
| 9074 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9075 | Py_ssize_t length, |
| 9076 | char *output, |
| 9077 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9078 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9079 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9080 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9081 | enum PyUnicode_Kind kind; |
| 9082 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9083 | |
| 9084 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9085 | PyErr_BadArgument(); |
| 9086 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9087 | } |
| 9088 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9089 | unicode = PyUnicode_FromUnicode(s, length); |
| 9090 | if (unicode == NULL) |
| 9091 | return -1; |
| 9092 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9093 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9094 | Py_DECREF(unicode); |
| 9095 | return -1; |
| 9096 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9097 | kind = PyUnicode_KIND(unicode); |
| 9098 | data = PyUnicode_DATA(unicode); |
| 9099 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9100 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9101 | PyObject *exc; |
| 9102 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9103 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9104 | Py_ssize_t startpos; |
| 9105 | |
| 9106 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9107 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9108 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9109 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9110 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9111 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9112 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9113 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9114 | if (decimal >= 0) { |
| 9115 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9116 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9117 | continue; |
| 9118 | } |
| 9119 | if (0 < ch && ch < 256) { |
| 9120 | *output++ = (char)ch; |
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; |
| 9123 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9124 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9125 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9126 | exc = NULL; |
| 9127 | raise_encode_exception(&exc, "decimal", unicode, |
| 9128 | startpos, startpos+1, |
| 9129 | "invalid decimal Unicode string"); |
| 9130 | Py_XDECREF(exc); |
| 9131 | Py_DECREF(unicode); |
| 9132 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9133 | } |
| 9134 | /* 0-terminate the output string */ |
| 9135 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9136 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9137 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9138 | } |
| 9139 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9140 | /* --- Helpers ------------------------------------------------------------ */ |
| 9141 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9142 | /* helper macro to fixup start/end slice values */ |
| 9143 | #define ADJUST_INDICES(start, end, len) \ |
| 9144 | if (end > len) \ |
| 9145 | end = len; \ |
| 9146 | else if (end < 0) { \ |
| 9147 | end += len; \ |
| 9148 | if (end < 0) \ |
| 9149 | end = 0; \ |
| 9150 | } \ |
| 9151 | if (start < 0) { \ |
| 9152 | start += len; \ |
| 9153 | if (start < 0) \ |
| 9154 | start = 0; \ |
| 9155 | } |
| 9156 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9157 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9158 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9159 | Py_ssize_t start, |
| 9160 | Py_ssize_t end) |
| 9161 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9162 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9163 | void *buf1, *buf2; |
| 9164 | Py_ssize_t len1, len2, result; |
| 9165 | |
| 9166 | kind1 = PyUnicode_KIND(s1); |
| 9167 | kind2 = PyUnicode_KIND(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9168 | if (kind1 < kind2) |
| 9169 | return -1; |
| 9170 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9171 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9172 | len2 = PyUnicode_GET_LENGTH(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9173 | ADJUST_INDICES(start, end, len1); |
| 9174 | if (end - start < len2) |
| 9175 | return -1; |
| 9176 | |
| 9177 | buf1 = PyUnicode_DATA(s1); |
| 9178 | buf2 = PyUnicode_DATA(s2); |
| 9179 | if (len2 == 1) { |
| 9180 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 9181 | result = findchar((const char *)buf1 + kind1*start, |
| 9182 | kind1, end - start, ch, direction); |
| 9183 | if (result == -1) |
| 9184 | return -1; |
| 9185 | else |
| 9186 | return start + result; |
| 9187 | } |
| 9188 | |
| 9189 | if (kind2 != kind1) { |
| 9190 | buf2 = _PyUnicode_AsKind(s2, kind1); |
| 9191 | if (!buf2) |
| 9192 | return -2; |
| 9193 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9194 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9195 | if (direction > 0) { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9196 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9197 | case PyUnicode_1BYTE_KIND: |
| 9198 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9199 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9200 | else |
| 9201 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9202 | break; |
| 9203 | case PyUnicode_2BYTE_KIND: |
| 9204 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9205 | break; |
| 9206 | case PyUnicode_4BYTE_KIND: |
| 9207 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9208 | break; |
| 9209 | default: |
| 9210 | assert(0); result = -2; |
| 9211 | } |
| 9212 | } |
| 9213 | else { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9214 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9215 | case PyUnicode_1BYTE_KIND: |
| 9216 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9217 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9218 | else |
| 9219 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9220 | break; |
| 9221 | case PyUnicode_2BYTE_KIND: |
| 9222 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9223 | break; |
| 9224 | case PyUnicode_4BYTE_KIND: |
| 9225 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9226 | break; |
| 9227 | default: |
| 9228 | assert(0); result = -2; |
| 9229 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9230 | } |
| 9231 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9232 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9233 | PyMem_Free(buf2); |
| 9234 | |
| 9235 | return result; |
| 9236 | } |
| 9237 | |
| 9238 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9239 | _PyUnicode_InsertThousandsGrouping( |
| 9240 | PyObject *unicode, Py_ssize_t index, |
| 9241 | Py_ssize_t n_buffer, |
| 9242 | void *digits, Py_ssize_t n_digits, |
| 9243 | Py_ssize_t min_width, |
| 9244 | const char *grouping, PyObject *thousands_sep, |
| 9245 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9246 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9247 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9248 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9249 | Py_ssize_t thousands_sep_len; |
| 9250 | Py_ssize_t len; |
| 9251 | |
| 9252 | if (unicode != NULL) { |
| 9253 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9254 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9255 | } |
| 9256 | else { |
| 9257 | kind = PyUnicode_1BYTE_KIND; |
| 9258 | data = NULL; |
| 9259 | } |
| 9260 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9261 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9262 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9263 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9264 | if (thousands_sep_kind < kind) { |
| 9265 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9266 | if (!thousands_sep_data) |
| 9267 | return -1; |
| 9268 | } |
| 9269 | else { |
| 9270 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9271 | if (!data) |
| 9272 | return -1; |
| 9273 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9274 | } |
| 9275 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9276 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9278 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9279 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9280 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9281 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9282 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9283 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9284 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9285 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9286 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9287 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9288 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9289 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9290 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9291 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9292 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9293 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9294 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9296 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9297 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) 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_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9300 | break; |
| 9301 | default: |
| 9302 | assert(0); |
| 9303 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9304 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9305 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9306 | if (thousands_sep_kind < kind) |
| 9307 | PyMem_Free(thousands_sep_data); |
| 9308 | else |
| 9309 | PyMem_Free(data); |
| 9310 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9311 | if (unicode == NULL) { |
| 9312 | *maxchar = 127; |
| 9313 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9314 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9315 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9316 | } |
| 9317 | } |
| 9318 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9319 | } |
| 9320 | |
| 9321 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9322 | Py_ssize_t |
| 9323 | PyUnicode_Count(PyObject *str, |
| 9324 | PyObject *substr, |
| 9325 | Py_ssize_t start, |
| 9326 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9327 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9328 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9329 | PyObject* str_obj; |
| 9330 | PyObject* sub_obj; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9331 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9332 | void *buf1 = NULL, *buf2 = NULL; |
| 9333 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9334 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9335 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9336 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9337 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9338 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9339 | if (!sub_obj) { |
| 9340 | Py_DECREF(str_obj); |
| 9341 | return -1; |
| 9342 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9343 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9344 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9345 | Py_DECREF(str_obj); |
| 9346 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9347 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9348 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9349 | kind1 = PyUnicode_KIND(str_obj); |
| 9350 | kind2 = PyUnicode_KIND(sub_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9351 | if (kind1 < kind2) { |
| 9352 | Py_DECREF(sub_obj); |
| 9353 | Py_DECREF(str_obj); |
| 9354 | return 0; |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9355 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9356 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9357 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9358 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9359 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9360 | if (end - start < len2) { |
| 9361 | Py_DECREF(sub_obj); |
| 9362 | Py_DECREF(str_obj); |
| 9363 | return 0; |
| 9364 | } |
| 9365 | |
| 9366 | buf1 = PyUnicode_DATA(str_obj); |
| 9367 | buf2 = PyUnicode_DATA(sub_obj); |
| 9368 | if (kind2 != kind1) { |
| 9369 | buf2 = _PyUnicode_AsKind(sub_obj, kind1); |
| 9370 | if (!buf2) |
| 9371 | goto onError; |
| 9372 | } |
| 9373 | |
| 9374 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9375 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9376 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9377 | result = asciilib_count( |
| 9378 | ((Py_UCS1*)buf1) + start, end - start, |
| 9379 | buf2, len2, PY_SSIZE_T_MAX |
| 9380 | ); |
| 9381 | else |
| 9382 | result = ucs1lib_count( |
| 9383 | ((Py_UCS1*)buf1) + start, end - start, |
| 9384 | buf2, len2, PY_SSIZE_T_MAX |
| 9385 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9386 | break; |
| 9387 | case PyUnicode_2BYTE_KIND: |
| 9388 | result = ucs2lib_count( |
| 9389 | ((Py_UCS2*)buf1) + start, end - start, |
| 9390 | buf2, len2, PY_SSIZE_T_MAX |
| 9391 | ); |
| 9392 | break; |
| 9393 | case PyUnicode_4BYTE_KIND: |
| 9394 | result = ucs4lib_count( |
| 9395 | ((Py_UCS4*)buf1) + start, end - start, |
| 9396 | buf2, len2, PY_SSIZE_T_MAX |
| 9397 | ); |
| 9398 | break; |
| 9399 | default: |
| 9400 | assert(0); result = 0; |
| 9401 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9402 | |
| 9403 | Py_DECREF(sub_obj); |
| 9404 | Py_DECREF(str_obj); |
| 9405 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9406 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9407 | PyMem_Free(buf2); |
| 9408 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9409 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9410 | onError: |
| 9411 | Py_DECREF(sub_obj); |
| 9412 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9413 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9414 | PyMem_Free(buf2); |
| 9415 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9416 | } |
| 9417 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9418 | Py_ssize_t |
| 9419 | PyUnicode_Find(PyObject *str, |
| 9420 | PyObject *sub, |
| 9421 | Py_ssize_t start, |
| 9422 | Py_ssize_t end, |
| 9423 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9424 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9425 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9427 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9428 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9429 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9430 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9431 | if (!sub) { |
| 9432 | Py_DECREF(str); |
| 9433 | return -2; |
| 9434 | } |
| 9435 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9436 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9437 | Py_DECREF(str); |
| 9438 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9439 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9440 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9441 | result = any_find_slice(direction, |
| 9442 | str, sub, start, end |
| 9443 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9445 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9446 | Py_DECREF(sub); |
| 9447 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9448 | return result; |
| 9449 | } |
| 9450 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9451 | Py_ssize_t |
| 9452 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9453 | Py_ssize_t start, Py_ssize_t end, |
| 9454 | int direction) |
| 9455 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9456 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9457 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9458 | if (PyUnicode_READY(str) == -1) |
| 9459 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9460 | if (start < 0 || end < 0) { |
| 9461 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9462 | return -2; |
| 9463 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9464 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9465 | end = PyUnicode_GET_LENGTH(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9466 | if (start >= end) |
| 9467 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9468 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9469 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9470 | kind, end-start, ch, direction); |
| 9471 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9472 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9473 | else |
| 9474 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9475 | } |
| 9476 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9477 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9478 | tailmatch(PyObject *self, |
| 9479 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9480 | Py_ssize_t start, |
| 9481 | Py_ssize_t end, |
| 9482 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9483 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9484 | int kind_self; |
| 9485 | int kind_sub; |
| 9486 | void *data_self; |
| 9487 | void *data_sub; |
| 9488 | Py_ssize_t offset; |
| 9489 | Py_ssize_t i; |
| 9490 | Py_ssize_t end_sub; |
| 9491 | |
| 9492 | if (PyUnicode_READY(self) == -1 || |
| 9493 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9494 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9496 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9497 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9498 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9499 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9500 | |
Serhiy Storchaka | d4ea03c | 2015-05-31 09:15:51 +0300 | [diff] [blame] | 9501 | if (PyUnicode_GET_LENGTH(substring) == 0) |
| 9502 | return 1; |
| 9503 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9504 | kind_self = PyUnicode_KIND(self); |
| 9505 | data_self = PyUnicode_DATA(self); |
| 9506 | kind_sub = PyUnicode_KIND(substring); |
| 9507 | data_sub = PyUnicode_DATA(substring); |
| 9508 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9509 | |
| 9510 | if (direction > 0) |
| 9511 | offset = end; |
| 9512 | else |
| 9513 | offset = start; |
| 9514 | |
| 9515 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9516 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9517 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9518 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9519 | /* If both are of the same kind, memcmp is sufficient */ |
| 9520 | if (kind_self == kind_sub) { |
| 9521 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9522 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9523 | data_sub, |
| 9524 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9525 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9526 | } |
| 9527 | /* otherwise we have to compare each character by first accesing it */ |
| 9528 | else { |
| 9529 | /* We do not need to compare 0 and len(substring)-1 because |
| 9530 | the if statement above ensured already that they are equal |
| 9531 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9532 | for (i = 1; i < end_sub; ++i) { |
| 9533 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9534 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9535 | return 0; |
| 9536 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9537 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9538 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9539 | } |
| 9540 | |
| 9541 | return 0; |
| 9542 | } |
| 9543 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9544 | Py_ssize_t |
| 9545 | PyUnicode_Tailmatch(PyObject *str, |
| 9546 | PyObject *substr, |
| 9547 | Py_ssize_t start, |
| 9548 | Py_ssize_t end, |
| 9549 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9550 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9551 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9553 | str = PyUnicode_FromObject(str); |
| 9554 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9555 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9556 | substr = PyUnicode_FromObject(substr); |
| 9557 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9558 | Py_DECREF(str); |
| 9559 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9560 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9561 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9562 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9563 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9564 | Py_DECREF(str); |
| 9565 | Py_DECREF(substr); |
| 9566 | return result; |
| 9567 | } |
| 9568 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9569 | /* Apply fixfct filter to the Unicode object self and return a |
| 9570 | reference to the modified object */ |
| 9571 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9572 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9573 | fixup(PyObject *self, |
| 9574 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9575 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9576 | PyObject *u; |
| 9577 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9578 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9579 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9580 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9581 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9582 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9583 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9584 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9585 | /* fix functions return the new maximum character in a string, |
| 9586 | if the kind of the resulting unicode object does not change, |
| 9587 | everything is fine. Otherwise we need to change the string kind |
| 9588 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9589 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9590 | |
| 9591 | if (maxchar_new == 0) { |
| 9592 | /* no changes */; |
| 9593 | if (PyUnicode_CheckExact(self)) { |
| 9594 | Py_DECREF(u); |
| 9595 | Py_INCREF(self); |
| 9596 | return self; |
| 9597 | } |
| 9598 | else |
| 9599 | return u; |
| 9600 | } |
| 9601 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9602 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9603 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9604 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9605 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9606 | |
| 9607 | /* In case the maximum character changed, we need to |
| 9608 | convert the string to the new category. */ |
| 9609 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9610 | if (v == NULL) { |
| 9611 | Py_DECREF(u); |
| 9612 | return NULL; |
| 9613 | } |
| 9614 | if (maxchar_new > maxchar_old) { |
| 9615 | /* If the maxchar increased so that the kind changed, not all |
| 9616 | characters are representable anymore and we need to fix the |
| 9617 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9618 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9619 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9620 | maxchar_old = fixfct(v); |
| 9621 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9622 | } |
| 9623 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9624 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9625 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9626 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9627 | Py_DECREF(u); |
| 9628 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9629 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9630 | } |
| 9631 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9632 | static PyObject * |
| 9633 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9634 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9635 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9636 | char *resdata, *data = PyUnicode_DATA(self); |
| 9637 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9638 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9639 | res = PyUnicode_New(len, 127); |
| 9640 | if (res == NULL) |
| 9641 | return NULL; |
| 9642 | resdata = PyUnicode_DATA(res); |
| 9643 | if (lower) |
| 9644 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9645 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9646 | _Py_bytes_upper(resdata, data, len); |
| 9647 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9648 | } |
| 9649 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9650 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9651 | 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] | 9652 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9653 | Py_ssize_t j; |
| 9654 | int final_sigma; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 9655 | Py_UCS4 c = 0; /* initialize to prevent gcc warning */ |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9656 | /* 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] | 9657 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9658 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9659 | |
| 9660 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9661 | */ |
| 9662 | for (j = i - 1; j >= 0; j--) { |
| 9663 | c = PyUnicode_READ(kind, data, j); |
| 9664 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9665 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9666 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9667 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9668 | if (final_sigma) { |
| 9669 | for (j = i + 1; j < length; j++) { |
| 9670 | c = PyUnicode_READ(kind, data, j); |
| 9671 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9672 | break; |
| 9673 | } |
| 9674 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9675 | } |
| 9676 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9677 | } |
| 9678 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9679 | static int |
| 9680 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9681 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9682 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9683 | /* Obscure special case. */ |
| 9684 | if (c == 0x3A3) { |
| 9685 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9686 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9687 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9688 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9689 | } |
| 9690 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9691 | static Py_ssize_t |
| 9692 | 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] | 9693 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9694 | Py_ssize_t i, k = 0; |
| 9695 | int n_res, j; |
| 9696 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9697 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9698 | c = PyUnicode_READ(kind, data, 0); |
| 9699 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9700 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9701 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9702 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9703 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9704 | for (i = 1; i < length; i++) { |
| 9705 | c = PyUnicode_READ(kind, data, i); |
| 9706 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9707 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9708 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9709 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9710 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9711 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9712 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9713 | } |
| 9714 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9715 | static Py_ssize_t |
| 9716 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9717 | Py_ssize_t i, k = 0; |
| 9718 | |
| 9719 | for (i = 0; i < length; i++) { |
| 9720 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9721 | int n_res, j; |
| 9722 | if (Py_UNICODE_ISUPPER(c)) { |
| 9723 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9724 | } |
| 9725 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9726 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9727 | } |
| 9728 | else { |
| 9729 | n_res = 1; |
| 9730 | mapped[0] = c; |
| 9731 | } |
| 9732 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9733 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9734 | res[k++] = mapped[j]; |
| 9735 | } |
| 9736 | } |
| 9737 | return k; |
| 9738 | } |
| 9739 | |
| 9740 | static Py_ssize_t |
| 9741 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9742 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9743 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9744 | Py_ssize_t i, k = 0; |
| 9745 | |
| 9746 | for (i = 0; i < length; i++) { |
| 9747 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9748 | int n_res, j; |
| 9749 | if (lower) |
| 9750 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9751 | else |
| 9752 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9753 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9754 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9755 | res[k++] = mapped[j]; |
| 9756 | } |
| 9757 | } |
| 9758 | return k; |
| 9759 | } |
| 9760 | |
| 9761 | static Py_ssize_t |
| 9762 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9763 | { |
| 9764 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9765 | } |
| 9766 | |
| 9767 | static Py_ssize_t |
| 9768 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9769 | { |
| 9770 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9771 | } |
| 9772 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9773 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9774 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9775 | { |
| 9776 | Py_ssize_t i, k = 0; |
| 9777 | |
| 9778 | for (i = 0; i < length; i++) { |
| 9779 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9780 | Py_UCS4 mapped[3]; |
| 9781 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9782 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9783 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9784 | res[k++] = mapped[j]; |
| 9785 | } |
| 9786 | } |
| 9787 | return k; |
| 9788 | } |
| 9789 | |
| 9790 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9791 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9792 | { |
| 9793 | Py_ssize_t i, k = 0; |
| 9794 | int previous_is_cased; |
| 9795 | |
| 9796 | previous_is_cased = 0; |
| 9797 | for (i = 0; i < length; i++) { |
| 9798 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9799 | Py_UCS4 mapped[3]; |
| 9800 | int n_res, j; |
| 9801 | |
| 9802 | if (previous_is_cased) |
| 9803 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9804 | else |
| 9805 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9806 | |
| 9807 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9808 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9809 | res[k++] = mapped[j]; |
| 9810 | } |
| 9811 | |
| 9812 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9813 | } |
| 9814 | return k; |
| 9815 | } |
| 9816 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9817 | static PyObject * |
| 9818 | case_operation(PyObject *self, |
| 9819 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9820 | { |
| 9821 | PyObject *res = NULL; |
| 9822 | Py_ssize_t length, newlength = 0; |
| 9823 | int kind, outkind; |
| 9824 | void *data, *outdata; |
| 9825 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9826 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9827 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9828 | |
| 9829 | kind = PyUnicode_KIND(self); |
| 9830 | data = PyUnicode_DATA(self); |
| 9831 | length = PyUnicode_GET_LENGTH(self); |
Antoine Pitrou | 4e33424 | 2014-10-15 23:14:53 +0200 | [diff] [blame] | 9832 | if ((size_t) length > PY_SSIZE_T_MAX / (3 * sizeof(Py_UCS4))) { |
Benjamin Peterson | e1bd38c | 2014-10-15 11:47:36 -0400 | [diff] [blame] | 9833 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 9834 | return NULL; |
| 9835 | } |
Benjamin Peterson | 1e211ff | 2014-10-15 12:17:21 -0400 | [diff] [blame] | 9836 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9837 | if (tmp == NULL) |
| 9838 | return PyErr_NoMemory(); |
| 9839 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9840 | res = PyUnicode_New(newlength, maxchar); |
| 9841 | if (res == NULL) |
| 9842 | goto leave; |
| 9843 | tmpend = tmp + newlength; |
| 9844 | outdata = PyUnicode_DATA(res); |
| 9845 | outkind = PyUnicode_KIND(res); |
| 9846 | switch (outkind) { |
| 9847 | case PyUnicode_1BYTE_KIND: |
| 9848 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9849 | break; |
| 9850 | case PyUnicode_2BYTE_KIND: |
| 9851 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9852 | break; |
| 9853 | case PyUnicode_4BYTE_KIND: |
| 9854 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9855 | break; |
| 9856 | default: |
| 9857 | assert(0); |
| 9858 | break; |
| 9859 | } |
| 9860 | leave: |
| 9861 | PyMem_FREE(tmp); |
| 9862 | return res; |
| 9863 | } |
| 9864 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9865 | PyObject * |
| 9866 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9867 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9868 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9869 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9870 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9871 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9872 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9873 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9874 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9875 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9876 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9877 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9878 | int use_memcpy; |
| 9879 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9880 | PyObject *last_obj; |
| 9881 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9882 | |
Benjamin Peterson | 9743b2c | 2014-02-15 13:02:52 -0500 | [diff] [blame] | 9883 | fseq = PySequence_Fast(seq, "can only join an iterable"); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9884 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9885 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9886 | } |
| 9887 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9888 | /* NOTE: the following code can't call back into Python code, |
| 9889 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9890 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9891 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9892 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9893 | /* If empty sequence, return u"". */ |
| 9894 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9895 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9896 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9897 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9898 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9899 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9900 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9901 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9902 | if (seqlen == 1) { |
| 9903 | if (PyUnicode_CheckExact(items[0])) { |
| 9904 | res = items[0]; |
| 9905 | Py_INCREF(res); |
| 9906 | Py_DECREF(fseq); |
| 9907 | return res; |
| 9908 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9909 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9910 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9911 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9912 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9913 | /* Set up sep and seplen */ |
| 9914 | if (separator == NULL) { |
| 9915 | /* fall back to a blank space separator */ |
| 9916 | sep = PyUnicode_FromOrdinal(' '); |
| 9917 | if (!sep) |
| 9918 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9919 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9920 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9921 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9922 | else { |
| 9923 | if (!PyUnicode_Check(separator)) { |
| 9924 | PyErr_Format(PyExc_TypeError, |
| 9925 | "separator: expected str instance," |
| 9926 | " %.80s found", |
| 9927 | Py_TYPE(separator)->tp_name); |
| 9928 | goto onError; |
| 9929 | } |
| 9930 | if (PyUnicode_READY(separator)) |
| 9931 | goto onError; |
| 9932 | sep = separator; |
| 9933 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9934 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9935 | /* inc refcount to keep this code path symmetric with the |
| 9936 | above case of a blank separator */ |
| 9937 | Py_INCREF(sep); |
| 9938 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9939 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9940 | } |
| 9941 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9942 | /* There are at least two things to join, or else we have a subclass |
| 9943 | * of str in the sequence. |
| 9944 | * Do a pre-pass to figure out the total amount of space we'll |
| 9945 | * need (sz), and see whether all argument are strings. |
| 9946 | */ |
| 9947 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9948 | #ifdef Py_DEBUG |
| 9949 | use_memcpy = 0; |
| 9950 | #else |
| 9951 | use_memcpy = 1; |
| 9952 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9953 | for (i = 0; i < seqlen; i++) { |
| 9954 | const Py_ssize_t old_sz = sz; |
| 9955 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9956 | if (!PyUnicode_Check(item)) { |
| 9957 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 9958 | "sequence item %zd: expected str instance," |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9959 | " %.80s found", |
| 9960 | i, Py_TYPE(item)->tp_name); |
| 9961 | goto onError; |
| 9962 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9963 | if (PyUnicode_READY(item) == -1) |
| 9964 | goto onError; |
| 9965 | sz += PyUnicode_GET_LENGTH(item); |
| 9966 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9967 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9968 | if (i != 0) |
| 9969 | sz += seplen; |
| 9970 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9971 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9972 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9973 | goto onError; |
| 9974 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9975 | if (use_memcpy && last_obj != NULL) { |
| 9976 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9977 | use_memcpy = 0; |
| 9978 | } |
| 9979 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9980 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9981 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9982 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9983 | if (res == NULL) |
| 9984 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9985 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9986 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9987 | #ifdef Py_DEBUG |
| 9988 | use_memcpy = 0; |
| 9989 | #else |
| 9990 | if (use_memcpy) { |
| 9991 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9992 | kind = PyUnicode_KIND(res); |
| 9993 | if (seplen != 0) |
| 9994 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9995 | } |
| 9996 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9997 | if (use_memcpy) { |
| 9998 | for (i = 0; i < seqlen; ++i) { |
| 9999 | Py_ssize_t itemlen; |
| 10000 | item = items[i]; |
| 10001 | |
| 10002 | /* Copy item, and maybe the separator. */ |
| 10003 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10004 | Py_MEMCPY(res_data, |
| 10005 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10006 | kind * seplen); |
| 10007 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10008 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 10009 | |
| 10010 | itemlen = PyUnicode_GET_LENGTH(item); |
| 10011 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10012 | Py_MEMCPY(res_data, |
| 10013 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10014 | kind * itemlen); |
| 10015 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10016 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 10017 | } |
| 10018 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 10019 | + kind * PyUnicode_GET_LENGTH(res)); |
| 10020 | } |
| 10021 | else { |
| 10022 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 10023 | Py_ssize_t itemlen; |
| 10024 | item = items[i]; |
| 10025 | |
| 10026 | /* Copy item, and maybe the separator. */ |
| 10027 | if (i && seplen != 0) { |
| 10028 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 10029 | res_offset += seplen; |
| 10030 | } |
| 10031 | |
| 10032 | itemlen = PyUnicode_GET_LENGTH(item); |
| 10033 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10034 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10035 | res_offset += itemlen; |
| 10036 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10037 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10038 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 10039 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10040 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10041 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10042 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10043 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10044 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10045 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10046 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10047 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10048 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10049 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10050 | return NULL; |
| 10051 | } |
| 10052 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10053 | #define FILL(kind, data, value, start, length) \ |
| 10054 | do { \ |
| 10055 | Py_ssize_t i_ = 0; \ |
| 10056 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 10057 | switch ((kind)) { \ |
| 10058 | case PyUnicode_1BYTE_KIND: { \ |
| 10059 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 10060 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10061 | break; \ |
| 10062 | } \ |
| 10063 | case PyUnicode_2BYTE_KIND: { \ |
| 10064 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 10065 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10066 | break; \ |
| 10067 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10068 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 10070 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10071 | break; \ |
| 10072 | } \ |
Serhiy Storchaka | 133b11b | 2014-12-01 18:56:28 +0200 | [diff] [blame] | 10073 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10074 | } \ |
| 10075 | } while (0) |
| 10076 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10077 | void |
| 10078 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10079 | Py_UCS4 fill_char) |
| 10080 | { |
| 10081 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 10082 | const void *data = PyUnicode_DATA(unicode); |
| 10083 | assert(PyUnicode_IS_READY(unicode)); |
| 10084 | assert(unicode_modifiable(unicode)); |
| 10085 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 10086 | assert(start >= 0); |
| 10087 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 10088 | FILL(kind, data, fill_char, start, length); |
| 10089 | } |
| 10090 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10091 | Py_ssize_t |
| 10092 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10093 | Py_UCS4 fill_char) |
| 10094 | { |
| 10095 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10096 | |
| 10097 | if (!PyUnicode_Check(unicode)) { |
| 10098 | PyErr_BadInternalCall(); |
| 10099 | return -1; |
| 10100 | } |
| 10101 | if (PyUnicode_READY(unicode) == -1) |
| 10102 | return -1; |
| 10103 | if (unicode_check_modifiable(unicode)) |
| 10104 | return -1; |
| 10105 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10106 | if (start < 0) { |
| 10107 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10108 | return -1; |
| 10109 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10110 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 10111 | PyErr_SetString(PyExc_ValueError, |
| 10112 | "fill character is bigger than " |
| 10113 | "the string maximum character"); |
| 10114 | return -1; |
| 10115 | } |
| 10116 | |
| 10117 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 10118 | length = Py_MIN(maxlen, length); |
| 10119 | if (length <= 0) |
| 10120 | return 0; |
| 10121 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10122 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10123 | return length; |
| 10124 | } |
| 10125 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10126 | static PyObject * |
| 10127 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10128 | Py_ssize_t left, |
| 10129 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10131 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 | PyObject *u; |
| 10133 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10134 | int kind; |
| 10135 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10136 | |
| 10137 | if (left < 0) |
| 10138 | left = 0; |
| 10139 | if (right < 0) |
| 10140 | right = 0; |
| 10141 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10142 | if (left == 0 && right == 0) |
| 10143 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10144 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10145 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10146 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10147 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10148 | return NULL; |
| 10149 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10151 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10152 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10153 | if (!u) |
| 10154 | return NULL; |
| 10155 | |
| 10156 | kind = PyUnicode_KIND(u); |
| 10157 | data = PyUnicode_DATA(u); |
| 10158 | if (left) |
| 10159 | FILL(kind, data, fill, 0, left); |
| 10160 | if (right) |
| 10161 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10162 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10163 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10164 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10165 | } |
| 10166 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10167 | PyObject * |
| 10168 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10169 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10170 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10171 | |
| 10172 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10173 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10174 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10175 | if (PyUnicode_READY(string) == -1) { |
| 10176 | Py_DECREF(string); |
| 10177 | return NULL; |
| 10178 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10179 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10180 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10181 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10182 | if (PyUnicode_IS_ASCII(string)) |
| 10183 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10184 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10185 | PyUnicode_GET_LENGTH(string), keepends); |
| 10186 | else |
| 10187 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10188 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10189 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10190 | break; |
| 10191 | case PyUnicode_2BYTE_KIND: |
| 10192 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10193 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10194 | PyUnicode_GET_LENGTH(string), keepends); |
| 10195 | break; |
| 10196 | case PyUnicode_4BYTE_KIND: |
| 10197 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10198 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10199 | PyUnicode_GET_LENGTH(string), keepends); |
| 10200 | break; |
| 10201 | default: |
| 10202 | assert(0); |
| 10203 | list = 0; |
| 10204 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10205 | Py_DECREF(string); |
| 10206 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10207 | } |
| 10208 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10209 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10210 | split(PyObject *self, |
| 10211 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10212 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10213 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10214 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | void *buf1, *buf2; |
| 10216 | Py_ssize_t len1, len2; |
| 10217 | PyObject* out; |
| 10218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10219 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10220 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10221 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10222 | if (PyUnicode_READY(self) == -1) |
| 10223 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10224 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10225 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10226 | switch (PyUnicode_KIND(self)) { |
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)) |
| 10229 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10230 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10231 | PyUnicode_GET_LENGTH(self), maxcount |
| 10232 | ); |
| 10233 | else |
| 10234 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10235 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10236 | PyUnicode_GET_LENGTH(self), maxcount |
| 10237 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 | case PyUnicode_2BYTE_KIND: |
| 10239 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10240 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10241 | PyUnicode_GET_LENGTH(self), maxcount |
| 10242 | ); |
| 10243 | case PyUnicode_4BYTE_KIND: |
| 10244 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10245 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | PyUnicode_GET_LENGTH(self), maxcount |
| 10247 | ); |
| 10248 | default: |
| 10249 | assert(0); |
| 10250 | return NULL; |
| 10251 | } |
| 10252 | |
| 10253 | if (PyUnicode_READY(substring) == -1) |
| 10254 | return NULL; |
| 10255 | |
| 10256 | kind1 = PyUnicode_KIND(self); |
| 10257 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10258 | len1 = PyUnicode_GET_LENGTH(self); |
| 10259 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10260 | if (kind1 < kind2 || len1 < len2) { |
| 10261 | out = PyList_New(1); |
| 10262 | if (out == NULL) |
| 10263 | return NULL; |
| 10264 | Py_INCREF(self); |
| 10265 | PyList_SET_ITEM(out, 0, self); |
| 10266 | return out; |
| 10267 | } |
| 10268 | buf1 = PyUnicode_DATA(self); |
| 10269 | buf2 = PyUnicode_DATA(substring); |
| 10270 | if (kind2 != kind1) { |
| 10271 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10272 | if (!buf2) |
| 10273 | return NULL; |
| 10274 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10275 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10276 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10277 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10278 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10279 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10280 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10281 | else |
| 10282 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10283 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10284 | break; |
| 10285 | case PyUnicode_2BYTE_KIND: |
| 10286 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10287 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 | break; |
| 10289 | case PyUnicode_4BYTE_KIND: |
| 10290 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10291 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10292 | break; |
| 10293 | default: |
| 10294 | out = NULL; |
| 10295 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10296 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10297 | PyMem_Free(buf2); |
| 10298 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10299 | } |
| 10300 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10301 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10302 | rsplit(PyObject *self, |
| 10303 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10304 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10305 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10306 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10307 | void *buf1, *buf2; |
| 10308 | Py_ssize_t len1, len2; |
| 10309 | PyObject* out; |
| 10310 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10311 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10312 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10313 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10314 | if (PyUnicode_READY(self) == -1) |
| 10315 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10316 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10317 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10318 | switch (PyUnicode_KIND(self)) { |
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)) |
| 10321 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10322 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10323 | PyUnicode_GET_LENGTH(self), maxcount |
| 10324 | ); |
| 10325 | else |
| 10326 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10327 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10328 | PyUnicode_GET_LENGTH(self), maxcount |
| 10329 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10330 | case PyUnicode_2BYTE_KIND: |
| 10331 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10332 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10333 | PyUnicode_GET_LENGTH(self), maxcount |
| 10334 | ); |
| 10335 | case PyUnicode_4BYTE_KIND: |
| 10336 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10337 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10338 | PyUnicode_GET_LENGTH(self), maxcount |
| 10339 | ); |
| 10340 | default: |
| 10341 | assert(0); |
| 10342 | return NULL; |
| 10343 | } |
| 10344 | |
| 10345 | if (PyUnicode_READY(substring) == -1) |
| 10346 | return NULL; |
| 10347 | |
| 10348 | kind1 = PyUnicode_KIND(self); |
| 10349 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10350 | len1 = PyUnicode_GET_LENGTH(self); |
| 10351 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10352 | if (kind1 < kind2 || len1 < len2) { |
| 10353 | out = PyList_New(1); |
| 10354 | if (out == NULL) |
| 10355 | return NULL; |
| 10356 | Py_INCREF(self); |
| 10357 | PyList_SET_ITEM(out, 0, self); |
| 10358 | return out; |
| 10359 | } |
| 10360 | buf1 = PyUnicode_DATA(self); |
| 10361 | buf2 = PyUnicode_DATA(substring); |
| 10362 | if (kind2 != kind1) { |
| 10363 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10364 | if (!buf2) |
| 10365 | return NULL; |
| 10366 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10367 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10368 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10369 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10370 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10371 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10372 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10373 | else |
| 10374 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10375 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10376 | break; |
| 10377 | case PyUnicode_2BYTE_KIND: |
| 10378 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10379 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10380 | break; |
| 10381 | case PyUnicode_4BYTE_KIND: |
| 10382 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10383 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10384 | break; |
| 10385 | default: |
| 10386 | out = NULL; |
| 10387 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10388 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10389 | PyMem_Free(buf2); |
| 10390 | return out; |
| 10391 | } |
| 10392 | |
| 10393 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10394 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10395 | 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] | 10396 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10397 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10398 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10399 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10400 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10401 | else |
| 10402 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10403 | case PyUnicode_2BYTE_KIND: |
| 10404 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10405 | case PyUnicode_4BYTE_KIND: |
| 10406 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10407 | } |
| 10408 | assert(0); |
| 10409 | return -1; |
| 10410 | } |
| 10411 | |
| 10412 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10413 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10414 | 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] | 10415 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10416 | switch (kind) { |
| 10417 | case PyUnicode_1BYTE_KIND: |
| 10418 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10419 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10420 | else |
| 10421 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10422 | case PyUnicode_2BYTE_KIND: |
| 10423 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10424 | case PyUnicode_4BYTE_KIND: |
| 10425 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10426 | } |
| 10427 | assert(0); |
| 10428 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10429 | } |
| 10430 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10431 | static void |
| 10432 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10433 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10434 | { |
| 10435 | int kind = PyUnicode_KIND(u); |
| 10436 | void *data = PyUnicode_DATA(u); |
| 10437 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10438 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10439 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10440 | (Py_UCS1 *)data + len, |
| 10441 | u1, u2, maxcount); |
| 10442 | } |
| 10443 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10444 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10445 | (Py_UCS2 *)data + len, |
| 10446 | u1, u2, maxcount); |
| 10447 | } |
| 10448 | else { |
| 10449 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10450 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10451 | (Py_UCS4 *)data + len, |
| 10452 | u1, u2, maxcount); |
| 10453 | } |
| 10454 | } |
| 10455 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10456 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | replace(PyObject *self, PyObject *str1, |
| 10458 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10459 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10460 | PyObject *u; |
| 10461 | char *sbuf = PyUnicode_DATA(self); |
| 10462 | char *buf1 = PyUnicode_DATA(str1); |
| 10463 | char *buf2 = PyUnicode_DATA(str2); |
| 10464 | int srelease = 0, release1 = 0, release2 = 0; |
| 10465 | int skind = PyUnicode_KIND(self); |
| 10466 | int kind1 = PyUnicode_KIND(str1); |
| 10467 | int kind2 = PyUnicode_KIND(str2); |
| 10468 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10469 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10470 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10471 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10472 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10473 | |
| 10474 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10475 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10476 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10477 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10478 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10479 | if (str1 == str2) |
| 10480 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10481 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10482 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10483 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10484 | if (maxchar < maxchar_str1) |
| 10485 | /* substring too wide to be present */ |
| 10486 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10487 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10488 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10489 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10490 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10491 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10492 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10493 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10494 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10495 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10496 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10497 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10498 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10499 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10500 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10501 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10502 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10503 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10504 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10505 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10506 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10507 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10508 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10509 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10510 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10511 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10512 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10513 | } |
| 10514 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10515 | int rkind = skind; |
| 10516 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10517 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10518 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10519 | if (kind1 < rkind) { |
| 10520 | /* widen substring */ |
| 10521 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10522 | if (!buf1) goto error; |
| 10523 | release1 = 1; |
| 10524 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10525 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10526 | if (i < 0) |
| 10527 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10528 | if (rkind > kind2) { |
| 10529 | /* widen replacement */ |
| 10530 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10531 | if (!buf2) goto error; |
| 10532 | release2 = 1; |
| 10533 | } |
| 10534 | else if (rkind < kind2) { |
| 10535 | /* widen self and buf1 */ |
| 10536 | rkind = kind2; |
| 10537 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10538 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10539 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10540 | if (!sbuf) goto error; |
| 10541 | srelease = 1; |
| 10542 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10543 | if (!buf1) goto error; |
| 10544 | release1 = 1; |
| 10545 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10546 | u = PyUnicode_New(slen, maxchar); |
| 10547 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10548 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10549 | assert(PyUnicode_KIND(u) == rkind); |
| 10550 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10551 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10552 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10553 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10554 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10555 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10556 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10557 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10558 | |
| 10559 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10560 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10561 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10562 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10563 | if (i == -1) |
| 10564 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10565 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10567 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10568 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10569 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10570 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10571 | } |
| 10572 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10573 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10574 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10575 | int rkind = skind; |
| 10576 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10577 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10578 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10579 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10580 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10581 | if (!buf1) goto error; |
| 10582 | release1 = 1; |
| 10583 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10584 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10585 | if (n == 0) |
| 10586 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10587 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10588 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10589 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10590 | if (!buf2) goto error; |
| 10591 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10592 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10593 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10594 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10595 | rkind = kind2; |
| 10596 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10597 | if (!sbuf) goto error; |
| 10598 | srelease = 1; |
| 10599 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10600 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10601 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10602 | if (!buf1) goto error; |
| 10603 | release1 = 1; |
| 10604 | } |
| 10605 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10606 | PyUnicode_GET_LENGTH(str1))); */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10607 | 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] | 10608 | PyErr_SetString(PyExc_OverflowError, |
| 10609 | "replace string is too long"); |
| 10610 | goto error; |
| 10611 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10612 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10613 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10614 | _Py_INCREF_UNICODE_EMPTY(); |
| 10615 | if (!unicode_empty) |
| 10616 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10617 | u = unicode_empty; |
| 10618 | goto done; |
| 10619 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10620 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10621 | PyErr_SetString(PyExc_OverflowError, |
| 10622 | "replace string is too long"); |
| 10623 | goto error; |
| 10624 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10625 | u = PyUnicode_New(new_size, maxchar); |
| 10626 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10627 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10628 | assert(PyUnicode_KIND(u) == rkind); |
| 10629 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10630 | ires = i = 0; |
| 10631 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10632 | while (n-- > 0) { |
| 10633 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10634 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10635 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10636 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10637 | if (j == -1) |
| 10638 | break; |
| 10639 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10640 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10641 | memcpy(res + rkind * ires, |
| 10642 | sbuf + rkind * i, |
| 10643 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10644 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10645 | } |
| 10646 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10647 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10648 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10649 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10650 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10651 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10652 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10653 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10654 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10655 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10656 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10657 | memcpy(res + rkind * ires, |
| 10658 | sbuf + rkind * i, |
| 10659 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10660 | } |
| 10661 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10662 | /* interleave */ |
| 10663 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10664 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10665 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10666 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10667 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10668 | if (--n <= 0) |
| 10669 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10670 | memcpy(res + rkind * ires, |
| 10671 | sbuf + rkind * i, |
| 10672 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10673 | ires++; |
| 10674 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10675 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10676 | memcpy(res + rkind * ires, |
| 10677 | sbuf + rkind * i, |
| 10678 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10679 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10680 | } |
| 10681 | |
| 10682 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10683 | unicode_adjust_maxchar(&u); |
| 10684 | if (u == NULL) |
| 10685 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10686 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10687 | |
| 10688 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10689 | if (srelease) |
| 10690 | PyMem_FREE(sbuf); |
| 10691 | if (release1) |
| 10692 | PyMem_FREE(buf1); |
| 10693 | if (release2) |
| 10694 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10695 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10696 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10697 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10698 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10699 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10700 | if (srelease) |
| 10701 | PyMem_FREE(sbuf); |
| 10702 | if (release1) |
| 10703 | PyMem_FREE(buf1); |
| 10704 | if (release2) |
| 10705 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10706 | return unicode_result_unchanged(self); |
| 10707 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10708 | error: |
| 10709 | if (srelease && sbuf) |
| 10710 | PyMem_FREE(sbuf); |
| 10711 | if (release1 && buf1) |
| 10712 | PyMem_FREE(buf1); |
| 10713 | if (release2 && buf2) |
| 10714 | PyMem_FREE(buf2); |
| 10715 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10716 | } |
| 10717 | |
| 10718 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10719 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10720 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10721 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10722 | \n\ |
| 10723 | 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] | 10724 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10725 | |
| 10726 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10727 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10728 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10729 | if (PyUnicode_READY(self) == -1) |
| 10730 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10731 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10732 | } |
| 10733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10734 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10735 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10736 | \n\ |
| 10737 | 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] | 10738 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10739 | |
| 10740 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10741 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10742 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10743 | if (PyUnicode_READY(self) == -1) |
| 10744 | return NULL; |
| 10745 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10746 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10747 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10748 | } |
| 10749 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10750 | PyDoc_STRVAR(casefold__doc__, |
| 10751 | "S.casefold() -> str\n\ |
| 10752 | \n\ |
| 10753 | Return a version of S suitable for caseless comparisons."); |
| 10754 | |
| 10755 | static PyObject * |
| 10756 | unicode_casefold(PyObject *self) |
| 10757 | { |
| 10758 | if (PyUnicode_READY(self) == -1) |
| 10759 | return NULL; |
| 10760 | if (PyUnicode_IS_ASCII(self)) |
| 10761 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10762 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10763 | } |
| 10764 | |
| 10765 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10766 | /* Argument converter. Coerces to a single unicode character */ |
| 10767 | |
| 10768 | static int |
| 10769 | convert_uc(PyObject *obj, void *addr) |
| 10770 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10771 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10772 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10773 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10774 | uniobj = PyUnicode_FromObject(obj); |
| 10775 | if (uniobj == NULL) { |
| 10776 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10777 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10778 | return 0; |
| 10779 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10780 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10781 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10782 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10783 | Py_DECREF(uniobj); |
| 10784 | return 0; |
| 10785 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10786 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10787 | Py_DECREF(uniobj); |
| 10788 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10789 | } |
| 10790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10791 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10792 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10793 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10794 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10795 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10796 | |
| 10797 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10798 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10799 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10800 | Py_ssize_t marg, left; |
| 10801 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10802 | Py_UCS4 fillchar = ' '; |
| 10803 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10804 | 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] | 10805 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10806 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10807 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10808 | return NULL; |
| 10809 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10810 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10811 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10812 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10813 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10814 | left = marg / 2 + (marg & width & 1); |
| 10815 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10816 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10817 | } |
| 10818 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10819 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10820 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10821 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10822 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10823 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10824 | #define COMPARE(TYPE1, TYPE2) \ |
| 10825 | do { \ |
| 10826 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10827 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10828 | TYPE1* end = p1 + len; \ |
| 10829 | Py_UCS4 c1, c2; \ |
| 10830 | for (; p1 != end; p1++, p2++) { \ |
| 10831 | c1 = *p1; \ |
| 10832 | c2 = *p2; \ |
| 10833 | if (c1 != c2) \ |
| 10834 | return (c1 < c2) ? -1 : 1; \ |
| 10835 | } \ |
| 10836 | } \ |
| 10837 | while (0) |
| 10838 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10839 | int kind1, kind2; |
| 10840 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10841 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10842 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10843 | kind1 = PyUnicode_KIND(str1); |
| 10844 | kind2 = PyUnicode_KIND(str2); |
| 10845 | data1 = PyUnicode_DATA(str1); |
| 10846 | data2 = PyUnicode_DATA(str2); |
| 10847 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10848 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10849 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10850 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10851 | switch(kind1) { |
| 10852 | case PyUnicode_1BYTE_KIND: |
| 10853 | { |
| 10854 | switch(kind2) { |
| 10855 | case PyUnicode_1BYTE_KIND: |
| 10856 | { |
| 10857 | int cmp = memcmp(data1, data2, len); |
| 10858 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10859 | if (cmp < 0) |
| 10860 | return -1; |
| 10861 | if (cmp > 0) |
| 10862 | return 1; |
| 10863 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10864 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10865 | case PyUnicode_2BYTE_KIND: |
| 10866 | COMPARE(Py_UCS1, Py_UCS2); |
| 10867 | break; |
| 10868 | case PyUnicode_4BYTE_KIND: |
| 10869 | COMPARE(Py_UCS1, Py_UCS4); |
| 10870 | break; |
| 10871 | default: |
| 10872 | assert(0); |
| 10873 | } |
| 10874 | break; |
| 10875 | } |
| 10876 | case PyUnicode_2BYTE_KIND: |
| 10877 | { |
| 10878 | switch(kind2) { |
| 10879 | case PyUnicode_1BYTE_KIND: |
| 10880 | COMPARE(Py_UCS2, Py_UCS1); |
| 10881 | break; |
| 10882 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10883 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10884 | COMPARE(Py_UCS2, Py_UCS2); |
| 10885 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10886 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10887 | case PyUnicode_4BYTE_KIND: |
| 10888 | COMPARE(Py_UCS2, Py_UCS4); |
| 10889 | break; |
| 10890 | default: |
| 10891 | assert(0); |
| 10892 | } |
| 10893 | break; |
| 10894 | } |
| 10895 | case PyUnicode_4BYTE_KIND: |
| 10896 | { |
| 10897 | switch(kind2) { |
| 10898 | case PyUnicode_1BYTE_KIND: |
| 10899 | COMPARE(Py_UCS4, Py_UCS1); |
| 10900 | break; |
| 10901 | case PyUnicode_2BYTE_KIND: |
| 10902 | COMPARE(Py_UCS4, Py_UCS2); |
| 10903 | break; |
| 10904 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10905 | { |
| 10906 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10907 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10908 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10909 | if (cmp < 0) |
| 10910 | return -1; |
| 10911 | if (cmp > 0) |
| 10912 | return 1; |
| 10913 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10914 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10915 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10916 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10917 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10918 | default: |
| 10919 | assert(0); |
| 10920 | } |
| 10921 | break; |
| 10922 | } |
| 10923 | default: |
| 10924 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10925 | } |
| 10926 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10927 | if (len1 == len2) |
| 10928 | return 0; |
| 10929 | if (len1 < len2) |
| 10930 | return -1; |
| 10931 | else |
| 10932 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10933 | |
| 10934 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10935 | } |
| 10936 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10937 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10938 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10939 | { |
| 10940 | int kind; |
| 10941 | void *data1, *data2; |
| 10942 | Py_ssize_t len; |
| 10943 | int cmp; |
| 10944 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10945 | len = PyUnicode_GET_LENGTH(str1); |
| 10946 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10947 | return 0; |
| 10948 | kind = PyUnicode_KIND(str1); |
| 10949 | if (PyUnicode_KIND(str2) != kind) |
| 10950 | return 0; |
| 10951 | data1 = PyUnicode_DATA(str1); |
| 10952 | data2 = PyUnicode_DATA(str2); |
| 10953 | |
| 10954 | cmp = memcmp(data1, data2, len * kind); |
| 10955 | return (cmp == 0); |
| 10956 | } |
| 10957 | |
| 10958 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10959 | int |
| 10960 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10961 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10962 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10963 | if (PyUnicode_READY(left) == -1 || |
| 10964 | PyUnicode_READY(right) == -1) |
| 10965 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10966 | |
| 10967 | /* a string is equal to itself */ |
| 10968 | if (left == right) |
| 10969 | return 0; |
| 10970 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10971 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10972 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10973 | PyErr_Format(PyExc_TypeError, |
| 10974 | "Can't compare %.100s and %.100s", |
| 10975 | left->ob_type->tp_name, |
| 10976 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10977 | return -1; |
| 10978 | } |
| 10979 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10980 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10981 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10982 | { |
| 10983 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10984 | if (right_str == NULL) |
| 10985 | return -1; |
| 10986 | return PyUnicode_Compare(left, right_str); |
| 10987 | } |
| 10988 | |
| 10989 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10990 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10991 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10992 | Py_ssize_t i; |
| 10993 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10994 | Py_UCS4 chr; |
| 10995 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10996 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10997 | if (PyUnicode_READY(uni) == -1) |
| 10998 | return -1; |
| 10999 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 11000 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 11001 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 11002 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 11003 | size_t len, len2 = strlen(str); |
| 11004 | int cmp; |
| 11005 | |
| 11006 | len = Py_MIN(len1, len2); |
| 11007 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 11008 | if (cmp != 0) { |
| 11009 | if (cmp < 0) |
| 11010 | return -1; |
| 11011 | else |
| 11012 | return 1; |
| 11013 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 11014 | if (len1 > len2) |
| 11015 | return 1; /* uni is longer */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11016 | if (len1 < len2) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 11017 | return -1; /* str is longer */ |
| 11018 | return 0; |
| 11019 | } |
| 11020 | else { |
| 11021 | void *data = PyUnicode_DATA(uni); |
| 11022 | /* Compare Unicode string and source character set string */ |
| 11023 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 11024 | if (chr != (unsigned char)str[i]) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 11025 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 11026 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 11027 | to C strings identical up to that point. */ |
| 11028 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 11029 | return 1; /* uni is longer */ |
| 11030 | if (str[i]) |
| 11031 | return -1; /* str is longer */ |
| 11032 | return 0; |
| 11033 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 11034 | } |
| 11035 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11036 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11037 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11038 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11039 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11040 | PyObject * |
| 11041 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11042 | { |
| 11043 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11044 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11045 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11046 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 11047 | Py_RETURN_NOTIMPLEMENTED; |
| 11048 | |
| 11049 | if (PyUnicode_READY(left) == -1 || |
| 11050 | PyUnicode_READY(right) == -1) |
| 11051 | return NULL; |
| 11052 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 11053 | if (left == right) { |
| 11054 | switch (op) { |
| 11055 | case Py_EQ: |
| 11056 | case Py_LE: |
| 11057 | case Py_GE: |
| 11058 | /* a string is equal to itself */ |
| 11059 | v = Py_True; |
| 11060 | break; |
| 11061 | case Py_NE: |
| 11062 | case Py_LT: |
| 11063 | case Py_GT: |
| 11064 | v = Py_False; |
| 11065 | break; |
| 11066 | default: |
| 11067 | PyErr_BadArgument(); |
| 11068 | return NULL; |
| 11069 | } |
| 11070 | } |
| 11071 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11072 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 11073 | result ^= (op == Py_NE); |
| 11074 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11075 | } |
| 11076 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 11077 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11078 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11079 | /* Convert the return value to a Boolean */ |
| 11080 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11081 | case Py_LE: |
| 11082 | v = TEST_COND(result <= 0); |
| 11083 | break; |
| 11084 | case Py_GE: |
| 11085 | v = TEST_COND(result >= 0); |
| 11086 | break; |
| 11087 | case Py_LT: |
| 11088 | v = TEST_COND(result == -1); |
| 11089 | break; |
| 11090 | case Py_GT: |
| 11091 | v = TEST_COND(result == 1); |
| 11092 | break; |
| 11093 | default: |
| 11094 | PyErr_BadArgument(); |
| 11095 | return NULL; |
| 11096 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11097 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11098 | Py_INCREF(v); |
| 11099 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11100 | } |
| 11101 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11102 | int |
Raymond Hettinger | ac2ef65 | 2015-07-04 16:04:44 -0700 | [diff] [blame] | 11103 | _PyUnicode_EQ(PyObject *aa, PyObject *bb) |
| 11104 | { |
| 11105 | return unicode_eq(aa, bb); |
| 11106 | } |
| 11107 | |
| 11108 | int |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11109 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11110 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11111 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11112 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11113 | void *buf1, *buf2; |
| 11114 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11115 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11116 | |
| 11117 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11118 | sub = PyUnicode_FromObject(element); |
| 11119 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11120 | PyErr_Format(PyExc_TypeError, |
| 11121 | "'in <string>' requires string as left operand, not %s", |
| 11122 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11123 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11124 | } |
| 11125 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11126 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11127 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11128 | Py_DECREF(sub); |
| 11129 | return -1; |
| 11130 | } |
| 11131 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11132 | kind1 = PyUnicode_KIND(str); |
| 11133 | kind2 = PyUnicode_KIND(sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11134 | if (kind1 < kind2) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11135 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 11136 | Py_DECREF(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11137 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11138 | } |
| 11139 | len1 = PyUnicode_GET_LENGTH(str); |
| 11140 | len2 = PyUnicode_GET_LENGTH(sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11141 | if (len1 < len2) { |
| 11142 | Py_DECREF(sub); |
| 11143 | Py_DECREF(str); |
| 11144 | return 0; |
| 11145 | } |
| 11146 | buf1 = PyUnicode_DATA(str); |
| 11147 | buf2 = PyUnicode_DATA(sub); |
| 11148 | if (len2 == 1) { |
| 11149 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 11150 | result = findchar((const char *)buf1, kind1, len1, ch, 1) != -1; |
| 11151 | Py_DECREF(sub); |
| 11152 | Py_DECREF(str); |
| 11153 | return result; |
| 11154 | } |
| 11155 | if (kind2 != kind1) { |
| 11156 | buf2 = _PyUnicode_AsKind(sub, kind1); |
| 11157 | if (!buf2) { |
| 11158 | Py_DECREF(sub); |
| 11159 | Py_DECREF(str); |
| 11160 | return -1; |
| 11161 | } |
| 11162 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11163 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11164 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11165 | case PyUnicode_1BYTE_KIND: |
| 11166 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11167 | break; |
| 11168 | case PyUnicode_2BYTE_KIND: |
| 11169 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11170 | break; |
| 11171 | case PyUnicode_4BYTE_KIND: |
| 11172 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11173 | break; |
| 11174 | default: |
| 11175 | result = -1; |
| 11176 | assert(0); |
| 11177 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11178 | |
| 11179 | Py_DECREF(str); |
| 11180 | Py_DECREF(sub); |
| 11181 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11182 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11183 | PyMem_Free(buf2); |
| 11184 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11185 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11186 | } |
| 11187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11188 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 11189 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11190 | PyObject * |
| 11191 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11192 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11193 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11194 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11195 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11196 | |
| 11197 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11198 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11200 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11201 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11202 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11203 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11204 | |
| 11205 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11206 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11207 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11208 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11209 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11210 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11211 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11212 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11213 | } |
| 11214 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11215 | u_len = PyUnicode_GET_LENGTH(u); |
| 11216 | v_len = PyUnicode_GET_LENGTH(v); |
| 11217 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11218 | PyErr_SetString(PyExc_OverflowError, |
| 11219 | "strings are too large to concat"); |
| 11220 | goto onError; |
| 11221 | } |
| 11222 | new_len = u_len + v_len; |
| 11223 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11224 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11225 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11226 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11227 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11228 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11229 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11230 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11231 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11232 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 11233 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11234 | Py_DECREF(u); |
| 11235 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11236 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11237 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11239 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | Py_XDECREF(u); |
| 11241 | Py_XDECREF(v); |
| 11242 | return NULL; |
| 11243 | } |
| 11244 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11245 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11246 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11247 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11248 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11249 | Py_UCS4 maxchar, maxchar2; |
| 11250 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11251 | |
| 11252 | if (p_left == NULL) { |
| 11253 | if (!PyErr_Occurred()) |
| 11254 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11255 | return; |
| 11256 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11257 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11258 | if (right == NULL || left == NULL |
| 11259 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11260 | if (!PyErr_Occurred()) |
| 11261 | PyErr_BadInternalCall(); |
| 11262 | goto error; |
| 11263 | } |
| 11264 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11265 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11266 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11267 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11268 | goto error; |
| 11269 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11270 | /* Shortcuts */ |
| 11271 | if (left == unicode_empty) { |
| 11272 | Py_DECREF(left); |
| 11273 | Py_INCREF(right); |
| 11274 | *p_left = right; |
| 11275 | return; |
| 11276 | } |
| 11277 | if (right == unicode_empty) |
| 11278 | return; |
| 11279 | |
| 11280 | left_len = PyUnicode_GET_LENGTH(left); |
| 11281 | right_len = PyUnicode_GET_LENGTH(right); |
| 11282 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11283 | PyErr_SetString(PyExc_OverflowError, |
| 11284 | "strings are too large to concat"); |
| 11285 | goto error; |
| 11286 | } |
| 11287 | new_len = left_len + right_len; |
| 11288 | |
| 11289 | if (unicode_modifiable(left) |
| 11290 | && PyUnicode_CheckExact(right) |
| 11291 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11292 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11293 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11294 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11295 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11296 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11297 | { |
| 11298 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11299 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11300 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11301 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11302 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11303 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11304 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11305 | else { |
| 11306 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11307 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11308 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11309 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11310 | /* Concat the two Unicode strings */ |
| 11311 | res = PyUnicode_New(new_len, maxchar); |
| 11312 | if (res == NULL) |
| 11313 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11314 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11315 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11316 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11317 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11318 | } |
| 11319 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11320 | return; |
| 11321 | |
| 11322 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11323 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11324 | } |
| 11325 | |
| 11326 | void |
| 11327 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11328 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11329 | PyUnicode_Append(pleft, right); |
| 11330 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11331 | } |
| 11332 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11333 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11334 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11335 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11336 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11337 | 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] | 11338 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | |
| 11340 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11341 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11342 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11343 | PyObject *substring = NULL; /* initialize to fix a compiler warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11344 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11345 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11346 | PyObject *result; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11347 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11348 | void *buf1, *buf2; |
| 11349 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11351 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11352 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11353 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11354 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11355 | kind1 = PyUnicode_KIND(self); |
| 11356 | kind2 = PyUnicode_KIND(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11357 | if (kind1 < kind2) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11358 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11359 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11360 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11361 | len1 = PyUnicode_GET_LENGTH(self); |
| 11362 | len2 = PyUnicode_GET_LENGTH(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11363 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11364 | if (end - start < len2) { |
| 11365 | Py_DECREF(substring); |
| 11366 | return PyLong_FromLong(0); |
| 11367 | } |
| 11368 | buf1 = PyUnicode_DATA(self); |
| 11369 | buf2 = PyUnicode_DATA(substring); |
| 11370 | if (kind2 != kind1) { |
| 11371 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 11372 | if (!buf2) { |
| 11373 | Py_DECREF(substring); |
| 11374 | return NULL; |
| 11375 | } |
| 11376 | } |
| 11377 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11378 | case PyUnicode_1BYTE_KIND: |
| 11379 | iresult = ucs1lib_count( |
| 11380 | ((Py_UCS1*)buf1) + start, end - start, |
| 11381 | buf2, len2, PY_SSIZE_T_MAX |
| 11382 | ); |
| 11383 | break; |
| 11384 | case PyUnicode_2BYTE_KIND: |
| 11385 | iresult = ucs2lib_count( |
| 11386 | ((Py_UCS2*)buf1) + start, end - start, |
| 11387 | buf2, len2, PY_SSIZE_T_MAX |
| 11388 | ); |
| 11389 | break; |
| 11390 | case PyUnicode_4BYTE_KIND: |
| 11391 | iresult = ucs4lib_count( |
| 11392 | ((Py_UCS4*)buf1) + start, end - start, |
| 11393 | buf2, len2, PY_SSIZE_T_MAX |
| 11394 | ); |
| 11395 | break; |
| 11396 | default: |
| 11397 | assert(0); iresult = 0; |
| 11398 | } |
| 11399 | |
| 11400 | result = PyLong_FromSsize_t(iresult); |
| 11401 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11402 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11403 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11404 | |
| 11405 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11407 | return result; |
| 11408 | } |
| 11409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11410 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11411 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11412 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11413 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11414 | 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] | 11415 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11416 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11417 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11418 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11419 | |
| 11420 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11421 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11422 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11423 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11424 | char *encoding = NULL; |
| 11425 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11426 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11427 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11428 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11430 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11431 | } |
| 11432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11433 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11434 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | \n\ |
| 11436 | 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] | 11437 | 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] | 11438 | |
| 11439 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11440 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11441 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11442 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11443 | Py_UCS4 ch; |
| 11444 | PyObject *u; |
| 11445 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11446 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11447 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11448 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11449 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11450 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11451 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11452 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11453 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11455 | if (PyUnicode_READY(self) == -1) |
| 11456 | return NULL; |
| 11457 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11458 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11459 | src_len = PyUnicode_GET_LENGTH(self); |
| 11460 | i = j = line_pos = 0; |
| 11461 | kind = PyUnicode_KIND(self); |
| 11462 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11463 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11464 | for (; i < src_len; i++) { |
| 11465 | ch = PyUnicode_READ(kind, src_data, i); |
| 11466 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11467 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11468 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11469 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11471 | goto overflow; |
| 11472 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11473 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11474 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11475 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11477 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11478 | goto overflow; |
| 11479 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11481 | if (ch == '\n' || ch == '\r') |
| 11482 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11484 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11485 | if (!found) |
| 11486 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11488 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11489 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11490 | if (!u) |
| 11491 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11492 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11494 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11496 | for (; i < src_len; i++) { |
| 11497 | ch = PyUnicode_READ(kind, src_data, i); |
| 11498 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11499 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11500 | incr = tabsize - (line_pos % tabsize); |
| 11501 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11502 | FILL(kind, dest_data, ' ', j, incr); |
| 11503 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11504 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11505 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11506 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11507 | line_pos++; |
| 11508 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11509 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11510 | if (ch == '\n' || ch == '\r') |
| 11511 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11513 | } |
| 11514 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11515 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11516 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11517 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11518 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11519 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | } |
| 11521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11522 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11523 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | \n\ |
| 11525 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11526 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | arguments start and end are interpreted as in slice notation.\n\ |
| 11528 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11529 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11530 | |
| 11531 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11532 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11534 | /* initialize variables to prevent gcc warning */ |
| 11535 | PyObject *substring = NULL; |
| 11536 | Py_ssize_t start = 0; |
| 11537 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11538 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11540 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11541 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11542 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11543 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11544 | if (PyUnicode_READY(self) == -1) { |
| 11545 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11546 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11547 | } |
| 11548 | if (PyUnicode_READY(substring) == -1) { |
| 11549 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11550 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11551 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11552 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11553 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11554 | |
| 11555 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11556 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11557 | if (result == -2) |
| 11558 | return NULL; |
| 11559 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11560 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11561 | } |
| 11562 | |
| 11563 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11564 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11566 | void *data; |
| 11567 | enum PyUnicode_Kind kind; |
| 11568 | Py_UCS4 ch; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11569 | |
| 11570 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11571 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11572 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11573 | } |
| 11574 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11575 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11576 | return NULL; |
| 11577 | } |
| 11578 | kind = PyUnicode_KIND(self); |
| 11579 | data = PyUnicode_DATA(self); |
| 11580 | ch = PyUnicode_READ(kind, data, index); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 11581 | return unicode_char(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11582 | } |
| 11583 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11584 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11585 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11586 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11587 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11588 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11589 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11590 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11591 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11592 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11593 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11594 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11595 | if (_PyUnicode_HASH(self) != -1) |
| 11596 | return _PyUnicode_HASH(self); |
| 11597 | if (PyUnicode_READY(self) == -1) |
| 11598 | return -1; |
| 11599 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11600 | /* |
| 11601 | We make the hash of the empty string be 0, rather than using |
| 11602 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11603 | */ |
| 11604 | if (len == 0) { |
| 11605 | _PyUnicode_HASH(self) = 0; |
| 11606 | return 0; |
| 11607 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11608 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11609 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11610 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11611 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11612 | } |
| 11613 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11614 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11615 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11616 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11617 | 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] | 11618 | |
| 11619 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11620 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11621 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11622 | /* initialize variables to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11623 | Py_ssize_t result; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11624 | PyObject *substring = NULL; |
| 11625 | Py_ssize_t start = 0; |
| 11626 | Py_ssize_t end = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11627 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11628 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11629 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11630 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11631 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11632 | if (PyUnicode_READY(self) == -1) { |
| 11633 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11634 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11635 | } |
| 11636 | if (PyUnicode_READY(substring) == -1) { |
| 11637 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11638 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11639 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11640 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11641 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11642 | |
| 11643 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11644 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11645 | if (result == -2) |
| 11646 | return NULL; |
| 11647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11648 | if (result < 0) { |
| 11649 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11650 | return NULL; |
| 11651 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11652 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11653 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11654 | } |
| 11655 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11656 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11657 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11658 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11659 | 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] | 11660 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11661 | |
| 11662 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11663 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11664 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11665 | Py_ssize_t i, length; |
| 11666 | int kind; |
| 11667 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11668 | int cased; |
| 11669 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11670 | if (PyUnicode_READY(self) == -1) |
| 11671 | return NULL; |
| 11672 | length = PyUnicode_GET_LENGTH(self); |
| 11673 | kind = PyUnicode_KIND(self); |
| 11674 | data = PyUnicode_DATA(self); |
| 11675 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11676 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11677 | if (length == 1) |
| 11678 | return PyBool_FromLong( |
| 11679 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11680 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11681 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11682 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11683 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11685 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11686 | for (i = 0; i < length; i++) { |
| 11687 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11688 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11689 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11690 | return PyBool_FromLong(0); |
| 11691 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11692 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11693 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11694 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11695 | } |
| 11696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11697 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11698 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11699 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11700 | 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] | 11701 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11702 | |
| 11703 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11704 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11705 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11706 | Py_ssize_t i, length; |
| 11707 | int kind; |
| 11708 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11709 | int cased; |
| 11710 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11711 | if (PyUnicode_READY(self) == -1) |
| 11712 | return NULL; |
| 11713 | length = PyUnicode_GET_LENGTH(self); |
| 11714 | kind = PyUnicode_KIND(self); |
| 11715 | data = PyUnicode_DATA(self); |
| 11716 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11717 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11718 | if (length == 1) |
| 11719 | return PyBool_FromLong( |
| 11720 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11721 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11722 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11723 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11724 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11725 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11726 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11727 | for (i = 0; i < length; i++) { |
| 11728 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11729 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11730 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11731 | return PyBool_FromLong(0); |
| 11732 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11733 | cased = 1; |
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(cased); |
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(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11739 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11740 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11741 | Return True if S is a titlecased string and there is at least one\n\ |
| 11742 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11743 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11744 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11745 | |
| 11746 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11747 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11748 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11749 | Py_ssize_t i, length; |
| 11750 | int kind; |
| 11751 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11752 | int cased, previous_is_cased; |
| 11753 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11754 | if (PyUnicode_READY(self) == -1) |
| 11755 | return NULL; |
| 11756 | length = PyUnicode_GET_LENGTH(self); |
| 11757 | kind = PyUnicode_KIND(self); |
| 11758 | data = PyUnicode_DATA(self); |
| 11759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11760 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11761 | if (length == 1) { |
| 11762 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11763 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11764 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11765 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11766 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11767 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11768 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11769 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | cased = 0; |
| 11772 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11773 | for (i = 0; i < length; i++) { |
| 11774 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11775 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11776 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11777 | if (previous_is_cased) |
| 11778 | return PyBool_FromLong(0); |
| 11779 | previous_is_cased = 1; |
| 11780 | cased = 1; |
| 11781 | } |
| 11782 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11783 | if (!previous_is_cased) |
| 11784 | return PyBool_FromLong(0); |
| 11785 | previous_is_cased = 1; |
| 11786 | cased = 1; |
| 11787 | } |
| 11788 | else |
| 11789 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11790 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11791 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11792 | } |
| 11793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11794 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11795 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11796 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11797 | Return True if all characters in S are whitespace\n\ |
| 11798 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11799 | |
| 11800 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11801 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11802 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11803 | Py_ssize_t i, length; |
| 11804 | int kind; |
| 11805 | void *data; |
| 11806 | |
| 11807 | if (PyUnicode_READY(self) == -1) |
| 11808 | return NULL; |
| 11809 | length = PyUnicode_GET_LENGTH(self); |
| 11810 | kind = PyUnicode_KIND(self); |
| 11811 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11813 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11814 | if (length == 1) |
| 11815 | return PyBool_FromLong( |
| 11816 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11817 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11818 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11819 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11820 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11821 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11822 | for (i = 0; i < length; i++) { |
| 11823 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11824 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11825 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11826 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11827 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11828 | } |
| 11829 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11830 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11831 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11832 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11833 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11834 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11835 | |
| 11836 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11837 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11838 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11839 | Py_ssize_t i, length; |
| 11840 | int kind; |
| 11841 | void *data; |
| 11842 | |
| 11843 | if (PyUnicode_READY(self) == -1) |
| 11844 | return NULL; |
| 11845 | length = PyUnicode_GET_LENGTH(self); |
| 11846 | kind = PyUnicode_KIND(self); |
| 11847 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11848 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11849 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11850 | if (length == 1) |
| 11851 | return PyBool_FromLong( |
| 11852 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11853 | |
| 11854 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11855 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11856 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11857 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11858 | for (i = 0; i < length; i++) { |
| 11859 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11860 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11861 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11862 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11863 | } |
| 11864 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11865 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11866 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11867 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11868 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11869 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11870 | |
| 11871 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11872 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11873 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11874 | int kind; |
| 11875 | void *data; |
| 11876 | Py_ssize_t len, i; |
| 11877 | |
| 11878 | if (PyUnicode_READY(self) == -1) |
| 11879 | return NULL; |
| 11880 | |
| 11881 | kind = PyUnicode_KIND(self); |
| 11882 | data = PyUnicode_DATA(self); |
| 11883 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11884 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11885 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11886 | if (len == 1) { |
| 11887 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11888 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11889 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11890 | |
| 11891 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11892 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11893 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11894 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11895 | for (i = 0; i < len; i++) { |
| 11896 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11897 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11898 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11899 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11900 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11901 | } |
| 11902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11903 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11904 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11905 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11906 | 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] | 11907 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11908 | |
| 11909 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11910 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | Py_ssize_t i, length; |
| 11913 | int kind; |
| 11914 | void *data; |
| 11915 | |
| 11916 | if (PyUnicode_READY(self) == -1) |
| 11917 | return NULL; |
| 11918 | length = PyUnicode_GET_LENGTH(self); |
| 11919 | kind = PyUnicode_KIND(self); |
| 11920 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11921 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11922 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11923 | if (length == 1) |
| 11924 | return PyBool_FromLong( |
| 11925 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11926 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11927 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11928 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11929 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11930 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11931 | for (i = 0; i < length; i++) { |
| 11932 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11933 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11935 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11936 | } |
| 11937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11938 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11939 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11940 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11941 | Return True if all characters in S are digits\n\ |
| 11942 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | |
| 11944 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11945 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11946 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11947 | Py_ssize_t i, length; |
| 11948 | int kind; |
| 11949 | void *data; |
| 11950 | |
| 11951 | if (PyUnicode_READY(self) == -1) |
| 11952 | return NULL; |
| 11953 | length = PyUnicode_GET_LENGTH(self); |
| 11954 | kind = PyUnicode_KIND(self); |
| 11955 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11956 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11957 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11958 | if (length == 1) { |
| 11959 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11960 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11961 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11962 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11963 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11964 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11965 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11966 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11967 | for (i = 0; i < length; i++) { |
| 11968 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11969 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11970 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11971 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11972 | } |
| 11973 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11974 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11975 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11976 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11977 | 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] | 11978 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11979 | |
| 11980 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11981 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11982 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11983 | Py_ssize_t i, length; |
| 11984 | int kind; |
| 11985 | void *data; |
| 11986 | |
| 11987 | if (PyUnicode_READY(self) == -1) |
| 11988 | return NULL; |
| 11989 | length = PyUnicode_GET_LENGTH(self); |
| 11990 | kind = PyUnicode_KIND(self); |
| 11991 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11992 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11993 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11994 | if (length == 1) |
| 11995 | return PyBool_FromLong( |
| 11996 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11997 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11998 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11999 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12000 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 12001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12002 | for (i = 0; i < length; i++) { |
| 12003 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12004 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12005 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 12006 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12007 | } |
| 12008 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12009 | int |
| 12010 | PyUnicode_IsIdentifier(PyObject *self) |
| 12011 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12012 | int kind; |
| 12013 | void *data; |
| 12014 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 12015 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12016 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12017 | if (PyUnicode_READY(self) == -1) { |
| 12018 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12019 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12020 | } |
| 12021 | |
| 12022 | /* Special case for empty strings */ |
| 12023 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 12024 | return 0; |
| 12025 | kind = PyUnicode_KIND(self); |
| 12026 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12027 | |
| 12028 | /* PEP 3131 says that the first character must be in |
| 12029 | XID_Start and subsequent characters in XID_Continue, |
| 12030 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12031 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12032 | letters, digits, underscore). However, given the current |
| 12033 | definition of XID_Start and XID_Continue, it is sufficient |
| 12034 | to check just for these, except that _ must be allowed |
| 12035 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12036 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 12037 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12038 | return 0; |
| 12039 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 12040 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12042 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12043 | return 1; |
| 12044 | } |
| 12045 | |
| 12046 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12047 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12048 | \n\ |
| 12049 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 12050 | to the language definition.\n\ |
| 12051 | \n\ |
| 12052 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 12053 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12054 | |
| 12055 | static PyObject* |
| 12056 | unicode_isidentifier(PyObject *self) |
| 12057 | { |
| 12058 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 12059 | } |
| 12060 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12061 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12062 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12063 | \n\ |
| 12064 | Return True if all characters in S are considered\n\ |
| 12065 | printable in repr() or S is empty, False otherwise."); |
| 12066 | |
| 12067 | static PyObject* |
| 12068 | unicode_isprintable(PyObject *self) |
| 12069 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12070 | Py_ssize_t i, length; |
| 12071 | int kind; |
| 12072 | void *data; |
| 12073 | |
| 12074 | if (PyUnicode_READY(self) == -1) |
| 12075 | return NULL; |
| 12076 | length = PyUnicode_GET_LENGTH(self); |
| 12077 | kind = PyUnicode_KIND(self); |
| 12078 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12079 | |
| 12080 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12081 | if (length == 1) |
| 12082 | return PyBool_FromLong( |
| 12083 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12084 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12085 | for (i = 0; i < length; i++) { |
| 12086 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12087 | Py_RETURN_FALSE; |
| 12088 | } |
| 12089 | } |
| 12090 | Py_RETURN_TRUE; |
| 12091 | } |
| 12092 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12093 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 12094 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12095 | \n\ |
| 12096 | 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] | 12097 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12098 | |
| 12099 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12100 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12101 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12102 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12103 | } |
| 12104 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12105 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12106 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12107 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12108 | if (PyUnicode_READY(self) == -1) |
| 12109 | return -1; |
| 12110 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12111 | } |
| 12112 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12113 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12114 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12115 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12116 | 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] | 12117 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12118 | |
| 12119 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12120 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12121 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12122 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12123 | Py_UCS4 fillchar = ' '; |
| 12124 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12125 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12126 | return NULL; |
| 12127 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12128 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12129 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12130 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12131 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12132 | return unicode_result_unchanged(self); |
| 12133 | |
| 12134 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12135 | } |
| 12136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12137 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12138 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12139 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12140 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12141 | |
| 12142 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12143 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12144 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12145 | if (PyUnicode_READY(self) == -1) |
| 12146 | return NULL; |
| 12147 | if (PyUnicode_IS_ASCII(self)) |
| 12148 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12149 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12150 | } |
| 12151 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12152 | #define LEFTSTRIP 0 |
| 12153 | #define RIGHTSTRIP 1 |
| 12154 | #define BOTHSTRIP 2 |
| 12155 | |
| 12156 | /* Arrays indexed by above */ |
| 12157 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 12158 | |
| 12159 | #define STRIPNAME(i) (stripformat[i]+3) |
| 12160 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12161 | /* externally visible for str.strip(unicode) */ |
| 12162 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12163 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12164 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12165 | void *data; |
| 12166 | int kind; |
| 12167 | Py_ssize_t i, j, len; |
| 12168 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12169 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12170 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12171 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 12172 | return NULL; |
| 12173 | |
| 12174 | kind = PyUnicode_KIND(self); |
| 12175 | data = PyUnicode_DATA(self); |
| 12176 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12177 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12178 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 12179 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12180 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12181 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12182 | i = 0; |
| 12183 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12184 | while (i < len) { |
| 12185 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12186 | if (!BLOOM(sepmask, ch)) |
| 12187 | break; |
| 12188 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12189 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12190 | i++; |
| 12191 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12192 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12193 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12194 | j = len; |
| 12195 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12196 | j--; |
| 12197 | while (j >= i) { |
| 12198 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12199 | if (!BLOOM(sepmask, ch)) |
| 12200 | break; |
| 12201 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12202 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12203 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12204 | } |
| 12205 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12206 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12207 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12208 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12209 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12210 | } |
| 12211 | |
| 12212 | PyObject* |
| 12213 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12214 | { |
| 12215 | unsigned char *data; |
| 12216 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12217 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12218 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12219 | if (PyUnicode_READY(self) == -1) |
| 12220 | return NULL; |
| 12221 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12222 | length = PyUnicode_GET_LENGTH(self); |
| 12223 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12224 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12225 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12226 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12227 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12228 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12229 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12230 | return NULL; |
| 12231 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12232 | if (start >= length || end < start) |
| 12233 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12234 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12235 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12236 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12237 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12238 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12239 | } |
| 12240 | else { |
| 12241 | kind = PyUnicode_KIND(self); |
| 12242 | data = PyUnicode_1BYTE_DATA(self); |
| 12243 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12244 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12245 | length); |
| 12246 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12247 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12248 | |
| 12249 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12250 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12251 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12252 | Py_ssize_t len, i, j; |
| 12253 | |
| 12254 | if (PyUnicode_READY(self) == -1) |
| 12255 | return NULL; |
| 12256 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12257 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12258 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12259 | if (PyUnicode_IS_ASCII(self)) { |
| 12260 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12261 | |
| 12262 | i = 0; |
| 12263 | if (striptype != RIGHTSTRIP) { |
| 12264 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12265 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12266 | if (!_Py_ascii_whitespace[ch]) |
| 12267 | break; |
| 12268 | i++; |
| 12269 | } |
| 12270 | } |
| 12271 | |
| 12272 | j = len; |
| 12273 | if (striptype != LEFTSTRIP) { |
| 12274 | j--; |
| 12275 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12276 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12277 | if (!_Py_ascii_whitespace[ch]) |
| 12278 | break; |
| 12279 | j--; |
| 12280 | } |
| 12281 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12282 | } |
| 12283 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12284 | else { |
| 12285 | int kind = PyUnicode_KIND(self); |
| 12286 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12287 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12288 | i = 0; |
| 12289 | if (striptype != RIGHTSTRIP) { |
| 12290 | while (i < len) { |
| 12291 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12292 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12293 | break; |
| 12294 | i++; |
| 12295 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12296 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12297 | |
| 12298 | j = len; |
| 12299 | if (striptype != LEFTSTRIP) { |
| 12300 | j--; |
| 12301 | while (j >= i) { |
| 12302 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12303 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12304 | break; |
| 12305 | j--; |
| 12306 | } |
| 12307 | j++; |
| 12308 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12309 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12310 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12311 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12312 | } |
| 12313 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12314 | |
| 12315 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12316 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12317 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12318 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12319 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12320 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12321 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12322 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12323 | if (sep != NULL && sep != Py_None) { |
| 12324 | if (PyUnicode_Check(sep)) |
| 12325 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12326 | else { |
| 12327 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12328 | "%s arg must be None or str", |
| 12329 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12330 | return NULL; |
| 12331 | } |
| 12332 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12333 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12334 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12335 | } |
| 12336 | |
| 12337 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12338 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12339 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12340 | \n\ |
| 12341 | Return a copy of the string S with leading and trailing\n\ |
| 12342 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12343 | 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] | 12344 | |
| 12345 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12346 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12347 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12348 | if (PyTuple_GET_SIZE(args) == 0) |
| 12349 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12350 | else |
| 12351 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12352 | } |
| 12353 | |
| 12354 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12355 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12356 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12357 | \n\ |
| 12358 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12359 | 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] | 12360 | |
| 12361 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12362 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12363 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12364 | if (PyTuple_GET_SIZE(args) == 0) |
| 12365 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12366 | else |
| 12367 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12368 | } |
| 12369 | |
| 12370 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12371 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12372 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12373 | \n\ |
| 12374 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12375 | 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] | 12376 | |
| 12377 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12378 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12379 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12380 | if (PyTuple_GET_SIZE(args) == 0) |
| 12381 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12382 | else |
| 12383 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12384 | } |
| 12385 | |
| 12386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12387 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12388 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12389 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12390 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12391 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12392 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12393 | if (len < 1) |
| 12394 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12395 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12396 | /* no repeat, return original string */ |
| 12397 | if (len == 1) |
| 12398 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12399 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12400 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12401 | return NULL; |
| 12402 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12403 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12404 | PyErr_SetString(PyExc_OverflowError, |
| 12405 | "repeated string is too long"); |
| 12406 | return NULL; |
| 12407 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12408 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12409 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12410 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12411 | if (!u) |
| 12412 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12413 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12415 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12416 | const int kind = PyUnicode_KIND(str); |
| 12417 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12418 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12419 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12420 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12421 | } |
| 12422 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12423 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12424 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12425 | ucs2[n] = fill_char; |
| 12426 | } else { |
| 12427 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12428 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12429 | for (n = 0; n < len; ++n) |
| 12430 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12431 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12432 | } |
| 12433 | else { |
| 12434 | /* number of characters copied this far */ |
| 12435 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12436 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12437 | char *to = (char *) PyUnicode_DATA(u); |
| 12438 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12439 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12440 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12441 | n = (done <= nchars-done) ? done : nchars-done; |
| 12442 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12443 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12444 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12445 | } |
| 12446 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12447 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12448 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12449 | } |
| 12450 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12451 | PyObject * |
| 12452 | PyUnicode_Replace(PyObject *obj, |
| 12453 | PyObject *subobj, |
| 12454 | PyObject *replobj, |
| 12455 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12456 | { |
| 12457 | PyObject *self; |
| 12458 | PyObject *str1; |
| 12459 | PyObject *str2; |
| 12460 | PyObject *result; |
| 12461 | |
| 12462 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12463 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12464 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12465 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12466 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12467 | Py_DECREF(self); |
| 12468 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12469 | } |
| 12470 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12471 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12472 | Py_DECREF(self); |
| 12473 | Py_DECREF(str1); |
| 12474 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12475 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12476 | if (PyUnicode_READY(self) == -1 || |
| 12477 | PyUnicode_READY(str1) == -1 || |
| 12478 | PyUnicode_READY(str2) == -1) |
| 12479 | result = NULL; |
| 12480 | else |
| 12481 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12482 | Py_DECREF(self); |
| 12483 | Py_DECREF(str1); |
| 12484 | Py_DECREF(str2); |
| 12485 | return result; |
| 12486 | } |
| 12487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12488 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12489 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12490 | \n\ |
| 12491 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12492 | old replaced by new. If the optional argument count is\n\ |
| 12493 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12494 | |
| 12495 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12496 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12497 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12498 | PyObject *str1; |
| 12499 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12500 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12501 | PyObject *result; |
| 12502 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12503 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12504 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12505 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12506 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12507 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12508 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12509 | return NULL; |
| 12510 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12511 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12512 | Py_DECREF(str1); |
| 12513 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12514 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12515 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12516 | result = NULL; |
| 12517 | else |
| 12518 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12519 | |
| 12520 | Py_DECREF(str1); |
| 12521 | Py_DECREF(str2); |
| 12522 | return result; |
| 12523 | } |
| 12524 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12525 | static PyObject * |
| 12526 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12527 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12528 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12529 | Py_ssize_t isize; |
| 12530 | Py_ssize_t osize, squote, dquote, i, o; |
| 12531 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12532 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12533 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12534 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12535 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12536 | return NULL; |
| 12537 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12538 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12539 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12540 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12541 | /* Compute length of output, quote characters, and |
| 12542 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12543 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12544 | max = 127; |
| 12545 | squote = dquote = 0; |
| 12546 | ikind = PyUnicode_KIND(unicode); |
| 12547 | for (i = 0; i < isize; i++) { |
| 12548 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12549 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12550 | switch (ch) { |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12551 | case '\'': squote++; break; |
| 12552 | case '"': dquote++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12553 | case '\\': case '\t': case '\r': case '\n': |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12554 | incr = 2; |
| 12555 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12556 | default: |
| 12557 | /* Fast-path ASCII */ |
| 12558 | if (ch < ' ' || ch == 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12559 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12560 | else if (ch < 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12561 | ; |
| 12562 | else if (Py_UNICODE_ISPRINTABLE(ch)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12563 | max = ch > max ? ch : max; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12564 | else if (ch < 0x100) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12565 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12566 | else if (ch < 0x10000) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12567 | incr = 6; /* \uHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12568 | else |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12569 | incr = 10; /* \uHHHHHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12570 | } |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12571 | if (osize > PY_SSIZE_T_MAX - incr) { |
| 12572 | PyErr_SetString(PyExc_OverflowError, |
| 12573 | "string is too long to generate repr"); |
| 12574 | return NULL; |
| 12575 | } |
| 12576 | osize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12577 | } |
| 12578 | |
| 12579 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12580 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12581 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12582 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12583 | if (dquote) |
| 12584 | /* Both squote and dquote present. Use squote, |
| 12585 | and escape them */ |
| 12586 | osize += squote; |
| 12587 | else |
| 12588 | quote = '"'; |
| 12589 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12590 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12591 | |
| 12592 | repr = PyUnicode_New(osize, max); |
| 12593 | if (repr == NULL) |
| 12594 | return NULL; |
| 12595 | okind = PyUnicode_KIND(repr); |
| 12596 | odata = PyUnicode_DATA(repr); |
| 12597 | |
| 12598 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12599 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12600 | if (unchanged) { |
| 12601 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12602 | unicode, 0, |
| 12603 | isize); |
| 12604 | } |
| 12605 | else { |
| 12606 | for (i = 0, o = 1; i < isize; i++) { |
| 12607 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12608 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12609 | /* Escape quotes and backslashes */ |
| 12610 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12611 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12612 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12613 | continue; |
| 12614 | } |
| 12615 | |
| 12616 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12617 | if (ch == '\t') { |
| 12618 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12619 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12620 | } |
| 12621 | else if (ch == '\n') { |
| 12622 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12623 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12624 | } |
| 12625 | else if (ch == '\r') { |
| 12626 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12627 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12628 | } |
| 12629 | |
| 12630 | /* Map non-printable US ASCII to '\xhh' */ |
| 12631 | else if (ch < ' ' || ch == 0x7F) { |
| 12632 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12633 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12634 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12635 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12636 | } |
| 12637 | |
| 12638 | /* Copy ASCII characters as-is */ |
| 12639 | else if (ch < 0x7F) { |
| 12640 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12641 | } |
| 12642 | |
| 12643 | /* Non-ASCII characters */ |
| 12644 | else { |
| 12645 | /* Map Unicode whitespace and control characters |
| 12646 | (categories Z* and C* except ASCII space) |
| 12647 | */ |
| 12648 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12649 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12650 | /* Map 8-bit characters to '\xhh' */ |
| 12651 | if (ch <= 0xff) { |
| 12652 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12653 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12654 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12655 | } |
| 12656 | /* Map 16-bit characters to '\uxxxx' */ |
| 12657 | else if (ch <= 0xffff) { |
| 12658 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12659 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12660 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12661 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12662 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12663 | } |
| 12664 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12665 | else { |
| 12666 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12667 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12668 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12669 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12670 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12671 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12672 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12673 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12674 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12675 | } |
| 12676 | } |
| 12677 | /* Copy characters as-is */ |
| 12678 | else { |
| 12679 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12680 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12681 | } |
| 12682 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12683 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12684 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12685 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12686 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12687 | } |
| 12688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12689 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12690 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12691 | \n\ |
| 12692 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12693 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12694 | arguments start and end are interpreted as in slice notation.\n\ |
| 12695 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12696 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12697 | |
| 12698 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12699 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12700 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12701 | /* initialize variables to prevent gcc warning */ |
| 12702 | PyObject *substring = NULL; |
| 12703 | Py_ssize_t start = 0; |
| 12704 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12705 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12706 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12707 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12708 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12709 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12710 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12711 | if (PyUnicode_READY(self) == -1) { |
| 12712 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12713 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12714 | } |
| 12715 | if (PyUnicode_READY(substring) == -1) { |
| 12716 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12717 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12718 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12719 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12720 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12721 | |
| 12722 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12723 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12724 | if (result == -2) |
| 12725 | return NULL; |
| 12726 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12727 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12728 | } |
| 12729 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12730 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12731 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12732 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12733 | 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] | 12734 | |
| 12735 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12736 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12737 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12738 | /* initialize variables to prevent gcc warning */ |
| 12739 | PyObject *substring = NULL; |
| 12740 | Py_ssize_t start = 0; |
| 12741 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12742 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12743 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12744 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12745 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12746 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12747 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12748 | if (PyUnicode_READY(self) == -1) { |
| 12749 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12750 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12751 | } |
| 12752 | if (PyUnicode_READY(substring) == -1) { |
| 12753 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12754 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12755 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12756 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12757 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12758 | |
| 12759 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12761 | if (result == -2) |
| 12762 | return NULL; |
| 12763 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12764 | if (result < 0) { |
| 12765 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12766 | return NULL; |
| 12767 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12768 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12769 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12770 | } |
| 12771 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12772 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12773 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12774 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12775 | 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] | 12776 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12777 | |
| 12778 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12779 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12780 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12781 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12782 | Py_UCS4 fillchar = ' '; |
| 12783 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12784 | 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] | 12785 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12786 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12787 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12788 | return NULL; |
| 12789 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12790 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12791 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12792 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12793 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12794 | } |
| 12795 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12796 | PyObject * |
| 12797 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12798 | { |
| 12799 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12801 | s = PyUnicode_FromObject(s); |
| 12802 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12803 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12804 | if (sep != NULL) { |
| 12805 | sep = PyUnicode_FromObject(sep); |
| 12806 | if (sep == NULL) { |
| 12807 | Py_DECREF(s); |
| 12808 | return NULL; |
| 12809 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12810 | } |
| 12811 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12812 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12813 | |
| 12814 | Py_DECREF(s); |
| 12815 | Py_XDECREF(sep); |
| 12816 | return result; |
| 12817 | } |
| 12818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12819 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12820 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12821 | \n\ |
| 12822 | Return a list of the words in S, using sep as the\n\ |
| 12823 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12824 | 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] | 12825 | whitespace string is a separator and empty strings are\n\ |
| 12826 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12827 | |
| 12828 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12829 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12830 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12831 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12832 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12833 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12834 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12835 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12836 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12837 | return NULL; |
| 12838 | |
| 12839 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12840 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12841 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12842 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12843 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12844 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12845 | } |
| 12846 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12847 | PyObject * |
| 12848 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12849 | { |
| 12850 | PyObject* str_obj; |
| 12851 | PyObject* sep_obj; |
| 12852 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12853 | int kind1, kind2; |
| 12854 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12855 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12856 | |
| 12857 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12858 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12859 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12860 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12861 | if (!sep_obj) { |
| 12862 | Py_DECREF(str_obj); |
| 12863 | return NULL; |
| 12864 | } |
| 12865 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12866 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12867 | Py_DECREF(str_obj); |
| 12868 | return NULL; |
| 12869 | } |
| 12870 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12871 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12872 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12873 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12874 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12875 | if (kind1 < kind2 || len1 < len2) { |
| 12876 | _Py_INCREF_UNICODE_EMPTY(); |
| 12877 | if (!unicode_empty) |
| 12878 | out = NULL; |
| 12879 | else { |
| 12880 | out = PyTuple_Pack(3, str_obj, unicode_empty, unicode_empty); |
| 12881 | Py_DECREF(unicode_empty); |
| 12882 | } |
| 12883 | Py_DECREF(sep_obj); |
| 12884 | Py_DECREF(str_obj); |
| 12885 | return out; |
| 12886 | } |
| 12887 | buf1 = PyUnicode_DATA(str_obj); |
| 12888 | buf2 = PyUnicode_DATA(sep_obj); |
| 12889 | if (kind2 != kind1) { |
| 12890 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12891 | if (!buf2) |
| 12892 | goto onError; |
| 12893 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12894 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12895 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12896 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12897 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12898 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12899 | else |
| 12900 | 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] | 12901 | break; |
| 12902 | case PyUnicode_2BYTE_KIND: |
| 12903 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12904 | break; |
| 12905 | case PyUnicode_4BYTE_KIND: |
| 12906 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12907 | break; |
| 12908 | default: |
| 12909 | assert(0); |
| 12910 | out = 0; |
| 12911 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12912 | |
| 12913 | Py_DECREF(sep_obj); |
| 12914 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12915 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12916 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12917 | |
| 12918 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12919 | onError: |
| 12920 | Py_DECREF(sep_obj); |
| 12921 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12922 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12923 | PyMem_Free(buf2); |
| 12924 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12925 | } |
| 12926 | |
| 12927 | |
| 12928 | PyObject * |
| 12929 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12930 | { |
| 12931 | PyObject* str_obj; |
| 12932 | PyObject* sep_obj; |
| 12933 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12934 | int kind1, kind2; |
| 12935 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12936 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12937 | |
| 12938 | str_obj = PyUnicode_FromObject(str_in); |
| 12939 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12940 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12941 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12942 | if (!sep_obj) { |
| 12943 | Py_DECREF(str_obj); |
| 12944 | return NULL; |
| 12945 | } |
| 12946 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12947 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12948 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12949 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12950 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12951 | if (kind1 < kind2 || len1 < len2) { |
| 12952 | _Py_INCREF_UNICODE_EMPTY(); |
| 12953 | if (!unicode_empty) |
| 12954 | out = NULL; |
| 12955 | else { |
| 12956 | out = PyTuple_Pack(3, unicode_empty, unicode_empty, str_obj); |
| 12957 | Py_DECREF(unicode_empty); |
| 12958 | } |
| 12959 | Py_DECREF(sep_obj); |
| 12960 | Py_DECREF(str_obj); |
| 12961 | return out; |
| 12962 | } |
| 12963 | buf1 = PyUnicode_DATA(str_obj); |
| 12964 | buf2 = PyUnicode_DATA(sep_obj); |
| 12965 | if (kind2 != kind1) { |
| 12966 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12967 | if (!buf2) |
| 12968 | goto onError; |
| 12969 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12970 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12971 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12972 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12973 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12974 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12975 | else |
| 12976 | 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] | 12977 | break; |
| 12978 | case PyUnicode_2BYTE_KIND: |
| 12979 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12980 | break; |
| 12981 | case PyUnicode_4BYTE_KIND: |
| 12982 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12983 | break; |
| 12984 | default: |
| 12985 | assert(0); |
| 12986 | out = 0; |
| 12987 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12988 | |
| 12989 | Py_DECREF(sep_obj); |
| 12990 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12991 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12992 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12993 | |
| 12994 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12995 | onError: |
| 12996 | Py_DECREF(sep_obj); |
| 12997 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12998 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12999 | PyMem_Free(buf2); |
| 13000 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13001 | } |
| 13002 | |
| 13003 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13004 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13005 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 13006 | 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] | 13007 | 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] | 13008 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13009 | |
| 13010 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13011 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13012 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13013 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13014 | } |
| 13015 | |
| 13016 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 13017 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13018 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 13019 | 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] | 13020 | 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] | 13021 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13022 | |
| 13023 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13024 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13025 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13026 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13027 | } |
| 13028 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13029 | PyObject * |
| 13030 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13031 | { |
| 13032 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13033 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13034 | s = PyUnicode_FromObject(s); |
| 13035 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13036 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13037 | if (sep != NULL) { |
| 13038 | sep = PyUnicode_FromObject(sep); |
| 13039 | if (sep == NULL) { |
| 13040 | Py_DECREF(s); |
| 13041 | return NULL; |
| 13042 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13043 | } |
| 13044 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13045 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13046 | |
| 13047 | Py_DECREF(s); |
| 13048 | Py_XDECREF(sep); |
| 13049 | return result; |
| 13050 | } |
| 13051 | |
| 13052 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13053 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13054 | \n\ |
| 13055 | Return a list of the words in S, using sep as the\n\ |
| 13056 | delimiter string, starting at the end of the string and\n\ |
| 13057 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 13058 | splits are done. If sep is not specified, any whitespace string\n\ |
| 13059 | is a separator."); |
| 13060 | |
| 13061 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13062 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13063 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13064 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13065 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13066 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13067 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13068 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 13069 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13070 | return NULL; |
| 13071 | |
| 13072 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13073 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13074 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13075 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13076 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13077 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13078 | } |
| 13079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13080 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13081 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13082 | \n\ |
| 13083 | 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] | 13084 | 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] | 13085 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13086 | |
| 13087 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13088 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13089 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13090 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 13091 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13092 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13093 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 13094 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13095 | return NULL; |
| 13096 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13097 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13098 | } |
| 13099 | |
| 13100 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 13101 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13102 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13103 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13104 | } |
| 13105 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13106 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13107 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13108 | \n\ |
| 13109 | 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] | 13110 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13111 | |
| 13112 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13113 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13114 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 13115 | if (PyUnicode_READY(self) == -1) |
| 13116 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13117 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13118 | } |
| 13119 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 13120 | /*[clinic input] |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13121 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13122 | @staticmethod |
| 13123 | str.maketrans as unicode_maketrans |
| 13124 | |
| 13125 | x: object |
| 13126 | |
| 13127 | y: unicode=NULL |
| 13128 | |
| 13129 | z: unicode=NULL |
| 13130 | |
| 13131 | / |
| 13132 | |
| 13133 | Return a translation table usable for str.translate(). |
| 13134 | |
| 13135 | If there is only one argument, it must be a dictionary mapping Unicode |
| 13136 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 13137 | Character keys will be then converted to ordinals. |
| 13138 | If there are two arguments, they must be strings of equal length, and |
| 13139 | in the resulting dictionary, each character in x will be mapped to the |
| 13140 | character at the same position in y. If there is a third argument, it |
| 13141 | 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] | 13142 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13143 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13144 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 13145 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 13146 | /*[clinic end generated code: output=a925c89452bd5881 input=7bfbf529a293c6c5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13147 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13148 | PyObject *new = NULL, *key, *value; |
| 13149 | Py_ssize_t i = 0; |
| 13150 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13151 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13152 | new = PyDict_New(); |
| 13153 | if (!new) |
| 13154 | return NULL; |
| 13155 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13156 | int x_kind, y_kind, z_kind; |
| 13157 | void *x_data, *y_data, *z_data; |
| 13158 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13159 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13160 | if (!PyUnicode_Check(x)) { |
| 13161 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 13162 | "be a string if there is a second argument"); |
| 13163 | goto err; |
| 13164 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13165 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13166 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 13167 | "arguments must have equal length"); |
| 13168 | goto err; |
| 13169 | } |
| 13170 | /* 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] | 13171 | x_kind = PyUnicode_KIND(x); |
| 13172 | y_kind = PyUnicode_KIND(y); |
| 13173 | x_data = PyUnicode_DATA(x); |
| 13174 | y_data = PyUnicode_DATA(y); |
| 13175 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 13176 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 13177 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13178 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 13179 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 13180 | if (!value) { |
| 13181 | Py_DECREF(key); |
| 13182 | goto err; |
| 13183 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13184 | res = PyDict_SetItem(new, key, value); |
| 13185 | Py_DECREF(key); |
| 13186 | Py_DECREF(value); |
| 13187 | if (res < 0) |
| 13188 | goto err; |
| 13189 | } |
| 13190 | /* create entries for deleting chars in z */ |
| 13191 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13192 | z_kind = PyUnicode_KIND(z); |
| 13193 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13194 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13195 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13196 | if (!key) |
| 13197 | goto err; |
| 13198 | res = PyDict_SetItem(new, key, Py_None); |
| 13199 | Py_DECREF(key); |
| 13200 | if (res < 0) |
| 13201 | goto err; |
| 13202 | } |
| 13203 | } |
| 13204 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13205 | int kind; |
| 13206 | void *data; |
| 13207 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13208 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 13209 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13210 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 13211 | "to maketrans it must be a dict"); |
| 13212 | goto err; |
| 13213 | } |
| 13214 | /* copy entries into the new dict, converting string keys to int keys */ |
| 13215 | while (PyDict_Next(x, &i, &key, &value)) { |
| 13216 | if (PyUnicode_Check(key)) { |
| 13217 | /* convert string keys to integer keys */ |
| 13218 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13219 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13220 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13221 | "table must be of length 1"); |
| 13222 | goto err; |
| 13223 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13224 | kind = PyUnicode_KIND(key); |
| 13225 | data = PyUnicode_DATA(key); |
| 13226 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13227 | if (!newkey) |
| 13228 | goto err; |
| 13229 | res = PyDict_SetItem(new, newkey, value); |
| 13230 | Py_DECREF(newkey); |
| 13231 | if (res < 0) |
| 13232 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13233 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13234 | /* just keep integer keys */ |
| 13235 | if (PyDict_SetItem(new, key, value) < 0) |
| 13236 | goto err; |
| 13237 | } else { |
| 13238 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13239 | "be strings or integers"); |
| 13240 | goto err; |
| 13241 | } |
| 13242 | } |
| 13243 | } |
| 13244 | return new; |
| 13245 | err: |
| 13246 | Py_DECREF(new); |
| 13247 | return NULL; |
| 13248 | } |
| 13249 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13250 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13251 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13252 | \n\ |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 13253 | Return a copy of the string S in which each character has been mapped\n\ |
| 13254 | through the given translation table. The table must implement\n\ |
| 13255 | lookup/indexing via __getitem__, for instance a dictionary or list,\n\ |
| 13256 | mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n\ |
| 13257 | this operation raises LookupError, the character is left untouched.\n\ |
| 13258 | Characters mapped to None are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13259 | |
| 13260 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13261 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13262 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13263 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13264 | } |
| 13265 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13266 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13267 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13268 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13269 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13270 | |
| 13271 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13272 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13273 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13274 | if (PyUnicode_READY(self) == -1) |
| 13275 | return NULL; |
| 13276 | if (PyUnicode_IS_ASCII(self)) |
| 13277 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13278 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13279 | } |
| 13280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13281 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13282 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13283 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13284 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13285 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13286 | |
| 13287 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13288 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13289 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13290 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13291 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13292 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13293 | int kind; |
| 13294 | void *data; |
| 13295 | Py_UCS4 chr; |
| 13296 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13297 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13298 | return NULL; |
| 13299 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13300 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13301 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13302 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13303 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13304 | return unicode_result_unchanged(self); |
| 13305 | |
| 13306 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13307 | |
| 13308 | u = pad(self, fill, 0, '0'); |
| 13309 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13310 | if (u == NULL) |
| 13311 | return NULL; |
| 13312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13313 | kind = PyUnicode_KIND(u); |
| 13314 | data = PyUnicode_DATA(u); |
| 13315 | chr = PyUnicode_READ(kind, data, fill); |
| 13316 | |
| 13317 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13318 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13319 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13320 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13321 | } |
| 13322 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13323 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13324 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13325 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13326 | |
| 13327 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13328 | static PyObject * |
| 13329 | unicode__decimal2ascii(PyObject *self) |
| 13330 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13331 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13332 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13333 | #endif |
| 13334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13335 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13336 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13337 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13338 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13339 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13340 | With optional end, stop comparing S at that position.\n\ |
| 13341 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13342 | |
| 13343 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13344 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13345 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13346 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13347 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13348 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13349 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13350 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13351 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13352 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13353 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13354 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13355 | if (PyTuple_Check(subobj)) { |
| 13356 | Py_ssize_t i; |
| 13357 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13358 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13359 | if (substring == NULL) |
| 13360 | return NULL; |
| 13361 | result = tailmatch(self, substring, start, end, -1); |
| 13362 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13363 | if (result == -1) |
| 13364 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13365 | if (result) { |
| 13366 | Py_RETURN_TRUE; |
| 13367 | } |
| 13368 | } |
| 13369 | /* nothing matched */ |
| 13370 | Py_RETURN_FALSE; |
| 13371 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13372 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13373 | if (substring == NULL) { |
| 13374 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13375 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13376 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13377 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13378 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13379 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13380 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13381 | if (result == -1) |
| 13382 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13383 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13384 | } |
| 13385 | |
| 13386 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13387 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13388 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13389 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13390 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13391 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13392 | With optional end, stop comparing S at that position.\n\ |
| 13393 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13394 | |
| 13395 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13396 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13397 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13398 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13399 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13400 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13401 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13402 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13403 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13404 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13405 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13406 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13407 | if (PyTuple_Check(subobj)) { |
| 13408 | Py_ssize_t i; |
| 13409 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13410 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13411 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13412 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13413 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13414 | result = tailmatch(self, substring, start, end, +1); |
| 13415 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13416 | if (result == -1) |
| 13417 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13418 | if (result) { |
| 13419 | Py_RETURN_TRUE; |
| 13420 | } |
| 13421 | } |
| 13422 | Py_RETURN_FALSE; |
| 13423 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13424 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13425 | if (substring == NULL) { |
| 13426 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13427 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13428 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13429 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13430 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13431 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13432 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13433 | if (result == -1) |
| 13434 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13435 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13436 | } |
| 13437 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13438 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13439 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13440 | { |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13441 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13442 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13443 | |
| 13444 | if (!writer->readonly) { |
| 13445 | writer->kind = PyUnicode_KIND(writer->buffer); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13446 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13447 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13448 | else { |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13449 | /* use a value smaller than PyUnicode_1BYTE_KIND() so |
| 13450 | _PyUnicodeWriter_PrepareKind() will copy the buffer. */ |
| 13451 | writer->kind = PyUnicode_WCHAR_KIND; |
| 13452 | assert(writer->kind <= PyUnicode_1BYTE_KIND); |
| 13453 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13454 | /* Copy-on-write mode: set buffer size to 0 so |
| 13455 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13456 | * next write. */ |
| 13457 | writer->size = 0; |
| 13458 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13459 | } |
| 13460 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13461 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13462 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13463 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13464 | memset(writer, 0, sizeof(*writer)); |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13465 | |
| 13466 | /* ASCII is the bare minimum */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13467 | writer->min_char = 127; |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13468 | |
| 13469 | /* use a value smaller than PyUnicode_1BYTE_KIND() so |
| 13470 | _PyUnicodeWriter_PrepareKind() will copy the buffer. */ |
| 13471 | writer->kind = PyUnicode_WCHAR_KIND; |
| 13472 | assert(writer->kind <= PyUnicode_1BYTE_KIND); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13473 | } |
| 13474 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13475 | int |
| 13476 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13477 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13478 | { |
| 13479 | Py_ssize_t newlen; |
| 13480 | PyObject *newbuffer; |
| 13481 | |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 13482 | /* ensure that the _PyUnicodeWriter_Prepare macro was used */ |
Victor Stinner | 6174474 | 2015-09-22 01:01:17 +0200 | [diff] [blame] | 13483 | assert((maxchar > writer->maxchar && length >= 0) |
| 13484 | || length > 0); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13485 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13486 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13487 | PyErr_NoMemory(); |
| 13488 | return -1; |
| 13489 | } |
| 13490 | newlen = writer->pos + length; |
| 13491 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13492 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13493 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13494 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13495 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13496 | if (writer->overallocate |
| 13497 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13498 | /* overallocate to limit the number of realloc() */ |
| 13499 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13500 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13501 | if (newlen < writer->min_length) |
| 13502 | newlen = writer->min_length; |
| 13503 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13504 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13505 | if (writer->buffer == NULL) |
| 13506 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13507 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13508 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13509 | if (writer->overallocate |
| 13510 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13511 | /* overallocate to limit the number of realloc() */ |
| 13512 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13513 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13514 | if (newlen < writer->min_length) |
| 13515 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13516 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13517 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13518 | /* resize + widen */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 13519 | maxchar = Py_MAX(maxchar, writer->maxchar); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13520 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13521 | if (newbuffer == NULL) |
| 13522 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13523 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13524 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13525 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13526 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13527 | } |
| 13528 | else { |
| 13529 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13530 | if (newbuffer == NULL) |
| 13531 | return -1; |
| 13532 | } |
| 13533 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13534 | } |
| 13535 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13536 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13537 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13538 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13539 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13540 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13541 | writer->buffer, 0, writer->pos); |
| 13542 | Py_DECREF(writer->buffer); |
| 13543 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13544 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13545 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13546 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13547 | |
| 13548 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13549 | } |
| 13550 | |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 13551 | int |
| 13552 | _PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer, |
| 13553 | enum PyUnicode_Kind kind) |
| 13554 | { |
| 13555 | Py_UCS4 maxchar; |
| 13556 | |
| 13557 | /* ensure that the _PyUnicodeWriter_PrepareKind macro was used */ |
| 13558 | assert(writer->kind < kind); |
| 13559 | |
| 13560 | switch (kind) |
| 13561 | { |
| 13562 | case PyUnicode_1BYTE_KIND: maxchar = 0xff; break; |
| 13563 | case PyUnicode_2BYTE_KIND: maxchar = 0xffff; break; |
| 13564 | case PyUnicode_4BYTE_KIND: maxchar = 0x10ffff; break; |
| 13565 | default: |
| 13566 | assert(0 && "invalid kind"); |
| 13567 | return -1; |
| 13568 | } |
| 13569 | |
| 13570 | return _PyUnicodeWriter_PrepareInternal(writer, 0, maxchar); |
| 13571 | } |
| 13572 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13573 | Py_LOCAL_INLINE(int) |
| 13574 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13575 | { |
| 13576 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13577 | return -1; |
| 13578 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13579 | writer->pos++; |
| 13580 | return 0; |
| 13581 | } |
| 13582 | |
| 13583 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13584 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13585 | { |
| 13586 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13587 | } |
| 13588 | |
| 13589 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13590 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13591 | { |
| 13592 | Py_UCS4 maxchar; |
| 13593 | Py_ssize_t len; |
| 13594 | |
| 13595 | if (PyUnicode_READY(str) == -1) |
| 13596 | return -1; |
| 13597 | len = PyUnicode_GET_LENGTH(str); |
| 13598 | if (len == 0) |
| 13599 | return 0; |
| 13600 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13601 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13602 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 1912b39 | 2015-03-26 09:37:23 +0100 | [diff] [blame] | 13603 | assert(_PyUnicode_CheckConsistency(str, 1)); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13604 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13605 | Py_INCREF(str); |
| 13606 | writer->buffer = str; |
| 13607 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13608 | writer->pos += len; |
| 13609 | return 0; |
| 13610 | } |
| 13611 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13612 | return -1; |
| 13613 | } |
| 13614 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13615 | str, 0, len); |
| 13616 | writer->pos += len; |
| 13617 | return 0; |
| 13618 | } |
| 13619 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13620 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13621 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13622 | Py_ssize_t start, Py_ssize_t end) |
| 13623 | { |
| 13624 | Py_UCS4 maxchar; |
| 13625 | Py_ssize_t len; |
| 13626 | |
| 13627 | if (PyUnicode_READY(str) == -1) |
| 13628 | return -1; |
| 13629 | |
| 13630 | assert(0 <= start); |
| 13631 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13632 | assert(start <= end); |
| 13633 | |
| 13634 | if (end == 0) |
| 13635 | return 0; |
| 13636 | |
| 13637 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13638 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13639 | |
| 13640 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13641 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13642 | else |
| 13643 | maxchar = writer->maxchar; |
| 13644 | len = end - start; |
| 13645 | |
| 13646 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13647 | return -1; |
| 13648 | |
| 13649 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13650 | str, start, len); |
| 13651 | writer->pos += len; |
| 13652 | return 0; |
| 13653 | } |
| 13654 | |
| 13655 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13656 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13657 | const char *ascii, Py_ssize_t len) |
| 13658 | { |
| 13659 | if (len == -1) |
| 13660 | len = strlen(ascii); |
| 13661 | |
| 13662 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13663 | |
| 13664 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13665 | PyObject *str; |
| 13666 | |
| 13667 | str = _PyUnicode_FromASCII(ascii, len); |
| 13668 | if (str == NULL) |
| 13669 | return -1; |
| 13670 | |
| 13671 | writer->readonly = 1; |
| 13672 | writer->buffer = str; |
| 13673 | _PyUnicodeWriter_Update(writer); |
| 13674 | writer->pos += len; |
| 13675 | return 0; |
| 13676 | } |
| 13677 | |
| 13678 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13679 | return -1; |
| 13680 | |
| 13681 | switch (writer->kind) |
| 13682 | { |
| 13683 | case PyUnicode_1BYTE_KIND: |
| 13684 | { |
| 13685 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13686 | Py_UCS1 *data = writer->data; |
| 13687 | |
| 13688 | Py_MEMCPY(data + writer->pos, str, len); |
| 13689 | break; |
| 13690 | } |
| 13691 | case PyUnicode_2BYTE_KIND: |
| 13692 | { |
| 13693 | _PyUnicode_CONVERT_BYTES( |
| 13694 | Py_UCS1, Py_UCS2, |
| 13695 | ascii, ascii + len, |
| 13696 | (Py_UCS2 *)writer->data + writer->pos); |
| 13697 | break; |
| 13698 | } |
| 13699 | case PyUnicode_4BYTE_KIND: |
| 13700 | { |
| 13701 | _PyUnicode_CONVERT_BYTES( |
| 13702 | Py_UCS1, Py_UCS4, |
| 13703 | ascii, ascii + len, |
| 13704 | (Py_UCS4 *)writer->data + writer->pos); |
| 13705 | break; |
| 13706 | } |
| 13707 | default: |
| 13708 | assert(0); |
| 13709 | } |
| 13710 | |
| 13711 | writer->pos += len; |
| 13712 | return 0; |
| 13713 | } |
| 13714 | |
| 13715 | int |
| 13716 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13717 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13718 | { |
| 13719 | Py_UCS4 maxchar; |
| 13720 | |
| 13721 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13722 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13723 | return -1; |
| 13724 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13725 | writer->pos += len; |
| 13726 | return 0; |
| 13727 | } |
| 13728 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13729 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13730 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13731 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13732 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13733 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13734 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13735 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13736 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13737 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13738 | str = writer->buffer; |
| 13739 | writer->buffer = NULL; |
| 13740 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13741 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13742 | } |
Victor Stinner | 6c2cdae | 2015-10-12 13:29:43 +0200 | [diff] [blame] | 13743 | if (writer->pos == 0) { |
| 13744 | Py_CLEAR(writer->buffer); |
| 13745 | |
| 13746 | /* Get the empty Unicode string singleton ('') */ |
| 13747 | _Py_INCREF_UNICODE_EMPTY(); |
| 13748 | str = unicode_empty; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13749 | } |
Victor Stinner | 6c2cdae | 2015-10-12 13:29:43 +0200 | [diff] [blame] | 13750 | else { |
| 13751 | str = writer->buffer; |
| 13752 | writer->buffer = NULL; |
| 13753 | |
| 13754 | if (PyUnicode_GET_LENGTH(str) != writer->pos) { |
| 13755 | PyObject *str2; |
| 13756 | str2 = resize_compact(str, writer->pos); |
| 13757 | if (str2 == NULL) |
| 13758 | return NULL; |
| 13759 | str = str2; |
| 13760 | } |
| 13761 | } |
| 13762 | |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13763 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13764 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13765 | } |
| 13766 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13767 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13768 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13769 | { |
| 13770 | Py_CLEAR(writer->buffer); |
| 13771 | } |
| 13772 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13773 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13774 | |
| 13775 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13776 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13777 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13778 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13779 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13780 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13781 | PyDoc_STRVAR(format_map__doc__, |
| 13782 | "S.format_map(mapping) -> str\n\ |
| 13783 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13784 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13785 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13786 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13787 | static PyObject * |
| 13788 | unicode__format__(PyObject* self, PyObject* args) |
| 13789 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13790 | PyObject *format_spec; |
| 13791 | _PyUnicodeWriter writer; |
| 13792 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13793 | |
| 13794 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13795 | return NULL; |
| 13796 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13797 | if (PyUnicode_READY(self) == -1) |
| 13798 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13799 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13800 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13801 | self, format_spec, 0, |
| 13802 | PyUnicode_GET_LENGTH(format_spec)); |
| 13803 | if (ret == -1) { |
| 13804 | _PyUnicodeWriter_Dealloc(&writer); |
| 13805 | return NULL; |
| 13806 | } |
| 13807 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13808 | } |
| 13809 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13810 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13811 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13812 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13813 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13814 | |
| 13815 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13816 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13817 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13818 | Py_ssize_t size; |
| 13819 | |
| 13820 | /* If it's a compact object, account for base structure + |
| 13821 | character data. */ |
| 13822 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13823 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13824 | else if (PyUnicode_IS_COMPACT(v)) |
| 13825 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13826 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13827 | else { |
| 13828 | /* If it is a two-block object, account for base object, and |
| 13829 | for character block if present. */ |
| 13830 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13831 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13832 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13833 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13834 | } |
| 13835 | /* 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] | 13836 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13837 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13838 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13839 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13840 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13841 | |
| 13842 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13843 | } |
| 13844 | |
| 13845 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13846 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13847 | |
| 13848 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13849 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13850 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13851 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13852 | if (!copy) |
| 13853 | return NULL; |
| 13854 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13855 | } |
| 13856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13857 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13858 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13859 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13860 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13861 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13862 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13863 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13864 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13865 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13866 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13867 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13868 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13869 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13870 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13871 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13872 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13873 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13874 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13875 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13876 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13877 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13878 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13879 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13880 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13881 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13882 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13883 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13884 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13885 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13886 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13887 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13888 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13889 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13890 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13891 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13892 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13893 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13894 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13895 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13896 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13897 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13898 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13899 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13900 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13901 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13902 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13903 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13904 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13905 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13906 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13907 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13908 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13909 | #endif |
| 13910 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13911 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13912 | {NULL, NULL} |
| 13913 | }; |
| 13914 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13915 | static PyObject * |
| 13916 | unicode_mod(PyObject *v, PyObject *w) |
| 13917 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13918 | if (!PyUnicode_Check(v)) |
| 13919 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13920 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13921 | } |
| 13922 | |
| 13923 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13924 | 0, /*nb_add*/ |
| 13925 | 0, /*nb_subtract*/ |
| 13926 | 0, /*nb_multiply*/ |
| 13927 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13928 | }; |
| 13929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13930 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13931 | (lenfunc) unicode_length, /* sq_length */ |
| 13932 | PyUnicode_Concat, /* sq_concat */ |
| 13933 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13934 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13935 | 0, /* sq_slice */ |
| 13936 | 0, /* sq_ass_item */ |
| 13937 | 0, /* sq_ass_slice */ |
| 13938 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13939 | }; |
| 13940 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13941 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13942 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13943 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13944 | if (PyUnicode_READY(self) == -1) |
| 13945 | return NULL; |
| 13946 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13947 | if (PyIndex_Check(item)) { |
| 13948 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13949 | if (i == -1 && PyErr_Occurred()) |
| 13950 | return NULL; |
| 13951 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13952 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13953 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13954 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13955 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13956 | PyObject *result; |
| 13957 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13958 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13959 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13960 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13961 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13962 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13963 | return NULL; |
| 13964 | } |
| 13965 | |
| 13966 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13967 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13968 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13969 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13970 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13971 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13972 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13973 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13974 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13975 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13976 | src_kind = PyUnicode_KIND(self); |
| 13977 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13978 | if (!PyUnicode_IS_ASCII(self)) { |
| 13979 | kind_limit = kind_maxchar_limit(src_kind); |
| 13980 | max_char = 0; |
| 13981 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13982 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13983 | if (ch > max_char) { |
| 13984 | max_char = ch; |
| 13985 | if (max_char >= kind_limit) |
| 13986 | break; |
| 13987 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13988 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13989 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13990 | else |
| 13991 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13992 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13993 | if (result == NULL) |
| 13994 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13995 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13996 | dest_data = PyUnicode_DATA(result); |
| 13997 | |
| 13998 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13999 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 14000 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 14001 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14002 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 14003 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 14004 | } else { |
| 14005 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 14006 | return NULL; |
| 14007 | } |
| 14008 | } |
| 14009 | |
| 14010 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14011 | (lenfunc)unicode_length, /* mp_length */ |
| 14012 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 14013 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 14014 | }; |
| 14015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14017 | /* Helpers for PyUnicode_Format() */ |
| 14018 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14019 | struct unicode_formatter_t { |
| 14020 | PyObject *args; |
| 14021 | int args_owned; |
| 14022 | Py_ssize_t arglen, argidx; |
| 14023 | PyObject *dict; |
| 14024 | |
| 14025 | enum PyUnicode_Kind fmtkind; |
| 14026 | Py_ssize_t fmtcnt, fmtpos; |
| 14027 | void *fmtdata; |
| 14028 | PyObject *fmtstr; |
| 14029 | |
| 14030 | _PyUnicodeWriter writer; |
| 14031 | }; |
| 14032 | |
| 14033 | struct unicode_format_arg_t { |
| 14034 | Py_UCS4 ch; |
| 14035 | int flags; |
| 14036 | Py_ssize_t width; |
| 14037 | int prec; |
| 14038 | int sign; |
| 14039 | }; |
| 14040 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14041 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14042 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14043 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14044 | Py_ssize_t argidx = ctx->argidx; |
| 14045 | |
| 14046 | if (argidx < ctx->arglen) { |
| 14047 | ctx->argidx++; |
| 14048 | if (ctx->arglen < 0) |
| 14049 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14050 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14051 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14052 | } |
| 14053 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14054 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14055 | return NULL; |
| 14056 | } |
| 14057 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 14058 | /* 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] | 14059 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14060 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 14061 | otherwise. |
| 14062 | |
| 14063 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14064 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14065 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 14066 | PyObject **p_output, |
| 14067 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14068 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 14069 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14070 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14071 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14072 | int prec; |
| 14073 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14075 | x = PyFloat_AsDouble(v); |
| 14076 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14077 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 14078 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14079 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14080 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14081 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 14082 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14083 | if (arg->flags & F_ALT) |
| 14084 | dtoa_flags = Py_DTSF_ALT; |
| 14085 | else |
| 14086 | dtoa_flags = 0; |
| 14087 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 14088 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14089 | return -1; |
| 14090 | len = strlen(p); |
| 14091 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 14092 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 14093 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14094 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 14095 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14096 | } |
| 14097 | else |
| 14098 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 14099 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14100 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14101 | } |
| 14102 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14103 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 14104 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 14105 | * Python's regular ints. |
| 14106 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 14107 | * The output string is of the form |
| 14108 | * "-"? ("0x" | "0X")? digit+ |
| 14109 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 14110 | * set in flags. The case of hex digits will be correct, |
| 14111 | * There will be at least prec digits, zero-filled on the left if |
| 14112 | * necessary to get that many. |
| 14113 | * val object to be converted |
| 14114 | * flags bitmask of format flags; only F_ALT is looked at |
| 14115 | * prec minimum number of digits; 0-fill on left if needed |
| 14116 | * type a character in [duoxX]; u acts the same as d |
| 14117 | * |
| 14118 | * CAUTION: o, x and X conversions on regular ints can never |
| 14119 | * produce a '-' sign, but can for Python's unbounded ints. |
| 14120 | */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14121 | PyObject * |
| 14122 | _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14123 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14124 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14125 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14126 | Py_ssize_t i; |
| 14127 | int sign; /* 1 if '-', else 0 */ |
| 14128 | int len; /* number of characters */ |
| 14129 | Py_ssize_t llen; |
| 14130 | int numdigits; /* len == numnondigits + numdigits */ |
| 14131 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14132 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14133 | /* Avoid exceeding SSIZE_T_MAX */ |
| 14134 | if (prec > INT_MAX-3) { |
| 14135 | PyErr_SetString(PyExc_OverflowError, |
| 14136 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14137 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14138 | } |
| 14139 | |
| 14140 | assert(PyLong_Check(val)); |
| 14141 | |
| 14142 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14143 | default: |
| 14144 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14145 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14146 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14147 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 14148 | /* int and int subclasses should print numerically when a numeric */ |
| 14149 | /* format code is used (see issue18780) */ |
| 14150 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14151 | break; |
| 14152 | case 'o': |
| 14153 | numnondigits = 2; |
| 14154 | result = PyNumber_ToBase(val, 8); |
| 14155 | break; |
| 14156 | case 'x': |
| 14157 | case 'X': |
| 14158 | numnondigits = 2; |
| 14159 | result = PyNumber_ToBase(val, 16); |
| 14160 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14161 | } |
| 14162 | if (!result) |
| 14163 | return NULL; |
| 14164 | |
| 14165 | assert(unicode_modifiable(result)); |
| 14166 | assert(PyUnicode_IS_READY(result)); |
| 14167 | assert(PyUnicode_IS_ASCII(result)); |
| 14168 | |
| 14169 | /* To modify the string in-place, there can only be one reference. */ |
| 14170 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 14171 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14172 | PyErr_BadInternalCall(); |
| 14173 | return NULL; |
| 14174 | } |
| 14175 | buf = PyUnicode_DATA(result); |
| 14176 | llen = PyUnicode_GET_LENGTH(result); |
| 14177 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 14178 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14179 | PyErr_SetString(PyExc_ValueError, |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14180 | "string too large in _PyUnicode_FormatLong"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14181 | return NULL; |
| 14182 | } |
| 14183 | len = (int)llen; |
| 14184 | sign = buf[0] == '-'; |
| 14185 | numnondigits += sign; |
| 14186 | numdigits = len - numnondigits; |
| 14187 | assert(numdigits > 0); |
| 14188 | |
| 14189 | /* Get rid of base marker unless F_ALT */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14190 | if (((alt) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14191 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 14192 | assert(buf[sign] == '0'); |
| 14193 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 14194 | buf[sign+1] == 'o'); |
| 14195 | numnondigits -= 2; |
| 14196 | buf += 2; |
| 14197 | len -= 2; |
| 14198 | if (sign) |
| 14199 | buf[0] = '-'; |
| 14200 | assert(len == numnondigits + numdigits); |
| 14201 | assert(numdigits > 0); |
| 14202 | } |
| 14203 | |
| 14204 | /* Fill with leading zeroes to meet minimum width. */ |
| 14205 | if (prec > numdigits) { |
| 14206 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 14207 | numnondigits + prec); |
| 14208 | char *b1; |
| 14209 | if (!r1) { |
| 14210 | Py_DECREF(result); |
| 14211 | return NULL; |
| 14212 | } |
| 14213 | b1 = PyBytes_AS_STRING(r1); |
| 14214 | for (i = 0; i < numnondigits; ++i) |
| 14215 | *b1++ = *buf++; |
| 14216 | for (i = 0; i < prec - numdigits; i++) |
| 14217 | *b1++ = '0'; |
| 14218 | for (i = 0; i < numdigits; i++) |
| 14219 | *b1++ = *buf++; |
| 14220 | *b1 = '\0'; |
| 14221 | Py_DECREF(result); |
| 14222 | result = r1; |
| 14223 | buf = PyBytes_AS_STRING(result); |
| 14224 | len = numnondigits + prec; |
| 14225 | } |
| 14226 | |
| 14227 | /* Fix up case for hex conversions. */ |
| 14228 | if (type == 'X') { |
| 14229 | /* Need to convert all lower case letters to upper case. |
| 14230 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 14231 | for (i = 0; i < len; i++) |
| 14232 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 14233 | buf[i] -= 'a'-'A'; |
| 14234 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14235 | if (!PyUnicode_Check(result) |
| 14236 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14237 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14238 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14239 | Py_DECREF(result); |
| 14240 | result = unicode; |
| 14241 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14242 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 14243 | if (PyUnicode_Resize(&result, len) < 0) |
| 14244 | Py_CLEAR(result); |
| 14245 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14246 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14247 | } |
| 14248 | |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14249 | /* Format an integer or a float as an integer. |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14250 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14251 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14252 | * -1 and raise an exception on error */ |
| 14253 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14254 | mainformatlong(PyObject *v, |
| 14255 | struct unicode_format_arg_t *arg, |
| 14256 | PyObject **p_output, |
| 14257 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14258 | { |
| 14259 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14260 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14261 | |
| 14262 | if (!PyNumber_Check(v)) |
| 14263 | goto wrongtype; |
| 14264 | |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14265 | /* 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] | 14266 | if (!PyLong_Check(v)) { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14267 | if (type == 'o' || type == 'x' || type == 'X') { |
| 14268 | iobj = PyNumber_Index(v); |
| 14269 | if (iobj == NULL) { |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14270 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14271 | goto wrongtype; |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14272 | return -1; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14273 | } |
| 14274 | } |
| 14275 | else { |
| 14276 | iobj = PyNumber_Long(v); |
| 14277 | if (iobj == NULL ) { |
| 14278 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14279 | goto wrongtype; |
| 14280 | return -1; |
| 14281 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14282 | } |
| 14283 | assert(PyLong_Check(iobj)); |
| 14284 | } |
| 14285 | else { |
| 14286 | iobj = v; |
| 14287 | Py_INCREF(iobj); |
| 14288 | } |
| 14289 | |
| 14290 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14291 | && arg->width == -1 && arg->prec == -1 |
| 14292 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14293 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14294 | { |
| 14295 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14296 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14297 | int base; |
| 14298 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14299 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14300 | { |
| 14301 | default: |
| 14302 | assert(0 && "'type' not in [diuoxX]"); |
| 14303 | case 'd': |
| 14304 | case 'i': |
| 14305 | case 'u': |
| 14306 | base = 10; |
| 14307 | break; |
| 14308 | case 'o': |
| 14309 | base = 8; |
| 14310 | break; |
| 14311 | case 'x': |
| 14312 | case 'X': |
| 14313 | base = 16; |
| 14314 | break; |
| 14315 | } |
| 14316 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14317 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14318 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14319 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14320 | } |
| 14321 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14322 | return 1; |
| 14323 | } |
| 14324 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14325 | res = _PyUnicode_FormatLong(iobj, arg->flags & F_ALT, arg->prec, type); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14326 | Py_DECREF(iobj); |
| 14327 | if (res == NULL) |
| 14328 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14329 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14330 | return 0; |
| 14331 | |
| 14332 | wrongtype: |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14333 | switch(type) |
| 14334 | { |
| 14335 | case 'o': |
| 14336 | case 'x': |
| 14337 | case 'X': |
| 14338 | PyErr_Format(PyExc_TypeError, |
| 14339 | "%%%c format: an integer is required, " |
| 14340 | "not %.200s", |
| 14341 | type, Py_TYPE(v)->tp_name); |
| 14342 | break; |
| 14343 | default: |
| 14344 | PyErr_Format(PyExc_TypeError, |
| 14345 | "%%%c format: a number is required, " |
| 14346 | "not %.200s", |
| 14347 | type, Py_TYPE(v)->tp_name); |
| 14348 | break; |
| 14349 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14350 | return -1; |
| 14351 | } |
| 14352 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14353 | static Py_UCS4 |
| 14354 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14355 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14356 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14357 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14358 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14359 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14360 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14361 | goto onError; |
| 14362 | } |
| 14363 | else { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14364 | PyObject *iobj; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14365 | long x; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14366 | /* make sure number is a type of integer */ |
| 14367 | if (!PyLong_Check(v)) { |
| 14368 | iobj = PyNumber_Index(v); |
| 14369 | if (iobj == NULL) { |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14370 | goto onError; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14371 | } |
| 14372 | v = iobj; |
| 14373 | Py_DECREF(iobj); |
| 14374 | } |
| 14375 | /* Integer input truncated to a character */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14376 | x = PyLong_AsLong(v); |
| 14377 | if (x == -1 && PyErr_Occurred()) |
| 14378 | goto onError; |
| 14379 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14380 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14381 | PyErr_SetString(PyExc_OverflowError, |
| 14382 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14383 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14384 | } |
| 14385 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14386 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14387 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14388 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14389 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14390 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14391 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14392 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14393 | } |
| 14394 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14395 | /* Parse options of an argument: flags, width, precision. |
| 14396 | Handle also "%(name)" syntax. |
| 14397 | |
| 14398 | Return 0 if the argument has been formatted into arg->str. |
| 14399 | Return 1 if the argument has been written into ctx->writer, |
| 14400 | Raise an exception and return -1 on error. */ |
| 14401 | static int |
| 14402 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14403 | struct unicode_format_arg_t *arg) |
| 14404 | { |
| 14405 | #define FORMAT_READ(ctx) \ |
| 14406 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14407 | |
| 14408 | PyObject *v; |
| 14409 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14410 | if (arg->ch == '(') { |
| 14411 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14412 | Py_ssize_t keystart; |
| 14413 | Py_ssize_t keylen; |
| 14414 | PyObject *key; |
| 14415 | int pcount = 1; |
| 14416 | |
| 14417 | if (ctx->dict == NULL) { |
| 14418 | PyErr_SetString(PyExc_TypeError, |
| 14419 | "format requires a mapping"); |
| 14420 | return -1; |
| 14421 | } |
| 14422 | ++ctx->fmtpos; |
| 14423 | --ctx->fmtcnt; |
| 14424 | keystart = ctx->fmtpos; |
| 14425 | /* Skip over balanced parentheses */ |
| 14426 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14427 | arg->ch = FORMAT_READ(ctx); |
| 14428 | if (arg->ch == ')') |
| 14429 | --pcount; |
| 14430 | else if (arg->ch == '(') |
| 14431 | ++pcount; |
| 14432 | ctx->fmtpos++; |
| 14433 | } |
| 14434 | keylen = ctx->fmtpos - keystart - 1; |
| 14435 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14436 | PyErr_SetString(PyExc_ValueError, |
| 14437 | "incomplete format key"); |
| 14438 | return -1; |
| 14439 | } |
| 14440 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14441 | keystart, keystart + keylen); |
| 14442 | if (key == NULL) |
| 14443 | return -1; |
| 14444 | if (ctx->args_owned) { |
| 14445 | Py_DECREF(ctx->args); |
| 14446 | ctx->args_owned = 0; |
| 14447 | } |
| 14448 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14449 | Py_DECREF(key); |
| 14450 | if (ctx->args == NULL) |
| 14451 | return -1; |
| 14452 | ctx->args_owned = 1; |
| 14453 | ctx->arglen = -1; |
| 14454 | ctx->argidx = -2; |
| 14455 | } |
| 14456 | |
| 14457 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14458 | while (--ctx->fmtcnt >= 0) { |
| 14459 | arg->ch = FORMAT_READ(ctx); |
| 14460 | ctx->fmtpos++; |
| 14461 | switch (arg->ch) { |
| 14462 | case '-': arg->flags |= F_LJUST; continue; |
| 14463 | case '+': arg->flags |= F_SIGN; continue; |
| 14464 | case ' ': arg->flags |= F_BLANK; continue; |
| 14465 | case '#': arg->flags |= F_ALT; continue; |
| 14466 | case '0': arg->flags |= F_ZERO; continue; |
| 14467 | } |
| 14468 | break; |
| 14469 | } |
| 14470 | |
| 14471 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14472 | if (arg->ch == '*') { |
| 14473 | v = unicode_format_getnextarg(ctx); |
| 14474 | if (v == NULL) |
| 14475 | return -1; |
| 14476 | if (!PyLong_Check(v)) { |
| 14477 | PyErr_SetString(PyExc_TypeError, |
| 14478 | "* wants int"); |
| 14479 | return -1; |
| 14480 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14481 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14482 | if (arg->width == -1 && PyErr_Occurred()) |
| 14483 | return -1; |
| 14484 | if (arg->width < 0) { |
| 14485 | arg->flags |= F_LJUST; |
| 14486 | arg->width = -arg->width; |
| 14487 | } |
| 14488 | if (--ctx->fmtcnt >= 0) { |
| 14489 | arg->ch = FORMAT_READ(ctx); |
| 14490 | ctx->fmtpos++; |
| 14491 | } |
| 14492 | } |
| 14493 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14494 | arg->width = arg->ch - '0'; |
| 14495 | while (--ctx->fmtcnt >= 0) { |
| 14496 | arg->ch = FORMAT_READ(ctx); |
| 14497 | ctx->fmtpos++; |
| 14498 | if (arg->ch < '0' || arg->ch > '9') |
| 14499 | break; |
| 14500 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14501 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14502 | '0' and '9', casting to int is safe. */ |
| 14503 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14504 | PyErr_SetString(PyExc_ValueError, |
| 14505 | "width too big"); |
| 14506 | return -1; |
| 14507 | } |
| 14508 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14509 | } |
| 14510 | } |
| 14511 | |
| 14512 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14513 | if (arg->ch == '.') { |
| 14514 | arg->prec = 0; |
| 14515 | if (--ctx->fmtcnt >= 0) { |
| 14516 | arg->ch = FORMAT_READ(ctx); |
| 14517 | ctx->fmtpos++; |
| 14518 | } |
| 14519 | if (arg->ch == '*') { |
| 14520 | v = unicode_format_getnextarg(ctx); |
| 14521 | if (v == NULL) |
| 14522 | return -1; |
| 14523 | if (!PyLong_Check(v)) { |
| 14524 | PyErr_SetString(PyExc_TypeError, |
| 14525 | "* wants int"); |
| 14526 | return -1; |
| 14527 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14528 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14529 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14530 | return -1; |
| 14531 | if (arg->prec < 0) |
| 14532 | arg->prec = 0; |
| 14533 | if (--ctx->fmtcnt >= 0) { |
| 14534 | arg->ch = FORMAT_READ(ctx); |
| 14535 | ctx->fmtpos++; |
| 14536 | } |
| 14537 | } |
| 14538 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14539 | arg->prec = arg->ch - '0'; |
| 14540 | while (--ctx->fmtcnt >= 0) { |
| 14541 | arg->ch = FORMAT_READ(ctx); |
| 14542 | ctx->fmtpos++; |
| 14543 | if (arg->ch < '0' || arg->ch > '9') |
| 14544 | break; |
| 14545 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14546 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14547 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14548 | return -1; |
| 14549 | } |
| 14550 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14551 | } |
| 14552 | } |
| 14553 | } |
| 14554 | |
| 14555 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14556 | if (ctx->fmtcnt >= 0) { |
| 14557 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14558 | if (--ctx->fmtcnt >= 0) { |
| 14559 | arg->ch = FORMAT_READ(ctx); |
| 14560 | ctx->fmtpos++; |
| 14561 | } |
| 14562 | } |
| 14563 | } |
| 14564 | if (ctx->fmtcnt < 0) { |
| 14565 | PyErr_SetString(PyExc_ValueError, |
| 14566 | "incomplete format"); |
| 14567 | return -1; |
| 14568 | } |
| 14569 | return 0; |
| 14570 | |
| 14571 | #undef FORMAT_READ |
| 14572 | } |
| 14573 | |
| 14574 | /* Format one argument. Supported conversion specifiers: |
| 14575 | |
| 14576 | - "s", "r", "a": any type |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14577 | - "i", "d", "u": int or float |
| 14578 | - "o", "x", "X": int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14579 | - "e", "E", "f", "F", "g", "G": float |
| 14580 | - "c": int or str (1 character) |
| 14581 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14582 | When possible, the output is written directly into the Unicode writer |
| 14583 | (ctx->writer). A string is created when padding is required. |
| 14584 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14585 | Return 0 if the argument has been formatted into *p_str, |
| 14586 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14587 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14588 | static int |
| 14589 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14590 | struct unicode_format_arg_t *arg, |
| 14591 | PyObject **p_str) |
| 14592 | { |
| 14593 | PyObject *v; |
| 14594 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14595 | |
| 14596 | if (ctx->fmtcnt == 0) |
| 14597 | ctx->writer.overallocate = 0; |
| 14598 | |
| 14599 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14600 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14601 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14602 | return 1; |
| 14603 | } |
| 14604 | |
| 14605 | v = unicode_format_getnextarg(ctx); |
| 14606 | if (v == NULL) |
| 14607 | return -1; |
| 14608 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14609 | |
| 14610 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14611 | case 's': |
| 14612 | case 'r': |
| 14613 | case 'a': |
| 14614 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14615 | /* Fast path */ |
| 14616 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14617 | return -1; |
| 14618 | return 1; |
| 14619 | } |
| 14620 | |
| 14621 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14622 | *p_str = v; |
| 14623 | Py_INCREF(*p_str); |
| 14624 | } |
| 14625 | else { |
| 14626 | if (arg->ch == 's') |
| 14627 | *p_str = PyObject_Str(v); |
| 14628 | else if (arg->ch == 'r') |
| 14629 | *p_str = PyObject_Repr(v); |
| 14630 | else |
| 14631 | *p_str = PyObject_ASCII(v); |
| 14632 | } |
| 14633 | break; |
| 14634 | |
| 14635 | case 'i': |
| 14636 | case 'd': |
| 14637 | case 'u': |
| 14638 | case 'o': |
| 14639 | case 'x': |
| 14640 | case 'X': |
| 14641 | { |
| 14642 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14643 | if (ret != 0) |
| 14644 | return ret; |
| 14645 | arg->sign = 1; |
| 14646 | break; |
| 14647 | } |
| 14648 | |
| 14649 | case 'e': |
| 14650 | case 'E': |
| 14651 | case 'f': |
| 14652 | case 'F': |
| 14653 | case 'g': |
| 14654 | case 'G': |
| 14655 | if (arg->width == -1 && arg->prec == -1 |
| 14656 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14657 | { |
| 14658 | /* Fast path */ |
| 14659 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14660 | return -1; |
| 14661 | return 1; |
| 14662 | } |
| 14663 | |
| 14664 | arg->sign = 1; |
| 14665 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14666 | return -1; |
| 14667 | break; |
| 14668 | |
| 14669 | case 'c': |
| 14670 | { |
| 14671 | Py_UCS4 ch = formatchar(v); |
| 14672 | if (ch == (Py_UCS4) -1) |
| 14673 | return -1; |
| 14674 | if (arg->width == -1 && arg->prec == -1) { |
| 14675 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14676 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14677 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14678 | return 1; |
| 14679 | } |
| 14680 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14681 | break; |
| 14682 | } |
| 14683 | |
| 14684 | default: |
| 14685 | PyErr_Format(PyExc_ValueError, |
| 14686 | "unsupported format character '%c' (0x%x) " |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 14687 | "at index %zd", |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14688 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14689 | (int)arg->ch, |
| 14690 | ctx->fmtpos - 1); |
| 14691 | return -1; |
| 14692 | } |
| 14693 | if (*p_str == NULL) |
| 14694 | return -1; |
| 14695 | assert (PyUnicode_Check(*p_str)); |
| 14696 | return 0; |
| 14697 | } |
| 14698 | |
| 14699 | static int |
| 14700 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14701 | struct unicode_format_arg_t *arg, |
| 14702 | PyObject *str) |
| 14703 | { |
| 14704 | Py_ssize_t len; |
| 14705 | enum PyUnicode_Kind kind; |
| 14706 | void *pbuf; |
| 14707 | Py_ssize_t pindex; |
| 14708 | Py_UCS4 signchar; |
| 14709 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14710 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14711 | Py_ssize_t sublen; |
| 14712 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14713 | Py_UCS4 fill; |
| 14714 | |
| 14715 | fill = ' '; |
| 14716 | if (arg->sign && arg->flags & F_ZERO) |
| 14717 | fill = '0'; |
| 14718 | |
| 14719 | if (PyUnicode_READY(str) == -1) |
| 14720 | return -1; |
| 14721 | |
| 14722 | len = PyUnicode_GET_LENGTH(str); |
| 14723 | if ((arg->width == -1 || arg->width <= len) |
| 14724 | && (arg->prec == -1 || arg->prec >= len) |
| 14725 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14726 | { |
| 14727 | /* Fast path */ |
| 14728 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14729 | return -1; |
| 14730 | return 0; |
| 14731 | } |
| 14732 | |
| 14733 | /* Truncate the string for "s", "r" and "a" formats |
| 14734 | if the precision is set */ |
| 14735 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14736 | if (arg->prec >= 0 && len > arg->prec) |
| 14737 | len = arg->prec; |
| 14738 | } |
| 14739 | |
| 14740 | /* Adjust sign and width */ |
| 14741 | kind = PyUnicode_KIND(str); |
| 14742 | pbuf = PyUnicode_DATA(str); |
| 14743 | pindex = 0; |
| 14744 | signchar = '\0'; |
| 14745 | if (arg->sign) { |
| 14746 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14747 | if (ch == '-' || ch == '+') { |
| 14748 | signchar = ch; |
| 14749 | len--; |
| 14750 | pindex++; |
| 14751 | } |
| 14752 | else if (arg->flags & F_SIGN) |
| 14753 | signchar = '+'; |
| 14754 | else if (arg->flags & F_BLANK) |
| 14755 | signchar = ' '; |
| 14756 | else |
| 14757 | arg->sign = 0; |
| 14758 | } |
| 14759 | if (arg->width < len) |
| 14760 | arg->width = len; |
| 14761 | |
| 14762 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14763 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14764 | if (!(arg->flags & F_LJUST)) { |
| 14765 | if (arg->sign) { |
| 14766 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14767 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14768 | } |
| 14769 | else { |
| 14770 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14771 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14772 | } |
| 14773 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14774 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14775 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14776 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14777 | } |
| 14778 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14779 | buflen = arg->width; |
| 14780 | if (arg->sign && len == arg->width) |
| 14781 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14782 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14783 | return -1; |
| 14784 | |
| 14785 | /* Write the sign if needed */ |
| 14786 | if (arg->sign) { |
| 14787 | if (fill != ' ') { |
| 14788 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14789 | writer->pos += 1; |
| 14790 | } |
| 14791 | if (arg->width > len) |
| 14792 | arg->width--; |
| 14793 | } |
| 14794 | |
| 14795 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14796 | if the alternate form is used. |
| 14797 | For example, write "0x" for the "%#x" format. */ |
| 14798 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14799 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14800 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14801 | if (fill != ' ') { |
| 14802 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14803 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14804 | writer->pos += 2; |
| 14805 | pindex += 2; |
| 14806 | } |
| 14807 | arg->width -= 2; |
| 14808 | if (arg->width < 0) |
| 14809 | arg->width = 0; |
| 14810 | len -= 2; |
| 14811 | } |
| 14812 | |
| 14813 | /* Pad left with the fill character if needed */ |
| 14814 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14815 | sublen = arg->width - len; |
| 14816 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14817 | writer->pos += sublen; |
| 14818 | arg->width = len; |
| 14819 | } |
| 14820 | |
| 14821 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14822 | the alternate form is used */ |
| 14823 | if (fill == ' ') { |
| 14824 | if (arg->sign) { |
| 14825 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14826 | writer->pos += 1; |
| 14827 | } |
| 14828 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14829 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14830 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14831 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14832 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14833 | writer->pos += 2; |
| 14834 | pindex += 2; |
| 14835 | } |
| 14836 | } |
| 14837 | |
| 14838 | /* Write characters */ |
| 14839 | if (len) { |
| 14840 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14841 | str, pindex, len); |
| 14842 | writer->pos += len; |
| 14843 | } |
| 14844 | |
| 14845 | /* Pad right with the fill character if needed */ |
| 14846 | if (arg->width > len) { |
| 14847 | sublen = arg->width - len; |
| 14848 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14849 | writer->pos += sublen; |
| 14850 | } |
| 14851 | return 0; |
| 14852 | } |
| 14853 | |
| 14854 | /* Helper of PyUnicode_Format(): format one arg. |
| 14855 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14856 | static int |
| 14857 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14858 | { |
| 14859 | struct unicode_format_arg_t arg; |
| 14860 | PyObject *str; |
| 14861 | int ret; |
| 14862 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14863 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14864 | arg.flags = 0; |
| 14865 | arg.width = -1; |
| 14866 | arg.prec = -1; |
| 14867 | arg.sign = 0; |
| 14868 | str = NULL; |
| 14869 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14870 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14871 | if (ret == -1) |
| 14872 | return -1; |
| 14873 | |
| 14874 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14875 | if (ret == -1) |
| 14876 | return -1; |
| 14877 | |
| 14878 | if (ret != 1) { |
| 14879 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14880 | Py_DECREF(str); |
| 14881 | if (ret == -1) |
| 14882 | return -1; |
| 14883 | } |
| 14884 | |
| 14885 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14886 | PyErr_SetString(PyExc_TypeError, |
| 14887 | "not all arguments converted during string formatting"); |
| 14888 | return -1; |
| 14889 | } |
| 14890 | return 0; |
| 14891 | } |
| 14892 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14893 | PyObject * |
| 14894 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14895 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14896 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14898 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14899 | PyErr_BadInternalCall(); |
| 14900 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14901 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14902 | |
| 14903 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14904 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14905 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14906 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14907 | Py_DECREF(ctx.fmtstr); |
| 14908 | return NULL; |
| 14909 | } |
| 14910 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14911 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14912 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14913 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14914 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14915 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14916 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14917 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14919 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14920 | ctx.arglen = PyTuple_Size(args); |
| 14921 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14922 | } |
| 14923 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14924 | ctx.arglen = -1; |
| 14925 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14926 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14927 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14928 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14929 | ctx.dict = args; |
| 14930 | else |
| 14931 | ctx.dict = NULL; |
| 14932 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14933 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14934 | while (--ctx.fmtcnt >= 0) { |
| 14935 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14936 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14937 | |
| 14938 | nonfmtpos = ctx.fmtpos++; |
| 14939 | while (ctx.fmtcnt >= 0 && |
| 14940 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14941 | ctx.fmtpos++; |
| 14942 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14943 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14944 | if (ctx.fmtcnt < 0) { |
| 14945 | ctx.fmtpos--; |
| 14946 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14947 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14948 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14949 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14950 | nonfmtpos, ctx.fmtpos) < 0) |
| 14951 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14952 | } |
| 14953 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14954 | ctx.fmtpos++; |
| 14955 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14956 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14957 | } |
| 14958 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14959 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14960 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14961 | PyErr_SetString(PyExc_TypeError, |
| 14962 | "not all arguments converted during string formatting"); |
| 14963 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14964 | } |
| 14965 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14966 | if (ctx.args_owned) { |
| 14967 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14968 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14969 | Py_DECREF(ctx.fmtstr); |
| 14970 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14971 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14972 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14973 | Py_DECREF(ctx.fmtstr); |
| 14974 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14975 | if (ctx.args_owned) { |
| 14976 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14977 | } |
| 14978 | return NULL; |
| 14979 | } |
| 14980 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14981 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14982 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14983 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14984 | static PyObject * |
| 14985 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14986 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14987 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14988 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14989 | char *encoding = NULL; |
| 14990 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14991 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14992 | if (type != &PyUnicode_Type) |
| 14993 | return unicode_subtype_new(type, args, kwds); |
| 14994 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14995 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14996 | return NULL; |
| 14997 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14998 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14999 | if (encoding == NULL && errors == NULL) |
| 15000 | return PyObject_Str(x); |
| 15001 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15002 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 15003 | } |
| 15004 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 15005 | static PyObject * |
| 15006 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 15007 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15008 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15009 | Py_ssize_t length, char_size; |
| 15010 | int share_wstr, share_utf8; |
| 15011 | unsigned int kind; |
| 15012 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 15013 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15014 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15015 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15016 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15017 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15018 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15019 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 15020 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 15021 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15022 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 15023 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15024 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15025 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15026 | if (self == NULL) { |
| 15027 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15028 | return NULL; |
| 15029 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15030 | kind = PyUnicode_KIND(unicode); |
| 15031 | length = PyUnicode_GET_LENGTH(unicode); |
| 15032 | |
| 15033 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 15034 | #ifdef Py_DEBUG |
| 15035 | _PyUnicode_HASH(self) = -1; |
| 15036 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15037 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 15038 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15039 | _PyUnicode_STATE(self).interned = 0; |
| 15040 | _PyUnicode_STATE(self).kind = kind; |
| 15041 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 15042 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15043 | _PyUnicode_STATE(self).ready = 1; |
| 15044 | _PyUnicode_WSTR(self) = NULL; |
| 15045 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 15046 | _PyUnicode_UTF8(self) = NULL; |
| 15047 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 15048 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15049 | |
| 15050 | share_utf8 = 0; |
| 15051 | share_wstr = 0; |
| 15052 | if (kind == PyUnicode_1BYTE_KIND) { |
| 15053 | char_size = 1; |
| 15054 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 15055 | share_utf8 = 1; |
| 15056 | } |
| 15057 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 15058 | char_size = 2; |
| 15059 | if (sizeof(wchar_t) == 2) |
| 15060 | share_wstr = 1; |
| 15061 | } |
| 15062 | else { |
| 15063 | assert(kind == PyUnicode_4BYTE_KIND); |
| 15064 | char_size = 4; |
| 15065 | if (sizeof(wchar_t) == 4) |
| 15066 | share_wstr = 1; |
| 15067 | } |
| 15068 | |
| 15069 | /* Ensure we won't overflow the length. */ |
| 15070 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 15071 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15072 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15073 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15074 | data = PyObject_MALLOC((length + 1) * char_size); |
| 15075 | if (data == NULL) { |
| 15076 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15077 | goto onError; |
| 15078 | } |
| 15079 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 15080 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15081 | if (share_utf8) { |
| 15082 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 15083 | _PyUnicode_UTF8(self) = data; |
| 15084 | } |
| 15085 | if (share_wstr) { |
| 15086 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 15087 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 15088 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15089 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15090 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 15091 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 15092 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 15093 | #ifdef Py_DEBUG |
| 15094 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 15095 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 15096 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15097 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15098 | |
| 15099 | onError: |
| 15100 | Py_DECREF(unicode); |
| 15101 | Py_DECREF(self); |
| 15102 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 15103 | } |
| 15104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 15105 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 15106 | "str(object='') -> str\n\ |
| 15107 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 15108 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 15109 | Create a new string object from the given object. If encoding or\n\ |
| 15110 | errors is specified, then the object must expose a data buffer\n\ |
| 15111 | that will be decoded using the given encoding and error handler.\n\ |
| 15112 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 15113 | or repr(object).\n\ |
| 15114 | encoding defaults to sys.getdefaultencoding().\n\ |
| 15115 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 15116 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15117 | static PyObject *unicode_iter(PyObject *seq); |
| 15118 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15119 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 15120 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15121 | "str", /* tp_name */ |
| 15122 | sizeof(PyUnicodeObject), /* tp_size */ |
| 15123 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15124 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15125 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 15126 | 0, /* tp_print */ |
| 15127 | 0, /* tp_getattr */ |
| 15128 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15129 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15130 | unicode_repr, /* tp_repr */ |
| 15131 | &unicode_as_number, /* tp_as_number */ |
| 15132 | &unicode_as_sequence, /* tp_as_sequence */ |
| 15133 | &unicode_as_mapping, /* tp_as_mapping */ |
| 15134 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 15135 | 0, /* tp_call*/ |
| 15136 | (reprfunc) unicode_str, /* tp_str */ |
| 15137 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15138 | 0, /* tp_setattro */ |
| 15139 | 0, /* tp_as_buffer */ |
| 15140 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15141 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15142 | unicode_doc, /* tp_doc */ |
| 15143 | 0, /* tp_traverse */ |
| 15144 | 0, /* tp_clear */ |
| 15145 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 15146 | 0, /* tp_weaklistoffset */ |
| 15147 | unicode_iter, /* tp_iter */ |
| 15148 | 0, /* tp_iternext */ |
| 15149 | unicode_methods, /* tp_methods */ |
| 15150 | 0, /* tp_members */ |
| 15151 | 0, /* tp_getset */ |
| 15152 | &PyBaseObject_Type, /* tp_base */ |
| 15153 | 0, /* tp_dict */ |
| 15154 | 0, /* tp_descr_get */ |
| 15155 | 0, /* tp_descr_set */ |
| 15156 | 0, /* tp_dictoffset */ |
| 15157 | 0, /* tp_init */ |
| 15158 | 0, /* tp_alloc */ |
| 15159 | unicode_new, /* tp_new */ |
| 15160 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15161 | }; |
| 15162 | |
| 15163 | /* Initialize the Unicode implementation */ |
| 15164 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15165 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15166 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15167 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15168 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15169 | 0x000A, /* LINE FEED */ |
| 15170 | 0x000D, /* CARRIAGE RETURN */ |
| 15171 | 0x001C, /* FILE SEPARATOR */ |
| 15172 | 0x001D, /* GROUP SEPARATOR */ |
| 15173 | 0x001E, /* RECORD SEPARATOR */ |
| 15174 | 0x0085, /* NEXT LINE */ |
| 15175 | 0x2028, /* LINE SEPARATOR */ |
| 15176 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 15177 | }; |
| 15178 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 15179 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 15180 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 15181 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15182 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 15183 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 15184 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 15185 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15186 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15187 | |
| 15188 | /* initialize the linebreak bloom filter */ |
| 15189 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15190 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 15191 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 15192 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 15193 | if (PyType_Ready(&EncodingMapType) < 0) |
| 15194 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15195 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 15196 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 15197 | Py_FatalError("Can't initialize field name iterator type"); |
| 15198 | |
| 15199 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 15200 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 15201 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15202 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15203 | } |
| 15204 | |
| 15205 | /* Finalize the Unicode implementation */ |
| 15206 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15207 | int |
| 15208 | PyUnicode_ClearFreeList(void) |
| 15209 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15210 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15211 | } |
| 15212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15213 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 15214 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15215 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 15216 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15217 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15218 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 15219 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15220 | for (i = 0; i < 256; i++) |
| 15221 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 15222 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15223 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15224 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 15225 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15226 | void |
| 15227 | PyUnicode_InternInPlace(PyObject **p) |
| 15228 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15229 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15230 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15231 | #ifdef Py_DEBUG |
| 15232 | assert(s != NULL); |
| 15233 | assert(_PyUnicode_CHECK(s)); |
| 15234 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15235 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15236 | return; |
| 15237 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15238 | /* If it's a subclass, we don't really know what putting |
| 15239 | it in the interned dict might do. */ |
| 15240 | if (!PyUnicode_CheckExact(s)) |
| 15241 | return; |
| 15242 | if (PyUnicode_CHECK_INTERNED(s)) |
| 15243 | return; |
| 15244 | if (interned == NULL) { |
| 15245 | interned = PyDict_New(); |
| 15246 | if (interned == NULL) { |
| 15247 | PyErr_Clear(); /* Don't leave an exception */ |
| 15248 | return; |
| 15249 | } |
| 15250 | } |
| 15251 | /* It might be that the GetItem call fails even |
| 15252 | though the key is present in the dictionary, |
| 15253 | namely when this happens during a stack overflow. */ |
| 15254 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15255 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15256 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15257 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15258 | if (t) { |
| 15259 | Py_INCREF(t); |
| 15260 | Py_DECREF(*p); |
| 15261 | *p = t; |
| 15262 | return; |
| 15263 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15264 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15265 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15266 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15267 | PyErr_Clear(); |
| 15268 | PyThreadState_GET()->recursion_critical = 0; |
| 15269 | return; |
| 15270 | } |
| 15271 | PyThreadState_GET()->recursion_critical = 0; |
| 15272 | /* The two references in interned are not counted by refcnt. |
| 15273 | The deallocator will take care of this */ |
| 15274 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15275 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15276 | } |
| 15277 | |
| 15278 | void |
| 15279 | PyUnicode_InternImmortal(PyObject **p) |
| 15280 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15281 | PyUnicode_InternInPlace(p); |
| 15282 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 15283 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15284 | Py_INCREF(*p); |
| 15285 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15286 | } |
| 15287 | |
| 15288 | PyObject * |
| 15289 | PyUnicode_InternFromString(const char *cp) |
| 15290 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15291 | PyObject *s = PyUnicode_FromString(cp); |
| 15292 | if (s == NULL) |
| 15293 | return NULL; |
| 15294 | PyUnicode_InternInPlace(&s); |
| 15295 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15296 | } |
| 15297 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15298 | void |
| 15299 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15300 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15301 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15302 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15303 | Py_ssize_t i, n; |
| 15304 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15305 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15306 | if (interned == NULL || !PyDict_Check(interned)) |
| 15307 | return; |
| 15308 | keys = PyDict_Keys(interned); |
| 15309 | if (keys == NULL || !PyList_Check(keys)) { |
| 15310 | PyErr_Clear(); |
| 15311 | return; |
| 15312 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15313 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15314 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15315 | detector, interned unicode strings are not forcibly deallocated; |
| 15316 | rather, we give them their stolen references back, and then clear |
| 15317 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15318 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15319 | n = PyList_GET_SIZE(keys); |
| 15320 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15321 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15322 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15323 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15324 | if (PyUnicode_READY(s) == -1) { |
| 15325 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15326 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15327 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15328 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15329 | case SSTATE_NOT_INTERNED: |
| 15330 | /* XXX Shouldn't happen */ |
| 15331 | break; |
| 15332 | case SSTATE_INTERNED_IMMORTAL: |
| 15333 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15334 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15335 | break; |
| 15336 | case SSTATE_INTERNED_MORTAL: |
| 15337 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15338 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15339 | break; |
| 15340 | default: |
| 15341 | Py_FatalError("Inconsistent interned string state."); |
| 15342 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15343 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15344 | } |
| 15345 | fprintf(stderr, "total size of all interned strings: " |
| 15346 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15347 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15348 | Py_DECREF(keys); |
| 15349 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15350 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15351 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15352 | |
| 15353 | |
| 15354 | /********************* Unicode Iterator **************************/ |
| 15355 | |
| 15356 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15357 | PyObject_HEAD |
| 15358 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15359 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15360 | } unicodeiterobject; |
| 15361 | |
| 15362 | static void |
| 15363 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15364 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15365 | _PyObject_GC_UNTRACK(it); |
| 15366 | Py_XDECREF(it->it_seq); |
| 15367 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15368 | } |
| 15369 | |
| 15370 | static int |
| 15371 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15372 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15373 | Py_VISIT(it->it_seq); |
| 15374 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15375 | } |
| 15376 | |
| 15377 | static PyObject * |
| 15378 | unicodeiter_next(unicodeiterobject *it) |
| 15379 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15380 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15381 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15382 | assert(it != NULL); |
| 15383 | seq = it->it_seq; |
| 15384 | if (seq == NULL) |
| 15385 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15386 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15387 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15388 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15389 | int kind = PyUnicode_KIND(seq); |
| 15390 | void *data = PyUnicode_DATA(seq); |
| 15391 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15392 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15393 | if (item != NULL) |
| 15394 | ++it->it_index; |
| 15395 | return item; |
| 15396 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15397 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15398 | Py_DECREF(seq); |
| 15399 | it->it_seq = NULL; |
| 15400 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15401 | } |
| 15402 | |
| 15403 | static PyObject * |
| 15404 | unicodeiter_len(unicodeiterobject *it) |
| 15405 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15406 | Py_ssize_t len = 0; |
| 15407 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15408 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15409 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15410 | } |
| 15411 | |
| 15412 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15413 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15414 | static PyObject * |
| 15415 | unicodeiter_reduce(unicodeiterobject *it) |
| 15416 | { |
| 15417 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15418 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15419 | it->it_seq, it->it_index); |
| 15420 | } else { |
| 15421 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15422 | if (u == NULL) |
| 15423 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15424 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15425 | } |
| 15426 | } |
| 15427 | |
| 15428 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15429 | |
| 15430 | static PyObject * |
| 15431 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15432 | { |
| 15433 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15434 | if (index == -1 && PyErr_Occurred()) |
| 15435 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 15436 | if (it->it_seq != NULL) { |
| 15437 | if (index < 0) |
| 15438 | index = 0; |
| 15439 | else if (index > PyUnicode_GET_LENGTH(it->it_seq)) |
| 15440 | index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */ |
| 15441 | it->it_index = index; |
| 15442 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15443 | Py_RETURN_NONE; |
| 15444 | } |
| 15445 | |
| 15446 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15447 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15448 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15449 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15450 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15451 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15452 | reduce_doc}, |
| 15453 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15454 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15455 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15456 | }; |
| 15457 | |
| 15458 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15459 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15460 | "str_iterator", /* tp_name */ |
| 15461 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15462 | 0, /* tp_itemsize */ |
| 15463 | /* methods */ |
| 15464 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15465 | 0, /* tp_print */ |
| 15466 | 0, /* tp_getattr */ |
| 15467 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15468 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15469 | 0, /* tp_repr */ |
| 15470 | 0, /* tp_as_number */ |
| 15471 | 0, /* tp_as_sequence */ |
| 15472 | 0, /* tp_as_mapping */ |
| 15473 | 0, /* tp_hash */ |
| 15474 | 0, /* tp_call */ |
| 15475 | 0, /* tp_str */ |
| 15476 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15477 | 0, /* tp_setattro */ |
| 15478 | 0, /* tp_as_buffer */ |
| 15479 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15480 | 0, /* tp_doc */ |
| 15481 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15482 | 0, /* tp_clear */ |
| 15483 | 0, /* tp_richcompare */ |
| 15484 | 0, /* tp_weaklistoffset */ |
| 15485 | PyObject_SelfIter, /* tp_iter */ |
| 15486 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15487 | unicodeiter_methods, /* tp_methods */ |
| 15488 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15489 | }; |
| 15490 | |
| 15491 | static PyObject * |
| 15492 | unicode_iter(PyObject *seq) |
| 15493 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15494 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15495 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15496 | if (!PyUnicode_Check(seq)) { |
| 15497 | PyErr_BadInternalCall(); |
| 15498 | return NULL; |
| 15499 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15500 | if (PyUnicode_READY(seq) == -1) |
| 15501 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15502 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15503 | if (it == NULL) |
| 15504 | return NULL; |
| 15505 | it->it_index = 0; |
| 15506 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15507 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15508 | _PyObject_GC_TRACK(it); |
| 15509 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15510 | } |
| 15511 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15512 | |
| 15513 | size_t |
| 15514 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15515 | { |
| 15516 | int res = 0; |
| 15517 | while(*u++) |
| 15518 | res++; |
| 15519 | return res; |
| 15520 | } |
| 15521 | |
| 15522 | Py_UNICODE* |
| 15523 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15524 | { |
| 15525 | Py_UNICODE *u = s1; |
| 15526 | while ((*u++ = *s2++)); |
| 15527 | return s1; |
| 15528 | } |
| 15529 | |
| 15530 | Py_UNICODE* |
| 15531 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15532 | { |
| 15533 | Py_UNICODE *u = s1; |
| 15534 | while ((*u++ = *s2++)) |
| 15535 | if (n-- == 0) |
| 15536 | break; |
| 15537 | return s1; |
| 15538 | } |
| 15539 | |
| 15540 | Py_UNICODE* |
| 15541 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15542 | { |
| 15543 | Py_UNICODE *u1 = s1; |
| 15544 | u1 += Py_UNICODE_strlen(u1); |
| 15545 | Py_UNICODE_strcpy(u1, s2); |
| 15546 | return s1; |
| 15547 | } |
| 15548 | |
| 15549 | int |
| 15550 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15551 | { |
| 15552 | while (*s1 && *s2 && *s1 == *s2) |
| 15553 | s1++, s2++; |
| 15554 | if (*s1 && *s2) |
| 15555 | return (*s1 < *s2) ? -1 : +1; |
| 15556 | if (*s1) |
| 15557 | return 1; |
| 15558 | if (*s2) |
| 15559 | return -1; |
| 15560 | return 0; |
| 15561 | } |
| 15562 | |
| 15563 | int |
| 15564 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15565 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15566 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15567 | for (; n != 0; n--) { |
| 15568 | u1 = *s1; |
| 15569 | u2 = *s2; |
| 15570 | if (u1 != u2) |
| 15571 | return (u1 < u2) ? -1 : +1; |
| 15572 | if (u1 == '\0') |
| 15573 | return 0; |
| 15574 | s1++; |
| 15575 | s2++; |
| 15576 | } |
| 15577 | return 0; |
| 15578 | } |
| 15579 | |
| 15580 | Py_UNICODE* |
| 15581 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15582 | { |
| 15583 | const Py_UNICODE *p; |
| 15584 | for (p = s; *p; p++) |
| 15585 | if (*p == c) |
| 15586 | return (Py_UNICODE*)p; |
| 15587 | return NULL; |
| 15588 | } |
| 15589 | |
| 15590 | Py_UNICODE* |
| 15591 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15592 | { |
| 15593 | const Py_UNICODE *p; |
| 15594 | p = s + Py_UNICODE_strlen(s); |
| 15595 | while (p != s) { |
| 15596 | p--; |
| 15597 | if (*p == c) |
| 15598 | return (Py_UNICODE*)p; |
| 15599 | } |
| 15600 | return NULL; |
| 15601 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15602 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15603 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15604 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15605 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15606 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15607 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15608 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15609 | if (!PyUnicode_Check(unicode)) { |
| 15610 | PyErr_BadArgument(); |
| 15611 | return NULL; |
| 15612 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15613 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15614 | if (u == NULL) |
| 15615 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15616 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 15617 | 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] | 15618 | PyErr_NoMemory(); |
| 15619 | return NULL; |
| 15620 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15621 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15622 | size *= sizeof(Py_UNICODE); |
| 15623 | copy = PyMem_Malloc(size); |
| 15624 | if (copy == NULL) { |
| 15625 | PyErr_NoMemory(); |
| 15626 | return NULL; |
| 15627 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15628 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15629 | return copy; |
| 15630 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15631 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15632 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15633 | to the string.Formatter class implemented in Python. */ |
| 15634 | |
| 15635 | static PyMethodDef _string_methods[] = { |
| 15636 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15637 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15638 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15639 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15640 | {NULL, NULL} |
| 15641 | }; |
| 15642 | |
| 15643 | static struct PyModuleDef _string_module = { |
| 15644 | PyModuleDef_HEAD_INIT, |
| 15645 | "_string", |
| 15646 | PyDoc_STR("string helper module"), |
| 15647 | 0, |
| 15648 | _string_methods, |
| 15649 | NULL, |
| 15650 | NULL, |
| 15651 | NULL, |
| 15652 | NULL |
| 15653 | }; |
| 15654 | |
| 15655 | PyMODINIT_FUNC |
| 15656 | PyInit__string(void) |
| 15657 | { |
| 15658 | return PyModule_Create(&_string_module); |
| 15659 | } |
| 15660 | |
| 15661 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15662 | #ifdef __cplusplus |
| 15663 | } |
| 15664 | #endif |