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* |
| 568 | backslashreplace(_PyBytesWriter *writer, Py_ssize_t prealloc_per_char, |
| 569 | char *str, |
| 570 | PyObject *unicode, Py_ssize_t collstart, Py_ssize_t collend) |
| 571 | { |
| 572 | Py_ssize_t size, i, prealloc; |
| 573 | Py_UCS4 ch; |
| 574 | enum PyUnicode_Kind kind; |
| 575 | void *data; |
| 576 | |
| 577 | assert(PyUnicode_IS_READY(unicode)); |
| 578 | kind = PyUnicode_KIND(unicode); |
| 579 | data = PyUnicode_DATA(unicode); |
| 580 | |
| 581 | size = 0; |
| 582 | /* determine replacement size */ |
| 583 | for (i = collstart; i < collend; ++i) { |
| 584 | Py_ssize_t incr; |
| 585 | |
| 586 | ch = PyUnicode_READ(kind, data, i); |
| 587 | if (ch < 0x100) |
| 588 | incr = 2+2; |
| 589 | else if (ch < 0x10000) |
| 590 | incr = 2+4; |
| 591 | else { |
| 592 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 3fa36ff | 2015-10-09 03:37:11 +0200 | [diff] [blame] | 593 | incr = 2+8; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 594 | } |
| 595 | if (size > PY_SSIZE_T_MAX - incr) { |
| 596 | PyErr_SetString(PyExc_OverflowError, |
| 597 | "encoded result is too long for a Python string"); |
| 598 | return NULL; |
| 599 | } |
| 600 | size += incr; |
| 601 | } |
| 602 | |
| 603 | prealloc = prealloc_per_char * (collend - collstart); |
| 604 | if (size > prealloc) { |
| 605 | str = _PyBytesWriter_Prepare(writer, str, size - prealloc); |
| 606 | if (str == NULL) |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | /* generate replacement */ |
| 611 | for (i = collstart; i < collend; ++i) { |
| 612 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | 797485e | 2015-10-09 03:17:30 +0200 | [diff] [blame] | 613 | *str++ = '\\'; |
| 614 | if (ch >= 0x00010000) { |
| 615 | *str++ = 'U'; |
| 616 | *str++ = Py_hexdigits[(ch>>28)&0xf]; |
| 617 | *str++ = Py_hexdigits[(ch>>24)&0xf]; |
| 618 | *str++ = Py_hexdigits[(ch>>20)&0xf]; |
| 619 | *str++ = Py_hexdigits[(ch>>16)&0xf]; |
| 620 | *str++ = Py_hexdigits[(ch>>12)&0xf]; |
| 621 | *str++ = Py_hexdigits[(ch>>8)&0xf]; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 622 | } |
Victor Stinner | 797485e | 2015-10-09 03:17:30 +0200 | [diff] [blame] | 623 | else if (ch >= 0x100) { |
| 624 | *str++ = 'u'; |
| 625 | *str++ = Py_hexdigits[(ch>>12)&0xf]; |
| 626 | *str++ = Py_hexdigits[(ch>>8)&0xf]; |
| 627 | } |
| 628 | else |
| 629 | *str++ = 'x'; |
| 630 | *str++ = Py_hexdigits[(ch>>4)&0xf]; |
| 631 | *str++ = Py_hexdigits[ch&0xf]; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 632 | } |
| 633 | return str; |
| 634 | } |
| 635 | |
| 636 | /* Implementation of the "xmlcharrefreplace" error handler for 8-bit encodings: |
| 637 | ASCII, Latin1, UTF-8, etc. */ |
| 638 | static char* |
| 639 | xmlcharrefreplace(_PyBytesWriter *writer, Py_ssize_t prealloc_per_char, |
| 640 | char *str, |
| 641 | PyObject *unicode, Py_ssize_t collstart, Py_ssize_t collend) |
| 642 | { |
| 643 | Py_ssize_t size, i, prealloc; |
| 644 | Py_UCS4 ch; |
| 645 | enum PyUnicode_Kind kind; |
| 646 | void *data; |
| 647 | |
| 648 | assert(PyUnicode_IS_READY(unicode)); |
| 649 | kind = PyUnicode_KIND(unicode); |
| 650 | data = PyUnicode_DATA(unicode); |
| 651 | |
| 652 | size = 0; |
| 653 | /* determine replacement size */ |
| 654 | for (i = collstart; i < collend; ++i) { |
| 655 | Py_ssize_t incr; |
| 656 | |
| 657 | ch = PyUnicode_READ(kind, data, i); |
| 658 | if (ch < 10) |
| 659 | incr = 2+1+1; |
| 660 | else if (ch < 100) |
| 661 | incr = 2+2+1; |
| 662 | else if (ch < 1000) |
| 663 | incr = 2+3+1; |
| 664 | else if (ch < 10000) |
| 665 | incr = 2+4+1; |
| 666 | else if (ch < 100000) |
| 667 | incr = 2+5+1; |
| 668 | else if (ch < 1000000) |
| 669 | incr = 2+6+1; |
| 670 | else { |
| 671 | assert(ch <= MAX_UNICODE); |
| 672 | incr = 2+7+1; |
| 673 | } |
| 674 | if (size > PY_SSIZE_T_MAX - incr) { |
| 675 | PyErr_SetString(PyExc_OverflowError, |
| 676 | "encoded result is too long for a Python string"); |
| 677 | return NULL; |
| 678 | } |
| 679 | size += incr; |
| 680 | } |
| 681 | |
| 682 | prealloc = prealloc_per_char * (collend - collstart); |
| 683 | if (size > prealloc) { |
| 684 | str = _PyBytesWriter_Prepare(writer, str, size - prealloc); |
| 685 | if (str == NULL) |
| 686 | return NULL; |
| 687 | } |
| 688 | |
| 689 | /* generate replacement */ |
| 690 | for (i = collstart; i < collend; ++i) { |
| 691 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
| 692 | } |
| 693 | return str; |
| 694 | } |
| 695 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 696 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 697 | |
| 698 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 699 | to keep things simple, we use a single bitmask, using the least 5 |
| 700 | bits from each unicode characters as the bit index. */ |
| 701 | |
| 702 | /* the linebreak mask is set up by Unicode_Init below */ |
| 703 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 704 | #if LONG_BIT >= 128 |
| 705 | #define BLOOM_WIDTH 128 |
| 706 | #elif LONG_BIT >= 64 |
| 707 | #define BLOOM_WIDTH 64 |
| 708 | #elif LONG_BIT >= 32 |
| 709 | #define BLOOM_WIDTH 32 |
| 710 | #else |
| 711 | #error "LONG_BIT is smaller than 32" |
| 712 | #endif |
| 713 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 714 | #define BLOOM_MASK unsigned long |
| 715 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 716 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 717 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 718 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 719 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 720 | #define BLOOM_LINEBREAK(ch) \ |
| 721 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 722 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 723 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 724 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 725 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 726 | { |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 727 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN) \ |
| 728 | do { \ |
| 729 | TYPE *data = (TYPE *)PTR; \ |
| 730 | TYPE *end = data + LEN; \ |
| 731 | Py_UCS4 ch; \ |
| 732 | for (; data != end; data++) { \ |
| 733 | ch = *data; \ |
| 734 | MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ |
| 735 | } \ |
| 736 | break; \ |
| 737 | } while (0) |
| 738 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 739 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 740 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 741 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 742 | |
| 743 | mask = 0; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 744 | switch (kind) { |
| 745 | case PyUnicode_1BYTE_KIND: |
| 746 | BLOOM_UPDATE(Py_UCS1, mask, ptr, len); |
| 747 | break; |
| 748 | case PyUnicode_2BYTE_KIND: |
| 749 | BLOOM_UPDATE(Py_UCS2, mask, ptr, len); |
| 750 | break; |
| 751 | case PyUnicode_4BYTE_KIND: |
| 752 | BLOOM_UPDATE(Py_UCS4, mask, ptr, len); |
| 753 | break; |
| 754 | default: |
| 755 | assert(0); |
| 756 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 757 | return mask; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 758 | |
| 759 | #undef BLOOM_UPDATE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 762 | /* Compilation of templated routines */ |
| 763 | |
| 764 | #include "stringlib/asciilib.h" |
| 765 | #include "stringlib/fastsearch.h" |
| 766 | #include "stringlib/partition.h" |
| 767 | #include "stringlib/split.h" |
| 768 | #include "stringlib/count.h" |
| 769 | #include "stringlib/find.h" |
| 770 | #include "stringlib/find_max_char.h" |
| 771 | #include "stringlib/localeutil.h" |
| 772 | #include "stringlib/undef.h" |
| 773 | |
| 774 | #include "stringlib/ucs1lib.h" |
| 775 | #include "stringlib/fastsearch.h" |
| 776 | #include "stringlib/partition.h" |
| 777 | #include "stringlib/split.h" |
| 778 | #include "stringlib/count.h" |
| 779 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 780 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 781 | #include "stringlib/find_max_char.h" |
| 782 | #include "stringlib/localeutil.h" |
| 783 | #include "stringlib/undef.h" |
| 784 | |
| 785 | #include "stringlib/ucs2lib.h" |
| 786 | #include "stringlib/fastsearch.h" |
| 787 | #include "stringlib/partition.h" |
| 788 | #include "stringlib/split.h" |
| 789 | #include "stringlib/count.h" |
| 790 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 791 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 792 | #include "stringlib/find_max_char.h" |
| 793 | #include "stringlib/localeutil.h" |
| 794 | #include "stringlib/undef.h" |
| 795 | |
| 796 | #include "stringlib/ucs4lib.h" |
| 797 | #include "stringlib/fastsearch.h" |
| 798 | #include "stringlib/partition.h" |
| 799 | #include "stringlib/split.h" |
| 800 | #include "stringlib/count.h" |
| 801 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 802 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 803 | #include "stringlib/find_max_char.h" |
| 804 | #include "stringlib/localeutil.h" |
| 805 | #include "stringlib/undef.h" |
| 806 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 807 | #include "stringlib/unicodedefs.h" |
| 808 | #include "stringlib/fastsearch.h" |
| 809 | #include "stringlib/count.h" |
| 810 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 811 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 813 | /* --- Unicode Object ----------------------------------------------------- */ |
| 814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 815 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 816 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 817 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 818 | Py_LOCAL_INLINE(Py_ssize_t) findchar(const void *s, int kind, |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 819 | Py_ssize_t size, Py_UCS4 ch, |
| 820 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 821 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 822 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 823 | |
| 824 | switch (kind) { |
| 825 | case PyUnicode_1BYTE_KIND: |
| 826 | { |
| 827 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 828 | if (ch1 == ch) |
| 829 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 830 | else |
| 831 | return -1; |
| 832 | } |
| 833 | case PyUnicode_2BYTE_KIND: |
| 834 | { |
| 835 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 836 | if (ch2 == ch) |
| 837 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 838 | else |
| 839 | return -1; |
| 840 | } |
| 841 | case PyUnicode_4BYTE_KIND: |
| 842 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 843 | default: |
| 844 | assert(0); |
| 845 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 846 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 849 | #ifdef Py_DEBUG |
| 850 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 851 | earlier. |
| 852 | |
| 853 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 854 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 855 | invalid character in Unicode 6.0. */ |
| 856 | static void |
| 857 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 858 | { |
| 859 | int kind = PyUnicode_KIND(unicode); |
| 860 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 861 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 862 | if (length <= old_length) |
| 863 | return; |
| 864 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 865 | } |
| 866 | #endif |
| 867 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 868 | static PyObject* |
| 869 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 870 | { |
| 871 | Py_ssize_t char_size; |
| 872 | Py_ssize_t struct_size; |
| 873 | Py_ssize_t new_size; |
| 874 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 875 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 876 | #ifdef Py_DEBUG |
| 877 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 878 | #endif |
| 879 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 880 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 881 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 882 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 883 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 884 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 885 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 886 | struct_size = sizeof(PyASCIIObject); |
| 887 | else |
| 888 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 889 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 890 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 891 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 892 | PyErr_NoMemory(); |
| 893 | return NULL; |
| 894 | } |
| 895 | new_size = (struct_size + (length + 1) * char_size); |
| 896 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 897 | _Py_DEC_REFTOTAL; |
| 898 | _Py_ForgetReference(unicode); |
| 899 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 900 | new_unicode = (PyObject *)PyObject_REALLOC(unicode, new_size); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 901 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 902 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 903 | PyErr_NoMemory(); |
| 904 | return NULL; |
| 905 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 906 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 907 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 908 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 909 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 910 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 911 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 912 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 913 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 914 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 915 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 916 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 917 | _PyUnicode_WSTR(unicode) = NULL; |
| 918 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 919 | #ifdef Py_DEBUG |
| 920 | unicode_fill_invalid(unicode, old_length); |
| 921 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 922 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 923 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 924 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 925 | return unicode; |
| 926 | } |
| 927 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 928 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 929 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 930 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 931 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 932 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 933 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 934 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 935 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 936 | if (PyUnicode_IS_READY(unicode)) { |
| 937 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 938 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 939 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 940 | #ifdef Py_DEBUG |
| 941 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 942 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 943 | |
| 944 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 945 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 946 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 947 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 948 | |
| 949 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 950 | PyErr_NoMemory(); |
| 951 | return -1; |
| 952 | } |
| 953 | new_size = (length + 1) * char_size; |
| 954 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 955 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 956 | { |
| 957 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 958 | _PyUnicode_UTF8(unicode) = NULL; |
| 959 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 960 | } |
| 961 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 962 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 963 | if (data == NULL) { |
| 964 | PyErr_NoMemory(); |
| 965 | return -1; |
| 966 | } |
| 967 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 968 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 969 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 970 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 971 | } |
| 972 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 973 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 974 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 975 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 976 | _PyUnicode_LENGTH(unicode) = length; |
| 977 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 978 | #ifdef Py_DEBUG |
| 979 | unicode_fill_invalid(unicode, old_length); |
| 980 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 981 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 982 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 983 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 984 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 985 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 986 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 987 | |
| 988 | /* check for integer overflow */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 989 | 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] | 990 | PyErr_NoMemory(); |
| 991 | return -1; |
| 992 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 993 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 994 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 995 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 996 | if (!wstr) { |
| 997 | PyErr_NoMemory(); |
| 998 | return -1; |
| 999 | } |
| 1000 | _PyUnicode_WSTR(unicode) = wstr; |
| 1001 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 1002 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1003 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1004 | return 0; |
| 1005 | } |
| 1006 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1007 | static PyObject* |
| 1008 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 1009 | { |
| 1010 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1011 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1012 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1013 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1014 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1015 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1016 | |
| 1017 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1018 | if (copy == NULL) |
| 1019 | return NULL; |
| 1020 | |
| 1021 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1022 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1023 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 1024 | } |
| 1025 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1026 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 1027 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1028 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1029 | if (w == NULL) |
| 1030 | return NULL; |
| 1031 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 1032 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 1033 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 1034 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1035 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1039 | /* 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] | 1040 | Ux0000 terminated; some code (e.g. new_identifier) |
| 1041 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1042 | |
| 1043 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1044 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1045 | |
| 1046 | */ |
| 1047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1048 | static PyUnicodeObject * |
| 1049 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1050 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1051 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1053 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1054 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1055 | if (length == 0 && unicode_empty != NULL) { |
| 1056 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1057 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 1060 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 1061 | 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] | 1062 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 1063 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1064 | if (length < 0) { |
| 1065 | PyErr_SetString(PyExc_SystemError, |
| 1066 | "Negative size passed to _PyUnicode_New"); |
| 1067 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1070 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 1071 | if (unicode == NULL) |
| 1072 | return NULL; |
| 1073 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 1074 | |
| 1075 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 1076 | _PyUnicode_HASH(unicode) = -1; |
| 1077 | _PyUnicode_STATE(unicode).interned = 0; |
| 1078 | _PyUnicode_STATE(unicode).kind = 0; |
| 1079 | _PyUnicode_STATE(unicode).compact = 0; |
| 1080 | _PyUnicode_STATE(unicode).ready = 0; |
| 1081 | _PyUnicode_STATE(unicode).ascii = 0; |
| 1082 | _PyUnicode_DATA_ANY(unicode) = NULL; |
| 1083 | _PyUnicode_LENGTH(unicode) = 0; |
| 1084 | _PyUnicode_UTF8(unicode) = NULL; |
| 1085 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 1086 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1087 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 1088 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1089 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1090 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1091 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 1092 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1093 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 1094 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1095 | * the caller fails before initializing str -- unicode_resize() |
| 1096 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 1097 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 1098 | * We don't want unicode_resize to read uninitialized memory in |
| 1099 | * that case. |
| 1100 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 1102 | _PyUnicode_WSTR(unicode)[length] = 0; |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 1103 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1104 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1105 | return unicode; |
| 1106 | } |
| 1107 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1108 | static const char* |
| 1109 | unicode_kind_name(PyObject *unicode) |
| 1110 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 1111 | /* don't check consistency: unicode_kind_name() is called from |
| 1112 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1113 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 1114 | { |
| 1115 | if (!PyUnicode_IS_READY(unicode)) |
| 1116 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1117 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1118 | { |
| 1119 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1120 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1121 | return "legacy ascii"; |
| 1122 | else |
| 1123 | return "legacy latin1"; |
| 1124 | case PyUnicode_2BYTE_KIND: |
| 1125 | return "legacy UCS2"; |
| 1126 | case PyUnicode_4BYTE_KIND: |
| 1127 | return "legacy UCS4"; |
| 1128 | default: |
| 1129 | return "<legacy invalid kind>"; |
| 1130 | } |
| 1131 | } |
| 1132 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1133 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1134 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1135 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1136 | return "ascii"; |
| 1137 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1138 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1139 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1140 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1141 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1142 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1143 | default: |
| 1144 | return "<invalid compact kind>"; |
| 1145 | } |
| 1146 | } |
| 1147 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1149 | /* Functions wrapping macros for use in debugger */ |
| 1150 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1151 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | void *_PyUnicode_compact_data(void *unicode) { |
| 1155 | return _PyUnicode_COMPACT_DATA(unicode); |
| 1156 | } |
| 1157 | void *_PyUnicode_data(void *unicode){ |
| 1158 | printf("obj %p\n", unicode); |
| 1159 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 1160 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 1161 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 1162 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 1163 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 1164 | return PyUnicode_DATA(unicode); |
| 1165 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1166 | |
| 1167 | void |
| 1168 | _PyUnicode_Dump(PyObject *op) |
| 1169 | { |
| 1170 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1171 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 1172 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 1173 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1174 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1175 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1176 | { |
| 1177 | if (ascii->state.ascii) |
| 1178 | data = (ascii + 1); |
| 1179 | else |
| 1180 | data = (compact + 1); |
| 1181 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1182 | else |
| 1183 | data = unicode->data.any; |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1184 | printf("%s: len=%" PY_FORMAT_SIZE_T "u, ", |
| 1185 | unicode_kind_name(op), ascii->length); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1186 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1187 | if (ascii->wstr == data) |
| 1188 | printf("shared "); |
| 1189 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1190 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1191 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1192 | printf(" (%" PY_FORMAT_SIZE_T "u), ", compact->wstr_length); |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1193 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1194 | printf("shared "); |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1195 | printf("utf8=%p (%" PY_FORMAT_SIZE_T "u)", |
| 1196 | compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1197 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1198 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1199 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1200 | #endif |
| 1201 | |
| 1202 | PyObject * |
| 1203 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1204 | { |
| 1205 | PyObject *obj; |
| 1206 | PyCompactUnicodeObject *unicode; |
| 1207 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1208 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1209 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1210 | Py_ssize_t char_size; |
| 1211 | Py_ssize_t struct_size; |
| 1212 | |
| 1213 | /* Optimization for empty strings */ |
| 1214 | if (size == 0 && unicode_empty != NULL) { |
| 1215 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1216 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1219 | is_ascii = 0; |
| 1220 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1221 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1222 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1223 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1224 | char_size = 1; |
| 1225 | is_ascii = 1; |
| 1226 | struct_size = sizeof(PyASCIIObject); |
| 1227 | } |
| 1228 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1229 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1230 | char_size = 1; |
| 1231 | } |
| 1232 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1233 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1234 | char_size = 2; |
| 1235 | if (sizeof(wchar_t) == 2) |
| 1236 | is_sharing = 1; |
| 1237 | } |
| 1238 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1239 | if (maxchar > MAX_UNICODE) { |
| 1240 | PyErr_SetString(PyExc_SystemError, |
| 1241 | "invalid maximum character passed to PyUnicode_New"); |
| 1242 | return NULL; |
| 1243 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1244 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1245 | char_size = 4; |
| 1246 | if (sizeof(wchar_t) == 4) |
| 1247 | is_sharing = 1; |
| 1248 | } |
| 1249 | |
| 1250 | /* Ensure we won't overflow the size. */ |
| 1251 | if (size < 0) { |
| 1252 | PyErr_SetString(PyExc_SystemError, |
| 1253 | "Negative size passed to PyUnicode_New"); |
| 1254 | return NULL; |
| 1255 | } |
| 1256 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1257 | return PyErr_NoMemory(); |
| 1258 | |
| 1259 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1260 | * PyObject_New() so we are able to allocate space for the object and |
| 1261 | * it's data buffer. |
| 1262 | */ |
| 1263 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1264 | if (obj == NULL) |
| 1265 | return PyErr_NoMemory(); |
| 1266 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1267 | if (obj == NULL) |
| 1268 | return NULL; |
| 1269 | |
| 1270 | unicode = (PyCompactUnicodeObject *)obj; |
| 1271 | if (is_ascii) |
| 1272 | data = ((PyASCIIObject*)obj) + 1; |
| 1273 | else |
| 1274 | data = unicode + 1; |
| 1275 | _PyUnicode_LENGTH(unicode) = size; |
| 1276 | _PyUnicode_HASH(unicode) = -1; |
| 1277 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1278 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1279 | _PyUnicode_STATE(unicode).compact = 1; |
| 1280 | _PyUnicode_STATE(unicode).ready = 1; |
| 1281 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1282 | if (is_ascii) { |
| 1283 | ((char*)data)[size] = 0; |
| 1284 | _PyUnicode_WSTR(unicode) = NULL; |
| 1285 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1286 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1287 | ((char*)data)[size] = 0; |
| 1288 | _PyUnicode_WSTR(unicode) = NULL; |
| 1289 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1290 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1291 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1292 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1293 | else { |
| 1294 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1295 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1296 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1297 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1298 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1299 | ((Py_UCS4*)data)[size] = 0; |
| 1300 | if (is_sharing) { |
| 1301 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1302 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1303 | } |
| 1304 | else { |
| 1305 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1306 | _PyUnicode_WSTR(unicode) = NULL; |
| 1307 | } |
| 1308 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1309 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1310 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1311 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1312 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1313 | return obj; |
| 1314 | } |
| 1315 | |
| 1316 | #if SIZEOF_WCHAR_T == 2 |
| 1317 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1318 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1319 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1320 | |
| 1321 | This function assumes that unicode can hold one more code point than wstr |
| 1322 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1323 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1324 | 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] | 1325 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1326 | { |
| 1327 | const wchar_t *iter; |
| 1328 | Py_UCS4 *ucs4_out; |
| 1329 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1330 | assert(unicode != NULL); |
| 1331 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1332 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1333 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1334 | |
| 1335 | for (iter = begin; iter < end; ) { |
| 1336 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1337 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1338 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1339 | && (iter+1) < end |
| 1340 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1341 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1342 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1343 | iter += 2; |
| 1344 | } |
| 1345 | else { |
| 1346 | *ucs4_out++ = *iter; |
| 1347 | iter++; |
| 1348 | } |
| 1349 | } |
| 1350 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1351 | _PyUnicode_GET_LENGTH(unicode))); |
| 1352 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | } |
| 1354 | #endif |
| 1355 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1356 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1357 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1358 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1359 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1360 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1361 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1362 | return -1; |
| 1363 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1364 | return 0; |
| 1365 | } |
| 1366 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1367 | static int |
| 1368 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1369 | PyObject *from, Py_ssize_t from_start, |
| 1370 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1371 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1372 | unsigned int from_kind, to_kind; |
| 1373 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1374 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1375 | assert(0 <= how_many); |
| 1376 | assert(0 <= from_start); |
| 1377 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1378 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1379 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1380 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1381 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1382 | assert(PyUnicode_Check(to)); |
| 1383 | assert(PyUnicode_IS_READY(to)); |
| 1384 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1385 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1386 | if (how_many == 0) |
| 1387 | return 0; |
| 1388 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1389 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1390 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1391 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1392 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1394 | #ifdef Py_DEBUG |
| 1395 | if (!check_maxchar |
| 1396 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1397 | { |
| 1398 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1399 | Py_UCS4 ch; |
| 1400 | Py_ssize_t i; |
| 1401 | for (i=0; i < how_many; i++) { |
| 1402 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1403 | assert(ch <= to_maxchar); |
| 1404 | } |
| 1405 | } |
| 1406 | #endif |
| 1407 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1408 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1409 | if (check_maxchar |
| 1410 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1411 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1412 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1413 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1414 | Py_UCS4 max_char; |
| 1415 | max_char = ucs1lib_find_max_char(from_data, |
| 1416 | (Py_UCS1*)from_data + how_many); |
| 1417 | if (max_char >= 128) |
| 1418 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1419 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1420 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1421 | (char*)from_data + from_kind * from_start, |
| 1422 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1423 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1424 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1425 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1426 | { |
| 1427 | _PyUnicode_CONVERT_BYTES( |
| 1428 | Py_UCS1, Py_UCS2, |
| 1429 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1430 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1431 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1432 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1433 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1434 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1435 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1436 | { |
| 1437 | _PyUnicode_CONVERT_BYTES( |
| 1438 | Py_UCS1, Py_UCS4, |
| 1439 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1440 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1441 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1442 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1443 | } |
| 1444 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1445 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1446 | { |
| 1447 | _PyUnicode_CONVERT_BYTES( |
| 1448 | Py_UCS2, Py_UCS4, |
| 1449 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1450 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1451 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1452 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1453 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1454 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1455 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1456 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1457 | if (!check_maxchar) { |
| 1458 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1459 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1460 | { |
| 1461 | _PyUnicode_CONVERT_BYTES( |
| 1462 | Py_UCS2, Py_UCS1, |
| 1463 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1464 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1465 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1466 | ); |
| 1467 | } |
| 1468 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1469 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1470 | { |
| 1471 | _PyUnicode_CONVERT_BYTES( |
| 1472 | Py_UCS4, Py_UCS1, |
| 1473 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1474 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1475 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1476 | ); |
| 1477 | } |
| 1478 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1479 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1480 | { |
| 1481 | _PyUnicode_CONVERT_BYTES( |
| 1482 | Py_UCS4, Py_UCS2, |
| 1483 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1484 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1485 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1486 | ); |
| 1487 | } |
| 1488 | else { |
| 1489 | assert(0); |
| 1490 | return -1; |
| 1491 | } |
| 1492 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1493 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1494 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1495 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1496 | Py_ssize_t i; |
| 1497 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1498 | for (i=0; i < how_many; i++) { |
| 1499 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1500 | if (ch > to_maxchar) |
| 1501 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1502 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1503 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1504 | } |
| 1505 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1506 | return 0; |
| 1507 | } |
| 1508 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1509 | void |
| 1510 | _PyUnicode_FastCopyCharacters( |
| 1511 | PyObject *to, Py_ssize_t to_start, |
| 1512 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1513 | { |
| 1514 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1515 | } |
| 1516 | |
| 1517 | Py_ssize_t |
| 1518 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1519 | PyObject *from, Py_ssize_t from_start, |
| 1520 | Py_ssize_t how_many) |
| 1521 | { |
| 1522 | int err; |
| 1523 | |
| 1524 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1525 | PyErr_BadInternalCall(); |
| 1526 | return -1; |
| 1527 | } |
| 1528 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1529 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1530 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1531 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1532 | return -1; |
| 1533 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1534 | if (from_start < 0) { |
| 1535 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1536 | return -1; |
| 1537 | } |
| 1538 | if (to_start < 0) { |
| 1539 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1540 | return -1; |
| 1541 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1542 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1543 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1544 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 1545 | "Cannot write %zi characters at %zi " |
| 1546 | "in a string of %zi characters", |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1547 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1548 | return -1; |
| 1549 | } |
| 1550 | |
| 1551 | if (how_many == 0) |
| 1552 | return 0; |
| 1553 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1554 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1555 | return -1; |
| 1556 | |
| 1557 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1558 | if (err) { |
| 1559 | PyErr_Format(PyExc_SystemError, |
| 1560 | "Cannot copy %s characters " |
| 1561 | "into a string of %s characters", |
| 1562 | unicode_kind_name(from), |
| 1563 | unicode_kind_name(to)); |
| 1564 | return -1; |
| 1565 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1566 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1569 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1570 | correct string length can be computed before converting a string to UCS4. |
| 1571 | This function counts single surrogates as a character and not as a pair. |
| 1572 | |
| 1573 | Return 0 on success, or -1 on error. */ |
| 1574 | static int |
| 1575 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1576 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1577 | { |
| 1578 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1579 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1580 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1581 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1582 | *num_surrogates = 0; |
| 1583 | *maxchar = 0; |
| 1584 | |
| 1585 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1586 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1587 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1588 | && (iter+1) < end |
| 1589 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1590 | { |
| 1591 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1592 | ++(*num_surrogates); |
| 1593 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1594 | } |
| 1595 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1596 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1597 | { |
| 1598 | ch = *iter; |
| 1599 | iter++; |
| 1600 | } |
| 1601 | if (ch > *maxchar) { |
| 1602 | *maxchar = ch; |
| 1603 | if (*maxchar > MAX_UNICODE) { |
| 1604 | PyErr_Format(PyExc_ValueError, |
| 1605 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1606 | ch); |
| 1607 | return -1; |
| 1608 | } |
| 1609 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1610 | } |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1614 | int |
| 1615 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1616 | { |
| 1617 | wchar_t *end; |
| 1618 | Py_UCS4 maxchar = 0; |
| 1619 | Py_ssize_t num_surrogates; |
| 1620 | #if SIZEOF_WCHAR_T == 2 |
| 1621 | Py_ssize_t length_wo_surrogates; |
| 1622 | #endif |
| 1623 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1624 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1625 | strings were created using _PyObject_New() and where no canonical |
| 1626 | representation (the str field) has been set yet aka strings |
| 1627 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1628 | assert(_PyUnicode_CHECK(unicode)); |
| 1629 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1630 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1631 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1632 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1633 | /* Actually, it should neither be interned nor be anything else: */ |
| 1634 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1635 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1636 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1637 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1638 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1639 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1640 | |
| 1641 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1642 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1643 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1644 | PyErr_NoMemory(); |
| 1645 | return -1; |
| 1646 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1647 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1648 | _PyUnicode_WSTR(unicode), end, |
| 1649 | PyUnicode_1BYTE_DATA(unicode)); |
| 1650 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1651 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1652 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1653 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1654 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1655 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1656 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1657 | } |
| 1658 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1659 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1660 | _PyUnicode_UTF8(unicode) = NULL; |
| 1661 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1662 | } |
| 1663 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1664 | _PyUnicode_WSTR(unicode) = NULL; |
| 1665 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1666 | } |
| 1667 | /* In this case we might have to convert down from 4-byte native |
| 1668 | wchar_t to 2-byte unicode. */ |
| 1669 | else if (maxchar < 65536) { |
| 1670 | assert(num_surrogates == 0 && |
| 1671 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1672 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1673 | #if SIZEOF_WCHAR_T == 2 |
| 1674 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1675 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1676 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1677 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1678 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1679 | _PyUnicode_UTF8(unicode) = NULL; |
| 1680 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1681 | #else |
| 1682 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1683 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1684 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1685 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1686 | PyErr_NoMemory(); |
| 1687 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1688 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1689 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1690 | _PyUnicode_WSTR(unicode), end, |
| 1691 | PyUnicode_2BYTE_DATA(unicode)); |
| 1692 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1693 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1694 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1695 | _PyUnicode_UTF8(unicode) = NULL; |
| 1696 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1697 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1698 | _PyUnicode_WSTR(unicode) = NULL; |
| 1699 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1700 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1701 | } |
| 1702 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1703 | else { |
| 1704 | #if SIZEOF_WCHAR_T == 2 |
| 1705 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1706 | new normalized 4-byte version. */ |
| 1707 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 1708 | if (length_wo_surrogates > PY_SSIZE_T_MAX / 4 - 1) { |
| 1709 | PyErr_NoMemory(); |
| 1710 | return -1; |
| 1711 | } |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1712 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1713 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1714 | PyErr_NoMemory(); |
| 1715 | return -1; |
| 1716 | } |
| 1717 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1718 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1719 | _PyUnicode_UTF8(unicode) = NULL; |
| 1720 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1721 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1722 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1723 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1724 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1725 | _PyUnicode_WSTR(unicode) = NULL; |
| 1726 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1727 | #else |
| 1728 | assert(num_surrogates == 0); |
| 1729 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1730 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1731 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1732 | _PyUnicode_UTF8(unicode) = NULL; |
| 1733 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1734 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1735 | #endif |
| 1736 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1737 | } |
| 1738 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1739 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1740 | return 0; |
| 1741 | } |
| 1742 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1743 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1744 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1745 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1746 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1747 | case SSTATE_NOT_INTERNED: |
| 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_MORTAL: |
| 1751 | /* revive dead object temporarily for DelItem */ |
| 1752 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1753 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1754 | Py_FatalError( |
| 1755 | "deletion of interned string failed"); |
| 1756 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1757 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1758 | case SSTATE_INTERNED_IMMORTAL: |
| 1759 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1760 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1761 | default: |
| 1762 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1765 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1766 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1767 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1768 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1769 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1770 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1771 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1772 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1775 | #ifdef Py_DEBUG |
| 1776 | static int |
| 1777 | unicode_is_singleton(PyObject *unicode) |
| 1778 | { |
| 1779 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1780 | if (unicode == unicode_empty) |
| 1781 | return 1; |
| 1782 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1783 | { |
| 1784 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1785 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1786 | return 1; |
| 1787 | } |
| 1788 | return 0; |
| 1789 | } |
| 1790 | #endif |
| 1791 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1792 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1793 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1794 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1795 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1796 | if (Py_REFCNT(unicode) != 1) |
| 1797 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1798 | if (_PyUnicode_HASH(unicode) != -1) |
| 1799 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1800 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1801 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1802 | if (!PyUnicode_CheckExact(unicode)) |
| 1803 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1804 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1805 | /* singleton refcount is greater than 1 */ |
| 1806 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1807 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1808 | return 1; |
| 1809 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1810 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1811 | static int |
| 1812 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1813 | { |
| 1814 | PyObject *unicode; |
| 1815 | Py_ssize_t old_length; |
| 1816 | |
| 1817 | assert(p_unicode != NULL); |
| 1818 | unicode = *p_unicode; |
| 1819 | |
| 1820 | assert(unicode != NULL); |
| 1821 | assert(PyUnicode_Check(unicode)); |
| 1822 | assert(0 <= length); |
| 1823 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1824 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1825 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1826 | else |
| 1827 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1828 | if (old_length == length) |
| 1829 | return 0; |
| 1830 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1831 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1832 | _Py_INCREF_UNICODE_EMPTY(); |
| 1833 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1834 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1835 | Py_DECREF(*p_unicode); |
| 1836 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1837 | return 0; |
| 1838 | } |
| 1839 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1840 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1841 | PyObject *copy = resize_copy(unicode, length); |
| 1842 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1843 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1844 | Py_DECREF(*p_unicode); |
| 1845 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1846 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1849 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1850 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1851 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1852 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1853 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1854 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1855 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1856 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1859 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1860 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1861 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1862 | PyObject *unicode; |
| 1863 | if (p_unicode == NULL) { |
| 1864 | PyErr_BadInternalCall(); |
| 1865 | return -1; |
| 1866 | } |
| 1867 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1868 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1869 | { |
| 1870 | PyErr_BadInternalCall(); |
| 1871 | return -1; |
| 1872 | } |
| 1873 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1874 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1875 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1876 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1877 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1878 | WARNING: The function doesn't copy the terminating null character and |
| 1879 | doesn't check the maximum character (may write a latin1 character in an |
| 1880 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1881 | static void |
| 1882 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1883 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1884 | { |
| 1885 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1886 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1887 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1888 | |
| 1889 | switch (kind) { |
| 1890 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1891 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1892 | #ifdef Py_DEBUG |
| 1893 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1894 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1895 | (const Py_UCS1*)str, |
| 1896 | (const Py_UCS1*)str + len); |
| 1897 | assert(maxchar < 128); |
| 1898 | } |
| 1899 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1900 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1901 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1902 | } |
| 1903 | case PyUnicode_2BYTE_KIND: { |
| 1904 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1905 | Py_UCS2 *ucs2 = start; |
| 1906 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1907 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1908 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1909 | *ucs2 = (Py_UCS2)*str; |
| 1910 | |
| 1911 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1912 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1913 | } |
| 1914 | default: { |
| 1915 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1916 | Py_UCS4 *ucs4 = start; |
| 1917 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1918 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1919 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1920 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1921 | *ucs4 = (Py_UCS4)*str; |
| 1922 | |
| 1923 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1928 | static PyObject* |
| 1929 | get_latin1_char(unsigned char ch) |
| 1930 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1931 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1932 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1933 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1934 | if (!unicode) |
| 1935 | return NULL; |
| 1936 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1937 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1938 | unicode_latin1[ch] = unicode; |
| 1939 | } |
| 1940 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1941 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1942 | } |
| 1943 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1944 | static PyObject* |
| 1945 | unicode_char(Py_UCS4 ch) |
| 1946 | { |
| 1947 | PyObject *unicode; |
| 1948 | |
| 1949 | assert(ch <= MAX_UNICODE); |
| 1950 | |
Victor Stinner | f3b46b4 | 2014-01-03 13:16:00 +0100 | [diff] [blame] | 1951 | if (ch < 256) |
| 1952 | return get_latin1_char(ch); |
| 1953 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1954 | unicode = PyUnicode_New(1, ch); |
| 1955 | if (unicode == NULL) |
| 1956 | return NULL; |
| 1957 | switch (PyUnicode_KIND(unicode)) { |
| 1958 | case PyUnicode_1BYTE_KIND: |
| 1959 | PyUnicode_1BYTE_DATA(unicode)[0] = (Py_UCS1)ch; |
| 1960 | break; |
| 1961 | case PyUnicode_2BYTE_KIND: |
| 1962 | PyUnicode_2BYTE_DATA(unicode)[0] = (Py_UCS2)ch; |
| 1963 | break; |
| 1964 | default: |
| 1965 | assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1966 | PyUnicode_4BYTE_DATA(unicode)[0] = ch; |
| 1967 | } |
| 1968 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1969 | return unicode; |
| 1970 | } |
| 1971 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1972 | PyObject * |
| 1973 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1974 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1975 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1976 | Py_UCS4 maxchar = 0; |
| 1977 | Py_ssize_t num_surrogates; |
| 1978 | |
| 1979 | if (u == NULL) |
| 1980 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1981 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1982 | /* If the Unicode data is known at construction time, we can apply |
| 1983 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1984 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1985 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1986 | if (size == 0) |
| 1987 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1988 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1989 | /* Single character Unicode objects in the Latin-1 range are |
| 1990 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1991 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | return get_latin1_char((unsigned char)*u); |
| 1993 | |
| 1994 | /* If not empty and not single character, copy the Unicode data |
| 1995 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1996 | if (find_maxchar_surrogates(u, u + size, |
| 1997 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1998 | return NULL; |
| 1999 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2000 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2001 | if (!unicode) |
| 2002 | return NULL; |
| 2003 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2004 | switch (PyUnicode_KIND(unicode)) { |
| 2005 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 2006 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2007 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 2008 | break; |
| 2009 | case PyUnicode_2BYTE_KIND: |
| 2010 | #if Py_UNICODE_SIZE == 2 |
| 2011 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 2012 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 2013 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2014 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 2015 | #endif |
| 2016 | break; |
| 2017 | case PyUnicode_4BYTE_KIND: |
| 2018 | #if SIZEOF_WCHAR_T == 2 |
| 2019 | /* This is the only case which has to process surrogates, thus |
| 2020 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 2021 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2022 | #else |
| 2023 | assert(num_surrogates == 0); |
| 2024 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 2025 | #endif |
| 2026 | break; |
| 2027 | default: |
| 2028 | assert(0 && "Impossible state"); |
| 2029 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2030 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2031 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2034 | PyObject * |
| 2035 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 2036 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2037 | if (size < 0) { |
| 2038 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2039 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2040 | return NULL; |
| 2041 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2042 | if (u != NULL) |
| 2043 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 2044 | else |
| 2045 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2048 | PyObject * |
| 2049 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2050 | { |
| 2051 | size_t size = strlen(u); |
| 2052 | if (size > PY_SSIZE_T_MAX) { |
| 2053 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 2054 | return NULL; |
| 2055 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2056 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2059 | PyObject * |
| 2060 | _PyUnicode_FromId(_Py_Identifier *id) |
| 2061 | { |
| 2062 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 2063 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 2064 | strlen(id->string), |
| 2065 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2066 | if (!id->object) |
| 2067 | return NULL; |
| 2068 | PyUnicode_InternInPlace(&id->object); |
| 2069 | assert(!id->next); |
| 2070 | id->next = static_strings; |
| 2071 | static_strings = id; |
| 2072 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2073 | return id->object; |
| 2074 | } |
| 2075 | |
| 2076 | void |
| 2077 | _PyUnicode_ClearStaticStrings() |
| 2078 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2079 | _Py_Identifier *tmp, *s = static_strings; |
| 2080 | while (s) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 2081 | Py_CLEAR(s->object); |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2082 | tmp = s->next; |
| 2083 | s->next = NULL; |
| 2084 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2085 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 2086 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2087 | } |
| 2088 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 2089 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2090 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2091 | PyObject* |
| 2092 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2093 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 2094 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2095 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 2096 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 2097 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2098 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 2099 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 2100 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 2101 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2102 | unicode = PyUnicode_New(size, 127); |
| 2103 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2104 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 2105 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 2106 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 2107 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2108 | } |
| 2109 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2110 | static Py_UCS4 |
| 2111 | kind_maxchar_limit(unsigned int kind) |
| 2112 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2113 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2114 | case PyUnicode_1BYTE_KIND: |
| 2115 | return 0x80; |
| 2116 | case PyUnicode_2BYTE_KIND: |
| 2117 | return 0x100; |
| 2118 | case PyUnicode_4BYTE_KIND: |
| 2119 | return 0x10000; |
| 2120 | default: |
| 2121 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2122 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 2126 | Py_LOCAL_INLINE(Py_UCS4) |
| 2127 | align_maxchar(Py_UCS4 maxchar) |
| 2128 | { |
| 2129 | if (maxchar <= 127) |
| 2130 | return 127; |
| 2131 | else if (maxchar <= 255) |
| 2132 | return 255; |
| 2133 | else if (maxchar <= 65535) |
| 2134 | return 65535; |
| 2135 | else |
| 2136 | return MAX_UNICODE; |
| 2137 | } |
| 2138 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 2139 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 2140 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2141 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2142 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2143 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2144 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2145 | if (size == 0) |
| 2146 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2147 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 2148 | if (size == 1) |
| 2149 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2150 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2151 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2152 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2153 | if (!res) |
| 2154 | return NULL; |
| 2155 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2156 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2157 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2160 | static PyObject* |
| 2161 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 | { |
| 2163 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2164 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2165 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2166 | if (size == 0) |
| 2167 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2168 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2169 | if (size == 1) |
| 2170 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2171 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2172 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2173 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2174 | if (!res) |
| 2175 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2176 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2177 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2178 | else { |
| 2179 | _PyUnicode_CONVERT_BYTES( |
| 2180 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 2181 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2182 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2183 | return res; |
| 2184 | } |
| 2185 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2186 | static PyObject* |
| 2187 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2188 | { |
| 2189 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2190 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2191 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2192 | if (size == 0) |
| 2193 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2194 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2195 | if (size == 1) |
| 2196 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2197 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2198 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2199 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2200 | if (!res) |
| 2201 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2202 | if (max_char < 256) |
| 2203 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2204 | PyUnicode_1BYTE_DATA(res)); |
| 2205 | else if (max_char < 0x10000) |
| 2206 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2207 | PyUnicode_2BYTE_DATA(res)); |
| 2208 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2209 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2210 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2211 | return res; |
| 2212 | } |
| 2213 | |
| 2214 | PyObject* |
| 2215 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2216 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2217 | if (size < 0) { |
| 2218 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2219 | return NULL; |
| 2220 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2221 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2222 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2223 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2224 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2225 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2226 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2227 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2228 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2229 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2230 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2231 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2232 | } |
| 2233 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2234 | Py_UCS4 |
| 2235 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2236 | { |
| 2237 | enum PyUnicode_Kind kind; |
| 2238 | void *startptr, *endptr; |
| 2239 | |
| 2240 | assert(PyUnicode_IS_READY(unicode)); |
| 2241 | assert(0 <= start); |
| 2242 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2243 | assert(start <= end); |
| 2244 | |
| 2245 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2246 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2247 | |
| 2248 | if (start == end) |
| 2249 | return 127; |
| 2250 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2251 | if (PyUnicode_IS_ASCII(unicode)) |
| 2252 | return 127; |
| 2253 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2254 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2255 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2256 | endptr = (char *)startptr + end * kind; |
| 2257 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2258 | switch(kind) { |
| 2259 | case PyUnicode_1BYTE_KIND: |
| 2260 | return ucs1lib_find_max_char(startptr, endptr); |
| 2261 | case PyUnicode_2BYTE_KIND: |
| 2262 | return ucs2lib_find_max_char(startptr, endptr); |
| 2263 | case PyUnicode_4BYTE_KIND: |
| 2264 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2265 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2266 | assert(0); |
| 2267 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2268 | } |
| 2269 | } |
| 2270 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2271 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2272 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2273 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2274 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2275 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2276 | { |
| 2277 | PyObject *unicode, *copy; |
| 2278 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2279 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2280 | unsigned int kind; |
| 2281 | |
| 2282 | assert(p_unicode != NULL); |
| 2283 | unicode = *p_unicode; |
| 2284 | assert(PyUnicode_IS_READY(unicode)); |
| 2285 | if (PyUnicode_IS_ASCII(unicode)) |
| 2286 | return; |
| 2287 | |
| 2288 | len = PyUnicode_GET_LENGTH(unicode); |
| 2289 | kind = PyUnicode_KIND(unicode); |
| 2290 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2291 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2292 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2293 | if (max_char >= 128) |
| 2294 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2295 | } |
| 2296 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2297 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2298 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2299 | if (max_char >= 256) |
| 2300 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2301 | } |
| 2302 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2303 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2304 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2305 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2306 | if (max_char >= 0x10000) |
| 2307 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2308 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2309 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2310 | if (copy != NULL) |
| 2311 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2312 | Py_DECREF(unicode); |
| 2313 | *p_unicode = copy; |
| 2314 | } |
| 2315 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2316 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2317 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2318 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2319 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2320 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2321 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2322 | if (!PyUnicode_Check(unicode)) { |
| 2323 | PyErr_BadInternalCall(); |
| 2324 | return NULL; |
| 2325 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2326 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2327 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2328 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2329 | length = PyUnicode_GET_LENGTH(unicode); |
| 2330 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2331 | if (!copy) |
| 2332 | return NULL; |
| 2333 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2334 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2335 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2336 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2337 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2338 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2339 | } |
| 2340 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2341 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2342 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2343 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2344 | |
| 2345 | void* |
| 2346 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2347 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2348 | Py_ssize_t len; |
| 2349 | void *result; |
| 2350 | unsigned int skind; |
| 2351 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2352 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2353 | return NULL; |
| 2354 | |
| 2355 | len = PyUnicode_GET_LENGTH(s); |
| 2356 | skind = PyUnicode_KIND(s); |
| 2357 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2358 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2359 | return NULL; |
| 2360 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2361 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2362 | case PyUnicode_2BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2363 | result = PyMem_New(Py_UCS2, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2364 | if (!result) |
| 2365 | return PyErr_NoMemory(); |
| 2366 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2367 | _PyUnicode_CONVERT_BYTES( |
| 2368 | Py_UCS1, Py_UCS2, |
| 2369 | PyUnicode_1BYTE_DATA(s), |
| 2370 | PyUnicode_1BYTE_DATA(s) + len, |
| 2371 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2372 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2373 | case PyUnicode_4BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2374 | result = PyMem_New(Py_UCS4, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2375 | if (!result) |
| 2376 | return PyErr_NoMemory(); |
| 2377 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2378 | _PyUnicode_CONVERT_BYTES( |
| 2379 | Py_UCS2, Py_UCS4, |
| 2380 | PyUnicode_2BYTE_DATA(s), |
| 2381 | PyUnicode_2BYTE_DATA(s) + len, |
| 2382 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2383 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2384 | else { |
| 2385 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2386 | _PyUnicode_CONVERT_BYTES( |
| 2387 | Py_UCS1, Py_UCS4, |
| 2388 | PyUnicode_1BYTE_DATA(s), |
| 2389 | PyUnicode_1BYTE_DATA(s) + len, |
| 2390 | result); |
| 2391 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2392 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2393 | default: |
| 2394 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2395 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2396 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2397 | return NULL; |
| 2398 | } |
| 2399 | |
| 2400 | static Py_UCS4* |
| 2401 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2402 | int copy_null) |
| 2403 | { |
| 2404 | int kind; |
| 2405 | void *data; |
| 2406 | Py_ssize_t len, targetlen; |
| 2407 | if (PyUnicode_READY(string) == -1) |
| 2408 | return NULL; |
| 2409 | kind = PyUnicode_KIND(string); |
| 2410 | data = PyUnicode_DATA(string); |
| 2411 | len = PyUnicode_GET_LENGTH(string); |
| 2412 | targetlen = len; |
| 2413 | if (copy_null) |
| 2414 | targetlen++; |
| 2415 | if (!target) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2416 | target = PyMem_New(Py_UCS4, targetlen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2417 | if (!target) { |
| 2418 | PyErr_NoMemory(); |
| 2419 | return NULL; |
| 2420 | } |
| 2421 | } |
| 2422 | else { |
| 2423 | if (targetsize < targetlen) { |
| 2424 | PyErr_Format(PyExc_SystemError, |
| 2425 | "string is longer than the buffer"); |
| 2426 | if (copy_null && 0 < targetsize) |
| 2427 | target[0] = 0; |
| 2428 | return NULL; |
| 2429 | } |
| 2430 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2431 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2432 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2433 | _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] | 2434 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2435 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2436 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2437 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2438 | } |
| 2439 | else { |
| 2440 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2441 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2442 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2443 | if (copy_null) |
| 2444 | target[len] = 0; |
| 2445 | return target; |
| 2446 | } |
| 2447 | |
| 2448 | Py_UCS4* |
| 2449 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2450 | int copy_null) |
| 2451 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2452 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2453 | PyErr_BadInternalCall(); |
| 2454 | return NULL; |
| 2455 | } |
| 2456 | return as_ucs4(string, target, targetsize, copy_null); |
| 2457 | } |
| 2458 | |
| 2459 | Py_UCS4* |
| 2460 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2461 | { |
| 2462 | return as_ucs4(string, NULL, 0, 1); |
| 2463 | } |
| 2464 | |
| 2465 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2466 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2467 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2468 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2469 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2470 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2471 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2472 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2473 | PyErr_BadInternalCall(); |
| 2474 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2477 | if (size == -1) { |
| 2478 | size = wcslen(w); |
| 2479 | } |
| 2480 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2481 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2484 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2485 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2486 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2487 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2488 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2489 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2490 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2491 | static int |
| 2492 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2493 | Py_ssize_t width, Py_ssize_t precision) |
| 2494 | { |
| 2495 | Py_ssize_t length, fill, arglen; |
| 2496 | Py_UCS4 maxchar; |
| 2497 | |
| 2498 | if (PyUnicode_READY(str) == -1) |
| 2499 | return -1; |
| 2500 | |
| 2501 | length = PyUnicode_GET_LENGTH(str); |
| 2502 | if ((precision == -1 || precision >= length) |
| 2503 | && width <= length) |
| 2504 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2505 | |
| 2506 | if (precision != -1) |
| 2507 | length = Py_MIN(precision, length); |
| 2508 | |
| 2509 | arglen = Py_MAX(length, width); |
| 2510 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2511 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2512 | else |
| 2513 | maxchar = writer->maxchar; |
| 2514 | |
| 2515 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2516 | return -1; |
| 2517 | |
| 2518 | if (width > length) { |
| 2519 | fill = width - length; |
| 2520 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2521 | return -1; |
| 2522 | writer->pos += fill; |
| 2523 | } |
| 2524 | |
| 2525 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2526 | str, 0, length); |
| 2527 | writer->pos += length; |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
| 2531 | static int |
| 2532 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2533 | Py_ssize_t width, Py_ssize_t precision) |
| 2534 | { |
| 2535 | /* UTF-8 */ |
| 2536 | Py_ssize_t length; |
| 2537 | PyObject *unicode; |
| 2538 | int res; |
| 2539 | |
| 2540 | length = strlen(str); |
| 2541 | if (precision != -1) |
| 2542 | length = Py_MIN(length, precision); |
| 2543 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2544 | if (unicode == NULL) |
| 2545 | return -1; |
| 2546 | |
| 2547 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2548 | Py_DECREF(unicode); |
| 2549 | return res; |
| 2550 | } |
| 2551 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2552 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2553 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2554 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2555 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2556 | const char *p; |
| 2557 | Py_ssize_t len; |
| 2558 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2559 | Py_ssize_t width; |
| 2560 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2561 | int longflag; |
| 2562 | int longlongflag; |
| 2563 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2564 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2565 | |
| 2566 | p = f; |
| 2567 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2568 | zeropad = 0; |
| 2569 | if (*f == '0') { |
| 2570 | zeropad = 1; |
| 2571 | f++; |
| 2572 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2573 | |
| 2574 | /* 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] | 2575 | width = -1; |
| 2576 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2577 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2578 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2579 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2580 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2581 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2582 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2583 | return NULL; |
| 2584 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2585 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2586 | f++; |
| 2587 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2588 | } |
| 2589 | precision = -1; |
| 2590 | if (*f == '.') { |
| 2591 | f++; |
| 2592 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2593 | precision = (*f - '0'); |
| 2594 | f++; |
| 2595 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2596 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2597 | PyErr_SetString(PyExc_ValueError, |
| 2598 | "precision too big"); |
| 2599 | return NULL; |
| 2600 | } |
| 2601 | precision = (precision * 10) + (*f - '0'); |
| 2602 | f++; |
| 2603 | } |
| 2604 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2605 | if (*f == '%') { |
| 2606 | /* "%.3%s" => f points to "3" */ |
| 2607 | f--; |
| 2608 | } |
| 2609 | } |
| 2610 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2611 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2612 | f--; |
| 2613 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2614 | |
| 2615 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2616 | longflag = 0; |
| 2617 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2618 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2619 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2620 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2621 | longflag = 1; |
| 2622 | ++f; |
| 2623 | } |
| 2624 | #ifdef HAVE_LONG_LONG |
| 2625 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2626 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2627 | longlongflag = 1; |
| 2628 | f += 2; |
| 2629 | } |
| 2630 | #endif |
| 2631 | } |
| 2632 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2633 | 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] | 2634 | size_tflag = 1; |
| 2635 | ++f; |
| 2636 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2637 | |
| 2638 | if (f[1] == '\0') |
| 2639 | writer->overallocate = 0; |
| 2640 | |
| 2641 | switch (*f) { |
| 2642 | case 'c': |
| 2643 | { |
| 2644 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2645 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2646 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2647 | "character argument not in range(0x110000)"); |
| 2648 | return NULL; |
| 2649 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2650 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2651 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2652 | break; |
| 2653 | } |
| 2654 | |
| 2655 | case 'i': |
| 2656 | case 'd': |
| 2657 | case 'u': |
| 2658 | case 'x': |
| 2659 | { |
| 2660 | /* used by sprintf */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2661 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2662 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2663 | |
| 2664 | if (*f == 'u') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2665 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2666 | len = sprintf(buffer, "%lu", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2667 | va_arg(*vargs, unsigned long)); |
| 2668 | #ifdef HAVE_LONG_LONG |
| 2669 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2670 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2671 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2672 | #endif |
| 2673 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2674 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2675 | va_arg(*vargs, size_t)); |
| 2676 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2677 | len = sprintf(buffer, "%u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2678 | va_arg(*vargs, unsigned int)); |
| 2679 | } |
| 2680 | else if (*f == 'x') { |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2681 | len = sprintf(buffer, "%x", va_arg(*vargs, int)); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2682 | } |
| 2683 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2684 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2685 | len = sprintf(buffer, "%li", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2686 | va_arg(*vargs, long)); |
| 2687 | #ifdef HAVE_LONG_LONG |
| 2688 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2689 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2690 | va_arg(*vargs, PY_LONG_LONG)); |
| 2691 | #endif |
| 2692 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2693 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2694 | va_arg(*vargs, Py_ssize_t)); |
| 2695 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2696 | len = sprintf(buffer, "%i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2697 | va_arg(*vargs, int)); |
| 2698 | } |
| 2699 | assert(len >= 0); |
| 2700 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2701 | if (precision < len) |
| 2702 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2703 | |
| 2704 | arglen = Py_MAX(precision, width); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2705 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2706 | return NULL; |
| 2707 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2708 | if (width > precision) { |
| 2709 | Py_UCS4 fillchar; |
| 2710 | fill = width - precision; |
| 2711 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2712 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2713 | return NULL; |
| 2714 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2715 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2716 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2717 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2718 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2719 | return NULL; |
| 2720 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2721 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2722 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2723 | if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) |
| 2724 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2725 | break; |
| 2726 | } |
| 2727 | |
| 2728 | case 'p': |
| 2729 | { |
| 2730 | char number[MAX_LONG_LONG_CHARS]; |
| 2731 | |
| 2732 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2733 | assert(len >= 0); |
| 2734 | |
| 2735 | /* %p is ill-defined: ensure leading 0x. */ |
| 2736 | if (number[1] == 'X') |
| 2737 | number[1] = 'x'; |
| 2738 | else if (number[1] != 'x') { |
| 2739 | memmove(number + 2, number, |
| 2740 | strlen(number) + 1); |
| 2741 | number[0] = '0'; |
| 2742 | number[1] = 'x'; |
| 2743 | len += 2; |
| 2744 | } |
| 2745 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2746 | if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2747 | return NULL; |
| 2748 | break; |
| 2749 | } |
| 2750 | |
| 2751 | case 's': |
| 2752 | { |
| 2753 | /* UTF-8 */ |
| 2754 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2755 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2756 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2757 | break; |
| 2758 | } |
| 2759 | |
| 2760 | case 'U': |
| 2761 | { |
| 2762 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2763 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2764 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2765 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2766 | return NULL; |
| 2767 | break; |
| 2768 | } |
| 2769 | |
| 2770 | case 'V': |
| 2771 | { |
| 2772 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2773 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2774 | if (obj) { |
| 2775 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2776 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2777 | return NULL; |
| 2778 | } |
| 2779 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2780 | assert(str != NULL); |
| 2781 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2782 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2783 | } |
| 2784 | break; |
| 2785 | } |
| 2786 | |
| 2787 | case 'S': |
| 2788 | { |
| 2789 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2790 | PyObject *str; |
| 2791 | assert(obj); |
| 2792 | str = PyObject_Str(obj); |
| 2793 | if (!str) |
| 2794 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2795 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2796 | Py_DECREF(str); |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | Py_DECREF(str); |
| 2800 | break; |
| 2801 | } |
| 2802 | |
| 2803 | case 'R': |
| 2804 | { |
| 2805 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2806 | PyObject *repr; |
| 2807 | assert(obj); |
| 2808 | repr = PyObject_Repr(obj); |
| 2809 | if (!repr) |
| 2810 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2811 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2812 | Py_DECREF(repr); |
| 2813 | return NULL; |
| 2814 | } |
| 2815 | Py_DECREF(repr); |
| 2816 | break; |
| 2817 | } |
| 2818 | |
| 2819 | case 'A': |
| 2820 | { |
| 2821 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2822 | PyObject *ascii; |
| 2823 | assert(obj); |
| 2824 | ascii = PyObject_ASCII(obj); |
| 2825 | if (!ascii) |
| 2826 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2827 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2828 | Py_DECREF(ascii); |
| 2829 | return NULL; |
| 2830 | } |
| 2831 | Py_DECREF(ascii); |
| 2832 | break; |
| 2833 | } |
| 2834 | |
| 2835 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2836 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2837 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2838 | break; |
| 2839 | |
| 2840 | default: |
| 2841 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2842 | of the format string to the output string. (we cannot just |
| 2843 | skip the code, since there's no way to know what's in the |
| 2844 | argument list) */ |
| 2845 | len = strlen(p); |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2846 | if (_PyUnicodeWriter_WriteLatin1String(writer, p, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2847 | return NULL; |
| 2848 | f = p+len; |
| 2849 | return f; |
| 2850 | } |
| 2851 | |
| 2852 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2853 | return f; |
| 2854 | } |
| 2855 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2856 | PyObject * |
| 2857 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2858 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2859 | va_list vargs2; |
| 2860 | const char *f; |
| 2861 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2862 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2863 | _PyUnicodeWriter_Init(&writer); |
| 2864 | writer.min_length = strlen(format) + 100; |
| 2865 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2866 | |
| 2867 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2868 | Copy it to be able to pass a reference to a subfunction. */ |
| 2869 | Py_VA_COPY(vargs2, vargs); |
| 2870 | |
| 2871 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2872 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2873 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2874 | if (f == NULL) |
| 2875 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2876 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2877 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2878 | const char *p; |
| 2879 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2880 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2881 | p = f; |
| 2882 | do |
| 2883 | { |
| 2884 | if ((unsigned char)*p > 127) { |
| 2885 | PyErr_Format(PyExc_ValueError, |
| 2886 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2887 | "string, got a non-ASCII byte: 0x%02x", |
| 2888 | (unsigned char)*p); |
| 2889 | return NULL; |
| 2890 | } |
| 2891 | p++; |
| 2892 | } |
| 2893 | while (*p != '\0' && *p != '%'); |
| 2894 | len = p - f; |
| 2895 | |
| 2896 | if (*p == '\0') |
| 2897 | writer.overallocate = 0; |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2898 | |
| 2899 | if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2900 | goto fail; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2901 | |
| 2902 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2903 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2904 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2905 | return _PyUnicodeWriter_Finish(&writer); |
| 2906 | |
| 2907 | fail: |
| 2908 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2909 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2912 | PyObject * |
| 2913 | PyUnicode_FromFormat(const char *format, ...) |
| 2914 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2915 | PyObject* ret; |
| 2916 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2917 | |
| 2918 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2919 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2920 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2921 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2922 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2923 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2924 | va_end(vargs); |
| 2925 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2928 | #ifdef HAVE_WCHAR_H |
| 2929 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2930 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2931 | convert a Unicode object to a wide character string. |
| 2932 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2933 | - 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] | 2934 | character) required to convert the unicode object. Ignore size argument. |
| 2935 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2936 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2937 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2938 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2939 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2940 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2941 | wchar_t *w, |
| 2942 | Py_ssize_t size) |
| 2943 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2944 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2945 | const wchar_t *wstr; |
| 2946 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2947 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2948 | if (wstr == NULL) |
| 2949 | return -1; |
| 2950 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2951 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2952 | if (size > res) |
| 2953 | size = res + 1; |
| 2954 | else |
| 2955 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2956 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2957 | return res; |
| 2958 | } |
| 2959 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2960 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2964 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2965 | wchar_t *w, |
| 2966 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2967 | { |
| 2968 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2969 | PyErr_BadInternalCall(); |
| 2970 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2971 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2972 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2975 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2976 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2977 | Py_ssize_t *size) |
| 2978 | { |
| 2979 | wchar_t* buffer; |
| 2980 | Py_ssize_t buflen; |
| 2981 | |
| 2982 | if (unicode == NULL) { |
| 2983 | PyErr_BadInternalCall(); |
| 2984 | return NULL; |
| 2985 | } |
| 2986 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2987 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2988 | if (buflen == -1) |
| 2989 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2990 | buffer = PyMem_NEW(wchar_t, buflen); |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2991 | if (buffer == NULL) { |
| 2992 | PyErr_NoMemory(); |
| 2993 | return NULL; |
| 2994 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2995 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2996 | if (buflen == -1) { |
| 2997 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2998 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2999 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 3000 | if (size != NULL) |
| 3001 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 3002 | return buffer; |
| 3003 | } |
| 3004 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3005 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3006 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3007 | PyObject * |
| 3008 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3009 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3010 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3011 | PyErr_SetString(PyExc_ValueError, |
| 3012 | "chr() arg not in range(0x110000)"); |
| 3013 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3014 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 3015 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 3016 | return unicode_char((Py_UCS4)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3019 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3020 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3021 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3022 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3023 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3024 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3025 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 3026 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3027 | Py_INCREF(obj); |
| 3028 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3029 | } |
| 3030 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3031 | /* For a Unicode subtype that's not a Unicode object, |
| 3032 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 3033 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3034 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3035 | PyErr_Format(PyExc_TypeError, |
| 3036 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 3037 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3038 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3041 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3042 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3043 | const char *encoding, |
| 3044 | const char *errors) |
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 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3047 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3049 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3050 | PyErr_BadInternalCall(); |
| 3051 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3052 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3053 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3054 | /* Decoding bytes objects is the most common case and should be fast */ |
| 3055 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3056 | if (PyBytes_GET_SIZE(obj) == 0) |
| 3057 | _Py_RETURN_UNICODE_EMPTY(); |
| 3058 | v = PyUnicode_Decode( |
| 3059 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 3060 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3061 | return v; |
| 3062 | } |
| 3063 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3064 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3065 | PyErr_SetString(PyExc_TypeError, |
| 3066 | "decoding str is not supported"); |
| 3067 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3068 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 3069 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3070 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 3071 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 3072 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 3073 | "coercing to str: need a bytes-like object, %.80s found", |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3074 | Py_TYPE(obj)->tp_name); |
| 3075 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 3076 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3077 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3078 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3079 | PyBuffer_Release(&buffer); |
| 3080 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3081 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 3082 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 3083 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 3084 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 3085 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3088 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3089 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 3090 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3091 | int |
| 3092 | _Py_normalize_encoding(const char *encoding, |
| 3093 | char *lower, |
| 3094 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3095 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3096 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3097 | char *l; |
| 3098 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3099 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3100 | if (encoding == NULL) { |
Victor Stinner | 66b3270 | 2013-11-07 23:12:23 +0100 | [diff] [blame] | 3101 | /* 6 == strlen("utf-8") + 1 */ |
Victor Stinner | df23e30 | 2013-11-07 13:33:36 +0100 | [diff] [blame] | 3102 | if (lower_len < 6) |
| 3103 | return 0; |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 3104 | strcpy(lower, "utf-8"); |
| 3105 | return 1; |
| 3106 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3107 | e = encoding; |
| 3108 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3109 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3110 | while (*e) { |
| 3111 | if (l == l_end) |
| 3112 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3113 | if (Py_ISUPPER(*e)) { |
| 3114 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3115 | } |
| 3116 | else if (*e == '_') { |
| 3117 | *l++ = '-'; |
| 3118 | e++; |
| 3119 | } |
| 3120 | else { |
| 3121 | *l++ = *e++; |
| 3122 | } |
| 3123 | } |
| 3124 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3125 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3128 | PyObject * |
| 3129 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3130 | Py_ssize_t size, |
| 3131 | const char *encoding, |
| 3132 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3133 | { |
| 3134 | PyObject *buffer = NULL, *unicode; |
| 3135 | Py_buffer info; |
| 3136 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3137 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3138 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3139 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3140 | if ((strcmp(lower, "utf-8") == 0) || |
| 3141 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3142 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3143 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3144 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3145 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3146 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3147 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3148 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3149 | else if (strcmp(lower, "mbcs") == 0) |
| 3150 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3151 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3152 | else if (strcmp(lower, "ascii") == 0) |
| 3153 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3154 | else if (strcmp(lower, "utf-16") == 0) |
| 3155 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3156 | else if (strcmp(lower, "utf-32") == 0) |
| 3157 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3158 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3159 | |
| 3160 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3161 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3162 | 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] | 3163 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3164 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3165 | if (buffer == NULL) |
| 3166 | goto onError; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3167 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3168 | if (unicode == NULL) |
| 3169 | goto onError; |
| 3170 | if (!PyUnicode_Check(unicode)) { |
| 3171 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3172 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3173 | "use codecs.decode() to decode to arbitrary types", |
| 3174 | encoding, |
| 3175 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3176 | Py_DECREF(unicode); |
| 3177 | goto onError; |
| 3178 | } |
| 3179 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3180 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3181 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3182 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3183 | Py_XDECREF(buffer); |
| 3184 | return NULL; |
| 3185 | } |
| 3186 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3187 | PyObject * |
| 3188 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3189 | const char *encoding, |
| 3190 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3191 | { |
| 3192 | PyObject *v; |
| 3193 | |
| 3194 | if (!PyUnicode_Check(unicode)) { |
| 3195 | PyErr_BadArgument(); |
| 3196 | goto onError; |
| 3197 | } |
| 3198 | |
| 3199 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3200 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3201 | |
| 3202 | /* Decode via the codec registry */ |
| 3203 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3204 | if (v == NULL) |
| 3205 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3206 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3207 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3208 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3209 | return NULL; |
| 3210 | } |
| 3211 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3212 | PyObject * |
| 3213 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3214 | const char *encoding, |
| 3215 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3216 | { |
| 3217 | PyObject *v; |
| 3218 | |
| 3219 | if (!PyUnicode_Check(unicode)) { |
| 3220 | PyErr_BadArgument(); |
| 3221 | goto onError; |
| 3222 | } |
| 3223 | |
| 3224 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3225 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3226 | |
| 3227 | /* Decode via the codec registry */ |
| 3228 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3229 | if (v == NULL) |
| 3230 | goto onError; |
| 3231 | if (!PyUnicode_Check(v)) { |
| 3232 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3233 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3234 | "use codecs.decode() to decode to arbitrary types", |
| 3235 | encoding, |
| 3236 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3237 | Py_DECREF(v); |
| 3238 | goto onError; |
| 3239 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3240 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3241 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3242 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3243 | return NULL; |
| 3244 | } |
| 3245 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3246 | PyObject * |
| 3247 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3248 | Py_ssize_t size, |
| 3249 | const char *encoding, |
| 3250 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3251 | { |
| 3252 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3254 | unicode = PyUnicode_FromUnicode(s, size); |
| 3255 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3256 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3257 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3258 | Py_DECREF(unicode); |
| 3259 | return v; |
| 3260 | } |
| 3261 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3262 | PyObject * |
| 3263 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3264 | const char *encoding, |
| 3265 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3266 | { |
| 3267 | PyObject *v; |
| 3268 | |
| 3269 | if (!PyUnicode_Check(unicode)) { |
| 3270 | PyErr_BadArgument(); |
| 3271 | goto onError; |
| 3272 | } |
| 3273 | |
| 3274 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3275 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3276 | |
| 3277 | /* Encode via the codec registry */ |
| 3278 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3279 | if (v == NULL) |
| 3280 | goto onError; |
| 3281 | return v; |
| 3282 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3283 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3284 | return NULL; |
| 3285 | } |
| 3286 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3287 | static size_t |
| 3288 | wcstombs_errorpos(const wchar_t *wstr) |
| 3289 | { |
| 3290 | size_t len; |
| 3291 | #if SIZEOF_WCHAR_T == 2 |
| 3292 | wchar_t buf[3]; |
| 3293 | #else |
| 3294 | wchar_t buf[2]; |
| 3295 | #endif |
| 3296 | char outbuf[MB_LEN_MAX]; |
| 3297 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3298 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3299 | #if SIZEOF_WCHAR_T == 2 |
| 3300 | buf[2] = 0; |
| 3301 | #else |
| 3302 | buf[1] = 0; |
| 3303 | #endif |
| 3304 | start = wstr; |
| 3305 | while (*wstr != L'\0') |
| 3306 | { |
| 3307 | previous = wstr; |
| 3308 | #if SIZEOF_WCHAR_T == 2 |
| 3309 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3310 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3311 | { |
| 3312 | buf[0] = wstr[0]; |
| 3313 | buf[1] = wstr[1]; |
| 3314 | wstr += 2; |
| 3315 | } |
| 3316 | else { |
| 3317 | buf[0] = *wstr; |
| 3318 | buf[1] = 0; |
| 3319 | wstr++; |
| 3320 | } |
| 3321 | #else |
| 3322 | buf[0] = *wstr; |
| 3323 | wstr++; |
| 3324 | #endif |
| 3325 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3326 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3327 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3331 | return 0; |
| 3332 | } |
| 3333 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3334 | static int |
| 3335 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3336 | { |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3337 | _Py_error_handler error_handler = get_error_handler(errors); |
| 3338 | switch (error_handler) |
| 3339 | { |
| 3340 | case _Py_ERROR_STRICT: |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3341 | *surrogateescape = 0; |
| 3342 | return 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3343 | case _Py_ERROR_SURROGATEESCAPE: |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3344 | *surrogateescape = 1; |
| 3345 | return 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 3346 | default: |
| 3347 | PyErr_Format(PyExc_ValueError, |
| 3348 | "only 'strict' and 'surrogateescape' error handlers " |
| 3349 | "are supported, not '%s'", |
| 3350 | errors); |
| 3351 | return -1; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3352 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3353 | } |
| 3354 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3355 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3356 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3357 | { |
| 3358 | Py_ssize_t wlen, wlen2; |
| 3359 | wchar_t *wstr; |
| 3360 | PyObject *bytes = NULL; |
| 3361 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3362 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3363 | PyObject *exc; |
| 3364 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3365 | int surrogateescape; |
| 3366 | |
| 3367 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3368 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3369 | |
| 3370 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3371 | if (wstr == NULL) |
| 3372 | return NULL; |
| 3373 | |
| 3374 | wlen2 = wcslen(wstr); |
| 3375 | if (wlen2 != wlen) { |
| 3376 | PyMem_Free(wstr); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3377 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3378 | return NULL; |
| 3379 | } |
| 3380 | |
| 3381 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3382 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3383 | char *str; |
| 3384 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3385 | str = Py_EncodeLocale(wstr, &error_pos); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3386 | if (str == NULL) { |
| 3387 | if (error_pos == (size_t)-1) { |
| 3388 | PyErr_NoMemory(); |
| 3389 | PyMem_Free(wstr); |
| 3390 | return NULL; |
| 3391 | } |
| 3392 | else { |
| 3393 | goto encode_error; |
| 3394 | } |
| 3395 | } |
| 3396 | PyMem_Free(wstr); |
| 3397 | |
| 3398 | bytes = PyBytes_FromString(str); |
| 3399 | PyMem_Free(str); |
| 3400 | } |
| 3401 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3402 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3403 | size_t len, len2; |
| 3404 | |
| 3405 | len = wcstombs(NULL, wstr, 0); |
| 3406 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3407 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3408 | goto encode_error; |
| 3409 | } |
| 3410 | |
| 3411 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3412 | if (bytes == NULL) { |
| 3413 | PyMem_Free(wstr); |
| 3414 | return NULL; |
| 3415 | } |
| 3416 | |
| 3417 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3418 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3419 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3420 | goto encode_error; |
| 3421 | } |
| 3422 | PyMem_Free(wstr); |
| 3423 | } |
| 3424 | return bytes; |
| 3425 | |
| 3426 | encode_error: |
| 3427 | errmsg = strerror(errno); |
| 3428 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3429 | |
| 3430 | if (error_pos == (size_t)-1) |
| 3431 | error_pos = wcstombs_errorpos(wstr); |
| 3432 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3433 | PyMem_Free(wstr); |
| 3434 | Py_XDECREF(bytes); |
| 3435 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3436 | if (errmsg != NULL) { |
| 3437 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3438 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3439 | if (wstr != NULL) { |
| 3440 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3441 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3442 | } else |
| 3443 | errmsg = NULL; |
| 3444 | } |
| 3445 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3446 | reason = PyUnicode_FromString( |
| 3447 | "wcstombs() encountered an unencodable " |
| 3448 | "wide character"); |
| 3449 | if (reason == NULL) |
| 3450 | return NULL; |
| 3451 | |
| 3452 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3453 | "locale", unicode, |
| 3454 | (Py_ssize_t)error_pos, |
| 3455 | (Py_ssize_t)(error_pos+1), |
| 3456 | reason); |
| 3457 | Py_DECREF(reason); |
| 3458 | if (exc != NULL) { |
| 3459 | PyCodec_StrictErrors(exc); |
| 3460 | Py_XDECREF(exc); |
| 3461 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3462 | return NULL; |
| 3463 | } |
| 3464 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3465 | PyObject * |
| 3466 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3467 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3468 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3469 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3470 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3471 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3472 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3473 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3474 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3475 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3476 | the Python codec requires to encode at least its own filename. Use the C |
| 3477 | version of the locale codec until the codec registry is initialized and |
| 3478 | the Python codec is loaded. |
| 3479 | |
| 3480 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3481 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3482 | subinterpreters. */ |
| 3483 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3484 | return PyUnicode_AsEncodedString(unicode, |
| 3485 | Py_FileSystemDefaultEncoding, |
| 3486 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3487 | } |
| 3488 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3489 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3490 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3491 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3494 | PyObject * |
| 3495 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3496 | const char *encoding, |
| 3497 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3498 | { |
| 3499 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3500 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3502 | if (!PyUnicode_Check(unicode)) { |
| 3503 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3504 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3505 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3506 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3507 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3508 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3509 | if ((strcmp(lower, "utf-8") == 0) || |
| 3510 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3511 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3512 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3513 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3514 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3515 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3516 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3517 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3518 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3519 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3520 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3521 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3522 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3523 | else if (strcmp(lower, "mbcs") == 0) |
| 3524 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3525 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3526 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3527 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3528 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3529 | |
| 3530 | /* Encode via the codec registry */ |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3531 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3532 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3533 | return NULL; |
| 3534 | |
| 3535 | /* The normal path */ |
| 3536 | if (PyBytes_Check(v)) |
| 3537 | return v; |
| 3538 | |
| 3539 | /* 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] | 3540 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3541 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3542 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3543 | |
| 3544 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3545 | "encoder %s returned bytearray instead of bytes; " |
| 3546 | "use codecs.encode() to encode to arbitrary types", |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3547 | encoding); |
| 3548 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3549 | Py_DECREF(v); |
| 3550 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3551 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3552 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3553 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3554 | Py_DECREF(v); |
| 3555 | return b; |
| 3556 | } |
| 3557 | |
| 3558 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3559 | "'%.400s' encoder returned '%.400s' instead of 'bytes'; " |
| 3560 | "use codecs.encode() to encode to arbitrary types", |
| 3561 | encoding, |
| 3562 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3563 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3564 | return NULL; |
| 3565 | } |
| 3566 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3567 | PyObject * |
| 3568 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3569 | const char *encoding, |
| 3570 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3571 | { |
| 3572 | PyObject *v; |
| 3573 | |
| 3574 | if (!PyUnicode_Check(unicode)) { |
| 3575 | PyErr_BadArgument(); |
| 3576 | goto onError; |
| 3577 | } |
| 3578 | |
| 3579 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3580 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3581 | |
| 3582 | /* Encode via the codec registry */ |
| 3583 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3584 | if (v == NULL) |
| 3585 | goto onError; |
| 3586 | if (!PyUnicode_Check(v)) { |
| 3587 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3588 | "'%.400s' encoder returned '%.400s' instead of 'str'; " |
| 3589 | "use codecs.encode() to encode to arbitrary types", |
| 3590 | encoding, |
| 3591 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3592 | Py_DECREF(v); |
| 3593 | goto onError; |
| 3594 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3595 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3596 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3597 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3598 | return NULL; |
| 3599 | } |
| 3600 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3601 | static size_t |
| 3602 | mbstowcs_errorpos(const char *str, size_t len) |
| 3603 | { |
| 3604 | #ifdef HAVE_MBRTOWC |
| 3605 | const char *start = str; |
| 3606 | mbstate_t mbs; |
| 3607 | size_t converted; |
| 3608 | wchar_t ch; |
| 3609 | |
| 3610 | memset(&mbs, 0, sizeof mbs); |
| 3611 | while (len) |
| 3612 | { |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3613 | converted = mbrtowc(&ch, str, len, &mbs); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3614 | if (converted == 0) |
| 3615 | /* Reached end of string */ |
| 3616 | break; |
| 3617 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3618 | /* Conversion error or incomplete character */ |
| 3619 | return str - start; |
| 3620 | } |
| 3621 | else { |
| 3622 | str += converted; |
| 3623 | len -= converted; |
| 3624 | } |
| 3625 | } |
| 3626 | /* failed to find the undecodable byte sequence */ |
| 3627 | return 0; |
| 3628 | #endif |
| 3629 | return 0; |
| 3630 | } |
| 3631 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3632 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3633 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3634 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3635 | { |
| 3636 | wchar_t smallbuf[256]; |
| 3637 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3638 | wchar_t *wstr; |
| 3639 | size_t wlen, wlen2; |
| 3640 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3641 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3642 | size_t error_pos; |
| 3643 | char *errmsg; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 3644 | PyObject *reason = NULL; /* initialize to prevent gcc warning */ |
| 3645 | PyObject *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3646 | |
| 3647 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3648 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3649 | |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3650 | if (str[len] != '\0' || (size_t)len != strlen(str)) { |
| 3651 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3652 | return NULL; |
| 3653 | } |
| 3654 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3655 | if (surrogateescape) { |
| 3656 | /* "surrogateescape" error handler */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3657 | wstr = Py_DecodeLocale(str, &wlen); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3658 | if (wstr == NULL) { |
| 3659 | if (wlen == (size_t)-1) |
| 3660 | PyErr_NoMemory(); |
| 3661 | else |
| 3662 | PyErr_SetFromErrno(PyExc_OSError); |
| 3663 | return NULL; |
| 3664 | } |
| 3665 | |
| 3666 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3667 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3668 | } |
| 3669 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3670 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3671 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3672 | wlen = mbstowcs(NULL, str, 0); |
| 3673 | #else |
| 3674 | wlen = len; |
| 3675 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3676 | if (wlen == (size_t)-1) |
| 3677 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3678 | if (wlen+1 <= smallbuf_len) { |
| 3679 | wstr = smallbuf; |
| 3680 | } |
| 3681 | else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3682 | wstr = PyMem_New(wchar_t, wlen+1); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3683 | if (!wstr) |
| 3684 | return PyErr_NoMemory(); |
| 3685 | } |
| 3686 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3687 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3688 | if (wlen2 == (size_t)-1) { |
| 3689 | if (wstr != smallbuf) |
| 3690 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3691 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3692 | } |
| 3693 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3694 | assert(wlen2 == wlen); |
| 3695 | #endif |
| 3696 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3697 | if (wstr != smallbuf) |
| 3698 | PyMem_Free(wstr); |
| 3699 | } |
| 3700 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3701 | |
| 3702 | decode_error: |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3703 | reason = NULL; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3704 | errmsg = strerror(errno); |
| 3705 | assert(errmsg != NULL); |
| 3706 | |
| 3707 | error_pos = mbstowcs_errorpos(str, len); |
| 3708 | if (errmsg != NULL) { |
| 3709 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3710 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3711 | if (wstr != NULL) { |
| 3712 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3713 | PyMem_RawFree(wstr); |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3714 | } |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3715 | } |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3716 | if (reason == NULL) |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3717 | reason = PyUnicode_FromString( |
| 3718 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3719 | if (reason == NULL) |
| 3720 | return NULL; |
| 3721 | |
| 3722 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3723 | "locale", str, len, |
| 3724 | (Py_ssize_t)error_pos, |
| 3725 | (Py_ssize_t)(error_pos+1), |
| 3726 | reason); |
| 3727 | Py_DECREF(reason); |
| 3728 | if (exc != NULL) { |
| 3729 | PyCodec_StrictErrors(exc); |
| 3730 | Py_XDECREF(exc); |
| 3731 | } |
| 3732 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3736 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3737 | { |
| 3738 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3739 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | |
| 3743 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3744 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3745 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3746 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3747 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3748 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3749 | PyObject* |
| 3750 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3751 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3752 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3753 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3754 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3755 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3756 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3757 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3758 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3759 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3760 | the Python codec requires to encode at least its own filename. Use the C |
| 3761 | version of the locale codec until the codec registry is initialized and |
| 3762 | the Python codec is loaded. |
| 3763 | |
| 3764 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3765 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3766 | subinterpreters. */ |
| 3767 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3768 | return PyUnicode_Decode(s, size, |
| 3769 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3770 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3771 | } |
| 3772 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3773 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3774 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3775 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3778 | |
| 3779 | int |
| 3780 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3781 | { |
| 3782 | PyObject *output = NULL; |
| 3783 | Py_ssize_t size; |
| 3784 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3785 | if (arg == NULL) { |
| 3786 | Py_DECREF(*(PyObject**)addr); |
| 3787 | return 1; |
| 3788 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3789 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3790 | output = arg; |
| 3791 | Py_INCREF(output); |
| 3792 | } |
| 3793 | else { |
| 3794 | arg = PyUnicode_FromObject(arg); |
| 3795 | if (!arg) |
| 3796 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3797 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3798 | Py_DECREF(arg); |
| 3799 | if (!output) |
| 3800 | return 0; |
| 3801 | if (!PyBytes_Check(output)) { |
| 3802 | Py_DECREF(output); |
| 3803 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3804 | return 0; |
| 3805 | } |
| 3806 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3807 | size = PyBytes_GET_SIZE(output); |
| 3808 | data = PyBytes_AS_STRING(output); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 3809 | if ((size_t)size != strlen(data)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3810 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3811 | Py_DECREF(output); |
| 3812 | return 0; |
| 3813 | } |
| 3814 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3815 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3819 | int |
| 3820 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3821 | { |
| 3822 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3823 | if (arg == NULL) { |
| 3824 | Py_DECREF(*(PyObject**)addr); |
| 3825 | return 1; |
| 3826 | } |
| 3827 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3828 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3829 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3830 | output = arg; |
| 3831 | Py_INCREF(output); |
| 3832 | } |
| 3833 | else { |
| 3834 | arg = PyBytes_FromObject(arg); |
| 3835 | if (!arg) |
| 3836 | return 0; |
| 3837 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3838 | PyBytes_GET_SIZE(arg)); |
| 3839 | Py_DECREF(arg); |
| 3840 | if (!output) |
| 3841 | return 0; |
| 3842 | if (!PyUnicode_Check(output)) { |
| 3843 | Py_DECREF(output); |
| 3844 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3845 | return 0; |
| 3846 | } |
| 3847 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3848 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3849 | Py_DECREF(output); |
| 3850 | return 0; |
| 3851 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3852 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3853 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3854 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3855 | Py_DECREF(output); |
| 3856 | return 0; |
| 3857 | } |
| 3858 | *(PyObject**)addr = output; |
| 3859 | return Py_CLEANUP_SUPPORTED; |
| 3860 | } |
| 3861 | |
| 3862 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3863 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3864 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3865 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3866 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3867 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3868 | if (!PyUnicode_Check(unicode)) { |
| 3869 | PyErr_BadArgument(); |
| 3870 | return NULL; |
| 3871 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3872 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3873 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3874 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3875 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3876 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3877 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3878 | if (bytes == NULL) |
| 3879 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3880 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3881 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3882 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3883 | Py_DECREF(bytes); |
| 3884 | return NULL; |
| 3885 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3886 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3887 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3888 | PyBytes_AS_STRING(bytes), |
| 3889 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3890 | Py_DECREF(bytes); |
| 3891 | } |
| 3892 | |
| 3893 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3894 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3895 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3899 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3900 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3901 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3902 | } |
| 3903 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3904 | Py_UNICODE * |
| 3905 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3906 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3907 | const unsigned char *one_byte; |
| 3908 | #if SIZEOF_WCHAR_T == 4 |
| 3909 | const Py_UCS2 *two_bytes; |
| 3910 | #else |
| 3911 | const Py_UCS4 *four_bytes; |
| 3912 | const Py_UCS4 *ucs4_end; |
| 3913 | Py_ssize_t num_surrogates; |
| 3914 | #endif |
| 3915 | wchar_t *w; |
| 3916 | wchar_t *wchar_end; |
| 3917 | |
| 3918 | if (!PyUnicode_Check(unicode)) { |
| 3919 | PyErr_BadArgument(); |
| 3920 | return NULL; |
| 3921 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3922 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3923 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3924 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3925 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3926 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3927 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3928 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3929 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3930 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3931 | num_surrogates = 0; |
| 3932 | |
| 3933 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3934 | if (*four_bytes > 0xFFFF) |
| 3935 | ++num_surrogates; |
| 3936 | } |
| 3937 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3938 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3939 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3940 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3941 | PyErr_NoMemory(); |
| 3942 | return NULL; |
| 3943 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3944 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3945 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3946 | w = _PyUnicode_WSTR(unicode); |
| 3947 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3948 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3949 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3950 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3951 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3952 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3953 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3954 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3955 | } |
| 3956 | else |
| 3957 | *w = *four_bytes; |
| 3958 | |
| 3959 | if (w > wchar_end) { |
| 3960 | assert(0 && "Miscalculated string end"); |
| 3961 | } |
| 3962 | } |
| 3963 | *w = 0; |
| 3964 | #else |
| 3965 | /* sizeof(wchar_t) == 4 */ |
| 3966 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3967 | "should share memory already."); |
| 3968 | return NULL; |
| 3969 | #endif |
| 3970 | } |
| 3971 | else { |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 3972 | if ((size_t)_PyUnicode_LENGTH(unicode) > |
| 3973 | PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 3974 | PyErr_NoMemory(); |
| 3975 | return NULL; |
| 3976 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3977 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3978 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3979 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3980 | PyErr_NoMemory(); |
| 3981 | return NULL; |
| 3982 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3983 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3984 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3985 | w = _PyUnicode_WSTR(unicode); |
| 3986 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3987 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3988 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3989 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3990 | for (; w < wchar_end; ++one_byte, ++w) |
| 3991 | *w = *one_byte; |
| 3992 | /* null-terminate the wstr */ |
| 3993 | *w = 0; |
| 3994 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3995 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3996 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3997 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3998 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3999 | *w = *two_bytes; |
| 4000 | /* null-terminate the wstr */ |
| 4001 | *w = 0; |
| 4002 | #else |
| 4003 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 4004 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 4005 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4006 | Py_FatalError("Impossible unicode object state, wstr " |
| 4007 | "and str should share memory already."); |
| 4008 | return NULL; |
| 4009 | #endif |
| 4010 | } |
| 4011 | else { |
| 4012 | assert(0 && "This should never happen."); |
| 4013 | } |
| 4014 | } |
| 4015 | } |
| 4016 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 4017 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 4018 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4021 | Py_UNICODE * |
| 4022 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4023 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4024 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4027 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4028 | Py_ssize_t |
| 4029 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4030 | { |
| 4031 | if (!PyUnicode_Check(unicode)) { |
| 4032 | PyErr_BadArgument(); |
| 4033 | goto onError; |
| 4034 | } |
| 4035 | return PyUnicode_GET_SIZE(unicode); |
| 4036 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4037 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4038 | return -1; |
| 4039 | } |
| 4040 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4041 | Py_ssize_t |
| 4042 | PyUnicode_GetLength(PyObject *unicode) |
| 4043 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4044 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4045 | PyErr_BadArgument(); |
| 4046 | return -1; |
| 4047 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 4048 | if (PyUnicode_READY(unicode) == -1) |
| 4049 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4050 | return PyUnicode_GET_LENGTH(unicode); |
| 4051 | } |
| 4052 | |
| 4053 | Py_UCS4 |
| 4054 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 4055 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 4056 | void *data; |
| 4057 | int kind; |
| 4058 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4059 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 4060 | PyErr_BadArgument(); |
| 4061 | return (Py_UCS4)-1; |
| 4062 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4063 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 4064 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4065 | return (Py_UCS4)-1; |
| 4066 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 4067 | data = PyUnicode_DATA(unicode); |
| 4068 | kind = PyUnicode_KIND(unicode); |
| 4069 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | int |
| 4073 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 4074 | { |
| 4075 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4076 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4077 | return -1; |
| 4078 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4079 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 4080 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4081 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 4082 | return -1; |
| 4083 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 4084 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 4085 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 4086 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 4087 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 4088 | return -1; |
| 4089 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4090 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 4091 | index, ch); |
| 4092 | return 0; |
| 4093 | } |
| 4094 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4095 | const char * |
| 4096 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4097 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 4098 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4101 | /* create or adjust a UnicodeDecodeError */ |
| 4102 | static void |
| 4103 | make_decode_exception(PyObject **exceptionObject, |
| 4104 | const char *encoding, |
| 4105 | const char *input, Py_ssize_t length, |
| 4106 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4107 | const char *reason) |
| 4108 | { |
| 4109 | if (*exceptionObject == NULL) { |
| 4110 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 4111 | encoding, input, length, startpos, endpos, reason); |
| 4112 | } |
| 4113 | else { |
| 4114 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 4115 | goto onError; |
| 4116 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 4117 | goto onError; |
| 4118 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4119 | goto onError; |
| 4120 | } |
| 4121 | return; |
| 4122 | |
| 4123 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4124 | Py_CLEAR(*exceptionObject); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4127 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4128 | /* error handling callback helper: |
| 4129 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4130 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4131 | and adjust various state variables. |
| 4132 | return 0 on success, -1 on error |
| 4133 | */ |
| 4134 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4135 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4136 | unicode_decode_call_errorhandler_wchar( |
| 4137 | const char *errors, PyObject **errorHandler, |
| 4138 | const char *encoding, const char *reason, |
| 4139 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4140 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4141 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4142 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4143 | 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] | 4144 | |
| 4145 | PyObject *restuple = NULL; |
| 4146 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4147 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4148 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4149 | Py_ssize_t requiredsize; |
| 4150 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4151 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4152 | wchar_t *repwstr; |
| 4153 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4154 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4155 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 4156 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4157 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4158 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4159 | *errorHandler = PyCodec_LookupError(errors); |
| 4160 | if (*errorHandler == NULL) |
| 4161 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4164 | make_decode_exception(exceptionObject, |
| 4165 | encoding, |
| 4166 | *input, *inend - *input, |
| 4167 | *startinpos, *endinpos, |
| 4168 | reason); |
| 4169 | if (*exceptionObject == NULL) |
| 4170 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4171 | |
| 4172 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4173 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4174 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4175 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4176 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4177 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4178 | } |
| 4179 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4180 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4181 | |
| 4182 | /* Copy back the bytes variables, which might have been modified by the |
| 4183 | callback */ |
| 4184 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4185 | if (!inputobj) |
| 4186 | goto onError; |
| 4187 | if (!PyBytes_Check(inputobj)) { |
| 4188 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4189 | } |
| 4190 | *input = PyBytes_AS_STRING(inputobj); |
| 4191 | insize = PyBytes_GET_SIZE(inputobj); |
| 4192 | *inend = *input + insize; |
| 4193 | /* we can DECREF safely, as the exception has another reference, |
| 4194 | so the object won't go away. */ |
| 4195 | Py_DECREF(inputobj); |
| 4196 | |
| 4197 | if (newpos<0) |
| 4198 | newpos = insize+newpos; |
| 4199 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4200 | 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] | 4201 | goto onError; |
| 4202 | } |
| 4203 | |
| 4204 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4205 | if (repwstr == NULL) |
| 4206 | goto onError; |
| 4207 | /* need more space? (at least enough for what we |
| 4208 | have+the replacement+the rest of the string (starting |
| 4209 | at the new input position), so we won't have to check space |
| 4210 | when there are no errors in the rest of the string) */ |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4211 | requiredsize = *outpos; |
| 4212 | if (requiredsize > PY_SSIZE_T_MAX - repwlen) |
| 4213 | goto overflow; |
| 4214 | requiredsize += repwlen; |
| 4215 | if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) |
| 4216 | goto overflow; |
| 4217 | requiredsize += insize - newpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4218 | if (requiredsize > outsize) { |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4219 | if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4220 | requiredsize = 2*outsize; |
| 4221 | if (unicode_resize(output, requiredsize) < 0) |
| 4222 | goto onError; |
| 4223 | } |
| 4224 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4225 | *outpos += repwlen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4226 | *endinpos = newpos; |
| 4227 | *inptr = *input + newpos; |
| 4228 | |
| 4229 | /* we made it! */ |
| 4230 | Py_XDECREF(restuple); |
| 4231 | return 0; |
| 4232 | |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4233 | overflow: |
| 4234 | PyErr_SetString(PyExc_OverflowError, |
| 4235 | "decoded result is too long for a Python string"); |
| 4236 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4237 | onError: |
| 4238 | Py_XDECREF(restuple); |
| 4239 | return -1; |
| 4240 | } |
| 4241 | #endif /* HAVE_MBCS */ |
| 4242 | |
| 4243 | static int |
| 4244 | unicode_decode_call_errorhandler_writer( |
| 4245 | const char *errors, PyObject **errorHandler, |
| 4246 | const char *encoding, const char *reason, |
| 4247 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4248 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4249 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4250 | { |
| 4251 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4252 | |
| 4253 | PyObject *restuple = NULL; |
| 4254 | PyObject *repunicode = NULL; |
| 4255 | Py_ssize_t insize; |
| 4256 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4257 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4258 | PyObject *inputobj = NULL; |
| 4259 | |
| 4260 | if (*errorHandler == NULL) { |
| 4261 | *errorHandler = PyCodec_LookupError(errors); |
| 4262 | if (*errorHandler == NULL) |
| 4263 | goto onError; |
| 4264 | } |
| 4265 | |
| 4266 | make_decode_exception(exceptionObject, |
| 4267 | encoding, |
| 4268 | *input, *inend - *input, |
| 4269 | *startinpos, *endinpos, |
| 4270 | reason); |
| 4271 | if (*exceptionObject == NULL) |
| 4272 | goto onError; |
| 4273 | |
| 4274 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4275 | if (restuple == NULL) |
| 4276 | goto onError; |
| 4277 | if (!PyTuple_Check(restuple)) { |
| 4278 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4279 | goto onError; |
| 4280 | } |
| 4281 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4282 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4283 | |
| 4284 | /* Copy back the bytes variables, which might have been modified by the |
| 4285 | callback */ |
| 4286 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4287 | if (!inputobj) |
| 4288 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4289 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4290 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4291 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4292 | *input = PyBytes_AS_STRING(inputobj); |
| 4293 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4294 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4295 | /* we can DECREF safely, as the exception has another reference, |
| 4296 | so the object won't go away. */ |
| 4297 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4298 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4299 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4300 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4301 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4302 | 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] | 4303 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4304 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4305 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4306 | if (PyUnicode_READY(repunicode) < 0) |
| 4307 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4308 | replen = PyUnicode_GET_LENGTH(repunicode); |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4309 | if (replen > 1) { |
| 4310 | writer->min_length += replen - 1; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4311 | writer->overallocate = 1; |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4312 | if (_PyUnicodeWriter_Prepare(writer, writer->min_length, |
| 4313 | PyUnicode_MAX_CHAR_VALUE(repunicode)) == -1) |
| 4314 | goto onError; |
| 4315 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4316 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4317 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4318 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4319 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4320 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4321 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4322 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4323 | Py_XDECREF(restuple); |
| 4324 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4325 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4326 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4327 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4328 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4331 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4332 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4333 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4334 | |
| 4335 | /* Three simple macros defining base-64. */ |
| 4336 | |
| 4337 | /* Is c a base-64 character? */ |
| 4338 | |
| 4339 | #define IS_BASE64(c) \ |
| 4340 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4341 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4342 | ((c) >= '0' && (c) <= '9') || \ |
| 4343 | (c) == '+' || (c) == '/') |
| 4344 | |
| 4345 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4346 | |
| 4347 | #define FROM_BASE64(c) \ |
| 4348 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4349 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4350 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4351 | (c) == '+' ? 62 : 63) |
| 4352 | |
| 4353 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4354 | |
| 4355 | #define TO_BASE64(n) \ |
| 4356 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4357 | |
| 4358 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4359 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4360 | * byte not decoding to itself is the + which begins a base64 |
| 4361 | * string. */ |
| 4362 | |
| 4363 | #define DECODE_DIRECT(c) \ |
| 4364 | ((c) <= 127 && (c) != '+') |
| 4365 | |
| 4366 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4367 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4368 | * the above). See RFC2152. This array identifies these different |
| 4369 | * sets: |
| 4370 | * 0 : "Set D" |
| 4371 | * alphanumeric and '(),-./:? |
| 4372 | * 1 : "Set O" |
| 4373 | * !"#$%&*;<=>@[]^_`{|} |
| 4374 | * 2 : "whitespace" |
| 4375 | * ht nl cr sp |
| 4376 | * 3 : special (must be base64 encoded) |
| 4377 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4378 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4379 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4380 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4381 | char utf7_category[128] = { |
| 4382 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4383 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4384 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4385 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4386 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4387 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4388 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4389 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4390 | /* @ A B C D E F G H I J K L M N O */ |
| 4391 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4392 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4393 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4394 | /* ` a b c d e f g h i j k l m n o */ |
| 4395 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4396 | /* p q r s t u v w x y z { | } ~ del */ |
| 4397 | 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] | 4398 | }; |
| 4399 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4400 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4401 | * answer depends on whether we are encoding set O as itself, and also |
| 4402 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4403 | * clear that the answers to these questions vary between |
| 4404 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4405 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4406 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4407 | ((c) < 128 && (c) > 0 && \ |
| 4408 | ((utf7_category[(c)] == 0) || \ |
| 4409 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4410 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4411 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4412 | PyObject * |
| 4413 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4414 | Py_ssize_t size, |
| 4415 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4417 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4418 | } |
| 4419 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4420 | /* The decoder. The only state we preserve is our read position, |
| 4421 | * i.e. how many characters we have consumed. So if we end in the |
| 4422 | * middle of a shift sequence we have to back off the read position |
| 4423 | * and the output to the beginning of the sequence, otherwise we lose |
| 4424 | * all the shift state (seen bits, number of bits seen, high |
| 4425 | * surrogate). */ |
| 4426 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4427 | PyObject * |
| 4428 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4429 | Py_ssize_t size, |
| 4430 | const char *errors, |
| 4431 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4432 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4433 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4434 | Py_ssize_t startinpos; |
| 4435 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4436 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4437 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4438 | const char *errmsg = ""; |
| 4439 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4440 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4441 | unsigned int base64bits = 0; |
| 4442 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4443 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4444 | PyObject *errorHandler = NULL; |
| 4445 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4446 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4447 | if (size == 0) { |
| 4448 | if (consumed) |
| 4449 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4450 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4451 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4452 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4453 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4454 | _PyUnicodeWriter_Init(&writer); |
| 4455 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4456 | |
| 4457 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4458 | e = s + size; |
| 4459 | |
| 4460 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4461 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4462 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4463 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4464 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4465 | if (inShift) { /* in a base-64 section */ |
| 4466 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4467 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4468 | base64bits += 6; |
| 4469 | s++; |
| 4470 | if (base64bits >= 16) { |
| 4471 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4472 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4473 | base64bits -= 16; |
| 4474 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4475 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4476 | if (surrogate) { |
| 4477 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4478 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4479 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4480 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4481 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4482 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4483 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4484 | } |
| 4485 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4486 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4487 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4488 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4489 | } |
| 4490 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4491 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4492 | /* first surrogate */ |
| 4493 | surrogate = outCh; |
| 4494 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4495 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4496 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4497 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4498 | } |
| 4499 | } |
| 4500 | } |
| 4501 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4502 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4503 | if (base64bits > 0) { /* left-over bits */ |
| 4504 | if (base64bits >= 6) { |
| 4505 | /* We've seen at least one base-64 character */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4506 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4507 | errmsg = "partial character in shift sequence"; |
| 4508 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4509 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4510 | else { |
| 4511 | /* Some bits remain; they should be zero */ |
| 4512 | if (base64buffer != 0) { |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4513 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4514 | errmsg = "non-zero padding bits in shift sequence"; |
| 4515 | goto utf7Error; |
| 4516 | } |
| 4517 | } |
| 4518 | } |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4519 | if (surrogate && DECODE_DIRECT(ch)) { |
| 4520 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
| 4521 | goto onError; |
| 4522 | } |
| 4523 | surrogate = 0; |
| 4524 | if (ch == '-') { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4525 | /* '-' is absorbed; other terminating |
| 4526 | characters are preserved */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4527 | s++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4528 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4529 | } |
| 4530 | } |
| 4531 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4532 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4533 | s++; /* consume '+' */ |
| 4534 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4535 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4536 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4537 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4538 | } |
| 4539 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4540 | inShift = 1; |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4541 | surrogate = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4542 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4543 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4544 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4545 | } |
| 4546 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4547 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4548 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4549 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4550 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4551 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4552 | else { |
| 4553 | startinpos = s-starts; |
| 4554 | s++; |
| 4555 | errmsg = "unexpected special character"; |
| 4556 | goto utf7Error; |
| 4557 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4558 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4559 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4560 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4561 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4562 | errors, &errorHandler, |
| 4563 | "utf7", errmsg, |
| 4564 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4565 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4566 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4569 | /* end of string */ |
| 4570 | |
| 4571 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4572 | /* if we're in an inconsistent state, that's an error */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4573 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4574 | if (surrogate || |
| 4575 | (base64bits >= 6) || |
| 4576 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4577 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4578 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4579 | errors, &errorHandler, |
| 4580 | "utf7", "unterminated shift sequence", |
| 4581 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4582 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4583 | goto onError; |
| 4584 | if (s < e) |
| 4585 | goto restart; |
| 4586 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4587 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4588 | |
| 4589 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4590 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4591 | if (inShift) { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4592 | *consumed = startinpos; |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4593 | if (writer.pos != shiftOutStart && writer.maxchar > 127) { |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4594 | PyObject *result = PyUnicode_FromKindAndData( |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4595 | writer.kind, writer.data, shiftOutStart); |
| 4596 | Py_XDECREF(errorHandler); |
| 4597 | Py_XDECREF(exc); |
| 4598 | _PyUnicodeWriter_Dealloc(&writer); |
| 4599 | return result; |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4600 | } |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4601 | writer.pos = shiftOutStart; /* back off output */ |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4602 | } |
| 4603 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4604 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4605 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4606 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4607 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4608 | Py_XDECREF(errorHandler); |
| 4609 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4610 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4611 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4612 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4613 | Py_XDECREF(errorHandler); |
| 4614 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4615 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4616 | return NULL; |
| 4617 | } |
| 4618 | |
| 4619 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4620 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4621 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4622 | int base64SetO, |
| 4623 | int base64WhiteSpace, |
| 4624 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4625 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4626 | int kind; |
| 4627 | void *data; |
| 4628 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4629 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4630 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4631 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4632 | unsigned int base64bits = 0; |
| 4633 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4634 | char * out; |
| 4635 | char * start; |
| 4636 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4637 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4638 | return NULL; |
| 4639 | kind = PyUnicode_KIND(str); |
| 4640 | data = PyUnicode_DATA(str); |
| 4641 | len = PyUnicode_GET_LENGTH(str); |
| 4642 | |
| 4643 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4644 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4645 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4646 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4647 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4648 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4649 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4650 | if (v == NULL) |
| 4651 | return NULL; |
| 4652 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4653 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4654 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4655 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4656 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4657 | if (inShift) { |
| 4658 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4659 | /* shifting out */ |
| 4660 | if (base64bits) { /* output remaining bits */ |
| 4661 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4662 | base64buffer = 0; |
| 4663 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4664 | } |
| 4665 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4666 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4667 | so no '-' is required, except if the character is itself a '-' */ |
| 4668 | if (IS_BASE64(ch) || ch == '-') { |
| 4669 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4670 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4671 | *out++ = (char) ch; |
| 4672 | } |
| 4673 | else { |
| 4674 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4675 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4676 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4677 | else { /* not in a shift sequence */ |
| 4678 | if (ch == '+') { |
| 4679 | *out++ = '+'; |
| 4680 | *out++ = '-'; |
| 4681 | } |
| 4682 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4683 | *out++ = (char) ch; |
| 4684 | } |
| 4685 | else { |
| 4686 | *out++ = '+'; |
| 4687 | inShift = 1; |
| 4688 | goto encode_char; |
| 4689 | } |
| 4690 | } |
| 4691 | continue; |
| 4692 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4693 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4694 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4695 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4696 | /* code first surrogate */ |
| 4697 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4698 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4699 | while (base64bits >= 6) { |
| 4700 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4701 | base64bits -= 6; |
| 4702 | } |
| 4703 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4704 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4705 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4706 | base64bits += 16; |
| 4707 | base64buffer = (base64buffer << 16) | ch; |
| 4708 | while (base64bits >= 6) { |
| 4709 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4710 | base64bits -= 6; |
| 4711 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4712 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4713 | if (base64bits) |
| 4714 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4715 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4716 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4717 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4718 | return NULL; |
| 4719 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4720 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4721 | PyObject * |
| 4722 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4723 | Py_ssize_t size, |
| 4724 | int base64SetO, |
| 4725 | int base64WhiteSpace, |
| 4726 | const char *errors) |
| 4727 | { |
| 4728 | PyObject *result; |
| 4729 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4730 | if (tmp == NULL) |
| 4731 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4732 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4733 | base64WhiteSpace, errors); |
| 4734 | Py_DECREF(tmp); |
| 4735 | return result; |
| 4736 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4737 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4738 | #undef IS_BASE64 |
| 4739 | #undef FROM_BASE64 |
| 4740 | #undef TO_BASE64 |
| 4741 | #undef DECODE_DIRECT |
| 4742 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4743 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4744 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4745 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4746 | PyObject * |
| 4747 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4748 | Py_ssize_t size, |
| 4749 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4750 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4751 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4752 | } |
| 4753 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4754 | #include "stringlib/asciilib.h" |
| 4755 | #include "stringlib/codecs.h" |
| 4756 | #include "stringlib/undef.h" |
| 4757 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4758 | #include "stringlib/ucs1lib.h" |
| 4759 | #include "stringlib/codecs.h" |
| 4760 | #include "stringlib/undef.h" |
| 4761 | |
| 4762 | #include "stringlib/ucs2lib.h" |
| 4763 | #include "stringlib/codecs.h" |
| 4764 | #include "stringlib/undef.h" |
| 4765 | |
| 4766 | #include "stringlib/ucs4lib.h" |
| 4767 | #include "stringlib/codecs.h" |
| 4768 | #include "stringlib/undef.h" |
| 4769 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4770 | /* Mask to quickly check whether a C 'long' contains a |
| 4771 | non-ASCII, UTF8-encoded char. */ |
| 4772 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4773 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4774 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4775 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4776 | #else |
| 4777 | # error C 'long' size should be either 4 or 8! |
| 4778 | #endif |
| 4779 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4780 | static Py_ssize_t |
| 4781 | 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] | 4782 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4783 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4784 | 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] | 4785 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4786 | /* |
| 4787 | * Issue #17237: m68k is a bit different from most architectures in |
| 4788 | * that objects do not use "natural alignment" - for example, int and |
| 4789 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4790 | * won't work; also, tests have shown that skipping the "optimised |
| 4791 | * version" will even speed up m68k. |
| 4792 | */ |
| 4793 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4794 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4795 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4796 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4797 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4798 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4799 | /* Help allocation */ |
| 4800 | const char *_p = p; |
| 4801 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4802 | while (_p < aligned_end) { |
| 4803 | unsigned long value = *(const unsigned long *) _p; |
| 4804 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4805 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4806 | *((unsigned long *)q) = value; |
| 4807 | _p += SIZEOF_LONG; |
| 4808 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4809 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4810 | p = _p; |
| 4811 | while (p < end) { |
| 4812 | if ((unsigned char)*p & 0x80) |
| 4813 | break; |
| 4814 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4815 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4816 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4817 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4818 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4819 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4820 | while (p < end) { |
| 4821 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4822 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4823 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4824 | /* Help allocation */ |
| 4825 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4826 | while (_p < aligned_end) { |
| 4827 | unsigned long value = *(unsigned long *) _p; |
| 4828 | if (value & ASCII_CHAR_MASK) |
| 4829 | break; |
| 4830 | _p += SIZEOF_LONG; |
| 4831 | } |
| 4832 | p = _p; |
| 4833 | if (_p == end) |
| 4834 | break; |
| 4835 | } |
| 4836 | if ((unsigned char)*p & 0x80) |
| 4837 | break; |
| 4838 | ++p; |
| 4839 | } |
| 4840 | memcpy(dest, start, p - start); |
| 4841 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4842 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4843 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4844 | PyObject * |
| 4845 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4846 | Py_ssize_t size, |
| 4847 | const char *errors, |
| 4848 | Py_ssize_t *consumed) |
| 4849 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4850 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4851 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4852 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4853 | |
| 4854 | Py_ssize_t startinpos; |
| 4855 | Py_ssize_t endinpos; |
| 4856 | const char *errmsg = ""; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4857 | PyObject *error_handler_obj = NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4858 | PyObject *exc = NULL; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4859 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4860 | |
| 4861 | if (size == 0) { |
| 4862 | if (consumed) |
| 4863 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4864 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4865 | } |
| 4866 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4867 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4868 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4869 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4870 | *consumed = 1; |
| 4871 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4872 | } |
| 4873 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4874 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4875 | writer.min_length = size; |
| 4876 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4877 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4878 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4879 | writer.pos = ascii_decode(s, end, writer.data); |
| 4880 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4881 | while (s < end) { |
| 4882 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4883 | int kind = writer.kind; |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4884 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4885 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4886 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4887 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4888 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4889 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4890 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4891 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4892 | } else { |
| 4893 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4894 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | switch (ch) { |
| 4898 | case 0: |
| 4899 | if (s == end || consumed) |
| 4900 | goto End; |
| 4901 | errmsg = "unexpected end of data"; |
| 4902 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4903 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4904 | break; |
| 4905 | case 1: |
| 4906 | errmsg = "invalid start byte"; |
| 4907 | startinpos = s - starts; |
| 4908 | endinpos = startinpos + 1; |
| 4909 | break; |
| 4910 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4911 | case 3: |
| 4912 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4913 | errmsg = "invalid continuation byte"; |
| 4914 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4915 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4916 | break; |
| 4917 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4918 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4919 | goto onError; |
| 4920 | continue; |
| 4921 | } |
| 4922 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4923 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 4924 | error_handler = get_error_handler(errors); |
| 4925 | |
| 4926 | switch (error_handler) { |
| 4927 | case _Py_ERROR_IGNORE: |
| 4928 | s += (endinpos - startinpos); |
| 4929 | break; |
| 4930 | |
| 4931 | case _Py_ERROR_REPLACE: |
| 4932 | if (_PyUnicodeWriter_WriteCharInline(&writer, 0xfffd) < 0) |
| 4933 | goto onError; |
| 4934 | s += (endinpos - startinpos); |
| 4935 | break; |
| 4936 | |
| 4937 | case _Py_ERROR_SURROGATEESCAPE: |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4938 | { |
| 4939 | Py_ssize_t i; |
| 4940 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4941 | if (_PyUnicodeWriter_PrepareKind(&writer, PyUnicode_2BYTE_KIND) < 0) |
| 4942 | goto onError; |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4943 | for (i=startinpos; i<endinpos; i++) { |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4944 | ch = (Py_UCS4)(unsigned char)(starts[i]); |
| 4945 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, |
| 4946 | ch + 0xdc00); |
| 4947 | writer.pos++; |
| 4948 | } |
| 4949 | s += (endinpos - startinpos); |
| 4950 | break; |
Victor Stinner | 74e8fac | 2015-10-05 13:49:26 +0200 | [diff] [blame] | 4951 | } |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4952 | |
| 4953 | default: |
| 4954 | if (unicode_decode_call_errorhandler_writer( |
| 4955 | errors, &error_handler_obj, |
| 4956 | "utf-8", errmsg, |
| 4957 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 4958 | &writer)) |
| 4959 | goto onError; |
| 4960 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4961 | } |
| 4962 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4963 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4964 | if (consumed) |
| 4965 | *consumed = s - starts; |
| 4966 | |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4967 | Py_XDECREF(error_handler_obj); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4968 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4969 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4970 | |
| 4971 | onError: |
Victor Stinner | 1d65d91 | 2015-10-05 13:43:50 +0200 | [diff] [blame] | 4972 | Py_XDECREF(error_handler_obj); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4973 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4974 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4975 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4976 | } |
| 4977 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4978 | #ifdef __APPLE__ |
| 4979 | |
| 4980 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4981 | used to decode the command line arguments on Mac OS X. |
| 4982 | |
| 4983 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4984 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4985 | |
| 4986 | wchar_t* |
| 4987 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4988 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4989 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4990 | wchar_t *unicode; |
| 4991 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4992 | |
| 4993 | /* Note: size will always be longer than the resulting Unicode |
| 4994 | character count */ |
Victor Stinner | f50e187 | 2015-03-20 11:32:24 +0100 | [diff] [blame] | 4995 | 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] | 4996 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4997 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4998 | if (!unicode) |
| 4999 | return NULL; |
| 5000 | |
| 5001 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5002 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5003 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5004 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5005 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5006 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5007 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5008 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5009 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5010 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5011 | if (ch > 0xFF) { |
| 5012 | #if SIZEOF_WCHAR_T == 4 |
| 5013 | assert(0); |
| 5014 | #else |
| 5015 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 5016 | /* compute and append the two surrogates: */ |
| 5017 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 5018 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 5019 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5020 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5021 | else { |
| 5022 | if (!ch && s == e) |
| 5023 | break; |
| 5024 | /* surrogateescape */ |
| 5025 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 5026 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5027 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 5028 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 5029 | return unicode; |
| 5030 | } |
| 5031 | |
| 5032 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5033 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5034 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 5035 | |
| 5036 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5037 | and allocate exactly as much space needed at the end. Else allocate the |
| 5038 | maximum possible needed (4 result bytes per Unicode character), and return |
| 5039 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 5040 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 5041 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 5042 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5043 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5044 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5045 | void *data; |
| 5046 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 5047 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5048 | if (!PyUnicode_Check(unicode)) { |
| 5049 | PyErr_BadArgument(); |
| 5050 | return NULL; |
| 5051 | } |
| 5052 | |
| 5053 | if (PyUnicode_READY(unicode) == -1) |
| 5054 | return NULL; |
| 5055 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 5056 | if (PyUnicode_UTF8(unicode)) |
| 5057 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 5058 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5059 | |
| 5060 | kind = PyUnicode_KIND(unicode); |
| 5061 | data = PyUnicode_DATA(unicode); |
| 5062 | size = PyUnicode_GET_LENGTH(unicode); |
| 5063 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5064 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 5065 | default: |
| 5066 | assert(0); |
| 5067 | case PyUnicode_1BYTE_KIND: |
| 5068 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 5069 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 5070 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 5071 | case PyUnicode_2BYTE_KIND: |
| 5072 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 5073 | case PyUnicode_4BYTE_KIND: |
| 5074 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 5075 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5078 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5079 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 5080 | Py_ssize_t size, |
| 5081 | const char *errors) |
| 5082 | { |
| 5083 | PyObject *v, *unicode; |
| 5084 | |
| 5085 | unicode = PyUnicode_FromUnicode(s, size); |
| 5086 | if (unicode == NULL) |
| 5087 | return NULL; |
| 5088 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 5089 | Py_DECREF(unicode); |
| 5090 | return v; |
| 5091 | } |
| 5092 | |
| 5093 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5094 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5095 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5096 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5099 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 5100 | |
| 5101 | PyObject * |
| 5102 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5103 | Py_ssize_t size, |
| 5104 | const char *errors, |
| 5105 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5106 | { |
| 5107 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 5108 | } |
| 5109 | |
| 5110 | PyObject * |
| 5111 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5112 | Py_ssize_t size, |
| 5113 | const char *errors, |
| 5114 | int *byteorder, |
| 5115 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5116 | { |
| 5117 | const char *starts = s; |
| 5118 | Py_ssize_t startinpos; |
| 5119 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5120 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 5121 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5122 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5123 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5124 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5125 | PyObject *errorHandler = NULL; |
| 5126 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 5127 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5128 | q = (unsigned char *)s; |
| 5129 | e = q + size; |
| 5130 | |
| 5131 | if (byteorder) |
| 5132 | bo = *byteorder; |
| 5133 | |
| 5134 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5135 | byte order setting accordingly. In native mode, the leading BOM |
| 5136 | mark is skipped, in all other modes, it is copied to the output |
| 5137 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5138 | if (bo == 0 && size >= 4) { |
| 5139 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5140 | if (bom == 0x0000FEFF) { |
| 5141 | bo = -1; |
| 5142 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5143 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5144 | else if (bom == 0xFFFE0000) { |
| 5145 | bo = 1; |
| 5146 | q += 4; |
| 5147 | } |
| 5148 | if (byteorder) |
| 5149 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5152 | if (q == e) { |
| 5153 | if (consumed) |
| 5154 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5155 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5156 | } |
| 5157 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5158 | #ifdef WORDS_BIGENDIAN |
| 5159 | le = bo < 0; |
| 5160 | #else |
| 5161 | le = bo <= 0; |
| 5162 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5163 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5164 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5165 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5166 | writer.min_length = (e - q + 3) / 4; |
| 5167 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5168 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5169 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5170 | while (1) { |
| 5171 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5172 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5173 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5174 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5175 | enum PyUnicode_Kind kind = writer.kind; |
| 5176 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5177 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5178 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5179 | if (le) { |
| 5180 | do { |
| 5181 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5182 | if (ch > maxch) |
| 5183 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5184 | if (kind != PyUnicode_1BYTE_KIND && |
| 5185 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5186 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5187 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5188 | q += 4; |
| 5189 | } while (q <= last); |
| 5190 | } |
| 5191 | else { |
| 5192 | do { |
| 5193 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5194 | if (ch > maxch) |
| 5195 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5196 | if (kind != PyUnicode_1BYTE_KIND && |
| 5197 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5198 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5199 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5200 | q += 4; |
| 5201 | } while (q <= last); |
| 5202 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5203 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5204 | } |
| 5205 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5206 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5207 | errmsg = "code point in surrogate code point range(0xd800, 0xe000)"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5208 | startinpos = ((const char *)q) - starts; |
| 5209 | endinpos = startinpos + 4; |
| 5210 | } |
| 5211 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5212 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5213 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5214 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5215 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5216 | startinpos = ((const char *)q) - starts; |
| 5217 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5218 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5219 | else { |
| 5220 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5221 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5222 | goto onError; |
| 5223 | q += 4; |
| 5224 | continue; |
| 5225 | } |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5226 | errmsg = "code point not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5227 | startinpos = ((const char *)q) - starts; |
| 5228 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5229 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5230 | |
| 5231 | /* The remaining input chars are ignored if the callback |
| 5232 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5233 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5234 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5235 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5236 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5237 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5238 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5241 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5242 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5243 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5244 | Py_XDECREF(errorHandler); |
| 5245 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5246 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5247 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5248 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5249 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5250 | Py_XDECREF(errorHandler); |
| 5251 | Py_XDECREF(exc); |
| 5252 | return NULL; |
| 5253 | } |
| 5254 | |
| 5255 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5256 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5257 | const char *errors, |
| 5258 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5259 | { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5260 | enum PyUnicode_Kind kind; |
| 5261 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5262 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5263 | PyObject *v; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5264 | PY_UINT32_T *out; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5265 | #if PY_LITTLE_ENDIAN |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5266 | int native_ordering = byteorder <= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5267 | #else |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5268 | int native_ordering = byteorder >= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5269 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5270 | const char *encoding; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5271 | Py_ssize_t nsize, pos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5272 | PyObject *errorHandler = NULL; |
| 5273 | PyObject *exc = NULL; |
| 5274 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5275 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5276 | if (!PyUnicode_Check(str)) { |
| 5277 | PyErr_BadArgument(); |
| 5278 | return NULL; |
| 5279 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5280 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5281 | return NULL; |
| 5282 | kind = PyUnicode_KIND(str); |
| 5283 | data = PyUnicode_DATA(str); |
| 5284 | len = PyUnicode_GET_LENGTH(str); |
| 5285 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5286 | if (len > PY_SSIZE_T_MAX / 4 - (byteorder == 0)) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5287 | return PyErr_NoMemory(); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5288 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5289 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5290 | if (v == NULL) |
| 5291 | return NULL; |
| 5292 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5293 | /* output buffer is 4-bytes aligned */ |
| 5294 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 4)); |
| 5295 | out = (PY_UINT32_T *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5296 | if (byteorder == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5297 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5298 | if (len == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5299 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5300 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5301 | if (byteorder == -1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5302 | encoding = "utf-32-le"; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5303 | else if (byteorder == 1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5304 | encoding = "utf-32-be"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5305 | else |
| 5306 | encoding = "utf-32"; |
| 5307 | |
| 5308 | if (kind == PyUnicode_1BYTE_KIND) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5309 | ucs1lib_utf32_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5310 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5313 | pos = 0; |
| 5314 | while (pos < len) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5315 | Py_ssize_t repsize, moreunits; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5316 | |
| 5317 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5318 | pos += ucs2lib_utf32_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5319 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5320 | } |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5321 | else { |
| 5322 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5323 | pos += ucs4lib_utf32_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5324 | &out, native_ordering); |
| 5325 | } |
| 5326 | if (pos == len) |
| 5327 | break; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5328 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5329 | rep = unicode_encode_call_errorhandler( |
| 5330 | errors, &errorHandler, |
| 5331 | encoding, "surrogates not allowed", |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5332 | str, &exc, pos, pos + 1, &pos); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5333 | if (!rep) |
| 5334 | goto error; |
| 5335 | |
| 5336 | if (PyBytes_Check(rep)) { |
| 5337 | repsize = PyBytes_GET_SIZE(rep); |
| 5338 | if (repsize & 3) { |
| 5339 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5340 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5341 | "surrogates not allowed"); |
| 5342 | goto error; |
| 5343 | } |
| 5344 | moreunits = repsize / 4; |
| 5345 | } |
| 5346 | else { |
| 5347 | assert(PyUnicode_Check(rep)); |
| 5348 | if (PyUnicode_READY(rep) < 0) |
| 5349 | goto error; |
| 5350 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5351 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5352 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5353 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5354 | "surrogates not allowed"); |
| 5355 | goto error; |
| 5356 | } |
| 5357 | } |
| 5358 | |
| 5359 | /* four bytes are reserved for each surrogate */ |
| 5360 | if (moreunits > 1) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5361 | Py_ssize_t outpos = out - (PY_UINT32_T*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5362 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5363 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5364 | /* integer overflow */ |
| 5365 | PyErr_NoMemory(); |
| 5366 | goto error; |
| 5367 | } |
| 5368 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5369 | goto error; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5370 | out = (PY_UINT32_T*) PyBytes_AS_STRING(v) + outpos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | if (PyBytes_Check(rep)) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5374 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5375 | out += moreunits; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5376 | } else /* rep is unicode */ { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5377 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5378 | ucs1lib_utf32_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5379 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | Py_CLEAR(rep); |
| 5383 | } |
| 5384 | |
| 5385 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5386 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5387 | handler is used. */ |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5388 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5389 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5390 | _PyBytes_Resize(&v, nsize); |
| 5391 | Py_XDECREF(errorHandler); |
| 5392 | Py_XDECREF(exc); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5393 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5394 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5395 | error: |
| 5396 | Py_XDECREF(rep); |
| 5397 | Py_XDECREF(errorHandler); |
| 5398 | Py_XDECREF(exc); |
| 5399 | Py_XDECREF(v); |
| 5400 | return NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5403 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5404 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5405 | Py_ssize_t size, |
| 5406 | const char *errors, |
| 5407 | int byteorder) |
| 5408 | { |
| 5409 | PyObject *result; |
| 5410 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5411 | if (tmp == NULL) |
| 5412 | return NULL; |
| 5413 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5414 | Py_DECREF(tmp); |
| 5415 | return result; |
| 5416 | } |
| 5417 | |
| 5418 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5419 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5420 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5421 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5424 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5425 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5426 | PyObject * |
| 5427 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5428 | Py_ssize_t size, |
| 5429 | const char *errors, |
| 5430 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5432 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5433 | } |
| 5434 | |
| 5435 | PyObject * |
| 5436 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5437 | Py_ssize_t size, |
| 5438 | const char *errors, |
| 5439 | int *byteorder, |
| 5440 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5441 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5442 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5443 | Py_ssize_t startinpos; |
| 5444 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5445 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5446 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5447 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5448 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5449 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5450 | PyObject *errorHandler = NULL; |
| 5451 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5452 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5454 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5455 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5456 | |
| 5457 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5458 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5460 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5461 | byte order setting accordingly. In native mode, the leading BOM |
| 5462 | mark is skipped, in all other modes, it is copied to the output |
| 5463 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5464 | if (bo == 0 && size >= 2) { |
| 5465 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5466 | if (bom == 0xFEFF) { |
| 5467 | q += 2; |
| 5468 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5469 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5470 | else if (bom == 0xFFFE) { |
| 5471 | q += 2; |
| 5472 | bo = 1; |
| 5473 | } |
| 5474 | if (byteorder) |
| 5475 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5476 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5477 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5478 | if (q == e) { |
| 5479 | if (consumed) |
| 5480 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5481 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5482 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5483 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5484 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5485 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5486 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5487 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5488 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5489 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5490 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5491 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5492 | /* Note: size will always be longer than the resulting Unicode |
| 5493 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5494 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5495 | writer.min_length = (e - q + 1) / 2; |
| 5496 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5497 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5498 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5499 | while (1) { |
| 5500 | Py_UCS4 ch = 0; |
| 5501 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5502 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5503 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5504 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5505 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5506 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5507 | native_ordering); |
| 5508 | else |
| 5509 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5510 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5511 | native_ordering); |
| 5512 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5513 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5514 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5515 | native_ordering); |
| 5516 | } else { |
| 5517 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5518 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5519 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5520 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5521 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5522 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5523 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5524 | switch (ch) |
| 5525 | { |
| 5526 | case 0: |
| 5527 | /* remaining byte at the end? (size should be even) */ |
| 5528 | if (q == e || consumed) |
| 5529 | goto End; |
| 5530 | errmsg = "truncated data"; |
| 5531 | startinpos = ((const char *)q) - starts; |
| 5532 | endinpos = ((const char *)e) - starts; |
| 5533 | break; |
| 5534 | /* The remaining input chars are ignored if the callback |
| 5535 | chooses to skip the input */ |
| 5536 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5537 | q -= 2; |
| 5538 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5539 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5540 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5541 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5542 | endinpos = ((const char *)e) - starts; |
| 5543 | break; |
| 5544 | case 2: |
| 5545 | errmsg = "illegal encoding"; |
| 5546 | startinpos = ((const char *)q) - 2 - starts; |
| 5547 | endinpos = startinpos + 2; |
| 5548 | break; |
| 5549 | case 3: |
| 5550 | errmsg = "illegal UTF-16 surrogate"; |
| 5551 | startinpos = ((const char *)q) - 4 - starts; |
| 5552 | endinpos = startinpos + 2; |
| 5553 | break; |
| 5554 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5555 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5556 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5557 | continue; |
| 5558 | } |
| 5559 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5560 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5561 | errors, |
| 5562 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5563 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5564 | &starts, |
| 5565 | (const char **)&e, |
| 5566 | &startinpos, |
| 5567 | &endinpos, |
| 5568 | &exc, |
| 5569 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5570 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5571 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5572 | } |
| 5573 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5574 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5575 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5576 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5577 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5578 | Py_XDECREF(errorHandler); |
| 5579 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5580 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5581 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5582 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5583 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5584 | Py_XDECREF(errorHandler); |
| 5585 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5586 | return NULL; |
| 5587 | } |
| 5588 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5589 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5590 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5591 | const char *errors, |
| 5592 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5593 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5594 | enum PyUnicode_Kind kind; |
| 5595 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5596 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5597 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5598 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5599 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5600 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5601 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5602 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5603 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5604 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5605 | const char *encoding; |
| 5606 | Py_ssize_t nsize, pos; |
| 5607 | PyObject *errorHandler = NULL; |
| 5608 | PyObject *exc = NULL; |
| 5609 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5610 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5611 | if (!PyUnicode_Check(str)) { |
| 5612 | PyErr_BadArgument(); |
| 5613 | return NULL; |
| 5614 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5615 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5616 | return NULL; |
| 5617 | kind = PyUnicode_KIND(str); |
| 5618 | data = PyUnicode_DATA(str); |
| 5619 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5620 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5621 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5622 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5623 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5624 | const Py_UCS4 *end = in + len; |
| 5625 | while (in < end) |
| 5626 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5627 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5628 | } |
| 5629 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5630 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5631 | nsize = len + pairs + (byteorder == 0); |
| 5632 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | if (v == NULL) |
| 5634 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5635 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5636 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5637 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5638 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5640 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5641 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5642 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5643 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5644 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5645 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5646 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5647 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5648 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5649 | if (byteorder < 0) |
| 5650 | encoding = "utf-16-le"; |
| 5651 | else if (byteorder > 0) |
| 5652 | encoding = "utf-16-be"; |
| 5653 | else |
| 5654 | encoding = "utf-16"; |
| 5655 | |
| 5656 | pos = 0; |
| 5657 | while (pos < len) { |
| 5658 | Py_ssize_t repsize, moreunits; |
| 5659 | |
| 5660 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5661 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5662 | &out, native_ordering); |
| 5663 | } |
| 5664 | else { |
| 5665 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5666 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5667 | &out, native_ordering); |
| 5668 | } |
| 5669 | if (pos == len) |
| 5670 | break; |
| 5671 | |
| 5672 | rep = unicode_encode_call_errorhandler( |
| 5673 | errors, &errorHandler, |
| 5674 | encoding, "surrogates not allowed", |
| 5675 | str, &exc, pos, pos + 1, &pos); |
| 5676 | if (!rep) |
| 5677 | goto error; |
| 5678 | |
| 5679 | if (PyBytes_Check(rep)) { |
| 5680 | repsize = PyBytes_GET_SIZE(rep); |
| 5681 | if (repsize & 1) { |
| 5682 | raise_encode_exception(&exc, encoding, |
| 5683 | str, pos - 1, pos, |
| 5684 | "surrogates not allowed"); |
| 5685 | goto error; |
| 5686 | } |
| 5687 | moreunits = repsize / 2; |
| 5688 | } |
| 5689 | else { |
| 5690 | assert(PyUnicode_Check(rep)); |
| 5691 | if (PyUnicode_READY(rep) < 0) |
| 5692 | goto error; |
| 5693 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5694 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5695 | raise_encode_exception(&exc, encoding, |
| 5696 | str, pos - 1, pos, |
| 5697 | "surrogates not allowed"); |
| 5698 | goto error; |
| 5699 | } |
| 5700 | } |
| 5701 | |
| 5702 | /* two bytes are reserved for each surrogate */ |
| 5703 | if (moreunits > 1) { |
| 5704 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5705 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5706 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5707 | /* integer overflow */ |
| 5708 | PyErr_NoMemory(); |
| 5709 | goto error; |
| 5710 | } |
| 5711 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5712 | goto error; |
| 5713 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5714 | } |
| 5715 | |
| 5716 | if (PyBytes_Check(rep)) { |
| 5717 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5718 | out += moreunits; |
| 5719 | } else /* rep is unicode */ { |
| 5720 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5721 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5722 | &out, native_ordering); |
| 5723 | } |
| 5724 | |
| 5725 | Py_CLEAR(rep); |
| 5726 | } |
| 5727 | |
| 5728 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5729 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5730 | is used. */ |
| 5731 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5732 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5733 | _PyBytes_Resize(&v, nsize); |
| 5734 | Py_XDECREF(errorHandler); |
| 5735 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5736 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5737 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5738 | error: |
| 5739 | Py_XDECREF(rep); |
| 5740 | Py_XDECREF(errorHandler); |
| 5741 | Py_XDECREF(exc); |
| 5742 | Py_XDECREF(v); |
| 5743 | return NULL; |
| 5744 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5745 | } |
| 5746 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5747 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5748 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5749 | Py_ssize_t size, |
| 5750 | const char *errors, |
| 5751 | int byteorder) |
| 5752 | { |
| 5753 | PyObject *result; |
| 5754 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5755 | if (tmp == NULL) |
| 5756 | return NULL; |
| 5757 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5758 | Py_DECREF(tmp); |
| 5759 | return result; |
| 5760 | } |
| 5761 | |
| 5762 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5763 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5765 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5769 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5770 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5771 | if all the escapes in the string make it still a valid ASCII string. |
| 5772 | Returns -1 if any escapes were found which cause the string to |
| 5773 | pop out of ASCII range. Otherwise returns the length of the |
| 5774 | required buffer to hold the string. |
| 5775 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5776 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5777 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5778 | { |
| 5779 | const unsigned char *p = (const unsigned char *)s; |
| 5780 | const unsigned char *end = p + size; |
| 5781 | Py_ssize_t length = 0; |
| 5782 | |
| 5783 | if (size < 0) |
| 5784 | return -1; |
| 5785 | |
| 5786 | for (; p < end; ++p) { |
| 5787 | if (*p > 127) { |
| 5788 | /* Non-ASCII */ |
| 5789 | return -1; |
| 5790 | } |
| 5791 | else if (*p != '\\') { |
| 5792 | /* Normal character */ |
| 5793 | ++length; |
| 5794 | } |
| 5795 | else { |
| 5796 | /* Backslash-escape, check next char */ |
| 5797 | ++p; |
| 5798 | /* Escape sequence reaches till end of string or |
| 5799 | non-ASCII follow-up. */ |
| 5800 | if (p >= end || *p > 127) |
| 5801 | return -1; |
| 5802 | switch (*p) { |
| 5803 | case '\n': |
| 5804 | /* backslash + \n result in zero characters */ |
| 5805 | break; |
| 5806 | case '\\': case '\'': case '\"': |
| 5807 | case 'b': case 'f': case 't': |
| 5808 | case 'n': case 'r': case 'v': case 'a': |
| 5809 | ++length; |
| 5810 | break; |
| 5811 | case '0': case '1': case '2': case '3': |
| 5812 | case '4': case '5': case '6': case '7': |
| 5813 | case 'x': case 'u': case 'U': case 'N': |
| 5814 | /* these do not guarantee ASCII characters */ |
| 5815 | return -1; |
| 5816 | default: |
| 5817 | /* count the backslash + the other character */ |
| 5818 | length += 2; |
| 5819 | } |
| 5820 | } |
| 5821 | } |
| 5822 | return length; |
| 5823 | } |
| 5824 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5825 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5826 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5827 | PyObject * |
| 5828 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5829 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5830 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5832 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5833 | Py_ssize_t startinpos; |
| 5834 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5835 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5837 | char* message; |
| 5838 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5839 | PyObject *errorHandler = NULL; |
| 5840 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5841 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5842 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5843 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5844 | if (len == 0) |
| 5845 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5846 | |
| 5847 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5848 | either the string is pure ASCII with named escapes like \n, etc. |
| 5849 | and we determined it's exact size (common case) |
| 5850 | or it contains \x, \u, ... escape sequences. then we create a |
| 5851 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5852 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5853 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5854 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5855 | } |
| 5856 | else { |
| 5857 | /* Escaped strings will always be longer than the resulting |
| 5858 | Unicode string, so we start with size here and then reduce the |
| 5859 | length after conversion to the true value. |
| 5860 | (but if the error callback returns a long replacement string |
| 5861 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5862 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5863 | } |
| 5864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5866 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | while (s < end) { |
| 5870 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5871 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5872 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5873 | |
| 5874 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5875 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5876 | x = (unsigned char)*s; |
| 5877 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5878 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5879 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | continue; |
| 5881 | } |
| 5882 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5883 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | /* \ - Escapes */ |
| 5885 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5886 | c = *s++; |
| 5887 | if (s > end) |
| 5888 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5889 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5890 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5892 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5893 | #define WRITECHAR(ch) \ |
| 5894 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5895 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5896 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5897 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5898 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5899 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5900 | case '\\': WRITECHAR('\\'); break; |
| 5901 | case '\'': WRITECHAR('\''); break; |
| 5902 | case '\"': WRITECHAR('\"'); break; |
| 5903 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5904 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5905 | case 'f': WRITECHAR('\014'); break; |
| 5906 | case 't': WRITECHAR('\t'); break; |
| 5907 | case 'n': WRITECHAR('\n'); break; |
| 5908 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5909 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5910 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5911 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5912 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5914 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | case '0': case '1': case '2': case '3': |
| 5916 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5917 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5918 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5919 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5920 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5921 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5923 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | break; |
| 5925 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5926 | /* hex escapes */ |
| 5927 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5929 | digits = 2; |
| 5930 | message = "truncated \\xXX escape"; |
| 5931 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5933 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5935 | digits = 4; |
| 5936 | message = "truncated \\uXXXX escape"; |
| 5937 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5939 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5940 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5941 | digits = 8; |
| 5942 | message = "truncated \\UXXXXXXXX escape"; |
| 5943 | hexescape: |
| 5944 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5945 | if (end - s < digits) { |
| 5946 | /* count only hex digits */ |
| 5947 | for (; s < end; ++s) { |
| 5948 | c = (unsigned char)*s; |
| 5949 | if (!Py_ISXDIGIT(c)) |
| 5950 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5951 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5952 | goto error; |
| 5953 | } |
| 5954 | for (; digits--; ++s) { |
| 5955 | c = (unsigned char)*s; |
| 5956 | if (!Py_ISXDIGIT(c)) |
| 5957 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5958 | chr = (chr<<4) & ~0xF; |
| 5959 | if (c >= '0' && c <= '9') |
| 5960 | chr += c - '0'; |
| 5961 | else if (c >= 'a' && c <= 'f') |
| 5962 | chr += 10 + c - 'a'; |
| 5963 | else |
| 5964 | chr += 10 + c - 'A'; |
| 5965 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5966 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5967 | /* _decoding_error will have already written into the |
| 5968 | target buffer. */ |
| 5969 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5970 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5971 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5972 | message = "illegal Unicode character"; |
| 5973 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5974 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5975 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5976 | break; |
| 5977 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5978 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5979 | case 'N': |
| 5980 | message = "malformed \\N character escape"; |
| 5981 | if (ucnhash_CAPI == NULL) { |
| 5982 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5983 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5984 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5985 | if (ucnhash_CAPI == NULL) |
| 5986 | goto ucnhashError; |
| 5987 | } |
| 5988 | if (*s == '{') { |
| 5989 | const char *start = s+1; |
| 5990 | /* look for the closing brace */ |
| 5991 | while (*s != '}' && s < end) |
| 5992 | s++; |
| 5993 | if (s > start && s < end && *s == '}') { |
| 5994 | /* found a name. look it up in the unicode database */ |
| 5995 | message = "unknown Unicode character name"; |
| 5996 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5997 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5998 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5999 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6000 | goto store; |
| 6001 | } |
| 6002 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6003 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6004 | |
| 6005 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6006 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6007 | message = "\\ at end of string"; |
| 6008 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6009 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6010 | } |
| 6011 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6012 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 6013 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6014 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 6015 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6017 | continue; |
| 6018 | |
| 6019 | error: |
| 6020 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 6021 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6022 | errors, &errorHandler, |
| 6023 | "unicodeescape", message, |
| 6024 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 6025 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 6026 | goto onError; |
| 6027 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6029 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6030 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 6031 | Py_XDECREF(errorHandler); |
| 6032 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6033 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 6034 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6035 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 6036 | PyErr_SetString( |
| 6037 | PyExc_UnicodeError, |
| 6038 | "\\N escapes not supported (can't load unicodedata module)" |
| 6039 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6040 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6041 | Py_XDECREF(errorHandler); |
| 6042 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 6043 | return NULL; |
| 6044 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6045 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6046 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6047 | Py_XDECREF(errorHandler); |
| 6048 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | return NULL; |
| 6050 | } |
| 6051 | |
| 6052 | /* Return a Unicode-Escape string version of the Unicode object. |
| 6053 | |
| 6054 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 6055 | appropriate. |
| 6056 | |
| 6057 | */ |
| 6058 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6059 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6060 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6062 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6063 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6065 | int kind; |
| 6066 | void *data; |
| 6067 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 6069 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6070 | escape. |
| 6071 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 6072 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 6073 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 6074 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6075 | */ |
| 6076 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6077 | if (!PyUnicode_Check(unicode)) { |
| 6078 | PyErr_BadArgument(); |
| 6079 | return NULL; |
| 6080 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6081 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6082 | return NULL; |
| 6083 | len = PyUnicode_GET_LENGTH(unicode); |
| 6084 | kind = PyUnicode_KIND(unicode); |
| 6085 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 6086 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6087 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 6088 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 6089 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 6090 | } |
| 6091 | |
| 6092 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6093 | return PyBytes_FromStringAndSize(NULL, 0); |
| 6094 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6095 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6096 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6097 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6098 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6099 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6100 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6101 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | if (repr == NULL) |
| 6103 | return NULL; |
| 6104 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6105 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6106 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6107 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 6108 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6109 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6110 | /* Escape backslashes */ |
| 6111 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | *p++ = '\\'; |
| 6113 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 6114 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6115 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6116 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6117 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 6118 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6119 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6120 | *p++ = '\\'; |
| 6121 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6122 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 6123 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 6124 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 6125 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 6126 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 6127 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 6128 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 6129 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6130 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 6131 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6134 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6135 | *p++ = '\\'; |
| 6136 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6137 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6138 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6139 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6140 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6141 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6142 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6143 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6144 | else if (ch == '\t') { |
| 6145 | *p++ = '\\'; |
| 6146 | *p++ = 't'; |
| 6147 | } |
| 6148 | else if (ch == '\n') { |
| 6149 | *p++ = '\\'; |
| 6150 | *p++ = 'n'; |
| 6151 | } |
| 6152 | else if (ch == '\r') { |
| 6153 | *p++ = '\\'; |
| 6154 | *p++ = 'r'; |
| 6155 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6156 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6157 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6158 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6160 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6161 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6162 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6163 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | /* Copy everything else as-is */ |
| 6166 | else |
| 6167 | *p++ = (char) ch; |
| 6168 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6170 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6171 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6172 | return NULL; |
| 6173 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6176 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6177 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6178 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6179 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6180 | PyObject *result; |
| 6181 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6182 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6183 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6184 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6185 | Py_DECREF(tmp); |
| 6186 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | } |
| 6188 | |
| 6189 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6190 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6191 | PyObject * |
| 6192 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6193 | Py_ssize_t size, |
| 6194 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6195 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6196 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6197 | Py_ssize_t startinpos; |
| 6198 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6199 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | const char *end; |
| 6201 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6202 | PyObject *errorHandler = NULL; |
| 6203 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6204 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6205 | if (size == 0) |
| 6206 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6207 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6208 | /* Escaped strings will always be longer than the resulting |
| 6209 | 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] | 6210 | length after conversion to the true value. (But decoding error |
| 6211 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6212 | _PyUnicodeWriter_Init(&writer); |
| 6213 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6215 | end = s + size; |
| 6216 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6217 | unsigned char c; |
| 6218 | Py_UCS4 x; |
| 6219 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6220 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6222 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6223 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6224 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6225 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6226 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6227 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6228 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6229 | startinpos = s-starts; |
| 6230 | |
| 6231 | /* \u-escapes are only interpreted iff the number of leading |
| 6232 | backslashes if odd */ |
| 6233 | bs = s; |
| 6234 | for (;s < end;) { |
| 6235 | if (*s != '\\') |
| 6236 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6237 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6238 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6239 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6240 | } |
| 6241 | if (((s - bs) & 1) == 0 || |
| 6242 | s >= end || |
| 6243 | (*s != 'u' && *s != 'U')) { |
| 6244 | continue; |
| 6245 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6246 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6247 | count = *s=='u' ? 4 : 8; |
| 6248 | s++; |
| 6249 | |
| 6250 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6251 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6252 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6253 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6254 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6255 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6256 | errors, &errorHandler, |
| 6257 | "rawunicodeescape", "truncated \\uXXXX", |
| 6258 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6259 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6260 | goto onError; |
| 6261 | goto nextByte; |
| 6262 | } |
| 6263 | x = (x<<4) & ~0xF; |
| 6264 | if (c >= '0' && c <= '9') |
| 6265 | x += c - '0'; |
| 6266 | else if (c >= 'a' && c <= 'f') |
| 6267 | x += 10 + c - 'a'; |
| 6268 | else |
| 6269 | x += 10 + c - 'A'; |
| 6270 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6271 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6272 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6273 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6274 | } |
| 6275 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6276 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6277 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6278 | errors, &errorHandler, |
| 6279 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6280 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6281 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6282 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6283 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6284 | nextByte: |
| 6285 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6286 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6287 | Py_XDECREF(errorHandler); |
| 6288 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6289 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6290 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6292 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6293 | Py_XDECREF(errorHandler); |
| 6294 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6295 | return NULL; |
| 6296 | } |
| 6297 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6298 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6299 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6300 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6301 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6302 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | char *p; |
| 6304 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6305 | Py_ssize_t expandsize, pos; |
| 6306 | int kind; |
| 6307 | void *data; |
| 6308 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6310 | if (!PyUnicode_Check(unicode)) { |
| 6311 | PyErr_BadArgument(); |
| 6312 | return NULL; |
| 6313 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6314 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6315 | return NULL; |
| 6316 | kind = PyUnicode_KIND(unicode); |
| 6317 | data = PyUnicode_DATA(unicode); |
| 6318 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6319 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6320 | bytes, and 1 byte characters 4. */ |
| 6321 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6322 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6323 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6324 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6325 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6326 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6327 | if (repr == NULL) |
| 6328 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6329 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6330 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6331 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6332 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6333 | for (pos = 0; pos < len; pos++) { |
| 6334 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6335 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6336 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6337 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6338 | *p++ = '\\'; |
| 6339 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6340 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6341 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6342 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6343 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6344 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6345 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6346 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6347 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6348 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6349 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6350 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6351 | *p++ = '\\'; |
| 6352 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6353 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6354 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6355 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6356 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 | /* Copy everything else as-is */ |
| 6359 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6360 | *p++ = (char) ch; |
| 6361 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6362 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6363 | assert(p > q); |
| 6364 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6365 | return NULL; |
| 6366 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6369 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6370 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6371 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6372 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6373 | PyObject *result; |
| 6374 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6375 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6376 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6377 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6378 | Py_DECREF(tmp); |
| 6379 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6380 | } |
| 6381 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6382 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6383 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6384 | PyObject * |
| 6385 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6386 | Py_ssize_t size, |
| 6387 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6388 | { |
| 6389 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6390 | Py_ssize_t startinpos; |
| 6391 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6392 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6393 | const char *end; |
| 6394 | const char *reason; |
| 6395 | PyObject *errorHandler = NULL; |
| 6396 | PyObject *exc = NULL; |
| 6397 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6398 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6399 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6400 | 1)) |
| 6401 | return NULL; |
| 6402 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6403 | if (size == 0) |
| 6404 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6405 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6406 | _PyUnicodeWriter_Init(&writer); |
| 6407 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6408 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6409 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6410 | } |
| 6411 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6412 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6413 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6414 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6415 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6416 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6417 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6418 | endinpos = end-starts; |
| 6419 | reason = "truncated input"; |
| 6420 | goto error; |
| 6421 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6422 | /* We copy the raw representation one byte at a time because the |
| 6423 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6424 | ((char *) &uch)[0] = s[0]; |
| 6425 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6426 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6427 | ((char *) &uch)[2] = s[2]; |
| 6428 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6429 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6430 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6431 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6432 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6433 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6434 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6435 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6436 | reason = "illegal code point (> 0x10FFFF)"; |
| 6437 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6438 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6439 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6440 | s += Py_UNICODE_SIZE; |
| 6441 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6442 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6443 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6444 | Py_UNICODE uch2; |
| 6445 | ((char *) &uch2)[0] = s[0]; |
| 6446 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6447 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6448 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6449 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6450 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6451 | } |
| 6452 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6453 | #endif |
| 6454 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6455 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6456 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6457 | continue; |
| 6458 | |
| 6459 | error: |
| 6460 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6461 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6462 | errors, &errorHandler, |
| 6463 | "unicode_internal", reason, |
| 6464 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6465 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6466 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6467 | } |
| 6468 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6469 | Py_XDECREF(errorHandler); |
| 6470 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6471 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6472 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6473 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6474 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6475 | Py_XDECREF(errorHandler); |
| 6476 | Py_XDECREF(exc); |
| 6477 | return NULL; |
| 6478 | } |
| 6479 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6480 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6481 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6482 | PyObject * |
| 6483 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6484 | Py_ssize_t size, |
| 6485 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6487 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6488 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6491 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6492 | static void |
| 6493 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6494 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6495 | PyObject *unicode, |
| 6496 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6497 | const char *reason) |
| 6498 | { |
| 6499 | if (*exceptionObject == NULL) { |
| 6500 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6501 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6502 | encoding, unicode, startpos, endpos, reason); |
| 6503 | } |
| 6504 | else { |
| 6505 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6506 | goto onError; |
| 6507 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6508 | goto onError; |
| 6509 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6510 | goto onError; |
| 6511 | return; |
| 6512 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 6513 | Py_CLEAR(*exceptionObject); |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6517 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6518 | static void |
| 6519 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6520 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6521 | PyObject *unicode, |
| 6522 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6523 | const char *reason) |
| 6524 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6525 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6526 | encoding, unicode, startpos, endpos, reason); |
| 6527 | if (*exceptionObject != NULL) |
| 6528 | PyCodec_StrictErrors(*exceptionObject); |
| 6529 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6530 | |
| 6531 | /* error handling callback helper: |
| 6532 | build arguments, call the callback and check the arguments, |
| 6533 | put the result into newpos and return the replacement string, which |
| 6534 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6535 | static PyObject * |
| 6536 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6537 | PyObject **errorHandler, |
| 6538 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6539 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6540 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6541 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6542 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6543 | 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] | 6544 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6545 | PyObject *restuple; |
| 6546 | PyObject *resunicode; |
| 6547 | |
| 6548 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6549 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6550 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6551 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6554 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6555 | return NULL; |
| 6556 | len = PyUnicode_GET_LENGTH(unicode); |
| 6557 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6558 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6559 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6560 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6561 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6562 | |
| 6563 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6564 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6565 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6567 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6568 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6569 | Py_DECREF(restuple); |
| 6570 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6571 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6572 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6573 | &resunicode, newpos)) { |
| 6574 | Py_DECREF(restuple); |
| 6575 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6576 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6577 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6578 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6579 | Py_DECREF(restuple); |
| 6580 | return NULL; |
| 6581 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6582 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6583 | *newpos = len + *newpos; |
| 6584 | if (*newpos<0 || *newpos>len) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 6585 | 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] | 6586 | Py_DECREF(restuple); |
| 6587 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6588 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6589 | Py_INCREF(resunicode); |
| 6590 | Py_DECREF(restuple); |
| 6591 | return resunicode; |
| 6592 | } |
| 6593 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6594 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6595 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6596 | const char *errors, |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6597 | const Py_UCS4 limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6598 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6599 | /* input state */ |
| 6600 | Py_ssize_t pos=0, size; |
| 6601 | int kind; |
| 6602 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6603 | /* pointer into the output */ |
| 6604 | char *str; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6605 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6606 | 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] | 6607 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6608 | PyObject *exc = NULL; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6609 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6610 | /* output object */ |
| 6611 | _PyBytesWriter writer; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6612 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6613 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6614 | return NULL; |
| 6615 | size = PyUnicode_GET_LENGTH(unicode); |
| 6616 | kind = PyUnicode_KIND(unicode); |
| 6617 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6618 | /* allocate enough for a simple encoding without |
| 6619 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6620 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6621 | return PyBytes_FromStringAndSize(NULL, 0); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6622 | |
| 6623 | _PyBytesWriter_Init(&writer); |
| 6624 | str = _PyBytesWriter_Alloc(&writer, size); |
| 6625 | if (str == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6626 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6627 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6628 | while (pos < size) { |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6629 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6630 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6631 | /* can we encode this? */ |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6632 | if (ch < limit) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | /* no overflow check, because we know that the space is enough */ |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6634 | *str++ = (char)ch; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6635 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6636 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6637 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6638 | PyObject *repunicode; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6639 | Py_ssize_t repsize, newpos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6640 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6641 | Py_ssize_t collstart = pos; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6642 | Py_ssize_t collend = collstart + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6643 | /* find all unecodable characters */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6644 | |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6645 | while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6646 | ++collend; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6647 | |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6648 | /* Only overallocate the buffer if it's not the last write */ |
| 6649 | writer.overallocate = (collend < size); |
| 6650 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6651 | /* 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] | 6652 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 6653 | error_handler = get_error_handler(errors); |
| 6654 | |
| 6655 | switch (error_handler) { |
| 6656 | case _Py_ERROR_STRICT: |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6657 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6658 | goto onError; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6659 | |
| 6660 | case _Py_ERROR_REPLACE: |
Victor Stinner | 01ada39 | 2015-10-01 21:54:51 +0200 | [diff] [blame] | 6661 | memset(str, '?', collend - collstart); |
| 6662 | str += (collend - collstart); |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6663 | /* fall through ignore error handler */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6664 | case _Py_ERROR_IGNORE: |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6665 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6666 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6667 | |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6668 | case _Py_ERROR_BACKSLASHREPLACE: |
| 6669 | str = backslashreplace(&writer, 1, str, |
| 6670 | unicode, collstart, collend); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6671 | if (str == NULL) |
| 6672 | goto onError; |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6673 | pos = collend; |
| 6674 | break; |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6675 | |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6676 | case _Py_ERROR_XMLCHARREFREPLACE: |
| 6677 | str = xmlcharrefreplace(&writer, 1, str, |
| 6678 | unicode, collstart, collend); |
| 6679 | if (str == NULL) |
| 6680 | goto onError; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6681 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6682 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6683 | |
Victor Stinner | c3713e9 | 2015-09-29 12:32:13 +0200 | [diff] [blame] | 6684 | case _Py_ERROR_SURROGATEESCAPE: |
| 6685 | for (i = collstart; i < collend; ++i) { |
| 6686 | ch = PyUnicode_READ(kind, data, i); |
| 6687 | if (ch < 0xdc80 || 0xdcff < ch) { |
| 6688 | /* Not a UTF-8b surrogate */ |
| 6689 | break; |
| 6690 | } |
| 6691 | *str++ = (char)(ch - 0xdc00); |
| 6692 | ++pos; |
| 6693 | } |
| 6694 | if (i >= collend) |
| 6695 | break; |
| 6696 | collstart = pos; |
| 6697 | assert(collstart != collend); |
| 6698 | /* fallback to general error handling */ |
| 6699 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6700 | default: |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6701 | repunicode = unicode_encode_call_errorhandler(errors, &error_handler_obj, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6702 | encoding, reason, unicode, &exc, |
| 6703 | collstart, collend, &newpos); |
| 6704 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6705 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6706 | goto onError; |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6707 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6708 | if (PyBytes_Check(repunicode)) { |
| 6709 | /* Directly copy bytes result to output. */ |
| 6710 | repsize = PyBytes_Size(repunicode); |
Victor Stinner | e7bf86c | 2015-10-09 01:39:28 +0200 | [diff] [blame] | 6711 | if (repsize > 1) { |
| 6712 | str = _PyBytesWriter_Prepare(&writer, str, repsize-1); |
| 6713 | if (str == NULL) |
| 6714 | goto onError; |
| 6715 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6716 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6717 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6718 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6719 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6720 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6721 | } |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6722 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6723 | /* need more space? (at least enough for what we |
| 6724 | have+the replacement+the rest of the string, so |
| 6725 | we won't have to check space for encodable characters) */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6726 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6727 | if (repsize > 1) { |
| 6728 | str = _PyBytesWriter_Prepare(&writer, str, repsize-1); |
| 6729 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6730 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6731 | } |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6732 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6733 | /* check if there is anything unencodable in the replacement |
| 6734 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6735 | for (i = 0; repsize-->0; ++i, ++str) { |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6736 | ch = PyUnicode_READ_CHAR(repunicode, i); |
| 6737 | if (ch >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6738 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6739 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6740 | Py_DECREF(repunicode); |
| 6741 | goto onError; |
| 6742 | } |
Victor Stinner | 0030cd5 | 2015-09-24 14:45:00 +0200 | [diff] [blame] | 6743 | *str = (char)ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6744 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6745 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6746 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6747 | } |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6748 | |
| 6749 | /* If overallocation was disabled, ensure that it was the last |
| 6750 | write. Otherwise, we missed an optimization */ |
| 6751 | assert(writer.overallocate || pos == size); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6752 | } |
| 6753 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6754 | |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6755 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6756 | Py_XDECREF(exc); |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6757 | return _PyBytesWriter_Finish(&writer, str); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6758 | |
| 6759 | onError: |
Victor Stinner | fdfbf78 | 2015-10-09 00:33:49 +0200 | [diff] [blame] | 6760 | _PyBytesWriter_Dealloc(&writer); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 6761 | Py_XDECREF(error_handler_obj); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6762 | Py_XDECREF(exc); |
| 6763 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6766 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6767 | PyObject * |
| 6768 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6769 | Py_ssize_t size, |
| 6770 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6772 | PyObject *result; |
| 6773 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6774 | if (unicode == NULL) |
| 6775 | return NULL; |
| 6776 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6777 | Py_DECREF(unicode); |
| 6778 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | } |
| 6780 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6781 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6782 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | { |
| 6784 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6785 | PyErr_BadArgument(); |
| 6786 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6788 | if (PyUnicode_READY(unicode) == -1) |
| 6789 | return NULL; |
| 6790 | /* Fast path: if it is a one-byte string, construct |
| 6791 | bytes object directly. */ |
| 6792 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6793 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6794 | PyUnicode_GET_LENGTH(unicode)); |
| 6795 | /* Non-Latin-1 characters present. Defer to above function to |
| 6796 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6797 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | PyObject* |
| 6801 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6802 | { |
| 6803 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | } |
| 6805 | |
| 6806 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6807 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6808 | PyObject * |
| 6809 | PyUnicode_DecodeASCII(const char *s, |
| 6810 | Py_ssize_t size, |
| 6811 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6813 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6814 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6815 | int kind; |
| 6816 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6817 | Py_ssize_t startinpos; |
| 6818 | Py_ssize_t endinpos; |
| 6819 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6820 | const char *e; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6821 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6822 | PyObject *exc = NULL; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6823 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6824 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6825 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6826 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6827 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6829 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6830 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6831 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6832 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6833 | writer.min_length = size; |
| 6834 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6835 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6836 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6837 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6838 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6839 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6840 | writer.pos = outpos; |
| 6841 | if (writer.pos == size) |
| 6842 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6843 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6844 | s += writer.pos; |
| 6845 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6846 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6847 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6848 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6849 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6850 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6851 | ++s; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6852 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6853 | } |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6854 | |
| 6855 | /* byte outsize range 0x00..0x7f: call the error handler */ |
| 6856 | |
| 6857 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 6858 | error_handler = get_error_handler(errors); |
| 6859 | |
| 6860 | switch (error_handler) |
| 6861 | { |
| 6862 | case _Py_ERROR_REPLACE: |
| 6863 | case _Py_ERROR_SURROGATEESCAPE: |
| 6864 | /* Fast-path: the error handler only writes one character, |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 6865 | but we may switch to UCS2 at the first write */ |
| 6866 | if (_PyUnicodeWriter_PrepareKind(&writer, PyUnicode_2BYTE_KIND) < 0) |
| 6867 | goto onError; |
| 6868 | kind = writer.kind; |
| 6869 | data = writer.data; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6870 | |
| 6871 | if (error_handler == _Py_ERROR_REPLACE) |
| 6872 | PyUnicode_WRITE(kind, data, writer.pos, 0xfffd); |
| 6873 | else |
| 6874 | PyUnicode_WRITE(kind, data, writer.pos, c + 0xdc00); |
| 6875 | writer.pos++; |
| 6876 | ++s; |
| 6877 | break; |
| 6878 | |
| 6879 | case _Py_ERROR_IGNORE: |
| 6880 | ++s; |
| 6881 | break; |
| 6882 | |
| 6883 | default: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6884 | startinpos = s-starts; |
| 6885 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6886 | if (unicode_decode_call_errorhandler_writer( |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6887 | errors, &error_handler_obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6888 | "ascii", "ordinal not in range(128)", |
| 6889 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6890 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6891 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6892 | kind = writer.kind; |
| 6893 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6894 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | } |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6896 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6897 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6898 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6899 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6900 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6901 | _PyUnicodeWriter_Dealloc(&writer); |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame] | 6902 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6903 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6904 | return NULL; |
| 6905 | } |
| 6906 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6907 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6908 | PyObject * |
| 6909 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6910 | Py_ssize_t size, |
| 6911 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6912 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6913 | PyObject *result; |
| 6914 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6915 | if (unicode == NULL) |
| 6916 | return NULL; |
| 6917 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6918 | Py_DECREF(unicode); |
| 6919 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6922 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6923 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 | { |
| 6925 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6926 | PyErr_BadArgument(); |
| 6927 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6928 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6929 | if (PyUnicode_READY(unicode) == -1) |
| 6930 | return NULL; |
| 6931 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6932 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6933 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6934 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6935 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6936 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6937 | } |
| 6938 | |
| 6939 | PyObject * |
| 6940 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6941 | { |
| 6942 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | } |
| 6944 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6945 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6946 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6947 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6948 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6949 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6950 | #define NEED_RETRY |
| 6951 | #endif |
| 6952 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6953 | #ifndef WC_ERR_INVALID_CHARS |
| 6954 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6955 | #endif |
| 6956 | |
| 6957 | static char* |
| 6958 | code_page_name(UINT code_page, PyObject **obj) |
| 6959 | { |
| 6960 | *obj = NULL; |
| 6961 | if (code_page == CP_ACP) |
| 6962 | return "mbcs"; |
| 6963 | if (code_page == CP_UTF7) |
| 6964 | return "CP_UTF7"; |
| 6965 | if (code_page == CP_UTF8) |
| 6966 | return "CP_UTF8"; |
| 6967 | |
| 6968 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6969 | if (*obj == NULL) |
| 6970 | return NULL; |
| 6971 | return PyBytes_AS_STRING(*obj); |
| 6972 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6973 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6974 | static DWORD |
| 6975 | decode_code_page_flags(UINT code_page) |
| 6976 | { |
| 6977 | if (code_page == CP_UTF7) { |
| 6978 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6979 | return 0; |
| 6980 | } |
| 6981 | else |
| 6982 | return MB_ERR_INVALID_CHARS; |
| 6983 | } |
| 6984 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6985 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6986 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6987 | * mode. |
| 6988 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6989 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6990 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6991 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6992 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6993 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6994 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6995 | const char *in, |
| 6996 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6997 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6998 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6999 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7000 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7001 | |
| 7002 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7003 | assert(insize > 0); |
| 7004 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 7005 | if (outsize <= 0) |
| 7006 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7007 | |
| 7008 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7009 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7010 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7011 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7012 | if (*v == NULL) |
| 7013 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7014 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7015 | } |
| 7016 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7017 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7018 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7019 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7020 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7021 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7022 | } |
| 7023 | |
| 7024 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7025 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 7026 | if (outsize <= 0) |
| 7027 | goto error; |
| 7028 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7029 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7030 | error: |
| 7031 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7032 | return -2; |
| 7033 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7034 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7035 | } |
| 7036 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7037 | /* |
| 7038 | * Decode a byte string from a code page into unicode object with an error |
| 7039 | * handler. |
| 7040 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7041 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7042 | * UnicodeDecodeError exception and returns -1 on error. |
| 7043 | */ |
| 7044 | static int |
| 7045 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7046 | PyObject **v, |
| 7047 | const char *in, const int size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7048 | const char *errors, int final) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7049 | { |
| 7050 | const char *startin = in; |
| 7051 | const char *endin = in + size; |
| 7052 | const DWORD flags = decode_code_page_flags(code_page); |
| 7053 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7054 | 2000 English version of the message. */ |
| 7055 | const char *reason = "No mapping for the Unicode character exists " |
| 7056 | "in the target code page."; |
| 7057 | /* each step cannot decode more than 1 character, but a character can be |
| 7058 | represented as a surrogate pair */ |
| 7059 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7060 | int insize; |
| 7061 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7062 | PyObject *errorHandler = NULL; |
| 7063 | PyObject *exc = NULL; |
| 7064 | PyObject *encoding_obj = NULL; |
| 7065 | char *encoding; |
| 7066 | DWORD err; |
| 7067 | int ret = -1; |
| 7068 | |
| 7069 | assert(size > 0); |
| 7070 | |
| 7071 | encoding = code_page_name(code_page, &encoding_obj); |
| 7072 | if (encoding == NULL) |
| 7073 | return -1; |
| 7074 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7075 | if ((errors == NULL || strcmp(errors, "strict") == 0) && final) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7076 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 7077 | UnicodeDecodeError. */ |
| 7078 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 7079 | if (exc != NULL) { |
| 7080 | PyCodec_StrictErrors(exc); |
| 7081 | Py_CLEAR(exc); |
| 7082 | } |
| 7083 | goto error; |
| 7084 | } |
| 7085 | |
| 7086 | if (*v == NULL) { |
| 7087 | /* Create unicode object */ |
| 7088 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7089 | PyErr_NoMemory(); |
| 7090 | goto error; |
| 7091 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 7092 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7093 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7094 | if (*v == NULL) |
| 7095 | goto error; |
| 7096 | startout = PyUnicode_AS_UNICODE(*v); |
| 7097 | } |
| 7098 | else { |
| 7099 | /* Extend unicode object */ |
| 7100 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7101 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7102 | PyErr_NoMemory(); |
| 7103 | goto error; |
| 7104 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7105 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7106 | goto error; |
| 7107 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7108 | } |
| 7109 | |
| 7110 | /* Decode the byte string character per character */ |
| 7111 | out = startout; |
| 7112 | while (in < endin) |
| 7113 | { |
| 7114 | /* Decode a character */ |
| 7115 | insize = 1; |
| 7116 | do |
| 7117 | { |
| 7118 | outsize = MultiByteToWideChar(code_page, flags, |
| 7119 | in, insize, |
| 7120 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7121 | if (outsize > 0) |
| 7122 | break; |
| 7123 | err = GetLastError(); |
| 7124 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7125 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7126 | { |
| 7127 | PyErr_SetFromWindowsErr(0); |
| 7128 | goto error; |
| 7129 | } |
| 7130 | insize++; |
| 7131 | } |
| 7132 | /* 4=maximum length of a UTF-8 sequence */ |
| 7133 | while (insize <= 4 && (in + insize) <= endin); |
| 7134 | |
| 7135 | if (outsize <= 0) { |
| 7136 | Py_ssize_t startinpos, endinpos, outpos; |
| 7137 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7138 | /* last character in partial decode? */ |
| 7139 | if (in + insize >= endin && !final) |
| 7140 | break; |
| 7141 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7142 | startinpos = in - startin; |
| 7143 | endinpos = startinpos + 1; |
| 7144 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7145 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7146 | errors, &errorHandler, |
| 7147 | encoding, reason, |
| 7148 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7149 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7150 | { |
| 7151 | goto error; |
| 7152 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7153 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7154 | } |
| 7155 | else { |
| 7156 | in += insize; |
| 7157 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7158 | out += outsize; |
| 7159 | } |
| 7160 | } |
| 7161 | |
| 7162 | /* write a NUL character at the end */ |
| 7163 | *out = 0; |
| 7164 | |
| 7165 | /* Extend unicode object */ |
| 7166 | outsize = out - startout; |
| 7167 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7168 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7169 | goto error; |
Victor Stinner | e1f17c6 | 2014-07-25 14:03:03 +0200 | [diff] [blame] | 7170 | /* (in - startin) <= size and size is an int */ |
| 7171 | ret = Py_SAFE_DOWNCAST(in - startin, Py_ssize_t, int); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | |
| 7173 | error: |
| 7174 | Py_XDECREF(encoding_obj); |
| 7175 | Py_XDECREF(errorHandler); |
| 7176 | Py_XDECREF(exc); |
| 7177 | return ret; |
| 7178 | } |
| 7179 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7180 | static PyObject * |
| 7181 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7182 | const char *s, Py_ssize_t size, |
| 7183 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7184 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7185 | PyObject *v = NULL; |
| 7186 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7187 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7188 | if (code_page < 0) { |
| 7189 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7190 | return NULL; |
| 7191 | } |
| 7192 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7193 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7194 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7195 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7196 | do |
| 7197 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7198 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7199 | if (size > INT_MAX) { |
| 7200 | chunk_size = INT_MAX; |
| 7201 | final = 0; |
| 7202 | done = 0; |
| 7203 | } |
| 7204 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7205 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7206 | { |
| 7207 | chunk_size = (int)size; |
| 7208 | final = (consumed == NULL); |
| 7209 | done = 1; |
| 7210 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7211 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7212 | if (chunk_size == 0 && done) { |
| 7213 | if (v != NULL) |
| 7214 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7215 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7216 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7217 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7218 | converted = decode_code_page_strict(code_page, &v, |
| 7219 | s, chunk_size); |
| 7220 | if (converted == -2) |
| 7221 | converted = decode_code_page_errors(code_page, &v, |
| 7222 | s, chunk_size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7223 | errors, final); |
| 7224 | assert(converted != 0 || done); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7225 | |
| 7226 | if (converted < 0) { |
| 7227 | Py_XDECREF(v); |
| 7228 | return NULL; |
| 7229 | } |
| 7230 | |
| 7231 | if (consumed) |
| 7232 | *consumed += converted; |
| 7233 | |
| 7234 | s += converted; |
| 7235 | size -= converted; |
| 7236 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7237 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7238 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7239 | } |
| 7240 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7241 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7242 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7243 | const char *s, |
| 7244 | Py_ssize_t size, |
| 7245 | const char *errors, |
| 7246 | Py_ssize_t *consumed) |
| 7247 | { |
| 7248 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7249 | } |
| 7250 | |
| 7251 | PyObject * |
| 7252 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7253 | Py_ssize_t size, |
| 7254 | const char *errors, |
| 7255 | Py_ssize_t *consumed) |
| 7256 | { |
| 7257 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7258 | } |
| 7259 | |
| 7260 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7261 | PyUnicode_DecodeMBCS(const char *s, |
| 7262 | Py_ssize_t size, |
| 7263 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7264 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7265 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7266 | } |
| 7267 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7268 | static DWORD |
| 7269 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7270 | { |
| 7271 | if (code_page == CP_UTF8) { |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 7272 | return WC_ERR_INVALID_CHARS; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7273 | } |
| 7274 | else if (code_page == CP_UTF7) { |
| 7275 | /* CP_UTF7 only supports flags=0 */ |
| 7276 | return 0; |
| 7277 | } |
| 7278 | else { |
| 7279 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7280 | return 0; |
| 7281 | else |
| 7282 | return WC_NO_BEST_FIT_CHARS; |
| 7283 | } |
| 7284 | } |
| 7285 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7286 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7287 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7288 | * mode. |
| 7289 | * |
| 7290 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7291 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7292 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7293 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7294 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7295 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7296 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7297 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7298 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7299 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7300 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7301 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7302 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7303 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7304 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7305 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7306 | /* Create a substring so that we can get the UTF-16 representation |
| 7307 | of just the slice under consideration. */ |
| 7308 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7309 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7310 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7311 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7312 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7313 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7314 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7315 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7316 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7317 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7318 | if (substring == NULL) |
| 7319 | return -1; |
| 7320 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7321 | if (p == NULL) { |
| 7322 | Py_DECREF(substring); |
| 7323 | return -1; |
| 7324 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7325 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7326 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7327 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7328 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7329 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7330 | NULL, 0, |
| 7331 | NULL, pusedDefaultChar); |
| 7332 | if (outsize <= 0) |
| 7333 | goto error; |
| 7334 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7335 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7336 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7337 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7338 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7339 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7340 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7341 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7342 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7343 | if (*outbytes == NULL) { |
| 7344 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7345 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7346 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7347 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7348 | } |
| 7349 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7350 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7351 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7352 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7353 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7354 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7355 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7356 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7357 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7358 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7359 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7360 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7361 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7362 | } |
| 7363 | |
| 7364 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7365 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7366 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7367 | out, outsize, |
| 7368 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7369 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7370 | if (outsize <= 0) |
| 7371 | goto error; |
| 7372 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7373 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7374 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7375 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7376 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7377 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7378 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7379 | return -2; |
| 7380 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7381 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7382 | } |
| 7383 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7384 | /* |
| 7385 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7386 | * error handler. |
| 7387 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7388 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7389 | * -1 on other error. |
| 7390 | */ |
| 7391 | static int |
| 7392 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7393 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7394 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7395 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7396 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7397 | Py_ssize_t pos = unicode_offset; |
| 7398 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7399 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7400 | 2000 English version of the message. */ |
| 7401 | const char *reason = "invalid character"; |
| 7402 | /* 4=maximum length of a UTF-8 sequence */ |
| 7403 | char buffer[4]; |
| 7404 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7405 | Py_ssize_t outsize; |
| 7406 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7407 | PyObject *errorHandler = NULL; |
| 7408 | PyObject *exc = NULL; |
| 7409 | PyObject *encoding_obj = NULL; |
| 7410 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7411 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7412 | PyObject *rep; |
| 7413 | int ret = -1; |
| 7414 | |
| 7415 | assert(insize > 0); |
| 7416 | |
| 7417 | encoding = code_page_name(code_page, &encoding_obj); |
| 7418 | if (encoding == NULL) |
| 7419 | return -1; |
| 7420 | |
| 7421 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7422 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7423 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7424 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7425 | if (exc != NULL) { |
| 7426 | PyCodec_StrictErrors(exc); |
| 7427 | Py_DECREF(exc); |
| 7428 | } |
| 7429 | Py_XDECREF(encoding_obj); |
| 7430 | return -1; |
| 7431 | } |
| 7432 | |
| 7433 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7434 | pusedDefaultChar = &usedDefaultChar; |
| 7435 | else |
| 7436 | pusedDefaultChar = NULL; |
| 7437 | |
| 7438 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7439 | PyErr_NoMemory(); |
| 7440 | goto error; |
| 7441 | } |
| 7442 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7443 | |
| 7444 | if (*outbytes == NULL) { |
| 7445 | /* Create string object */ |
| 7446 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7447 | if (*outbytes == NULL) |
| 7448 | goto error; |
| 7449 | out = PyBytes_AS_STRING(*outbytes); |
| 7450 | } |
| 7451 | else { |
| 7452 | /* Extend string object */ |
| 7453 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7454 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7455 | PyErr_NoMemory(); |
| 7456 | goto error; |
| 7457 | } |
| 7458 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7459 | goto error; |
| 7460 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7461 | } |
| 7462 | |
| 7463 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7464 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7465 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7466 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7467 | wchar_t chars[2]; |
| 7468 | int charsize; |
| 7469 | if (ch < 0x10000) { |
| 7470 | chars[0] = (wchar_t)ch; |
| 7471 | charsize = 1; |
| 7472 | } |
| 7473 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7474 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7475 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7476 | charsize = 2; |
| 7477 | } |
| 7478 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7479 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7480 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7481 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7482 | NULL, pusedDefaultChar); |
| 7483 | if (outsize > 0) { |
| 7484 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7485 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7486 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7487 | memcpy(out, buffer, outsize); |
| 7488 | out += outsize; |
| 7489 | continue; |
| 7490 | } |
| 7491 | } |
| 7492 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7493 | PyErr_SetFromWindowsErr(0); |
| 7494 | goto error; |
| 7495 | } |
| 7496 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7497 | rep = unicode_encode_call_errorhandler( |
| 7498 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7499 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7500 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7501 | if (rep == NULL) |
| 7502 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7503 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7504 | |
| 7505 | if (PyBytes_Check(rep)) { |
| 7506 | outsize = PyBytes_GET_SIZE(rep); |
| 7507 | if (outsize != 1) { |
| 7508 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7509 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7510 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7511 | Py_DECREF(rep); |
| 7512 | goto error; |
| 7513 | } |
| 7514 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7515 | } |
| 7516 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7517 | out += outsize; |
| 7518 | } |
| 7519 | else { |
| 7520 | Py_ssize_t i; |
| 7521 | enum PyUnicode_Kind kind; |
| 7522 | void *data; |
| 7523 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7524 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7525 | Py_DECREF(rep); |
| 7526 | goto error; |
| 7527 | } |
| 7528 | |
| 7529 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7530 | if (outsize != 1) { |
| 7531 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7532 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7533 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7534 | Py_DECREF(rep); |
| 7535 | goto error; |
| 7536 | } |
| 7537 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7538 | } |
| 7539 | kind = PyUnicode_KIND(rep); |
| 7540 | data = PyUnicode_DATA(rep); |
| 7541 | for (i=0; i < outsize; i++) { |
| 7542 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7543 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7544 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7545 | encoding, unicode, |
| 7546 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7547 | "unable to encode error handler result to ASCII"); |
| 7548 | Py_DECREF(rep); |
| 7549 | goto error; |
| 7550 | } |
| 7551 | *out = (unsigned char)ch; |
| 7552 | out++; |
| 7553 | } |
| 7554 | } |
| 7555 | Py_DECREF(rep); |
| 7556 | } |
| 7557 | /* write a NUL byte */ |
| 7558 | *out = 0; |
| 7559 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7560 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7561 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7562 | goto error; |
| 7563 | ret = 0; |
| 7564 | |
| 7565 | error: |
| 7566 | Py_XDECREF(encoding_obj); |
| 7567 | Py_XDECREF(errorHandler); |
| 7568 | Py_XDECREF(exc); |
| 7569 | return ret; |
| 7570 | } |
| 7571 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7572 | static PyObject * |
| 7573 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7574 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7575 | const char *errors) |
| 7576 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7577 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7578 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7579 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7580 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7581 | |
Victor Stinner | 29dacf2 | 2015-01-26 16:41:32 +0100 | [diff] [blame] | 7582 | if (!PyUnicode_Check(unicode)) { |
| 7583 | PyErr_BadArgument(); |
| 7584 | return NULL; |
| 7585 | } |
| 7586 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7587 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7588 | return NULL; |
| 7589 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7590 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7591 | if (code_page < 0) { |
| 7592 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7593 | return NULL; |
| 7594 | } |
| 7595 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7596 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7597 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7598 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7599 | offset = 0; |
| 7600 | do |
| 7601 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7602 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7603 | /* 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] | 7604 | chunks. */ |
| 7605 | if (len > INT_MAX/2) { |
| 7606 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7607 | done = 0; |
| 7608 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7609 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7610 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7611 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7612 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7613 | done = 1; |
| 7614 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7615 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7616 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7617 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7618 | errors); |
| 7619 | if (ret == -2) |
| 7620 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7621 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7622 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7623 | if (ret < 0) { |
| 7624 | Py_XDECREF(outbytes); |
| 7625 | return NULL; |
| 7626 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7627 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7628 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7629 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7630 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7631 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7632 | return outbytes; |
| 7633 | } |
| 7634 | |
| 7635 | PyObject * |
| 7636 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7637 | Py_ssize_t size, |
| 7638 | const char *errors) |
| 7639 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7640 | PyObject *unicode, *res; |
| 7641 | unicode = PyUnicode_FromUnicode(p, size); |
| 7642 | if (unicode == NULL) |
| 7643 | return NULL; |
| 7644 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7645 | Py_DECREF(unicode); |
| 7646 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7647 | } |
| 7648 | |
| 7649 | PyObject * |
| 7650 | PyUnicode_EncodeCodePage(int code_page, |
| 7651 | PyObject *unicode, |
| 7652 | const char *errors) |
| 7653 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7654 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7655 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7656 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7657 | PyObject * |
| 7658 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7659 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7660 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7661 | } |
| 7662 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7663 | #undef NEED_RETRY |
| 7664 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7665 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7666 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7667 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7668 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7669 | static int |
| 7670 | charmap_decode_string(const char *s, |
| 7671 | Py_ssize_t size, |
| 7672 | PyObject *mapping, |
| 7673 | const char *errors, |
| 7674 | _PyUnicodeWriter *writer) |
| 7675 | { |
| 7676 | const char *starts = s; |
| 7677 | const char *e; |
| 7678 | Py_ssize_t startinpos, endinpos; |
| 7679 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7680 | Py_ssize_t maplen; |
| 7681 | enum PyUnicode_Kind mapkind; |
| 7682 | void *mapdata; |
| 7683 | Py_UCS4 x; |
| 7684 | unsigned char ch; |
| 7685 | |
| 7686 | if (PyUnicode_READY(mapping) == -1) |
| 7687 | return -1; |
| 7688 | |
| 7689 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7690 | mapdata = PyUnicode_DATA(mapping); |
| 7691 | mapkind = PyUnicode_KIND(mapping); |
| 7692 | |
| 7693 | e = s + size; |
| 7694 | |
| 7695 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7696 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7697 | * is disabled in encoding aliases, latin1 is preferred because |
| 7698 | * its implementation is faster. */ |
| 7699 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7700 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7701 | Py_UCS4 maxchar = writer->maxchar; |
| 7702 | |
| 7703 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7704 | while (s < e) { |
| 7705 | ch = *s; |
| 7706 | x = mapdata_ucs1[ch]; |
| 7707 | if (x > maxchar) { |
| 7708 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7709 | goto onError; |
| 7710 | maxchar = writer->maxchar; |
| 7711 | outdata = (Py_UCS1 *)writer->data; |
| 7712 | } |
| 7713 | outdata[writer->pos] = x; |
| 7714 | writer->pos++; |
| 7715 | ++s; |
| 7716 | } |
| 7717 | return 0; |
| 7718 | } |
| 7719 | |
| 7720 | while (s < e) { |
| 7721 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7722 | enum PyUnicode_Kind outkind = writer->kind; |
| 7723 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7724 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7725 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7726 | Py_UCS4 maxchar = writer->maxchar; |
| 7727 | while (s < e) { |
| 7728 | ch = *s; |
| 7729 | x = mapdata_ucs2[ch]; |
| 7730 | if (x > maxchar) |
| 7731 | goto Error; |
| 7732 | outdata[writer->pos] = x; |
| 7733 | writer->pos++; |
| 7734 | ++s; |
| 7735 | } |
| 7736 | break; |
| 7737 | } |
| 7738 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7739 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7740 | while (s < e) { |
| 7741 | ch = *s; |
| 7742 | x = mapdata_ucs2[ch]; |
| 7743 | if (x == 0xFFFE) |
| 7744 | goto Error; |
| 7745 | outdata[writer->pos] = x; |
| 7746 | writer->pos++; |
| 7747 | ++s; |
| 7748 | } |
| 7749 | break; |
| 7750 | } |
| 7751 | } |
| 7752 | ch = *s; |
| 7753 | |
| 7754 | if (ch < maplen) |
| 7755 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7756 | else |
| 7757 | x = 0xfffe; /* invalid value */ |
| 7758 | Error: |
| 7759 | if (x == 0xfffe) |
| 7760 | { |
| 7761 | /* undefined mapping */ |
| 7762 | startinpos = s-starts; |
| 7763 | endinpos = startinpos+1; |
| 7764 | if (unicode_decode_call_errorhandler_writer( |
| 7765 | errors, &errorHandler, |
| 7766 | "charmap", "character maps to <undefined>", |
| 7767 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7768 | writer)) { |
| 7769 | goto onError; |
| 7770 | } |
| 7771 | continue; |
| 7772 | } |
| 7773 | |
| 7774 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7775 | goto onError; |
| 7776 | ++s; |
| 7777 | } |
| 7778 | Py_XDECREF(errorHandler); |
| 7779 | Py_XDECREF(exc); |
| 7780 | return 0; |
| 7781 | |
| 7782 | onError: |
| 7783 | Py_XDECREF(errorHandler); |
| 7784 | Py_XDECREF(exc); |
| 7785 | return -1; |
| 7786 | } |
| 7787 | |
| 7788 | static int |
| 7789 | charmap_decode_mapping(const char *s, |
| 7790 | Py_ssize_t size, |
| 7791 | PyObject *mapping, |
| 7792 | const char *errors, |
| 7793 | _PyUnicodeWriter *writer) |
| 7794 | { |
| 7795 | const char *starts = s; |
| 7796 | const char *e; |
| 7797 | Py_ssize_t startinpos, endinpos; |
| 7798 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7799 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7800 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7801 | |
| 7802 | e = s + size; |
| 7803 | |
| 7804 | while (s < e) { |
| 7805 | ch = *s; |
| 7806 | |
| 7807 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7808 | key = PyLong_FromLong((long)ch); |
| 7809 | if (key == NULL) |
| 7810 | goto onError; |
| 7811 | |
| 7812 | item = PyObject_GetItem(mapping, key); |
| 7813 | Py_DECREF(key); |
| 7814 | if (item == NULL) { |
| 7815 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7816 | /* No mapping found means: mapping is undefined. */ |
| 7817 | PyErr_Clear(); |
| 7818 | goto Undefined; |
| 7819 | } else |
| 7820 | goto onError; |
| 7821 | } |
| 7822 | |
| 7823 | /* Apply mapping */ |
| 7824 | if (item == Py_None) |
| 7825 | goto Undefined; |
| 7826 | if (PyLong_Check(item)) { |
| 7827 | long value = PyLong_AS_LONG(item); |
| 7828 | if (value == 0xFFFE) |
| 7829 | goto Undefined; |
| 7830 | if (value < 0 || value > MAX_UNICODE) { |
| 7831 | PyErr_Format(PyExc_TypeError, |
| 7832 | "character mapping must be in range(0x%lx)", |
| 7833 | (unsigned long)MAX_UNICODE + 1); |
| 7834 | goto onError; |
| 7835 | } |
| 7836 | |
| 7837 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7838 | goto onError; |
| 7839 | } |
| 7840 | else if (PyUnicode_Check(item)) { |
| 7841 | if (PyUnicode_READY(item) == -1) |
| 7842 | goto onError; |
| 7843 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7844 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7845 | if (value == 0xFFFE) |
| 7846 | goto Undefined; |
| 7847 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7848 | goto onError; |
| 7849 | } |
| 7850 | else { |
| 7851 | writer->overallocate = 1; |
| 7852 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7853 | goto onError; |
| 7854 | } |
| 7855 | } |
| 7856 | else { |
| 7857 | /* wrong return value */ |
| 7858 | PyErr_SetString(PyExc_TypeError, |
| 7859 | "character mapping must return integer, None or str"); |
| 7860 | goto onError; |
| 7861 | } |
| 7862 | Py_CLEAR(item); |
| 7863 | ++s; |
| 7864 | continue; |
| 7865 | |
| 7866 | Undefined: |
| 7867 | /* undefined mapping */ |
| 7868 | Py_CLEAR(item); |
| 7869 | startinpos = s-starts; |
| 7870 | endinpos = startinpos+1; |
| 7871 | if (unicode_decode_call_errorhandler_writer( |
| 7872 | errors, &errorHandler, |
| 7873 | "charmap", "character maps to <undefined>", |
| 7874 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7875 | writer)) { |
| 7876 | goto onError; |
| 7877 | } |
| 7878 | } |
| 7879 | Py_XDECREF(errorHandler); |
| 7880 | Py_XDECREF(exc); |
| 7881 | return 0; |
| 7882 | |
| 7883 | onError: |
| 7884 | Py_XDECREF(item); |
| 7885 | Py_XDECREF(errorHandler); |
| 7886 | Py_XDECREF(exc); |
| 7887 | return -1; |
| 7888 | } |
| 7889 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7890 | PyObject * |
| 7891 | PyUnicode_DecodeCharmap(const char *s, |
| 7892 | Py_ssize_t size, |
| 7893 | PyObject *mapping, |
| 7894 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7895 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7896 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7898 | /* Default to Latin-1 */ |
| 7899 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7901 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7902 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7903 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7904 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7905 | writer.min_length = size; |
| 7906 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7907 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7908 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7909 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7910 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7911 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7912 | } |
| 7913 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7914 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7915 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7916 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7917 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7918 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7919 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7920 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7921 | return NULL; |
| 7922 | } |
| 7923 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7924 | /* Charmap encoding: the lookup table */ |
| 7925 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7926 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7927 | PyObject_HEAD |
| 7928 | unsigned char level1[32]; |
| 7929 | int count2, count3; |
| 7930 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7931 | }; |
| 7932 | |
| 7933 | static PyObject* |
| 7934 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7935 | { |
| 7936 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7937 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7938 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7939 | } |
| 7940 | |
| 7941 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7942 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7943 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7944 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7945 | }; |
| 7946 | |
| 7947 | static void |
| 7948 | encoding_map_dealloc(PyObject* o) |
| 7949 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7950 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7951 | } |
| 7952 | |
| 7953 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7954 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7955 | "EncodingMap", /*tp_name*/ |
| 7956 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7957 | 0, /*tp_itemsize*/ |
| 7958 | /* methods */ |
| 7959 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7960 | 0, /*tp_print*/ |
| 7961 | 0, /*tp_getattr*/ |
| 7962 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7963 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7964 | 0, /*tp_repr*/ |
| 7965 | 0, /*tp_as_number*/ |
| 7966 | 0, /*tp_as_sequence*/ |
| 7967 | 0, /*tp_as_mapping*/ |
| 7968 | 0, /*tp_hash*/ |
| 7969 | 0, /*tp_call*/ |
| 7970 | 0, /*tp_str*/ |
| 7971 | 0, /*tp_getattro*/ |
| 7972 | 0, /*tp_setattro*/ |
| 7973 | 0, /*tp_as_buffer*/ |
| 7974 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7975 | 0, /*tp_doc*/ |
| 7976 | 0, /*tp_traverse*/ |
| 7977 | 0, /*tp_clear*/ |
| 7978 | 0, /*tp_richcompare*/ |
| 7979 | 0, /*tp_weaklistoffset*/ |
| 7980 | 0, /*tp_iter*/ |
| 7981 | 0, /*tp_iternext*/ |
| 7982 | encoding_map_methods, /*tp_methods*/ |
| 7983 | 0, /*tp_members*/ |
| 7984 | 0, /*tp_getset*/ |
| 7985 | 0, /*tp_base*/ |
| 7986 | 0, /*tp_dict*/ |
| 7987 | 0, /*tp_descr_get*/ |
| 7988 | 0, /*tp_descr_set*/ |
| 7989 | 0, /*tp_dictoffset*/ |
| 7990 | 0, /*tp_init*/ |
| 7991 | 0, /*tp_alloc*/ |
| 7992 | 0, /*tp_new*/ |
| 7993 | 0, /*tp_free*/ |
| 7994 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7995 | }; |
| 7996 | |
| 7997 | PyObject* |
| 7998 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7999 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8000 | PyObject *result; |
| 8001 | struct encoding_map *mresult; |
| 8002 | int i; |
| 8003 | int need_dict = 0; |
| 8004 | unsigned char level1[32]; |
| 8005 | unsigned char level2[512]; |
| 8006 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 8007 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8008 | int kind; |
| 8009 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8010 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8011 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8012 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8013 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8014 | PyErr_BadArgument(); |
| 8015 | return NULL; |
| 8016 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8017 | kind = PyUnicode_KIND(string); |
| 8018 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8019 | length = PyUnicode_GET_LENGTH(string); |
| 8020 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8021 | memset(level1, 0xFF, sizeof level1); |
| 8022 | memset(level2, 0xFF, sizeof level2); |
| 8023 | |
| 8024 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 8025 | or if there are non-BMP characters, we need to use |
| 8026 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8027 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8028 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8029 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8030 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8031 | ch = PyUnicode_READ(kind, data, i); |
| 8032 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8033 | need_dict = 1; |
| 8034 | break; |
| 8035 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8036 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8037 | /* unmapped character */ |
| 8038 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8039 | l1 = ch >> 11; |
| 8040 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8041 | if (level1[l1] == 0xFF) |
| 8042 | level1[l1] = count2++; |
| 8043 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8044 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
| 8047 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 8048 | need_dict = 1; |
| 8049 | |
| 8050 | if (need_dict) { |
| 8051 | PyObject *result = PyDict_New(); |
| 8052 | PyObject *key, *value; |
| 8053 | if (!result) |
| 8054 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8055 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8056 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8057 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8058 | if (!key || !value) |
| 8059 | goto failed1; |
| 8060 | if (PyDict_SetItem(result, key, value) == -1) |
| 8061 | goto failed1; |
| 8062 | Py_DECREF(key); |
| 8063 | Py_DECREF(value); |
| 8064 | } |
| 8065 | return result; |
| 8066 | failed1: |
| 8067 | Py_XDECREF(key); |
| 8068 | Py_XDECREF(value); |
| 8069 | Py_DECREF(result); |
| 8070 | return NULL; |
| 8071 | } |
| 8072 | |
| 8073 | /* Create a three-level trie */ |
| 8074 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 8075 | 16*count2 + 128*count3 - 1); |
| 8076 | if (!result) |
| 8077 | return PyErr_NoMemory(); |
| 8078 | PyObject_Init(result, &EncodingMapType); |
| 8079 | mresult = (struct encoding_map*)result; |
| 8080 | mresult->count2 = count2; |
| 8081 | mresult->count3 = count3; |
| 8082 | mlevel1 = mresult->level1; |
| 8083 | mlevel2 = mresult->level23; |
| 8084 | mlevel3 = mresult->level23 + 16*count2; |
| 8085 | memcpy(mlevel1, level1, 32); |
| 8086 | memset(mlevel2, 0xFF, 16*count2); |
| 8087 | memset(mlevel3, 0, 128*count3); |
| 8088 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8089 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8090 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8091 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8092 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8093 | /* unmapped character */ |
| 8094 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8095 | o1 = ch>>11; |
| 8096 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8097 | i2 = 16*mlevel1[o1] + o2; |
| 8098 | if (mlevel2[i2] == 0xFF) |
| 8099 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8100 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8101 | i3 = 128*mlevel2[i2] + o3; |
| 8102 | mlevel3[i3] = i; |
| 8103 | } |
| 8104 | return result; |
| 8105 | } |
| 8106 | |
| 8107 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8108 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8109 | { |
| 8110 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8111 | int l1 = c>>11; |
| 8112 | int l2 = (c>>7) & 0xF; |
| 8113 | int l3 = c & 0x7F; |
| 8114 | int i; |
| 8115 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8116 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8117 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8118 | if (c == 0) |
| 8119 | return 0; |
| 8120 | /* level 1*/ |
| 8121 | i = map->level1[l1]; |
| 8122 | if (i == 0xFF) { |
| 8123 | return -1; |
| 8124 | } |
| 8125 | /* level 2*/ |
| 8126 | i = map->level23[16*i+l2]; |
| 8127 | if (i == 0xFF) { |
| 8128 | return -1; |
| 8129 | } |
| 8130 | /* level 3 */ |
| 8131 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8132 | if (i == 0) { |
| 8133 | return -1; |
| 8134 | } |
| 8135 | return i; |
| 8136 | } |
| 8137 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8138 | /* Lookup the character ch in the mapping. If the character |
| 8139 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8140 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8141 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8142 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8143 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8144 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8145 | PyObject *x; |
| 8146 | |
| 8147 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8149 | x = PyObject_GetItem(mapping, w); |
| 8150 | Py_DECREF(w); |
| 8151 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8152 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8153 | /* No mapping found means: mapping is undefined. */ |
| 8154 | PyErr_Clear(); |
| 8155 | x = Py_None; |
| 8156 | Py_INCREF(x); |
| 8157 | return x; |
| 8158 | } else |
| 8159 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8160 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8161 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8162 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8163 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8164 | long value = PyLong_AS_LONG(x); |
| 8165 | if (value < 0 || value > 255) { |
| 8166 | PyErr_SetString(PyExc_TypeError, |
| 8167 | "character mapping must be in range(256)"); |
| 8168 | Py_DECREF(x); |
| 8169 | return NULL; |
| 8170 | } |
| 8171 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8173 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8174 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8175 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8176 | /* wrong return value */ |
| 8177 | PyErr_Format(PyExc_TypeError, |
| 8178 | "character mapping must return integer, bytes or None, not %.400s", |
| 8179 | x->ob_type->tp_name); |
| 8180 | Py_DECREF(x); |
| 8181 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8182 | } |
| 8183 | } |
| 8184 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8185 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8186 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8187 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8188 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8189 | /* exponentially overallocate to minimize reallocations */ |
| 8190 | if (requiredsize < 2*outsize) |
| 8191 | requiredsize = 2*outsize; |
| 8192 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8193 | return -1; |
| 8194 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8195 | } |
| 8196 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8197 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8198 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8199 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8200 | /* 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] | 8201 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8202 | space is available. Return a new reference to the object that |
| 8203 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8204 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8205 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8206 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8207 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8208 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8209 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8210 | PyObject *rep; |
| 8211 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8212 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8213 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8214 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8215 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8216 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8217 | if (res == -1) |
| 8218 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8219 | if (outsize<requiredsize) |
| 8220 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8221 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8222 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8223 | outstart[(*outpos)++] = (char)res; |
| 8224 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8225 | } |
| 8226 | |
| 8227 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8228 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8229 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8230 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8231 | Py_DECREF(rep); |
| 8232 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8233 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | if (PyLong_Check(rep)) { |
| 8235 | Py_ssize_t requiredsize = *outpos+1; |
| 8236 | if (outsize<requiredsize) |
| 8237 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8238 | Py_DECREF(rep); |
| 8239 | return enc_EXCEPTION; |
| 8240 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8241 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8242 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8243 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8244 | else { |
| 8245 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8246 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8247 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8248 | if (outsize<requiredsize) |
| 8249 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8250 | Py_DECREF(rep); |
| 8251 | return enc_EXCEPTION; |
| 8252 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8253 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8254 | memcpy(outstart + *outpos, repchars, repsize); |
| 8255 | *outpos += repsize; |
| 8256 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8257 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8258 | Py_DECREF(rep); |
| 8259 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8260 | } |
| 8261 | |
| 8262 | /* handle an error in PyUnicode_EncodeCharmap |
| 8263 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8264 | static int |
| 8265 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8266 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8267 | PyObject **exceptionObject, |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8268 | _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] | 8269 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8270 | { |
| 8271 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8272 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8273 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8274 | enum PyUnicode_Kind kind; |
| 8275 | void *data; |
| 8276 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8277 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8278 | Py_ssize_t collstartpos = *inpos; |
| 8279 | Py_ssize_t collendpos = *inpos+1; |
| 8280 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8281 | char *encoding = "charmap"; |
| 8282 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8283 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8284 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8285 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8286 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8287 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8288 | return -1; |
| 8289 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8290 | /* find all unencodable characters */ |
| 8291 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8292 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8293 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8294 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8295 | val = encoding_map_lookup(ch, mapping); |
| 8296 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8297 | break; |
| 8298 | ++collendpos; |
| 8299 | continue; |
| 8300 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8301 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8302 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8303 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | if (rep==NULL) |
| 8305 | return -1; |
| 8306 | else if (rep!=Py_None) { |
| 8307 | Py_DECREF(rep); |
| 8308 | break; |
| 8309 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8310 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8311 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8312 | } |
| 8313 | /* cache callback name lookup |
| 8314 | * (if not done yet, i.e. it's the first error) */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8315 | if (*error_handler == _Py_ERROR_UNKNOWN) |
| 8316 | *error_handler = get_error_handler(errors); |
| 8317 | |
| 8318 | switch (*error_handler) { |
| 8319 | case _Py_ERROR_STRICT: |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8320 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8321 | return -1; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8322 | |
| 8323 | case _Py_ERROR_REPLACE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8324 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8325 | x = charmapencode_output('?', mapping, res, respos); |
| 8326 | if (x==enc_EXCEPTION) { |
| 8327 | return -1; |
| 8328 | } |
| 8329 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8330 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8331 | return -1; |
| 8332 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8333 | } |
| 8334 | /* fall through */ |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8335 | case _Py_ERROR_IGNORE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8336 | *inpos = collendpos; |
| 8337 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8338 | |
| 8339 | case _Py_ERROR_XMLCHARREFREPLACE: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8340 | /* generate replacement (temporarily (mis)uses p) */ |
| 8341 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8342 | char buffer[2+29+1+1]; |
| 8343 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8344 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8345 | for (cp = buffer; *cp; ++cp) { |
| 8346 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8347 | if (x==enc_EXCEPTION) |
| 8348 | return -1; |
| 8349 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8350 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8351 | return -1; |
| 8352 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8353 | } |
| 8354 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8355 | *inpos = collendpos; |
| 8356 | break; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8357 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8358 | default: |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8359 | repunicode = unicode_encode_call_errorhandler(errors, error_handler_obj, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8360 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8361 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8362 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8363 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8364 | if (PyBytes_Check(repunicode)) { |
| 8365 | /* Directly copy bytes result to output. */ |
| 8366 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8367 | Py_ssize_t requiredsize; |
| 8368 | repsize = PyBytes_Size(repunicode); |
| 8369 | requiredsize = *respos + repsize; |
| 8370 | if (requiredsize > outsize) |
| 8371 | /* Make room for all additional bytes. */ |
| 8372 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8373 | Py_DECREF(repunicode); |
| 8374 | return -1; |
| 8375 | } |
| 8376 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8377 | PyBytes_AsString(repunicode), repsize); |
| 8378 | *respos += repsize; |
| 8379 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8380 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8381 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8382 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8383 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8384 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8385 | Py_DECREF(repunicode); |
| 8386 | return -1; |
| 8387 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8388 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8389 | data = PyUnicode_DATA(repunicode); |
| 8390 | kind = PyUnicode_KIND(repunicode); |
| 8391 | for (index = 0; index < repsize; index++) { |
| 8392 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8393 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8394 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8395 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8396 | return -1; |
| 8397 | } |
| 8398 | else if (x==enc_FAILED) { |
| 8399 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8400 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8401 | return -1; |
| 8402 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8403 | } |
| 8404 | *inpos = newpos; |
| 8405 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8406 | } |
| 8407 | return 0; |
| 8408 | } |
| 8409 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8410 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8411 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8412 | PyObject *mapping, |
| 8413 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8414 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8415 | /* output object */ |
| 8416 | PyObject *res = NULL; |
| 8417 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8418 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8419 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8420 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8421 | Py_ssize_t respos = 0; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8422 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8423 | PyObject *exc = NULL; |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8424 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8425 | void *data; |
| 8426 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8427 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8428 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8429 | return NULL; |
| 8430 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8431 | data = PyUnicode_DATA(unicode); |
| 8432 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8433 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | /* Default to Latin-1 */ |
| 8435 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8436 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8437 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8438 | /* allocate enough for a simple encoding without |
| 8439 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8440 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8441 | if (res == NULL) |
| 8442 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8443 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8444 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8445 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8446 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8447 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8448 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8449 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8450 | if (x==enc_EXCEPTION) /* error */ |
| 8451 | goto onError; |
| 8452 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8453 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8454 | &exc, |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8455 | &error_handler, &error_handler_obj, errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8456 | &res, &respos)) { |
| 8457 | goto onError; |
| 8458 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8459 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8460 | else |
| 8461 | /* done with this character => adjust input position */ |
| 8462 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8463 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8465 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8466 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8467 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8468 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8469 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8470 | Py_XDECREF(exc); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8471 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8472 | return res; |
| 8473 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8474 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8475 | Py_XDECREF(res); |
| 8476 | Py_XDECREF(exc); |
Victor Stinner | 5014920 | 2015-09-22 00:26:54 +0200 | [diff] [blame] | 8477 | Py_XDECREF(error_handler_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8478 | return NULL; |
| 8479 | } |
| 8480 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8481 | /* Deprecated */ |
| 8482 | PyObject * |
| 8483 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8484 | Py_ssize_t size, |
| 8485 | PyObject *mapping, |
| 8486 | const char *errors) |
| 8487 | { |
| 8488 | PyObject *result; |
| 8489 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8490 | if (unicode == NULL) |
| 8491 | return NULL; |
| 8492 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8493 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8494 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8495 | } |
| 8496 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8497 | PyObject * |
| 8498 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8499 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8500 | { |
| 8501 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8502 | PyErr_BadArgument(); |
| 8503 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8504 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8505 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | } |
| 8507 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8508 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8509 | static void |
| 8510 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8511 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8512 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8513 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8514 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8515 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8516 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8517 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8518 | } |
| 8519 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8520 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8521 | goto onError; |
| 8522 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8523 | goto onError; |
| 8524 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8525 | goto onError; |
| 8526 | return; |
| 8527 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 8528 | Py_CLEAR(*exceptionObject); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8529 | } |
| 8530 | } |
| 8531 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8532 | /* error handling callback helper: |
| 8533 | build arguments, call the callback and check the arguments, |
| 8534 | put the result into newpos and return the replacement string, which |
| 8535 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8536 | static PyObject * |
| 8537 | unicode_translate_call_errorhandler(const char *errors, |
| 8538 | PyObject **errorHandler, |
| 8539 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8540 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8541 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8542 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8543 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8544 | 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] | 8545 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8546 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8547 | PyObject *restuple; |
| 8548 | PyObject *resunicode; |
| 8549 | |
| 8550 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8551 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8552 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8554 | } |
| 8555 | |
| 8556 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8557 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8558 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8559 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8560 | |
| 8561 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8562 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8563 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8564 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8565 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8566 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8567 | Py_DECREF(restuple); |
| 8568 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8569 | } |
| 8570 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8571 | &resunicode, &i_newpos)) { |
| 8572 | Py_DECREF(restuple); |
| 8573 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8574 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8575 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8576 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8577 | else |
| 8578 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8579 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 8580 | 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] | 8581 | Py_DECREF(restuple); |
| 8582 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8583 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8584 | Py_INCREF(resunicode); |
| 8585 | Py_DECREF(restuple); |
| 8586 | return resunicode; |
| 8587 | } |
| 8588 | |
| 8589 | /* Lookup the character ch in the mapping and put the result in result, |
| 8590 | which must be decrefed by the caller. |
| 8591 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8592 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8593 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8594 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8595 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8596 | PyObject *x; |
| 8597 | |
| 8598 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8599 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8600 | x = PyObject_GetItem(mapping, w); |
| 8601 | Py_DECREF(w); |
| 8602 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8603 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8604 | /* No mapping found means: use 1:1 mapping. */ |
| 8605 | PyErr_Clear(); |
| 8606 | *result = NULL; |
| 8607 | return 0; |
| 8608 | } else |
| 8609 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8610 | } |
| 8611 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8612 | *result = x; |
| 8613 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8614 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8615 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8616 | long value = PyLong_AS_LONG(x); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8617 | if (value < 0 || value > MAX_UNICODE) { |
| 8618 | PyErr_Format(PyExc_ValueError, |
| 8619 | "character mapping must be in range(0x%x)", |
| 8620 | MAX_UNICODE+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8621 | Py_DECREF(x); |
| 8622 | return -1; |
| 8623 | } |
| 8624 | *result = x; |
| 8625 | return 0; |
| 8626 | } |
| 8627 | else if (PyUnicode_Check(x)) { |
| 8628 | *result = x; |
| 8629 | return 0; |
| 8630 | } |
| 8631 | else { |
| 8632 | /* wrong return value */ |
| 8633 | PyErr_SetString(PyExc_TypeError, |
| 8634 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8635 | Py_DECREF(x); |
| 8636 | return -1; |
| 8637 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8638 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8639 | |
| 8640 | /* lookup the character, write the result into the writer. |
| 8641 | Return 1 if the result was written into the writer, return 0 if the mapping |
| 8642 | was undefined, raise an exception return -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8643 | static int |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8644 | charmaptranslate_output(Py_UCS4 ch, PyObject *mapping, |
| 8645 | _PyUnicodeWriter *writer) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8646 | { |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8647 | PyObject *item; |
| 8648 | |
| 8649 | if (charmaptranslate_lookup(ch, mapping, &item)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8650 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8651 | |
| 8652 | if (item == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8653 | /* not found => default to 1:1 mapping */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8654 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8655 | return -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8656 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8657 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8658 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8659 | |
| 8660 | if (item == Py_None) { |
| 8661 | Py_DECREF(item); |
| 8662 | return 0; |
| 8663 | } |
| 8664 | |
| 8665 | if (PyLong_Check(item)) { |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8666 | long ch = (Py_UCS4)PyLong_AS_LONG(item); |
| 8667 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8668 | used it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8669 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
| 8670 | Py_DECREF(item); |
| 8671 | return -1; |
| 8672 | } |
| 8673 | Py_DECREF(item); |
| 8674 | return 1; |
| 8675 | } |
| 8676 | |
| 8677 | if (!PyUnicode_Check(item)) { |
| 8678 | Py_DECREF(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 | |
| 8682 | if (_PyUnicodeWriter_WriteStr(writer, item) < 0) { |
| 8683 | Py_DECREF(item); |
| 8684 | return -1; |
| 8685 | } |
| 8686 | |
| 8687 | Py_DECREF(item); |
| 8688 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8689 | } |
| 8690 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8691 | static int |
| 8692 | unicode_fast_translate_lookup(PyObject *mapping, Py_UCS1 ch, |
| 8693 | Py_UCS1 *translate) |
| 8694 | { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8695 | PyObject *item = NULL; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8696 | int ret = 0; |
| 8697 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8698 | if (charmaptranslate_lookup(ch, mapping, &item)) { |
| 8699 | return -1; |
| 8700 | } |
| 8701 | |
| 8702 | if (item == Py_None) { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8703 | /* deletion */ |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8704 | translate[ch] = 0xfe; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8705 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8706 | else if (item == NULL) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8707 | /* not found => default to 1:1 mapping */ |
| 8708 | translate[ch] = ch; |
| 8709 | return 1; |
| 8710 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8711 | else if (PyLong_Check(item)) { |
Victor Stinner | 4dd2525 | 2014-04-08 09:14:21 +0200 | [diff] [blame] | 8712 | long replace = PyLong_AS_LONG(item); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8713 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8714 | used it */ |
| 8715 | if (127 < replace) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8716 | /* invalid character or character outside ASCII: |
| 8717 | skip the fast translate */ |
| 8718 | goto exit; |
| 8719 | } |
| 8720 | translate[ch] = (Py_UCS1)replace; |
| 8721 | } |
| 8722 | else if (PyUnicode_Check(item)) { |
| 8723 | Py_UCS4 replace; |
| 8724 | |
| 8725 | if (PyUnicode_READY(item) == -1) { |
| 8726 | Py_DECREF(item); |
| 8727 | return -1; |
| 8728 | } |
| 8729 | if (PyUnicode_GET_LENGTH(item) != 1) |
| 8730 | goto exit; |
| 8731 | |
| 8732 | replace = PyUnicode_READ_CHAR(item, 0); |
| 8733 | if (replace > 127) |
| 8734 | goto exit; |
| 8735 | translate[ch] = (Py_UCS1)replace; |
| 8736 | } |
| 8737 | else { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8738 | /* not None, NULL, long or unicode */ |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8739 | goto exit; |
| 8740 | } |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8741 | ret = 1; |
| 8742 | |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8743 | exit: |
| 8744 | Py_DECREF(item); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8745 | return ret; |
| 8746 | } |
| 8747 | |
| 8748 | /* Fast path for ascii => ascii translation. Return 1 if the whole string |
| 8749 | was translated into writer, return 0 if the input string was partially |
| 8750 | translated into writer, raise an exception and return -1 on error. */ |
| 8751 | static int |
| 8752 | unicode_fast_translate(PyObject *input, PyObject *mapping, |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8753 | _PyUnicodeWriter *writer, int ignore) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8754 | { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8755 | Py_UCS1 ascii_table[128], ch, ch2; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8756 | Py_ssize_t len; |
| 8757 | Py_UCS1 *in, *end, *out; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8758 | int res = 0; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8759 | |
| 8760 | if (PyUnicode_READY(input) == -1) |
| 8761 | return -1; |
| 8762 | if (!PyUnicode_IS_ASCII(input)) |
| 8763 | return 0; |
| 8764 | len = PyUnicode_GET_LENGTH(input); |
| 8765 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8766 | memset(ascii_table, 0xff, 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8767 | |
| 8768 | in = PyUnicode_1BYTE_DATA(input); |
| 8769 | end = in + len; |
| 8770 | |
| 8771 | assert(PyUnicode_IS_ASCII(writer->buffer)); |
| 8772 | assert(PyUnicode_GET_LENGTH(writer->buffer) == len); |
| 8773 | out = PyUnicode_1BYTE_DATA(writer->buffer); |
| 8774 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8775 | for (; in < end; in++) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8776 | ch = *in; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8777 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8778 | if (ch2 == 0xff) { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8779 | int translate = unicode_fast_translate_lookup(mapping, ch, |
| 8780 | ascii_table); |
| 8781 | if (translate < 0) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8782 | return -1; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8783 | if (translate == 0) |
| 8784 | goto exit; |
| 8785 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8786 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8787 | if (ch2 == 0xfe) { |
| 8788 | if (ignore) |
| 8789 | continue; |
| 8790 | goto exit; |
| 8791 | } |
| 8792 | assert(ch2 < 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8793 | *out = ch2; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8794 | out++; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8795 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8796 | res = 1; |
| 8797 | |
| 8798 | exit: |
| 8799 | writer->pos = out - PyUnicode_1BYTE_DATA(writer->buffer); |
| 8800 | return res; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8801 | } |
| 8802 | |
Victor Stinner | 3222da2 | 2015-10-01 22:07:32 +0200 | [diff] [blame] | 8803 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8804 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8805 | PyObject *mapping, |
| 8806 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8807 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8808 | /* input object */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8809 | char *data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8810 | Py_ssize_t size, i; |
| 8811 | int kind; |
| 8812 | /* output buffer */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8813 | _PyUnicodeWriter writer; |
| 8814 | /* error handler */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8815 | char *reason = "character maps to <undefined>"; |
| 8816 | PyObject *errorHandler = NULL; |
| 8817 | PyObject *exc = NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8818 | int ignore; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8819 | int res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8820 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8821 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8822 | PyErr_BadArgument(); |
| 8823 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8824 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8825 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8826 | if (PyUnicode_READY(input) == -1) |
| 8827 | return NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8828 | data = (char*)PyUnicode_DATA(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8829 | kind = PyUnicode_KIND(input); |
| 8830 | size = PyUnicode_GET_LENGTH(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8831 | |
| 8832 | if (size == 0) { |
| 8833 | Py_INCREF(input); |
| 8834 | return input; |
| 8835 | } |
| 8836 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8837 | /* allocate enough for a simple 1:1 translation without |
| 8838 | replacements, if we need more, we'll resize */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8839 | _PyUnicodeWriter_Init(&writer); |
| 8840 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8841 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8842 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8843 | ignore = (errors != NULL && strcmp(errors, "ignore") == 0); |
| 8844 | |
| 8845 | res = unicode_fast_translate(input, mapping, &writer, ignore); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8846 | if (res < 0) { |
| 8847 | _PyUnicodeWriter_Dealloc(&writer); |
| 8848 | return NULL; |
| 8849 | } |
| 8850 | if (res == 1) |
| 8851 | return _PyUnicodeWriter_Finish(&writer); |
| 8852 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8853 | i = writer.pos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8854 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8855 | /* try to encode it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8856 | int translate; |
| 8857 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8858 | Py_ssize_t newpos; |
| 8859 | /* startpos for collecting untranslatable chars */ |
| 8860 | Py_ssize_t collstart; |
| 8861 | Py_ssize_t collend; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8862 | Py_UCS4 ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8863 | |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8864 | ch = PyUnicode_READ(kind, data, i); |
| 8865 | translate = charmaptranslate_output(ch, mapping, &writer); |
| 8866 | if (translate < 0) |
| 8867 | goto onError; |
| 8868 | |
| 8869 | if (translate != 0) { |
| 8870 | /* it worked => adjust input pointer */ |
| 8871 | ++i; |
| 8872 | continue; |
| 8873 | } |
| 8874 | |
| 8875 | /* untranslatable character */ |
| 8876 | collstart = i; |
| 8877 | collend = i+1; |
| 8878 | |
| 8879 | /* find all untranslatable characters */ |
| 8880 | while (collend < size) { |
| 8881 | PyObject *x; |
| 8882 | ch = PyUnicode_READ(kind, data, collend); |
| 8883 | if (charmaptranslate_lookup(ch, mapping, &x)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8884 | goto onError; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8885 | Py_XDECREF(x); |
| 8886 | if (x != Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8887 | break; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8888 | ++collend; |
| 8889 | } |
| 8890 | |
| 8891 | if (ignore) { |
| 8892 | i = collend; |
| 8893 | } |
| 8894 | else { |
| 8895 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 8896 | reason, input, &exc, |
| 8897 | collstart, collend, &newpos); |
| 8898 | if (repunicode == NULL) |
| 8899 | goto onError; |
| 8900 | if (_PyUnicodeWriter_WriteStr(&writer, repunicode) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8901 | Py_DECREF(repunicode); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8902 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8903 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8904 | Py_DECREF(repunicode); |
| 8905 | i = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8906 | } |
| 8907 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8908 | Py_XDECREF(exc); |
| 8909 | Py_XDECREF(errorHandler); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8910 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8911 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8912 | onError: |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8913 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8914 | Py_XDECREF(exc); |
| 8915 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8916 | return NULL; |
| 8917 | } |
| 8918 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8919 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8920 | PyObject * |
| 8921 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8922 | Py_ssize_t size, |
| 8923 | PyObject *mapping, |
| 8924 | const char *errors) |
| 8925 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8926 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8927 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8928 | if (!unicode) |
| 8929 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8930 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8931 | Py_DECREF(unicode); |
| 8932 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8933 | } |
| 8934 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8935 | PyObject * |
| 8936 | PyUnicode_Translate(PyObject *str, |
| 8937 | PyObject *mapping, |
| 8938 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8939 | { |
| 8940 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8941 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8942 | str = PyUnicode_FromObject(str); |
| 8943 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8944 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8945 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8946 | Py_DECREF(str); |
| 8947 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8948 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8950 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8951 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8952 | { |
| 8953 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8954 | called as a callback from fixup() which does it already. */ |
| 8955 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8956 | const int kind = PyUnicode_KIND(self); |
| 8957 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8958 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8959 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 | Py_ssize_t i; |
| 8961 | |
| 8962 | for (i = 0; i < len; ++i) { |
| 8963 | ch = PyUnicode_READ(kind, data, i); |
| 8964 | fixed = 0; |
| 8965 | if (ch > 127) { |
| 8966 | if (Py_UNICODE_ISSPACE(ch)) |
| 8967 | fixed = ' '; |
| 8968 | else { |
| 8969 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8970 | if (decimal >= 0) |
| 8971 | fixed = '0' + decimal; |
| 8972 | } |
| 8973 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8974 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8975 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8976 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8977 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8978 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8979 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8980 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 | } |
| 8982 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8983 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8984 | } |
| 8985 | |
| 8986 | PyObject * |
| 8987 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8988 | { |
| 8989 | if (!PyUnicode_Check(unicode)) { |
| 8990 | PyErr_BadInternalCall(); |
| 8991 | return NULL; |
| 8992 | } |
| 8993 | if (PyUnicode_READY(unicode) == -1) |
| 8994 | return NULL; |
| 8995 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8996 | /* If the string is already ASCII, just return the same string */ |
| 8997 | Py_INCREF(unicode); |
| 8998 | return unicode; |
| 8999 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9000 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | } |
| 9002 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9003 | PyObject * |
| 9004 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 9005 | Py_ssize_t length) |
| 9006 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9007 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9008 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9009 | Py_UCS4 maxchar; |
| 9010 | enum PyUnicode_Kind kind; |
| 9011 | void *data; |
| 9012 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 9013 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9014 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 9015 | Py_UCS4 ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9016 | if (ch > 127) { |
| 9017 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9018 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9019 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9020 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9021 | } |
| 9022 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9023 | |
| 9024 | /* Copy to a new string */ |
| 9025 | decimal = PyUnicode_New(length, maxchar); |
| 9026 | if (decimal == NULL) |
| 9027 | return decimal; |
| 9028 | kind = PyUnicode_KIND(decimal); |
| 9029 | data = PyUnicode_DATA(decimal); |
| 9030 | /* Iterate over code points */ |
| 9031 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 9032 | Py_UCS4 ch = s[i]; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 9033 | if (ch > 127) { |
| 9034 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 9035 | if (decimal >= 0) |
| 9036 | ch = '0' + decimal; |
| 9037 | } |
| 9038 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9039 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 9040 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 9041 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9042 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 9043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9044 | int |
| 9045 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 9046 | Py_ssize_t length, |
| 9047 | char *output, |
| 9048 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9049 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 9050 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9051 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9052 | enum PyUnicode_Kind kind; |
| 9053 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9054 | |
| 9055 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9056 | PyErr_BadArgument(); |
| 9057 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9058 | } |
| 9059 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9060 | unicode = PyUnicode_FromUnicode(s, length); |
| 9061 | if (unicode == NULL) |
| 9062 | return -1; |
| 9063 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 9064 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9065 | Py_DECREF(unicode); |
| 9066 | return -1; |
| 9067 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9068 | kind = PyUnicode_KIND(unicode); |
| 9069 | data = PyUnicode_DATA(unicode); |
| 9070 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9071 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9072 | PyObject *exc; |
| 9073 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9074 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9075 | Py_ssize_t startpos; |
| 9076 | |
| 9077 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9078 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9079 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9080 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9081 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9082 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9083 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9084 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 9085 | if (decimal >= 0) { |
| 9086 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9087 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9088 | continue; |
| 9089 | } |
| 9090 | if (0 < ch && ch < 256) { |
| 9091 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 9092 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9093 | continue; |
| 9094 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9095 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9096 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 9097 | exc = NULL; |
| 9098 | raise_encode_exception(&exc, "decimal", unicode, |
| 9099 | startpos, startpos+1, |
| 9100 | "invalid decimal Unicode string"); |
| 9101 | Py_XDECREF(exc); |
| 9102 | Py_DECREF(unicode); |
| 9103 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9104 | } |
| 9105 | /* 0-terminate the output string */ |
| 9106 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 9107 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9108 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 9109 | } |
| 9110 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | /* --- Helpers ------------------------------------------------------------ */ |
| 9112 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9113 | /* helper macro to fixup start/end slice values */ |
| 9114 | #define ADJUST_INDICES(start, end, len) \ |
| 9115 | if (end > len) \ |
| 9116 | end = len; \ |
| 9117 | else if (end < 0) { \ |
| 9118 | end += len; \ |
| 9119 | if (end < 0) \ |
| 9120 | end = 0; \ |
| 9121 | } \ |
| 9122 | if (start < 0) { \ |
| 9123 | start += len; \ |
| 9124 | if (start < 0) \ |
| 9125 | start = 0; \ |
| 9126 | } |
| 9127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9128 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9129 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9130 | Py_ssize_t start, |
| 9131 | Py_ssize_t end) |
| 9132 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9133 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9134 | void *buf1, *buf2; |
| 9135 | Py_ssize_t len1, len2, result; |
| 9136 | |
| 9137 | kind1 = PyUnicode_KIND(s1); |
| 9138 | kind2 = PyUnicode_KIND(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9139 | if (kind1 < kind2) |
| 9140 | return -1; |
| 9141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9142 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9143 | len2 = PyUnicode_GET_LENGTH(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9144 | ADJUST_INDICES(start, end, len1); |
| 9145 | if (end - start < len2) |
| 9146 | return -1; |
| 9147 | |
| 9148 | buf1 = PyUnicode_DATA(s1); |
| 9149 | buf2 = PyUnicode_DATA(s2); |
| 9150 | if (len2 == 1) { |
| 9151 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 9152 | result = findchar((const char *)buf1 + kind1*start, |
| 9153 | kind1, end - start, ch, direction); |
| 9154 | if (result == -1) |
| 9155 | return -1; |
| 9156 | else |
| 9157 | return start + result; |
| 9158 | } |
| 9159 | |
| 9160 | if (kind2 != kind1) { |
| 9161 | buf2 = _PyUnicode_AsKind(s2, kind1); |
| 9162 | if (!buf2) |
| 9163 | return -2; |
| 9164 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9165 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9166 | if (direction > 0) { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9167 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9168 | case PyUnicode_1BYTE_KIND: |
| 9169 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9170 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9171 | else |
| 9172 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9173 | break; |
| 9174 | case PyUnicode_2BYTE_KIND: |
| 9175 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9176 | break; |
| 9177 | case PyUnicode_4BYTE_KIND: |
| 9178 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9179 | break; |
| 9180 | default: |
| 9181 | assert(0); result = -2; |
| 9182 | } |
| 9183 | } |
| 9184 | else { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9185 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9186 | case PyUnicode_1BYTE_KIND: |
| 9187 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9188 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9189 | else |
| 9190 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9191 | break; |
| 9192 | case PyUnicode_2BYTE_KIND: |
| 9193 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9194 | break; |
| 9195 | case PyUnicode_4BYTE_KIND: |
| 9196 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9197 | break; |
| 9198 | default: |
| 9199 | assert(0); result = -2; |
| 9200 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9201 | } |
| 9202 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9203 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9204 | PyMem_Free(buf2); |
| 9205 | |
| 9206 | return result; |
| 9207 | } |
| 9208 | |
| 9209 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9210 | _PyUnicode_InsertThousandsGrouping( |
| 9211 | PyObject *unicode, Py_ssize_t index, |
| 9212 | Py_ssize_t n_buffer, |
| 9213 | void *digits, Py_ssize_t n_digits, |
| 9214 | Py_ssize_t min_width, |
| 9215 | const char *grouping, PyObject *thousands_sep, |
| 9216 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9217 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9218 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9219 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9220 | Py_ssize_t thousands_sep_len; |
| 9221 | Py_ssize_t len; |
| 9222 | |
| 9223 | if (unicode != NULL) { |
| 9224 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9225 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9226 | } |
| 9227 | else { |
| 9228 | kind = PyUnicode_1BYTE_KIND; |
| 9229 | data = NULL; |
| 9230 | } |
| 9231 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9232 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9233 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9234 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9235 | if (thousands_sep_kind < kind) { |
| 9236 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9237 | if (!thousands_sep_data) |
| 9238 | return -1; |
| 9239 | } |
| 9240 | else { |
| 9241 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9242 | if (!data) |
| 9243 | return -1; |
| 9244 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9245 | } |
| 9246 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9247 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9248 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9249 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9250 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9251 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9252 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9253 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9254 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9255 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9256 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9257 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9258 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9259 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9260 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9261 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9262 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9263 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9264 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9265 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9266 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9267 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9268 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9269 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9270 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9271 | break; |
| 9272 | default: |
| 9273 | assert(0); |
| 9274 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9275 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9276 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9277 | if (thousands_sep_kind < kind) |
| 9278 | PyMem_Free(thousands_sep_data); |
| 9279 | else |
| 9280 | PyMem_Free(data); |
| 9281 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9282 | if (unicode == NULL) { |
| 9283 | *maxchar = 127; |
| 9284 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9285 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9286 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9287 | } |
| 9288 | } |
| 9289 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9290 | } |
| 9291 | |
| 9292 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9293 | Py_ssize_t |
| 9294 | PyUnicode_Count(PyObject *str, |
| 9295 | PyObject *substr, |
| 9296 | Py_ssize_t start, |
| 9297 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9299 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9300 | PyObject* str_obj; |
| 9301 | PyObject* sub_obj; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9302 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9303 | void *buf1 = NULL, *buf2 = NULL; |
| 9304 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9305 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9306 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9307 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9308 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9309 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9310 | if (!sub_obj) { |
| 9311 | Py_DECREF(str_obj); |
| 9312 | return -1; |
| 9313 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9314 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9315 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9316 | Py_DECREF(str_obj); |
| 9317 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9318 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9319 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9320 | kind1 = PyUnicode_KIND(str_obj); |
| 9321 | kind2 = PyUnicode_KIND(sub_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9322 | if (kind1 < kind2) { |
| 9323 | Py_DECREF(sub_obj); |
| 9324 | Py_DECREF(str_obj); |
| 9325 | return 0; |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9326 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9327 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9329 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9330 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9331 | if (end - start < len2) { |
| 9332 | Py_DECREF(sub_obj); |
| 9333 | Py_DECREF(str_obj); |
| 9334 | return 0; |
| 9335 | } |
| 9336 | |
| 9337 | buf1 = PyUnicode_DATA(str_obj); |
| 9338 | buf2 = PyUnicode_DATA(sub_obj); |
| 9339 | if (kind2 != kind1) { |
| 9340 | buf2 = _PyUnicode_AsKind(sub_obj, kind1); |
| 9341 | if (!buf2) |
| 9342 | goto onError; |
| 9343 | } |
| 9344 | |
| 9345 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9346 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9347 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9348 | result = asciilib_count( |
| 9349 | ((Py_UCS1*)buf1) + start, end - start, |
| 9350 | buf2, len2, PY_SSIZE_T_MAX |
| 9351 | ); |
| 9352 | else |
| 9353 | result = ucs1lib_count( |
| 9354 | ((Py_UCS1*)buf1) + start, end - start, |
| 9355 | buf2, len2, PY_SSIZE_T_MAX |
| 9356 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9357 | break; |
| 9358 | case PyUnicode_2BYTE_KIND: |
| 9359 | result = ucs2lib_count( |
| 9360 | ((Py_UCS2*)buf1) + start, end - start, |
| 9361 | buf2, len2, PY_SSIZE_T_MAX |
| 9362 | ); |
| 9363 | break; |
| 9364 | case PyUnicode_4BYTE_KIND: |
| 9365 | result = ucs4lib_count( |
| 9366 | ((Py_UCS4*)buf1) + start, end - start, |
| 9367 | buf2, len2, PY_SSIZE_T_MAX |
| 9368 | ); |
| 9369 | break; |
| 9370 | default: |
| 9371 | assert(0); result = 0; |
| 9372 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9373 | |
| 9374 | Py_DECREF(sub_obj); |
| 9375 | Py_DECREF(str_obj); |
| 9376 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9377 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9378 | PyMem_Free(buf2); |
| 9379 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9380 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9381 | onError: |
| 9382 | Py_DECREF(sub_obj); |
| 9383 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9384 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9385 | PyMem_Free(buf2); |
| 9386 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9387 | } |
| 9388 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9389 | Py_ssize_t |
| 9390 | PyUnicode_Find(PyObject *str, |
| 9391 | PyObject *sub, |
| 9392 | Py_ssize_t start, |
| 9393 | Py_ssize_t end, |
| 9394 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9395 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9396 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9398 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9399 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9400 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9401 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9402 | if (!sub) { |
| 9403 | Py_DECREF(str); |
| 9404 | return -2; |
| 9405 | } |
| 9406 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9407 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9408 | Py_DECREF(str); |
| 9409 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9410 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9411 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9412 | result = any_find_slice(direction, |
| 9413 | str, sub, start, end |
| 9414 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9416 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9417 | Py_DECREF(sub); |
| 9418 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9419 | return result; |
| 9420 | } |
| 9421 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9422 | Py_ssize_t |
| 9423 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9424 | Py_ssize_t start, Py_ssize_t end, |
| 9425 | int direction) |
| 9426 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9427 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9428 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9429 | if (PyUnicode_READY(str) == -1) |
| 9430 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9431 | if (start < 0 || end < 0) { |
| 9432 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9433 | return -2; |
| 9434 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9435 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9436 | end = PyUnicode_GET_LENGTH(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9437 | if (start >= end) |
| 9438 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9439 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9440 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9441 | kind, end-start, ch, direction); |
| 9442 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9443 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9444 | else |
| 9445 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9446 | } |
| 9447 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9448 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9449 | tailmatch(PyObject *self, |
| 9450 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9451 | Py_ssize_t start, |
| 9452 | Py_ssize_t end, |
| 9453 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9454 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9455 | int kind_self; |
| 9456 | int kind_sub; |
| 9457 | void *data_self; |
| 9458 | void *data_sub; |
| 9459 | Py_ssize_t offset; |
| 9460 | Py_ssize_t i; |
| 9461 | Py_ssize_t end_sub; |
| 9462 | |
| 9463 | if (PyUnicode_READY(self) == -1 || |
| 9464 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9465 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9466 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9467 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9468 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9469 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9470 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9471 | |
Serhiy Storchaka | d4ea03c | 2015-05-31 09:15:51 +0300 | [diff] [blame] | 9472 | if (PyUnicode_GET_LENGTH(substring) == 0) |
| 9473 | return 1; |
| 9474 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9475 | kind_self = PyUnicode_KIND(self); |
| 9476 | data_self = PyUnicode_DATA(self); |
| 9477 | kind_sub = PyUnicode_KIND(substring); |
| 9478 | data_sub = PyUnicode_DATA(substring); |
| 9479 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9480 | |
| 9481 | if (direction > 0) |
| 9482 | offset = end; |
| 9483 | else |
| 9484 | offset = start; |
| 9485 | |
| 9486 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9487 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9488 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9489 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9490 | /* If both are of the same kind, memcmp is sufficient */ |
| 9491 | if (kind_self == kind_sub) { |
| 9492 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9493 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9494 | data_sub, |
| 9495 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9496 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9497 | } |
| 9498 | /* otherwise we have to compare each character by first accesing it */ |
| 9499 | else { |
| 9500 | /* We do not need to compare 0 and len(substring)-1 because |
| 9501 | the if statement above ensured already that they are equal |
| 9502 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9503 | for (i = 1; i < end_sub; ++i) { |
| 9504 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9505 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9506 | return 0; |
| 9507 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9508 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9509 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9510 | } |
| 9511 | |
| 9512 | return 0; |
| 9513 | } |
| 9514 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9515 | Py_ssize_t |
| 9516 | PyUnicode_Tailmatch(PyObject *str, |
| 9517 | PyObject *substr, |
| 9518 | Py_ssize_t start, |
| 9519 | Py_ssize_t end, |
| 9520 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9521 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9522 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9523 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9524 | str = PyUnicode_FromObject(str); |
| 9525 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9526 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9527 | substr = PyUnicode_FromObject(substr); |
| 9528 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9529 | Py_DECREF(str); |
| 9530 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9532 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9533 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9534 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9535 | Py_DECREF(str); |
| 9536 | Py_DECREF(substr); |
| 9537 | return result; |
| 9538 | } |
| 9539 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9540 | /* Apply fixfct filter to the Unicode object self and return a |
| 9541 | reference to the modified object */ |
| 9542 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9543 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9544 | fixup(PyObject *self, |
| 9545 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9546 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9547 | PyObject *u; |
| 9548 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9549 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9550 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9551 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9552 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9553 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9554 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9555 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9556 | /* fix functions return the new maximum character in a string, |
| 9557 | if the kind of the resulting unicode object does not change, |
| 9558 | everything is fine. Otherwise we need to change the string kind |
| 9559 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9560 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9561 | |
| 9562 | if (maxchar_new == 0) { |
| 9563 | /* no changes */; |
| 9564 | if (PyUnicode_CheckExact(self)) { |
| 9565 | Py_DECREF(u); |
| 9566 | Py_INCREF(self); |
| 9567 | return self; |
| 9568 | } |
| 9569 | else |
| 9570 | return u; |
| 9571 | } |
| 9572 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9573 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9574 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9575 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9576 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9577 | |
| 9578 | /* In case the maximum character changed, we need to |
| 9579 | convert the string to the new category. */ |
| 9580 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9581 | if (v == NULL) { |
| 9582 | Py_DECREF(u); |
| 9583 | return NULL; |
| 9584 | } |
| 9585 | if (maxchar_new > maxchar_old) { |
| 9586 | /* If the maxchar increased so that the kind changed, not all |
| 9587 | characters are representable anymore and we need to fix the |
| 9588 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9589 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9590 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9591 | maxchar_old = fixfct(v); |
| 9592 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9593 | } |
| 9594 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9595 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9596 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9597 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9598 | Py_DECREF(u); |
| 9599 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9600 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9601 | } |
| 9602 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9603 | static PyObject * |
| 9604 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9605 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9606 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9607 | char *resdata, *data = PyUnicode_DATA(self); |
| 9608 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9609 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9610 | res = PyUnicode_New(len, 127); |
| 9611 | if (res == NULL) |
| 9612 | return NULL; |
| 9613 | resdata = PyUnicode_DATA(res); |
| 9614 | if (lower) |
| 9615 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9616 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9617 | _Py_bytes_upper(resdata, data, len); |
| 9618 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9619 | } |
| 9620 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9621 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9622 | 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] | 9623 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9624 | Py_ssize_t j; |
| 9625 | int final_sigma; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 9626 | Py_UCS4 c = 0; /* initialize to prevent gcc warning */ |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9627 | /* 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] | 9628 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9629 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9630 | |
| 9631 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9632 | */ |
| 9633 | for (j = i - 1; j >= 0; j--) { |
| 9634 | c = PyUnicode_READ(kind, data, j); |
| 9635 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9636 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9637 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9638 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9639 | if (final_sigma) { |
| 9640 | for (j = i + 1; j < length; j++) { |
| 9641 | c = PyUnicode_READ(kind, data, j); |
| 9642 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9643 | break; |
| 9644 | } |
| 9645 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9646 | } |
| 9647 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9648 | } |
| 9649 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9650 | static int |
| 9651 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9652 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9653 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9654 | /* Obscure special case. */ |
| 9655 | if (c == 0x3A3) { |
| 9656 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9657 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9658 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9659 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9660 | } |
| 9661 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9662 | static Py_ssize_t |
| 9663 | 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] | 9664 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9665 | Py_ssize_t i, k = 0; |
| 9666 | int n_res, j; |
| 9667 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9668 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9669 | c = PyUnicode_READ(kind, data, 0); |
| 9670 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9671 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9672 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9673 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9674 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9675 | for (i = 1; i < length; i++) { |
| 9676 | c = PyUnicode_READ(kind, data, i); |
| 9677 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9678 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9679 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9680 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9681 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9682 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9683 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9684 | } |
| 9685 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9686 | static Py_ssize_t |
| 9687 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9688 | Py_ssize_t i, k = 0; |
| 9689 | |
| 9690 | for (i = 0; i < length; i++) { |
| 9691 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9692 | int n_res, j; |
| 9693 | if (Py_UNICODE_ISUPPER(c)) { |
| 9694 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9695 | } |
| 9696 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9697 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9698 | } |
| 9699 | else { |
| 9700 | n_res = 1; |
| 9701 | mapped[0] = c; |
| 9702 | } |
| 9703 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9704 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9705 | res[k++] = mapped[j]; |
| 9706 | } |
| 9707 | } |
| 9708 | return k; |
| 9709 | } |
| 9710 | |
| 9711 | static Py_ssize_t |
| 9712 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9713 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9714 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9715 | Py_ssize_t i, k = 0; |
| 9716 | |
| 9717 | for (i = 0; i < length; i++) { |
| 9718 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9719 | int n_res, j; |
| 9720 | if (lower) |
| 9721 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9722 | else |
| 9723 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9724 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9725 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9726 | res[k++] = mapped[j]; |
| 9727 | } |
| 9728 | } |
| 9729 | return k; |
| 9730 | } |
| 9731 | |
| 9732 | static Py_ssize_t |
| 9733 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9734 | { |
| 9735 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9736 | } |
| 9737 | |
| 9738 | static Py_ssize_t |
| 9739 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9740 | { |
| 9741 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9742 | } |
| 9743 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9744 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9745 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9746 | { |
| 9747 | Py_ssize_t i, k = 0; |
| 9748 | |
| 9749 | for (i = 0; i < length; i++) { |
| 9750 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9751 | Py_UCS4 mapped[3]; |
| 9752 | int j, n_res = _PyUnicode_ToFoldedFull(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 | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9755 | res[k++] = mapped[j]; |
| 9756 | } |
| 9757 | } |
| 9758 | return k; |
| 9759 | } |
| 9760 | |
| 9761 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9762 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9763 | { |
| 9764 | Py_ssize_t i, k = 0; |
| 9765 | int previous_is_cased; |
| 9766 | |
| 9767 | previous_is_cased = 0; |
| 9768 | for (i = 0; i < length; i++) { |
| 9769 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9770 | Py_UCS4 mapped[3]; |
| 9771 | int n_res, j; |
| 9772 | |
| 9773 | if (previous_is_cased) |
| 9774 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9775 | else |
| 9776 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9777 | |
| 9778 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9779 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9780 | res[k++] = mapped[j]; |
| 9781 | } |
| 9782 | |
| 9783 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9784 | } |
| 9785 | return k; |
| 9786 | } |
| 9787 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9788 | static PyObject * |
| 9789 | case_operation(PyObject *self, |
| 9790 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9791 | { |
| 9792 | PyObject *res = NULL; |
| 9793 | Py_ssize_t length, newlength = 0; |
| 9794 | int kind, outkind; |
| 9795 | void *data, *outdata; |
| 9796 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9797 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9798 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9799 | |
| 9800 | kind = PyUnicode_KIND(self); |
| 9801 | data = PyUnicode_DATA(self); |
| 9802 | length = PyUnicode_GET_LENGTH(self); |
Antoine Pitrou | 4e33424 | 2014-10-15 23:14:53 +0200 | [diff] [blame] | 9803 | if ((size_t) length > PY_SSIZE_T_MAX / (3 * sizeof(Py_UCS4))) { |
Benjamin Peterson | e1bd38c | 2014-10-15 11:47:36 -0400 | [diff] [blame] | 9804 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 9805 | return NULL; |
| 9806 | } |
Benjamin Peterson | 1e211ff | 2014-10-15 12:17:21 -0400 | [diff] [blame] | 9807 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9808 | if (tmp == NULL) |
| 9809 | return PyErr_NoMemory(); |
| 9810 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9811 | res = PyUnicode_New(newlength, maxchar); |
| 9812 | if (res == NULL) |
| 9813 | goto leave; |
| 9814 | tmpend = tmp + newlength; |
| 9815 | outdata = PyUnicode_DATA(res); |
| 9816 | outkind = PyUnicode_KIND(res); |
| 9817 | switch (outkind) { |
| 9818 | case PyUnicode_1BYTE_KIND: |
| 9819 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9820 | break; |
| 9821 | case PyUnicode_2BYTE_KIND: |
| 9822 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9823 | break; |
| 9824 | case PyUnicode_4BYTE_KIND: |
| 9825 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9826 | break; |
| 9827 | default: |
| 9828 | assert(0); |
| 9829 | break; |
| 9830 | } |
| 9831 | leave: |
| 9832 | PyMem_FREE(tmp); |
| 9833 | return res; |
| 9834 | } |
| 9835 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9836 | PyObject * |
| 9837 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9838 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9839 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9840 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9841 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9842 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9843 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9844 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9845 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9846 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9847 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9848 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9849 | int use_memcpy; |
| 9850 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9851 | PyObject *last_obj; |
| 9852 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9853 | |
Benjamin Peterson | 9743b2c | 2014-02-15 13:02:52 -0500 | [diff] [blame] | 9854 | fseq = PySequence_Fast(seq, "can only join an iterable"); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9855 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9856 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9857 | } |
| 9858 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9859 | /* NOTE: the following code can't call back into Python code, |
| 9860 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9861 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9862 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9863 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9864 | /* If empty sequence, return u"". */ |
| 9865 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9866 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9867 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9868 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9869 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9870 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9871 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9872 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9873 | if (seqlen == 1) { |
| 9874 | if (PyUnicode_CheckExact(items[0])) { |
| 9875 | res = items[0]; |
| 9876 | Py_INCREF(res); |
| 9877 | Py_DECREF(fseq); |
| 9878 | return res; |
| 9879 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9880 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9881 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9882 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9883 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9884 | /* Set up sep and seplen */ |
| 9885 | if (separator == NULL) { |
| 9886 | /* fall back to a blank space separator */ |
| 9887 | sep = PyUnicode_FromOrdinal(' '); |
| 9888 | if (!sep) |
| 9889 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9890 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9891 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9892 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9893 | else { |
| 9894 | if (!PyUnicode_Check(separator)) { |
| 9895 | PyErr_Format(PyExc_TypeError, |
| 9896 | "separator: expected str instance," |
| 9897 | " %.80s found", |
| 9898 | Py_TYPE(separator)->tp_name); |
| 9899 | goto onError; |
| 9900 | } |
| 9901 | if (PyUnicode_READY(separator)) |
| 9902 | goto onError; |
| 9903 | sep = separator; |
| 9904 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9905 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9906 | /* inc refcount to keep this code path symmetric with the |
| 9907 | above case of a blank separator */ |
| 9908 | Py_INCREF(sep); |
| 9909 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9910 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9911 | } |
| 9912 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9913 | /* There are at least two things to join, or else we have a subclass |
| 9914 | * of str in the sequence. |
| 9915 | * Do a pre-pass to figure out the total amount of space we'll |
| 9916 | * need (sz), and see whether all argument are strings. |
| 9917 | */ |
| 9918 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9919 | #ifdef Py_DEBUG |
| 9920 | use_memcpy = 0; |
| 9921 | #else |
| 9922 | use_memcpy = 1; |
| 9923 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9924 | for (i = 0; i < seqlen; i++) { |
| 9925 | const Py_ssize_t old_sz = sz; |
| 9926 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9927 | if (!PyUnicode_Check(item)) { |
| 9928 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 9929 | "sequence item %zd: expected str instance," |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9930 | " %.80s found", |
| 9931 | i, Py_TYPE(item)->tp_name); |
| 9932 | goto onError; |
| 9933 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9934 | if (PyUnicode_READY(item) == -1) |
| 9935 | goto onError; |
| 9936 | sz += PyUnicode_GET_LENGTH(item); |
| 9937 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9938 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9939 | if (i != 0) |
| 9940 | sz += seplen; |
| 9941 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9942 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9943 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9944 | goto onError; |
| 9945 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9946 | if (use_memcpy && last_obj != NULL) { |
| 9947 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9948 | use_memcpy = 0; |
| 9949 | } |
| 9950 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9951 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9952 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9953 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9954 | if (res == NULL) |
| 9955 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9956 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9957 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9958 | #ifdef Py_DEBUG |
| 9959 | use_memcpy = 0; |
| 9960 | #else |
| 9961 | if (use_memcpy) { |
| 9962 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9963 | kind = PyUnicode_KIND(res); |
| 9964 | if (seplen != 0) |
| 9965 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9966 | } |
| 9967 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9968 | if (use_memcpy) { |
| 9969 | for (i = 0; i < seqlen; ++i) { |
| 9970 | Py_ssize_t itemlen; |
| 9971 | item = items[i]; |
| 9972 | |
| 9973 | /* Copy item, and maybe the separator. */ |
| 9974 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9975 | Py_MEMCPY(res_data, |
| 9976 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9977 | kind * seplen); |
| 9978 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9979 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9980 | |
| 9981 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9982 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9983 | Py_MEMCPY(res_data, |
| 9984 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9985 | kind * itemlen); |
| 9986 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9987 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9988 | } |
| 9989 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9990 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9991 | } |
| 9992 | else { |
| 9993 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9994 | Py_ssize_t itemlen; |
| 9995 | item = items[i]; |
| 9996 | |
| 9997 | /* Copy item, and maybe the separator. */ |
| 9998 | if (i && seplen != 0) { |
| 9999 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 10000 | res_offset += seplen; |
| 10001 | } |
| 10002 | |
| 10003 | itemlen = PyUnicode_GET_LENGTH(item); |
| 10004 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10005 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10006 | res_offset += itemlen; |
| 10007 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 10008 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 10009 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 10010 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10011 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10012 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10013 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10014 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10015 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10016 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10017 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 10018 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10019 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 10020 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10021 | return NULL; |
| 10022 | } |
| 10023 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10024 | #define FILL(kind, data, value, start, length) \ |
| 10025 | do { \ |
| 10026 | Py_ssize_t i_ = 0; \ |
| 10027 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 10028 | switch ((kind)) { \ |
| 10029 | case PyUnicode_1BYTE_KIND: { \ |
| 10030 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 10031 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10032 | break; \ |
| 10033 | } \ |
| 10034 | case PyUnicode_2BYTE_KIND: { \ |
| 10035 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 10036 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10037 | break; \ |
| 10038 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 10039 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10040 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 10041 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 10042 | break; \ |
| 10043 | } \ |
Serhiy Storchaka | 133b11b | 2014-12-01 18:56:28 +0200 | [diff] [blame] | 10044 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10045 | } \ |
| 10046 | } while (0) |
| 10047 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10048 | void |
| 10049 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10050 | Py_UCS4 fill_char) |
| 10051 | { |
| 10052 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 10053 | const void *data = PyUnicode_DATA(unicode); |
| 10054 | assert(PyUnicode_IS_READY(unicode)); |
| 10055 | assert(unicode_modifiable(unicode)); |
| 10056 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 10057 | assert(start >= 0); |
| 10058 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 10059 | FILL(kind, data, fill_char, start, length); |
| 10060 | } |
| 10061 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10062 | Py_ssize_t |
| 10063 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 10064 | Py_UCS4 fill_char) |
| 10065 | { |
| 10066 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10067 | |
| 10068 | if (!PyUnicode_Check(unicode)) { |
| 10069 | PyErr_BadInternalCall(); |
| 10070 | return -1; |
| 10071 | } |
| 10072 | if (PyUnicode_READY(unicode) == -1) |
| 10073 | return -1; |
| 10074 | if (unicode_check_modifiable(unicode)) |
| 10075 | return -1; |
| 10076 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10077 | if (start < 0) { |
| 10078 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10079 | return -1; |
| 10080 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10081 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 10082 | PyErr_SetString(PyExc_ValueError, |
| 10083 | "fill character is bigger than " |
| 10084 | "the string maximum character"); |
| 10085 | return -1; |
| 10086 | } |
| 10087 | |
| 10088 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 10089 | length = Py_MIN(maxlen, length); |
| 10090 | if (length <= 0) |
| 10091 | return 0; |
| 10092 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10093 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 10094 | return length; |
| 10095 | } |
| 10096 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10097 | static PyObject * |
| 10098 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10099 | Py_ssize_t left, |
| 10100 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10101 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10102 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10103 | PyObject *u; |
| 10104 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10105 | int kind; |
| 10106 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10107 | |
| 10108 | if (left < 0) |
| 10109 | left = 0; |
| 10110 | if (right < 0) |
| 10111 | right = 0; |
| 10112 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10113 | if (left == 0 && right == 0) |
| 10114 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10115 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10116 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 10117 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 10118 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 10119 | return NULL; |
| 10120 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10121 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10122 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10123 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 10124 | if (!u) |
| 10125 | return NULL; |
| 10126 | |
| 10127 | kind = PyUnicode_KIND(u); |
| 10128 | data = PyUnicode_DATA(u); |
| 10129 | if (left) |
| 10130 | FILL(kind, data, fill, 0, left); |
| 10131 | if (right) |
| 10132 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10133 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10134 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10135 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10136 | } |
| 10137 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10138 | PyObject * |
| 10139 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10140 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10141 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10142 | |
| 10143 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10144 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10145 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10146 | if (PyUnicode_READY(string) == -1) { |
| 10147 | Py_DECREF(string); |
| 10148 | return NULL; |
| 10149 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10150 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10151 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10152 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10153 | if (PyUnicode_IS_ASCII(string)) |
| 10154 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10155 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10156 | PyUnicode_GET_LENGTH(string), keepends); |
| 10157 | else |
| 10158 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10159 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10160 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 | break; |
| 10162 | case PyUnicode_2BYTE_KIND: |
| 10163 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10164 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10165 | PyUnicode_GET_LENGTH(string), keepends); |
| 10166 | break; |
| 10167 | case PyUnicode_4BYTE_KIND: |
| 10168 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10169 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10170 | PyUnicode_GET_LENGTH(string), keepends); |
| 10171 | break; |
| 10172 | default: |
| 10173 | assert(0); |
| 10174 | list = 0; |
| 10175 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10176 | Py_DECREF(string); |
| 10177 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10178 | } |
| 10179 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10180 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10181 | split(PyObject *self, |
| 10182 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10183 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10184 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10185 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10186 | void *buf1, *buf2; |
| 10187 | Py_ssize_t len1, len2; |
| 10188 | PyObject* out; |
| 10189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10190 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10191 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10192 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10193 | if (PyUnicode_READY(self) == -1) |
| 10194 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10195 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10196 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10197 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10199 | if (PyUnicode_IS_ASCII(self)) |
| 10200 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10201 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10202 | PyUnicode_GET_LENGTH(self), maxcount |
| 10203 | ); |
| 10204 | else |
| 10205 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10206 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10207 | PyUnicode_GET_LENGTH(self), maxcount |
| 10208 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10209 | case PyUnicode_2BYTE_KIND: |
| 10210 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10211 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10212 | PyUnicode_GET_LENGTH(self), maxcount |
| 10213 | ); |
| 10214 | case PyUnicode_4BYTE_KIND: |
| 10215 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10216 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10217 | PyUnicode_GET_LENGTH(self), maxcount |
| 10218 | ); |
| 10219 | default: |
| 10220 | assert(0); |
| 10221 | return NULL; |
| 10222 | } |
| 10223 | |
| 10224 | if (PyUnicode_READY(substring) == -1) |
| 10225 | return NULL; |
| 10226 | |
| 10227 | kind1 = PyUnicode_KIND(self); |
| 10228 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10229 | len1 = PyUnicode_GET_LENGTH(self); |
| 10230 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10231 | if (kind1 < kind2 || len1 < len2) { |
| 10232 | out = PyList_New(1); |
| 10233 | if (out == NULL) |
| 10234 | return NULL; |
| 10235 | Py_INCREF(self); |
| 10236 | PyList_SET_ITEM(out, 0, self); |
| 10237 | return out; |
| 10238 | } |
| 10239 | buf1 = PyUnicode_DATA(self); |
| 10240 | buf2 = PyUnicode_DATA(substring); |
| 10241 | if (kind2 != kind1) { |
| 10242 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10243 | if (!buf2) |
| 10244 | return NULL; |
| 10245 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10247 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10248 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10249 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10250 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10251 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10252 | else |
| 10253 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10254 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10255 | break; |
| 10256 | case PyUnicode_2BYTE_KIND: |
| 10257 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10258 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10259 | break; |
| 10260 | case PyUnicode_4BYTE_KIND: |
| 10261 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10262 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10263 | break; |
| 10264 | default: |
| 10265 | out = NULL; |
| 10266 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10267 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10268 | PyMem_Free(buf2); |
| 10269 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10270 | } |
| 10271 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10272 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10273 | rsplit(PyObject *self, |
| 10274 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10275 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10276 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10277 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10278 | void *buf1, *buf2; |
| 10279 | Py_ssize_t len1, len2; |
| 10280 | PyObject* out; |
| 10281 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10282 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10283 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10284 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10285 | if (PyUnicode_READY(self) == -1) |
| 10286 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10287 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10288 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10289 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10290 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10291 | if (PyUnicode_IS_ASCII(self)) |
| 10292 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10293 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10294 | PyUnicode_GET_LENGTH(self), maxcount |
| 10295 | ); |
| 10296 | else |
| 10297 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10298 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10299 | PyUnicode_GET_LENGTH(self), maxcount |
| 10300 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | case PyUnicode_2BYTE_KIND: |
| 10302 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10303 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10304 | PyUnicode_GET_LENGTH(self), maxcount |
| 10305 | ); |
| 10306 | case PyUnicode_4BYTE_KIND: |
| 10307 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10308 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10309 | PyUnicode_GET_LENGTH(self), maxcount |
| 10310 | ); |
| 10311 | default: |
| 10312 | assert(0); |
| 10313 | return NULL; |
| 10314 | } |
| 10315 | |
| 10316 | if (PyUnicode_READY(substring) == -1) |
| 10317 | return NULL; |
| 10318 | |
| 10319 | kind1 = PyUnicode_KIND(self); |
| 10320 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10321 | len1 = PyUnicode_GET_LENGTH(self); |
| 10322 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10323 | if (kind1 < kind2 || len1 < len2) { |
| 10324 | out = PyList_New(1); |
| 10325 | if (out == NULL) |
| 10326 | return NULL; |
| 10327 | Py_INCREF(self); |
| 10328 | PyList_SET_ITEM(out, 0, self); |
| 10329 | return out; |
| 10330 | } |
| 10331 | buf1 = PyUnicode_DATA(self); |
| 10332 | buf2 = PyUnicode_DATA(substring); |
| 10333 | if (kind2 != kind1) { |
| 10334 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10335 | if (!buf2) |
| 10336 | return NULL; |
| 10337 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10338 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10339 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10340 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10341 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10342 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10343 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10344 | else |
| 10345 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10346 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10347 | break; |
| 10348 | case PyUnicode_2BYTE_KIND: |
| 10349 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10350 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10351 | break; |
| 10352 | case PyUnicode_4BYTE_KIND: |
| 10353 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10354 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10355 | break; |
| 10356 | default: |
| 10357 | out = NULL; |
| 10358 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10359 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | PyMem_Free(buf2); |
| 10361 | return out; |
| 10362 | } |
| 10363 | |
| 10364 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10365 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10366 | 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] | 10367 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10368 | switch (kind) { |
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(str1) && PyUnicode_IS_ASCII(str2)) |
| 10371 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10372 | else |
| 10373 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10374 | case PyUnicode_2BYTE_KIND: |
| 10375 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10376 | case PyUnicode_4BYTE_KIND: |
| 10377 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10378 | } |
| 10379 | assert(0); |
| 10380 | return -1; |
| 10381 | } |
| 10382 | |
| 10383 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10384 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10385 | 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] | 10386 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10387 | switch (kind) { |
| 10388 | case PyUnicode_1BYTE_KIND: |
| 10389 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10390 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10391 | else |
| 10392 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10393 | case PyUnicode_2BYTE_KIND: |
| 10394 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10395 | case PyUnicode_4BYTE_KIND: |
| 10396 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10397 | } |
| 10398 | assert(0); |
| 10399 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10400 | } |
| 10401 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10402 | static void |
| 10403 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10404 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10405 | { |
| 10406 | int kind = PyUnicode_KIND(u); |
| 10407 | void *data = PyUnicode_DATA(u); |
| 10408 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10409 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10410 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10411 | (Py_UCS1 *)data + len, |
| 10412 | u1, u2, maxcount); |
| 10413 | } |
| 10414 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10415 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10416 | (Py_UCS2 *)data + len, |
| 10417 | u1, u2, maxcount); |
| 10418 | } |
| 10419 | else { |
| 10420 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10421 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10422 | (Py_UCS4 *)data + len, |
| 10423 | u1, u2, maxcount); |
| 10424 | } |
| 10425 | } |
| 10426 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10427 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10428 | replace(PyObject *self, PyObject *str1, |
| 10429 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10430 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | PyObject *u; |
| 10432 | char *sbuf = PyUnicode_DATA(self); |
| 10433 | char *buf1 = PyUnicode_DATA(str1); |
| 10434 | char *buf2 = PyUnicode_DATA(str2); |
| 10435 | int srelease = 0, release1 = 0, release2 = 0; |
| 10436 | int skind = PyUnicode_KIND(self); |
| 10437 | int kind1 = PyUnicode_KIND(str1); |
| 10438 | int kind2 = PyUnicode_KIND(str2); |
| 10439 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10440 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10441 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10442 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10443 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10444 | |
| 10445 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10446 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10447 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10448 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10449 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10450 | if (str1 == str2) |
| 10451 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10452 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10453 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10454 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10455 | if (maxchar < maxchar_str1) |
| 10456 | /* substring too wide to be present */ |
| 10457 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10458 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10459 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10460 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10461 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10462 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10464 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10465 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10466 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10467 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10468 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10469 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10470 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10471 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10472 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10473 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10474 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10475 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10476 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10477 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10478 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10479 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10480 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10481 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10482 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10483 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10484 | } |
| 10485 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10486 | int rkind = skind; |
| 10487 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10488 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10490 | if (kind1 < rkind) { |
| 10491 | /* widen substring */ |
| 10492 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10493 | if (!buf1) goto error; |
| 10494 | release1 = 1; |
| 10495 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10496 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10497 | if (i < 0) |
| 10498 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10499 | if (rkind > kind2) { |
| 10500 | /* widen replacement */ |
| 10501 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10502 | if (!buf2) goto error; |
| 10503 | release2 = 1; |
| 10504 | } |
| 10505 | else if (rkind < kind2) { |
| 10506 | /* widen self and buf1 */ |
| 10507 | rkind = kind2; |
| 10508 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10509 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10510 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10511 | if (!sbuf) goto error; |
| 10512 | srelease = 1; |
| 10513 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10514 | if (!buf1) goto error; |
| 10515 | release1 = 1; |
| 10516 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10517 | u = PyUnicode_New(slen, maxchar); |
| 10518 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10519 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10520 | assert(PyUnicode_KIND(u) == rkind); |
| 10521 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10522 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10523 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10524 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10525 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10526 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10527 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10528 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10529 | |
| 10530 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10531 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10532 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10533 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10534 | if (i == -1) |
| 10535 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10536 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10537 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10538 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10539 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10540 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10541 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10542 | } |
| 10543 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10544 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10545 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10546 | int rkind = skind; |
| 10547 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10548 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10549 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10550 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10551 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10552 | if (!buf1) goto error; |
| 10553 | release1 = 1; |
| 10554 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10555 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10556 | if (n == 0) |
| 10557 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10558 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10559 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10560 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10561 | if (!buf2) goto error; |
| 10562 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10563 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10564 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10565 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10566 | rkind = kind2; |
| 10567 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10568 | if (!sbuf) goto error; |
| 10569 | srelease = 1; |
| 10570 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10571 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10572 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10573 | if (!buf1) goto error; |
| 10574 | release1 = 1; |
| 10575 | } |
| 10576 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10577 | PyUnicode_GET_LENGTH(str1))); */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10578 | 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] | 10579 | PyErr_SetString(PyExc_OverflowError, |
| 10580 | "replace string is too long"); |
| 10581 | goto error; |
| 10582 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10583 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10584 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10585 | _Py_INCREF_UNICODE_EMPTY(); |
| 10586 | if (!unicode_empty) |
| 10587 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10588 | u = unicode_empty; |
| 10589 | goto done; |
| 10590 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10591 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10592 | PyErr_SetString(PyExc_OverflowError, |
| 10593 | "replace string is too long"); |
| 10594 | goto error; |
| 10595 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10596 | u = PyUnicode_New(new_size, maxchar); |
| 10597 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10598 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10599 | assert(PyUnicode_KIND(u) == rkind); |
| 10600 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10601 | ires = i = 0; |
| 10602 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10603 | while (n-- > 0) { |
| 10604 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10605 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10606 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10607 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10608 | if (j == -1) |
| 10609 | break; |
| 10610 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10611 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10612 | memcpy(res + rkind * ires, |
| 10613 | sbuf + rkind * i, |
| 10614 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10615 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10616 | } |
| 10617 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10619 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10620 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10621 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10622 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10623 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10624 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10625 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10626 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10627 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10628 | memcpy(res + rkind * ires, |
| 10629 | sbuf + rkind * i, |
| 10630 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10631 | } |
| 10632 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10633 | /* interleave */ |
| 10634 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10635 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10636 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10637 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10638 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10639 | if (--n <= 0) |
| 10640 | break; |
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); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10644 | ires++; |
| 10645 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10646 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10647 | memcpy(res + rkind * ires, |
| 10648 | sbuf + rkind * i, |
| 10649 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10650 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10651 | } |
| 10652 | |
| 10653 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10654 | unicode_adjust_maxchar(&u); |
| 10655 | if (u == NULL) |
| 10656 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10657 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10658 | |
| 10659 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10660 | if (srelease) |
| 10661 | PyMem_FREE(sbuf); |
| 10662 | if (release1) |
| 10663 | PyMem_FREE(buf1); |
| 10664 | if (release2) |
| 10665 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10666 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10667 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10668 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10669 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10670 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10671 | if (srelease) |
| 10672 | PyMem_FREE(sbuf); |
| 10673 | if (release1) |
| 10674 | PyMem_FREE(buf1); |
| 10675 | if (release2) |
| 10676 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10677 | return unicode_result_unchanged(self); |
| 10678 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10679 | error: |
| 10680 | if (srelease && sbuf) |
| 10681 | PyMem_FREE(sbuf); |
| 10682 | if (release1 && buf1) |
| 10683 | PyMem_FREE(buf1); |
| 10684 | if (release2 && buf2) |
| 10685 | PyMem_FREE(buf2); |
| 10686 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10687 | } |
| 10688 | |
| 10689 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10690 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10691 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10692 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10693 | \n\ |
| 10694 | 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] | 10695 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | |
| 10697 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10698 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10699 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10700 | if (PyUnicode_READY(self) == -1) |
| 10701 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10702 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10703 | } |
| 10704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10705 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10706 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10707 | \n\ |
| 10708 | 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] | 10709 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10710 | |
| 10711 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10712 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10713 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10714 | if (PyUnicode_READY(self) == -1) |
| 10715 | return NULL; |
| 10716 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10717 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10718 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | } |
| 10720 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10721 | PyDoc_STRVAR(casefold__doc__, |
| 10722 | "S.casefold() -> str\n\ |
| 10723 | \n\ |
| 10724 | Return a version of S suitable for caseless comparisons."); |
| 10725 | |
| 10726 | static PyObject * |
| 10727 | unicode_casefold(PyObject *self) |
| 10728 | { |
| 10729 | if (PyUnicode_READY(self) == -1) |
| 10730 | return NULL; |
| 10731 | if (PyUnicode_IS_ASCII(self)) |
| 10732 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10733 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10734 | } |
| 10735 | |
| 10736 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10737 | /* Argument converter. Coerces to a single unicode character */ |
| 10738 | |
| 10739 | static int |
| 10740 | convert_uc(PyObject *obj, void *addr) |
| 10741 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10742 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10743 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10744 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10745 | uniobj = PyUnicode_FromObject(obj); |
| 10746 | if (uniobj == NULL) { |
| 10747 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10748 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10749 | return 0; |
| 10750 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10751 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10752 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10753 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10754 | Py_DECREF(uniobj); |
| 10755 | return 0; |
| 10756 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10757 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10758 | Py_DECREF(uniobj); |
| 10759 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10760 | } |
| 10761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10762 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10763 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10764 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10765 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10766 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | |
| 10768 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10769 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10770 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10771 | Py_ssize_t marg, left; |
| 10772 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10773 | Py_UCS4 fillchar = ' '; |
| 10774 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10775 | 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] | 10776 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10777 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10778 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10779 | return NULL; |
| 10780 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10781 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10782 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10783 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10784 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10785 | left = marg / 2 + (marg & width & 1); |
| 10786 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10787 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10788 | } |
| 10789 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10790 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10791 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10792 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10793 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10794 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10795 | #define COMPARE(TYPE1, TYPE2) \ |
| 10796 | do { \ |
| 10797 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10798 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10799 | TYPE1* end = p1 + len; \ |
| 10800 | Py_UCS4 c1, c2; \ |
| 10801 | for (; p1 != end; p1++, p2++) { \ |
| 10802 | c1 = *p1; \ |
| 10803 | c2 = *p2; \ |
| 10804 | if (c1 != c2) \ |
| 10805 | return (c1 < c2) ? -1 : 1; \ |
| 10806 | } \ |
| 10807 | } \ |
| 10808 | while (0) |
| 10809 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10810 | int kind1, kind2; |
| 10811 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10812 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10813 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10814 | kind1 = PyUnicode_KIND(str1); |
| 10815 | kind2 = PyUnicode_KIND(str2); |
| 10816 | data1 = PyUnicode_DATA(str1); |
| 10817 | data2 = PyUnicode_DATA(str2); |
| 10818 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10819 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10820 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10821 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10822 | switch(kind1) { |
| 10823 | case PyUnicode_1BYTE_KIND: |
| 10824 | { |
| 10825 | switch(kind2) { |
| 10826 | case PyUnicode_1BYTE_KIND: |
| 10827 | { |
| 10828 | int cmp = memcmp(data1, data2, len); |
| 10829 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10830 | if (cmp < 0) |
| 10831 | return -1; |
| 10832 | if (cmp > 0) |
| 10833 | return 1; |
| 10834 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10835 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10836 | case PyUnicode_2BYTE_KIND: |
| 10837 | COMPARE(Py_UCS1, Py_UCS2); |
| 10838 | break; |
| 10839 | case PyUnicode_4BYTE_KIND: |
| 10840 | COMPARE(Py_UCS1, Py_UCS4); |
| 10841 | break; |
| 10842 | default: |
| 10843 | assert(0); |
| 10844 | } |
| 10845 | break; |
| 10846 | } |
| 10847 | case PyUnicode_2BYTE_KIND: |
| 10848 | { |
| 10849 | switch(kind2) { |
| 10850 | case PyUnicode_1BYTE_KIND: |
| 10851 | COMPARE(Py_UCS2, Py_UCS1); |
| 10852 | break; |
| 10853 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10854 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10855 | COMPARE(Py_UCS2, Py_UCS2); |
| 10856 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10857 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10858 | case PyUnicode_4BYTE_KIND: |
| 10859 | COMPARE(Py_UCS2, Py_UCS4); |
| 10860 | break; |
| 10861 | default: |
| 10862 | assert(0); |
| 10863 | } |
| 10864 | break; |
| 10865 | } |
| 10866 | case PyUnicode_4BYTE_KIND: |
| 10867 | { |
| 10868 | switch(kind2) { |
| 10869 | case PyUnicode_1BYTE_KIND: |
| 10870 | COMPARE(Py_UCS4, Py_UCS1); |
| 10871 | break; |
| 10872 | case PyUnicode_2BYTE_KIND: |
| 10873 | COMPARE(Py_UCS4, Py_UCS2); |
| 10874 | break; |
| 10875 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10876 | { |
| 10877 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10878 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10879 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10880 | if (cmp < 0) |
| 10881 | return -1; |
| 10882 | if (cmp > 0) |
| 10883 | return 1; |
| 10884 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10885 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10886 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10887 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10888 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10889 | default: |
| 10890 | assert(0); |
| 10891 | } |
| 10892 | break; |
| 10893 | } |
| 10894 | default: |
| 10895 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10896 | } |
| 10897 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10898 | if (len1 == len2) |
| 10899 | return 0; |
| 10900 | if (len1 < len2) |
| 10901 | return -1; |
| 10902 | else |
| 10903 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10904 | |
| 10905 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10906 | } |
| 10907 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10908 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10909 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10910 | { |
| 10911 | int kind; |
| 10912 | void *data1, *data2; |
| 10913 | Py_ssize_t len; |
| 10914 | int cmp; |
| 10915 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10916 | len = PyUnicode_GET_LENGTH(str1); |
| 10917 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10918 | return 0; |
| 10919 | kind = PyUnicode_KIND(str1); |
| 10920 | if (PyUnicode_KIND(str2) != kind) |
| 10921 | return 0; |
| 10922 | data1 = PyUnicode_DATA(str1); |
| 10923 | data2 = PyUnicode_DATA(str2); |
| 10924 | |
| 10925 | cmp = memcmp(data1, data2, len * kind); |
| 10926 | return (cmp == 0); |
| 10927 | } |
| 10928 | |
| 10929 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10930 | int |
| 10931 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10932 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10933 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10934 | if (PyUnicode_READY(left) == -1 || |
| 10935 | PyUnicode_READY(right) == -1) |
| 10936 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10937 | |
| 10938 | /* a string is equal to itself */ |
| 10939 | if (left == right) |
| 10940 | return 0; |
| 10941 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10942 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10943 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10944 | PyErr_Format(PyExc_TypeError, |
| 10945 | "Can't compare %.100s and %.100s", |
| 10946 | left->ob_type->tp_name, |
| 10947 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10948 | return -1; |
| 10949 | } |
| 10950 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10951 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10952 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10953 | { |
| 10954 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10955 | if (right_str == NULL) |
| 10956 | return -1; |
| 10957 | return PyUnicode_Compare(left, right_str); |
| 10958 | } |
| 10959 | |
| 10960 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10961 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10962 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10963 | Py_ssize_t i; |
| 10964 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10965 | Py_UCS4 chr; |
| 10966 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10967 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10968 | if (PyUnicode_READY(uni) == -1) |
| 10969 | return -1; |
| 10970 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10971 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10972 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10973 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10974 | size_t len, len2 = strlen(str); |
| 10975 | int cmp; |
| 10976 | |
| 10977 | len = Py_MIN(len1, len2); |
| 10978 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 10979 | if (cmp != 0) { |
| 10980 | if (cmp < 0) |
| 10981 | return -1; |
| 10982 | else |
| 10983 | return 1; |
| 10984 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10985 | if (len1 > len2) |
| 10986 | return 1; /* uni is longer */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10987 | if (len1 < len2) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10988 | return -1; /* str is longer */ |
| 10989 | return 0; |
| 10990 | } |
| 10991 | else { |
| 10992 | void *data = PyUnicode_DATA(uni); |
| 10993 | /* Compare Unicode string and source character set string */ |
| 10994 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 10995 | if (chr != (unsigned char)str[i]) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10996 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10997 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10998 | to C strings identical up to that point. */ |
| 10999 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 11000 | return 1; /* uni is longer */ |
| 11001 | if (str[i]) |
| 11002 | return -1; /* str is longer */ |
| 11003 | return 0; |
| 11004 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 11005 | } |
| 11006 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11007 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11008 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11009 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11010 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11011 | PyObject * |
| 11012 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11013 | { |
| 11014 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11015 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11016 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11017 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 11018 | Py_RETURN_NOTIMPLEMENTED; |
| 11019 | |
| 11020 | if (PyUnicode_READY(left) == -1 || |
| 11021 | PyUnicode_READY(right) == -1) |
| 11022 | return NULL; |
| 11023 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 11024 | if (left == right) { |
| 11025 | switch (op) { |
| 11026 | case Py_EQ: |
| 11027 | case Py_LE: |
| 11028 | case Py_GE: |
| 11029 | /* a string is equal to itself */ |
| 11030 | v = Py_True; |
| 11031 | break; |
| 11032 | case Py_NE: |
| 11033 | case Py_LT: |
| 11034 | case Py_GT: |
| 11035 | v = Py_False; |
| 11036 | break; |
| 11037 | default: |
| 11038 | PyErr_BadArgument(); |
| 11039 | return NULL; |
| 11040 | } |
| 11041 | } |
| 11042 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11043 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 11044 | result ^= (op == Py_NE); |
| 11045 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11046 | } |
| 11047 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 11048 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11049 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11050 | /* Convert the return value to a Boolean */ |
| 11051 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 11052 | case Py_LE: |
| 11053 | v = TEST_COND(result <= 0); |
| 11054 | break; |
| 11055 | case Py_GE: |
| 11056 | v = TEST_COND(result >= 0); |
| 11057 | break; |
| 11058 | case Py_LT: |
| 11059 | v = TEST_COND(result == -1); |
| 11060 | break; |
| 11061 | case Py_GT: |
| 11062 | v = TEST_COND(result == 1); |
| 11063 | break; |
| 11064 | default: |
| 11065 | PyErr_BadArgument(); |
| 11066 | return NULL; |
| 11067 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11068 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 11069 | Py_INCREF(v); |
| 11070 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 11071 | } |
| 11072 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11073 | int |
Raymond Hettinger | ac2ef65 | 2015-07-04 16:04:44 -0700 | [diff] [blame] | 11074 | _PyUnicode_EQ(PyObject *aa, PyObject *bb) |
| 11075 | { |
| 11076 | return unicode_eq(aa, bb); |
| 11077 | } |
| 11078 | |
| 11079 | int |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11080 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11081 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11082 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11083 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11084 | void *buf1, *buf2; |
| 11085 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11086 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11087 | |
| 11088 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11089 | sub = PyUnicode_FromObject(element); |
| 11090 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11091 | PyErr_Format(PyExc_TypeError, |
| 11092 | "'in <string>' requires string as left operand, not %s", |
| 11093 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11094 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11095 | } |
| 11096 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11097 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11098 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11099 | Py_DECREF(sub); |
| 11100 | return -1; |
| 11101 | } |
| 11102 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11103 | kind1 = PyUnicode_KIND(str); |
| 11104 | kind2 = PyUnicode_KIND(sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11105 | if (kind1 < kind2) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11106 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 11107 | Py_DECREF(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11108 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11109 | } |
| 11110 | len1 = PyUnicode_GET_LENGTH(str); |
| 11111 | len2 = PyUnicode_GET_LENGTH(sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11112 | if (len1 < len2) { |
| 11113 | Py_DECREF(sub); |
| 11114 | Py_DECREF(str); |
| 11115 | return 0; |
| 11116 | } |
| 11117 | buf1 = PyUnicode_DATA(str); |
| 11118 | buf2 = PyUnicode_DATA(sub); |
| 11119 | if (len2 == 1) { |
| 11120 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 11121 | result = findchar((const char *)buf1, kind1, len1, ch, 1) != -1; |
| 11122 | Py_DECREF(sub); |
| 11123 | Py_DECREF(str); |
| 11124 | return result; |
| 11125 | } |
| 11126 | if (kind2 != kind1) { |
| 11127 | buf2 = _PyUnicode_AsKind(sub, kind1); |
| 11128 | if (!buf2) { |
| 11129 | Py_DECREF(sub); |
| 11130 | Py_DECREF(str); |
| 11131 | return -1; |
| 11132 | } |
| 11133 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11134 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11135 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11136 | case PyUnicode_1BYTE_KIND: |
| 11137 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11138 | break; |
| 11139 | case PyUnicode_2BYTE_KIND: |
| 11140 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11141 | break; |
| 11142 | case PyUnicode_4BYTE_KIND: |
| 11143 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11144 | break; |
| 11145 | default: |
| 11146 | result = -1; |
| 11147 | assert(0); |
| 11148 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11149 | |
| 11150 | Py_DECREF(str); |
| 11151 | Py_DECREF(sub); |
| 11152 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11153 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11154 | PyMem_Free(buf2); |
| 11155 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11156 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11157 | } |
| 11158 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11159 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 11160 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11161 | PyObject * |
| 11162 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11163 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11164 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11165 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11166 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11167 | |
| 11168 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11169 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11170 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11171 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11172 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11174 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | |
| 11176 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11177 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11178 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11179 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11180 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11181 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11182 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11183 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11184 | } |
| 11185 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11186 | u_len = PyUnicode_GET_LENGTH(u); |
| 11187 | v_len = PyUnicode_GET_LENGTH(v); |
| 11188 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11189 | PyErr_SetString(PyExc_OverflowError, |
| 11190 | "strings are too large to concat"); |
| 11191 | goto onError; |
| 11192 | } |
| 11193 | new_len = u_len + v_len; |
| 11194 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11195 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11196 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11197 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11200 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11201 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11202 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11203 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 11204 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11205 | Py_DECREF(u); |
| 11206 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11207 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11208 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11209 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11210 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11211 | Py_XDECREF(u); |
| 11212 | Py_XDECREF(v); |
| 11213 | return NULL; |
| 11214 | } |
| 11215 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11216 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11217 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11218 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11219 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11220 | Py_UCS4 maxchar, maxchar2; |
| 11221 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11222 | |
| 11223 | if (p_left == NULL) { |
| 11224 | if (!PyErr_Occurred()) |
| 11225 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11226 | return; |
| 11227 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11228 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11229 | if (right == NULL || left == NULL |
| 11230 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11231 | if (!PyErr_Occurred()) |
| 11232 | PyErr_BadInternalCall(); |
| 11233 | goto error; |
| 11234 | } |
| 11235 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11236 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11237 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11238 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11239 | goto error; |
| 11240 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11241 | /* Shortcuts */ |
| 11242 | if (left == unicode_empty) { |
| 11243 | Py_DECREF(left); |
| 11244 | Py_INCREF(right); |
| 11245 | *p_left = right; |
| 11246 | return; |
| 11247 | } |
| 11248 | if (right == unicode_empty) |
| 11249 | return; |
| 11250 | |
| 11251 | left_len = PyUnicode_GET_LENGTH(left); |
| 11252 | right_len = PyUnicode_GET_LENGTH(right); |
| 11253 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11254 | PyErr_SetString(PyExc_OverflowError, |
| 11255 | "strings are too large to concat"); |
| 11256 | goto error; |
| 11257 | } |
| 11258 | new_len = left_len + right_len; |
| 11259 | |
| 11260 | if (unicode_modifiable(left) |
| 11261 | && PyUnicode_CheckExact(right) |
| 11262 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11263 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11264 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11265 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11266 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11267 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11268 | { |
| 11269 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11270 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11271 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11272 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11273 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11274 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11275 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11276 | else { |
| 11277 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11278 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11279 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11280 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11281 | /* Concat the two Unicode strings */ |
| 11282 | res = PyUnicode_New(new_len, maxchar); |
| 11283 | if (res == NULL) |
| 11284 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11285 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11286 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11287 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11288 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11289 | } |
| 11290 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11291 | return; |
| 11292 | |
| 11293 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11294 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11295 | } |
| 11296 | |
| 11297 | void |
| 11298 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11299 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11300 | PyUnicode_Append(pleft, right); |
| 11301 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11302 | } |
| 11303 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11304 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11305 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11307 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11308 | 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] | 11309 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11310 | |
| 11311 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11312 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11313 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11314 | PyObject *substring = NULL; /* initialize to fix a compiler warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11315 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11316 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11317 | PyObject *result; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11318 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11319 | void *buf1, *buf2; |
| 11320 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11322 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11323 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11324 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11325 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11326 | kind1 = PyUnicode_KIND(self); |
| 11327 | kind2 = PyUnicode_KIND(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11328 | if (kind1 < kind2) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11329 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11330 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11331 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11332 | len1 = PyUnicode_GET_LENGTH(self); |
| 11333 | len2 = PyUnicode_GET_LENGTH(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11334 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11335 | if (end - start < len2) { |
| 11336 | Py_DECREF(substring); |
| 11337 | return PyLong_FromLong(0); |
| 11338 | } |
| 11339 | buf1 = PyUnicode_DATA(self); |
| 11340 | buf2 = PyUnicode_DATA(substring); |
| 11341 | if (kind2 != kind1) { |
| 11342 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 11343 | if (!buf2) { |
| 11344 | Py_DECREF(substring); |
| 11345 | return NULL; |
| 11346 | } |
| 11347 | } |
| 11348 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11349 | case PyUnicode_1BYTE_KIND: |
| 11350 | iresult = ucs1lib_count( |
| 11351 | ((Py_UCS1*)buf1) + start, end - start, |
| 11352 | buf2, len2, PY_SSIZE_T_MAX |
| 11353 | ); |
| 11354 | break; |
| 11355 | case PyUnicode_2BYTE_KIND: |
| 11356 | iresult = ucs2lib_count( |
| 11357 | ((Py_UCS2*)buf1) + start, end - start, |
| 11358 | buf2, len2, PY_SSIZE_T_MAX |
| 11359 | ); |
| 11360 | break; |
| 11361 | case PyUnicode_4BYTE_KIND: |
| 11362 | iresult = ucs4lib_count( |
| 11363 | ((Py_UCS4*)buf1) + start, end - start, |
| 11364 | buf2, len2, PY_SSIZE_T_MAX |
| 11365 | ); |
| 11366 | break; |
| 11367 | default: |
| 11368 | assert(0); iresult = 0; |
| 11369 | } |
| 11370 | |
| 11371 | result = PyLong_FromSsize_t(iresult); |
| 11372 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11373 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | |
| 11376 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11378 | return result; |
| 11379 | } |
| 11380 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11381 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11382 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11383 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11384 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11385 | 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] | 11386 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11387 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11388 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11389 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11390 | |
| 11391 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11392 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11393 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11394 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11395 | char *encoding = NULL; |
| 11396 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11397 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11398 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11399 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11400 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11401 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11402 | } |
| 11403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11404 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11405 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | \n\ |
| 11407 | 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] | 11408 | 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] | 11409 | |
| 11410 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11411 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11412 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11413 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11414 | Py_UCS4 ch; |
| 11415 | PyObject *u; |
| 11416 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11417 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11419 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11420 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11421 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11422 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11423 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11424 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11425 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11426 | if (PyUnicode_READY(self) == -1) |
| 11427 | return NULL; |
| 11428 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11429 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11430 | src_len = PyUnicode_GET_LENGTH(self); |
| 11431 | i = j = line_pos = 0; |
| 11432 | kind = PyUnicode_KIND(self); |
| 11433 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11434 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11435 | for (; i < src_len; i++) { |
| 11436 | ch = PyUnicode_READ(kind, src_data, i); |
| 11437 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11438 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11439 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11440 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11441 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11442 | goto overflow; |
| 11443 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11444 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11445 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11446 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11447 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11448 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11449 | goto overflow; |
| 11450 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11451 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11452 | if (ch == '\n' || ch == '\r') |
| 11453 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11454 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11455 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11456 | if (!found) |
| 11457 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11460 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11461 | if (!u) |
| 11462 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11463 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11464 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11465 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11466 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11467 | for (; i < src_len; i++) { |
| 11468 | ch = PyUnicode_READ(kind, src_data, i); |
| 11469 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11471 | incr = tabsize - (line_pos % tabsize); |
| 11472 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11473 | FILL(kind, dest_data, ' ', j, incr); |
| 11474 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11475 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11476 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11477 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11478 | line_pos++; |
| 11479 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +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 | } |
| 11485 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11486 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11487 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11488 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11489 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11490 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11491 | } |
| 11492 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11493 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11494 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | \n\ |
| 11496 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11497 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | arguments start and end are interpreted as in slice notation.\n\ |
| 11499 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11500 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | |
| 11502 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11503 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11505 | /* initialize variables to prevent gcc warning */ |
| 11506 | PyObject *substring = NULL; |
| 11507 | Py_ssize_t start = 0; |
| 11508 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11509 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11510 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11511 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11512 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11513 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11514 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11515 | if (PyUnicode_READY(self) == -1) { |
| 11516 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11517 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11518 | } |
| 11519 | if (PyUnicode_READY(substring) == -1) { |
| 11520 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11521 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11522 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11523 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11524 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11525 | |
| 11526 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11527 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11528 | if (result == -2) |
| 11529 | return NULL; |
| 11530 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11531 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11532 | } |
| 11533 | |
| 11534 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11535 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11536 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11537 | void *data; |
| 11538 | enum PyUnicode_Kind kind; |
| 11539 | Py_UCS4 ch; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11540 | |
| 11541 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11542 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11543 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11544 | } |
| 11545 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11546 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11547 | return NULL; |
| 11548 | } |
| 11549 | kind = PyUnicode_KIND(self); |
| 11550 | data = PyUnicode_DATA(self); |
| 11551 | ch = PyUnicode_READ(kind, data, index); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 11552 | return unicode_char(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 | } |
| 11554 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11555 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11556 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11557 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11558 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11560 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11561 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11562 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11563 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11564 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11565 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11566 | if (_PyUnicode_HASH(self) != -1) |
| 11567 | return _PyUnicode_HASH(self); |
| 11568 | if (PyUnicode_READY(self) == -1) |
| 11569 | return -1; |
| 11570 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11571 | /* |
| 11572 | We make the hash of the empty string be 0, rather than using |
| 11573 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11574 | */ |
| 11575 | if (len == 0) { |
| 11576 | _PyUnicode_HASH(self) = 0; |
| 11577 | return 0; |
| 11578 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11579 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11580 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11581 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11582 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11583 | } |
| 11584 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11585 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11586 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11587 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11588 | 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] | 11589 | |
| 11590 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11591 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11592 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11593 | /* initialize variables to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11594 | Py_ssize_t result; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11595 | PyObject *substring = NULL; |
| 11596 | Py_ssize_t start = 0; |
| 11597 | Py_ssize_t end = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11598 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11599 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11600 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11601 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11602 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11603 | if (PyUnicode_READY(self) == -1) { |
| 11604 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11605 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11606 | } |
| 11607 | if (PyUnicode_READY(substring) == -1) { |
| 11608 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11609 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11610 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11611 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11612 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11613 | |
| 11614 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11616 | if (result == -2) |
| 11617 | return NULL; |
| 11618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11619 | if (result < 0) { |
| 11620 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11621 | return NULL; |
| 11622 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11623 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11624 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11625 | } |
| 11626 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11627 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11628 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11629 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11630 | 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] | 11631 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11632 | |
| 11633 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11634 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11635 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11636 | Py_ssize_t i, length; |
| 11637 | int kind; |
| 11638 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11639 | int cased; |
| 11640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11641 | if (PyUnicode_READY(self) == -1) |
| 11642 | return NULL; |
| 11643 | length = PyUnicode_GET_LENGTH(self); |
| 11644 | kind = PyUnicode_KIND(self); |
| 11645 | data = PyUnicode_DATA(self); |
| 11646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11647 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11648 | if (length == 1) |
| 11649 | return PyBool_FromLong( |
| 11650 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11651 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11652 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11653 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11654 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11655 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11656 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11657 | for (i = 0; i < length; i++) { |
| 11658 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11660 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11661 | return PyBool_FromLong(0); |
| 11662 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11663 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11664 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11665 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11666 | } |
| 11667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11668 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11669 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11670 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11671 | 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] | 11672 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11673 | |
| 11674 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11675 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11676 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11677 | Py_ssize_t i, length; |
| 11678 | int kind; |
| 11679 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11680 | int cased; |
| 11681 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11682 | if (PyUnicode_READY(self) == -1) |
| 11683 | return NULL; |
| 11684 | length = PyUnicode_GET_LENGTH(self); |
| 11685 | kind = PyUnicode_KIND(self); |
| 11686 | data = PyUnicode_DATA(self); |
| 11687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11688 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11689 | if (length == 1) |
| 11690 | return PyBool_FromLong( |
| 11691 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11692 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11693 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11694 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11695 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11696 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11697 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11698 | for (i = 0; i < length; i++) { |
| 11699 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11700 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11701 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11702 | return PyBool_FromLong(0); |
| 11703 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11704 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11705 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11706 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11707 | } |
| 11708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11709 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11710 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11711 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11712 | Return True if S is a titlecased string and there is at least one\n\ |
| 11713 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11714 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11715 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11716 | |
| 11717 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11718 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11719 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11720 | Py_ssize_t i, length; |
| 11721 | int kind; |
| 11722 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11723 | int cased, previous_is_cased; |
| 11724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11725 | if (PyUnicode_READY(self) == -1) |
| 11726 | return NULL; |
| 11727 | length = PyUnicode_GET_LENGTH(self); |
| 11728 | kind = PyUnicode_KIND(self); |
| 11729 | data = PyUnicode_DATA(self); |
| 11730 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11731 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11732 | if (length == 1) { |
| 11733 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11734 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11735 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11736 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11737 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11738 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11739 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11740 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11741 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11742 | cased = 0; |
| 11743 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11744 | for (i = 0; i < length; i++) { |
| 11745 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11746 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11747 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11748 | if (previous_is_cased) |
| 11749 | return PyBool_FromLong(0); |
| 11750 | previous_is_cased = 1; |
| 11751 | cased = 1; |
| 11752 | } |
| 11753 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11754 | if (!previous_is_cased) |
| 11755 | return PyBool_FromLong(0); |
| 11756 | previous_is_cased = 1; |
| 11757 | cased = 1; |
| 11758 | } |
| 11759 | else |
| 11760 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11761 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11762 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11763 | } |
| 11764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11765 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11766 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11767 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11768 | Return True if all characters in S are whitespace\n\ |
| 11769 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11770 | |
| 11771 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11772 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11773 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11774 | Py_ssize_t i, length; |
| 11775 | int kind; |
| 11776 | void *data; |
| 11777 | |
| 11778 | if (PyUnicode_READY(self) == -1) |
| 11779 | return NULL; |
| 11780 | length = PyUnicode_GET_LENGTH(self); |
| 11781 | kind = PyUnicode_KIND(self); |
| 11782 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11784 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11785 | if (length == 1) |
| 11786 | return PyBool_FromLong( |
| 11787 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11788 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11789 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11790 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11791 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11793 | for (i = 0; i < length; i++) { |
| 11794 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11795 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11796 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11797 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11798 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11799 | } |
| 11800 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11801 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11802 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11803 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11804 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11805 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11806 | |
| 11807 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11808 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11809 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11810 | Py_ssize_t i, length; |
| 11811 | int kind; |
| 11812 | void *data; |
| 11813 | |
| 11814 | if (PyUnicode_READY(self) == -1) |
| 11815 | return NULL; |
| 11816 | length = PyUnicode_GET_LENGTH(self); |
| 11817 | kind = PyUnicode_KIND(self); |
| 11818 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11819 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11820 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11821 | if (length == 1) |
| 11822 | return PyBool_FromLong( |
| 11823 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11824 | |
| 11825 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11826 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11827 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11828 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11829 | for (i = 0; i < length; i++) { |
| 11830 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11831 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11832 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11833 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11834 | } |
| 11835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11836 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11837 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11838 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11839 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11840 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11841 | |
| 11842 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11843 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11844 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11845 | int kind; |
| 11846 | void *data; |
| 11847 | Py_ssize_t len, i; |
| 11848 | |
| 11849 | if (PyUnicode_READY(self) == -1) |
| 11850 | return NULL; |
| 11851 | |
| 11852 | kind = PyUnicode_KIND(self); |
| 11853 | data = PyUnicode_DATA(self); |
| 11854 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11855 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11856 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11857 | if (len == 1) { |
| 11858 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11859 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11860 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11861 | |
| 11862 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11863 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11864 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11865 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11866 | for (i = 0; i < len; i++) { |
| 11867 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11868 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11869 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11870 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11871 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11872 | } |
| 11873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11874 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11875 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11876 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11877 | 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] | 11878 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11879 | |
| 11880 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11881 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11882 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11883 | Py_ssize_t i, length; |
| 11884 | int kind; |
| 11885 | void *data; |
| 11886 | |
| 11887 | if (PyUnicode_READY(self) == -1) |
| 11888 | return NULL; |
| 11889 | length = PyUnicode_GET_LENGTH(self); |
| 11890 | kind = PyUnicode_KIND(self); |
| 11891 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11893 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11894 | if (length == 1) |
| 11895 | return PyBool_FromLong( |
| 11896 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11898 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11899 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11900 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11901 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11902 | for (i = 0; i < length; i++) { |
| 11903 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11904 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11905 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11906 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11907 | } |
| 11908 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11909 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11910 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11911 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11912 | Return True if all characters in S are digits\n\ |
| 11913 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11914 | |
| 11915 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11916 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11917 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | Py_ssize_t i, length; |
| 11919 | int kind; |
| 11920 | void *data; |
| 11921 | |
| 11922 | if (PyUnicode_READY(self) == -1) |
| 11923 | return NULL; |
| 11924 | length = PyUnicode_GET_LENGTH(self); |
| 11925 | kind = PyUnicode_KIND(self); |
| 11926 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11928 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11929 | if (length == 1) { |
| 11930 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11931 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11932 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11933 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11934 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11935 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11936 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11937 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11938 | for (i = 0; i < length; i++) { |
| 11939 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11940 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11941 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11942 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | } |
| 11944 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11945 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11946 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11947 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11948 | 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] | 11949 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11950 | |
| 11951 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11952 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11954 | Py_ssize_t i, length; |
| 11955 | int kind; |
| 11956 | void *data; |
| 11957 | |
| 11958 | if (PyUnicode_READY(self) == -1) |
| 11959 | return NULL; |
| 11960 | length = PyUnicode_GET_LENGTH(self); |
| 11961 | kind = PyUnicode_KIND(self); |
| 11962 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11963 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11964 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11965 | if (length == 1) |
| 11966 | return PyBool_FromLong( |
| 11967 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11968 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11969 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11970 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11971 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11972 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11973 | for (i = 0; i < length; i++) { |
| 11974 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11975 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11976 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11977 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11978 | } |
| 11979 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11980 | int |
| 11981 | PyUnicode_IsIdentifier(PyObject *self) |
| 11982 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11983 | int kind; |
| 11984 | void *data; |
| 11985 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11986 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11987 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11988 | if (PyUnicode_READY(self) == -1) { |
| 11989 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11990 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11991 | } |
| 11992 | |
| 11993 | /* Special case for empty strings */ |
| 11994 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11995 | return 0; |
| 11996 | kind = PyUnicode_KIND(self); |
| 11997 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11998 | |
| 11999 | /* PEP 3131 says that the first character must be in |
| 12000 | XID_Start and subsequent characters in XID_Continue, |
| 12001 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12002 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12003 | letters, digits, underscore). However, given the current |
| 12004 | definition of XID_Start and XID_Continue, it is sufficient |
| 12005 | to check just for these, except that _ must be allowed |
| 12006 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12007 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 12008 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12009 | return 0; |
| 12010 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 12011 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12012 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12013 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12014 | return 1; |
| 12015 | } |
| 12016 | |
| 12017 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12018 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12019 | \n\ |
| 12020 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 12021 | to the language definition.\n\ |
| 12022 | \n\ |
| 12023 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 12024 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12025 | |
| 12026 | static PyObject* |
| 12027 | unicode_isidentifier(PyObject *self) |
| 12028 | { |
| 12029 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 12030 | } |
| 12031 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12032 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12033 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12034 | \n\ |
| 12035 | Return True if all characters in S are considered\n\ |
| 12036 | printable in repr() or S is empty, False otherwise."); |
| 12037 | |
| 12038 | static PyObject* |
| 12039 | unicode_isprintable(PyObject *self) |
| 12040 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | Py_ssize_t i, length; |
| 12042 | int kind; |
| 12043 | void *data; |
| 12044 | |
| 12045 | if (PyUnicode_READY(self) == -1) |
| 12046 | return NULL; |
| 12047 | length = PyUnicode_GET_LENGTH(self); |
| 12048 | kind = PyUnicode_KIND(self); |
| 12049 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12050 | |
| 12051 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12052 | if (length == 1) |
| 12053 | return PyBool_FromLong( |
| 12054 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12055 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12056 | for (i = 0; i < length; i++) { |
| 12057 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12058 | Py_RETURN_FALSE; |
| 12059 | } |
| 12060 | } |
| 12061 | Py_RETURN_TRUE; |
| 12062 | } |
| 12063 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12064 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 12065 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12066 | \n\ |
| 12067 | 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] | 12068 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12069 | |
| 12070 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12071 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12072 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12073 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12074 | } |
| 12075 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12076 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12077 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12078 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12079 | if (PyUnicode_READY(self) == -1) |
| 12080 | return -1; |
| 12081 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12082 | } |
| 12083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12084 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12085 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12086 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12087 | Return S left-justified in a Unicode string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12088 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12089 | |
| 12090 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12091 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12092 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12093 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12094 | Py_UCS4 fillchar = ' '; |
| 12095 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12096 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12097 | return NULL; |
| 12098 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12099 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12100 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12101 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12102 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12103 | return unicode_result_unchanged(self); |
| 12104 | |
| 12105 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12106 | } |
| 12107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12108 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12109 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12110 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12111 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12112 | |
| 12113 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12114 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12115 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12116 | if (PyUnicode_READY(self) == -1) |
| 12117 | return NULL; |
| 12118 | if (PyUnicode_IS_ASCII(self)) |
| 12119 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12120 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12121 | } |
| 12122 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12123 | #define LEFTSTRIP 0 |
| 12124 | #define RIGHTSTRIP 1 |
| 12125 | #define BOTHSTRIP 2 |
| 12126 | |
| 12127 | /* Arrays indexed by above */ |
| 12128 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 12129 | |
| 12130 | #define STRIPNAME(i) (stripformat[i]+3) |
| 12131 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12132 | /* externally visible for str.strip(unicode) */ |
| 12133 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12134 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12135 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12136 | void *data; |
| 12137 | int kind; |
| 12138 | Py_ssize_t i, j, len; |
| 12139 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12140 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12142 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 12143 | return NULL; |
| 12144 | |
| 12145 | kind = PyUnicode_KIND(self); |
| 12146 | data = PyUnicode_DATA(self); |
| 12147 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12148 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12149 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 12150 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12151 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12152 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12153 | i = 0; |
| 12154 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12155 | while (i < len) { |
| 12156 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12157 | if (!BLOOM(sepmask, ch)) |
| 12158 | break; |
| 12159 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12160 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12161 | i++; |
| 12162 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12163 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12164 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12165 | j = len; |
| 12166 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12167 | j--; |
| 12168 | while (j >= i) { |
| 12169 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12170 | if (!BLOOM(sepmask, ch)) |
| 12171 | break; |
| 12172 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12173 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12174 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12175 | } |
| 12176 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12177 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12178 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12179 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12180 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12181 | } |
| 12182 | |
| 12183 | PyObject* |
| 12184 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12185 | { |
| 12186 | unsigned char *data; |
| 12187 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12188 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12189 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12190 | if (PyUnicode_READY(self) == -1) |
| 12191 | return NULL; |
| 12192 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12193 | length = PyUnicode_GET_LENGTH(self); |
| 12194 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12195 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12196 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12197 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12198 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12199 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12200 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12201 | return NULL; |
| 12202 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12203 | if (start >= length || end < start) |
| 12204 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12205 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12206 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12207 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12208 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12209 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12210 | } |
| 12211 | else { |
| 12212 | kind = PyUnicode_KIND(self); |
| 12213 | data = PyUnicode_1BYTE_DATA(self); |
| 12214 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12215 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12216 | length); |
| 12217 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12218 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12219 | |
| 12220 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12221 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12222 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12223 | Py_ssize_t len, i, j; |
| 12224 | |
| 12225 | if (PyUnicode_READY(self) == -1) |
| 12226 | return NULL; |
| 12227 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12228 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12229 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12230 | if (PyUnicode_IS_ASCII(self)) { |
| 12231 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12232 | |
| 12233 | i = 0; |
| 12234 | if (striptype != RIGHTSTRIP) { |
| 12235 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12236 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12237 | if (!_Py_ascii_whitespace[ch]) |
| 12238 | break; |
| 12239 | i++; |
| 12240 | } |
| 12241 | } |
| 12242 | |
| 12243 | j = len; |
| 12244 | if (striptype != LEFTSTRIP) { |
| 12245 | j--; |
| 12246 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12247 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12248 | if (!_Py_ascii_whitespace[ch]) |
| 12249 | break; |
| 12250 | j--; |
| 12251 | } |
| 12252 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12253 | } |
| 12254 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12255 | else { |
| 12256 | int kind = PyUnicode_KIND(self); |
| 12257 | void *data = PyUnicode_DATA(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 | i = 0; |
| 12260 | if (striptype != RIGHTSTRIP) { |
| 12261 | while (i < len) { |
| 12262 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12263 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12264 | break; |
| 12265 | i++; |
| 12266 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12267 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12268 | |
| 12269 | j = len; |
| 12270 | if (striptype != LEFTSTRIP) { |
| 12271 | j--; |
| 12272 | while (j >= i) { |
| 12273 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12274 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12275 | break; |
| 12276 | j--; |
| 12277 | } |
| 12278 | j++; |
| 12279 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12280 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12281 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12282 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | } |
| 12284 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12285 | |
| 12286 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12287 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12288 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12289 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12290 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12291 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12292 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12293 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12294 | if (sep != NULL && sep != Py_None) { |
| 12295 | if (PyUnicode_Check(sep)) |
| 12296 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12297 | else { |
| 12298 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12299 | "%s arg must be None or str", |
| 12300 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12301 | return NULL; |
| 12302 | } |
| 12303 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12304 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12305 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12306 | } |
| 12307 | |
| 12308 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12309 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12310 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12311 | \n\ |
| 12312 | Return a copy of the string S with leading and trailing\n\ |
| 12313 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12314 | 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] | 12315 | |
| 12316 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12317 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12318 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12319 | if (PyTuple_GET_SIZE(args) == 0) |
| 12320 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12321 | else |
| 12322 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12323 | } |
| 12324 | |
| 12325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12326 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12327 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12328 | \n\ |
| 12329 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12330 | 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] | 12331 | |
| 12332 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12333 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12334 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12335 | if (PyTuple_GET_SIZE(args) == 0) |
| 12336 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12337 | else |
| 12338 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12339 | } |
| 12340 | |
| 12341 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12342 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12343 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12344 | \n\ |
| 12345 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12346 | 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] | 12347 | |
| 12348 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12349 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12350 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12351 | if (PyTuple_GET_SIZE(args) == 0) |
| 12352 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12353 | else |
| 12354 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12355 | } |
| 12356 | |
| 12357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12358 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12359 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12360 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12361 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12362 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12363 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12364 | if (len < 1) |
| 12365 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12366 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12367 | /* no repeat, return original string */ |
| 12368 | if (len == 1) |
| 12369 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12370 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12371 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12372 | return NULL; |
| 12373 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12374 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12375 | PyErr_SetString(PyExc_OverflowError, |
| 12376 | "repeated string is too long"); |
| 12377 | return NULL; |
| 12378 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12379 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12380 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12381 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12382 | if (!u) |
| 12383 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12384 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12385 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12386 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12387 | const int kind = PyUnicode_KIND(str); |
| 12388 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12389 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12390 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12391 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12392 | } |
| 12393 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12394 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12395 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12396 | ucs2[n] = fill_char; |
| 12397 | } else { |
| 12398 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12399 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12400 | for (n = 0; n < len; ++n) |
| 12401 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12402 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12403 | } |
| 12404 | else { |
| 12405 | /* number of characters copied this far */ |
| 12406 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12407 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12408 | char *to = (char *) PyUnicode_DATA(u); |
| 12409 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12410 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12411 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12412 | n = (done <= nchars-done) ? done : nchars-done; |
| 12413 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12414 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12415 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12416 | } |
| 12417 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12418 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12419 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12420 | } |
| 12421 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12422 | PyObject * |
| 12423 | PyUnicode_Replace(PyObject *obj, |
| 12424 | PyObject *subobj, |
| 12425 | PyObject *replobj, |
| 12426 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12427 | { |
| 12428 | PyObject *self; |
| 12429 | PyObject *str1; |
| 12430 | PyObject *str2; |
| 12431 | PyObject *result; |
| 12432 | |
| 12433 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12434 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12435 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12436 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12437 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12438 | Py_DECREF(self); |
| 12439 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12440 | } |
| 12441 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12442 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12443 | Py_DECREF(self); |
| 12444 | Py_DECREF(str1); |
| 12445 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12446 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12447 | if (PyUnicode_READY(self) == -1 || |
| 12448 | PyUnicode_READY(str1) == -1 || |
| 12449 | PyUnicode_READY(str2) == -1) |
| 12450 | result = NULL; |
| 12451 | else |
| 12452 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12453 | Py_DECREF(self); |
| 12454 | Py_DECREF(str1); |
| 12455 | Py_DECREF(str2); |
| 12456 | return result; |
| 12457 | } |
| 12458 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12459 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12460 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12461 | \n\ |
| 12462 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12463 | old replaced by new. If the optional argument count is\n\ |
| 12464 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12465 | |
| 12466 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12467 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12468 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12469 | PyObject *str1; |
| 12470 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12471 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12472 | PyObject *result; |
| 12473 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12474 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12475 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12476 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12477 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12478 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12479 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12480 | return NULL; |
| 12481 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12482 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12483 | Py_DECREF(str1); |
| 12484 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12485 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12486 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12487 | result = NULL; |
| 12488 | else |
| 12489 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12490 | |
| 12491 | Py_DECREF(str1); |
| 12492 | Py_DECREF(str2); |
| 12493 | return result; |
| 12494 | } |
| 12495 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12496 | static PyObject * |
| 12497 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12498 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12499 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12500 | Py_ssize_t isize; |
| 12501 | Py_ssize_t osize, squote, dquote, i, o; |
| 12502 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12503 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12504 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12505 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12506 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12507 | return NULL; |
| 12508 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12509 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12510 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12512 | /* Compute length of output, quote characters, and |
| 12513 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12514 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12515 | max = 127; |
| 12516 | squote = dquote = 0; |
| 12517 | ikind = PyUnicode_KIND(unicode); |
| 12518 | for (i = 0; i < isize; i++) { |
| 12519 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12520 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12521 | switch (ch) { |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12522 | case '\'': squote++; break; |
| 12523 | case '"': dquote++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12524 | case '\\': case '\t': case '\r': case '\n': |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12525 | incr = 2; |
| 12526 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12527 | default: |
| 12528 | /* Fast-path ASCII */ |
| 12529 | if (ch < ' ' || ch == 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12530 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12531 | else if (ch < 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12532 | ; |
| 12533 | else if (Py_UNICODE_ISPRINTABLE(ch)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12534 | max = ch > max ? ch : max; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12535 | else if (ch < 0x100) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12536 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12537 | else if (ch < 0x10000) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12538 | incr = 6; /* \uHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12539 | else |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12540 | incr = 10; /* \uHHHHHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12541 | } |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12542 | if (osize > PY_SSIZE_T_MAX - incr) { |
| 12543 | PyErr_SetString(PyExc_OverflowError, |
| 12544 | "string is too long to generate repr"); |
| 12545 | return NULL; |
| 12546 | } |
| 12547 | osize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12548 | } |
| 12549 | |
| 12550 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12551 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12552 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12553 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12554 | if (dquote) |
| 12555 | /* Both squote and dquote present. Use squote, |
| 12556 | and escape them */ |
| 12557 | osize += squote; |
| 12558 | else |
| 12559 | quote = '"'; |
| 12560 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12561 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12562 | |
| 12563 | repr = PyUnicode_New(osize, max); |
| 12564 | if (repr == NULL) |
| 12565 | return NULL; |
| 12566 | okind = PyUnicode_KIND(repr); |
| 12567 | odata = PyUnicode_DATA(repr); |
| 12568 | |
| 12569 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12570 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12571 | if (unchanged) { |
| 12572 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12573 | unicode, 0, |
| 12574 | isize); |
| 12575 | } |
| 12576 | else { |
| 12577 | for (i = 0, o = 1; i < isize; i++) { |
| 12578 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12579 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12580 | /* Escape quotes and backslashes */ |
| 12581 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12582 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12583 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12584 | continue; |
| 12585 | } |
| 12586 | |
| 12587 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12588 | if (ch == '\t') { |
| 12589 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12590 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12591 | } |
| 12592 | else if (ch == '\n') { |
| 12593 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12594 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12595 | } |
| 12596 | else if (ch == '\r') { |
| 12597 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12598 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12599 | } |
| 12600 | |
| 12601 | /* Map non-printable US ASCII to '\xhh' */ |
| 12602 | else if (ch < ' ' || ch == 0x7F) { |
| 12603 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12604 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12605 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12606 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12607 | } |
| 12608 | |
| 12609 | /* Copy ASCII characters as-is */ |
| 12610 | else if (ch < 0x7F) { |
| 12611 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12612 | } |
| 12613 | |
| 12614 | /* Non-ASCII characters */ |
| 12615 | else { |
| 12616 | /* Map Unicode whitespace and control characters |
| 12617 | (categories Z* and C* except ASCII space) |
| 12618 | */ |
| 12619 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12620 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12621 | /* Map 8-bit characters to '\xhh' */ |
| 12622 | if (ch <= 0xff) { |
| 12623 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12624 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12625 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12626 | } |
| 12627 | /* Map 16-bit characters to '\uxxxx' */ |
| 12628 | else if (ch <= 0xffff) { |
| 12629 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12630 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12631 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12632 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12633 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12634 | } |
| 12635 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12636 | else { |
| 12637 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12638 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12639 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12640 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12641 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12642 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12643 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12644 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12645 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12646 | } |
| 12647 | } |
| 12648 | /* Copy characters as-is */ |
| 12649 | else { |
| 12650 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12651 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12652 | } |
| 12653 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12654 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12655 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12656 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12657 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12658 | } |
| 12659 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12660 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12661 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12662 | \n\ |
| 12663 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12664 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12665 | arguments start and end are interpreted as in slice notation.\n\ |
| 12666 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12667 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12668 | |
| 12669 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12670 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12671 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12672 | /* initialize variables to prevent gcc warning */ |
| 12673 | PyObject *substring = NULL; |
| 12674 | Py_ssize_t start = 0; |
| 12675 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12676 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12677 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12678 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12679 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12680 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12681 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12682 | if (PyUnicode_READY(self) == -1) { |
| 12683 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12684 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12685 | } |
| 12686 | if (PyUnicode_READY(substring) == -1) { |
| 12687 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12688 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12689 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12690 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12691 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12692 | |
| 12693 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12694 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12695 | if (result == -2) |
| 12696 | return NULL; |
| 12697 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12698 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12699 | } |
| 12700 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12701 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12702 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12703 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12704 | 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] | 12705 | |
| 12706 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12707 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12708 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12709 | /* initialize variables to prevent gcc warning */ |
| 12710 | PyObject *substring = NULL; |
| 12711 | Py_ssize_t start = 0; |
| 12712 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12713 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12714 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12715 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12716 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12717 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12718 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12719 | if (PyUnicode_READY(self) == -1) { |
| 12720 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12721 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12722 | } |
| 12723 | if (PyUnicode_READY(substring) == -1) { |
| 12724 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12725 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12726 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12727 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12728 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12729 | |
| 12730 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12732 | if (result == -2) |
| 12733 | return NULL; |
| 12734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12735 | if (result < 0) { |
| 12736 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12737 | return NULL; |
| 12738 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12739 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12740 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12741 | } |
| 12742 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12743 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12744 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12745 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12746 | 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] | 12747 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12748 | |
| 12749 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12750 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12751 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12752 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12753 | Py_UCS4 fillchar = ' '; |
| 12754 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12755 | 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] | 12756 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12757 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12758 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12759 | return NULL; |
| 12760 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12761 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12762 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12763 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12764 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12765 | } |
| 12766 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12767 | PyObject * |
| 12768 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12769 | { |
| 12770 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12771 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12772 | s = PyUnicode_FromObject(s); |
| 12773 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12774 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12775 | if (sep != NULL) { |
| 12776 | sep = PyUnicode_FromObject(sep); |
| 12777 | if (sep == NULL) { |
| 12778 | Py_DECREF(s); |
| 12779 | return NULL; |
| 12780 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12781 | } |
| 12782 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12783 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12784 | |
| 12785 | Py_DECREF(s); |
| 12786 | Py_XDECREF(sep); |
| 12787 | return result; |
| 12788 | } |
| 12789 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12790 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12791 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12792 | \n\ |
| 12793 | Return a list of the words in S, using sep as the\n\ |
| 12794 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12795 | 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] | 12796 | whitespace string is a separator and empty strings are\n\ |
| 12797 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12798 | |
| 12799 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12800 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12801 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12802 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12803 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12804 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12805 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12806 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12807 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12808 | return NULL; |
| 12809 | |
| 12810 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12811 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12812 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12813 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12814 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12815 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12816 | } |
| 12817 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12818 | PyObject * |
| 12819 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12820 | { |
| 12821 | PyObject* str_obj; |
| 12822 | PyObject* sep_obj; |
| 12823 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12824 | int kind1, kind2; |
| 12825 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12826 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12827 | |
| 12828 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12829 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12830 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12831 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12832 | if (!sep_obj) { |
| 12833 | Py_DECREF(str_obj); |
| 12834 | return NULL; |
| 12835 | } |
| 12836 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12837 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12838 | Py_DECREF(str_obj); |
| 12839 | return NULL; |
| 12840 | } |
| 12841 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12842 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12843 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12844 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12845 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12846 | if (kind1 < kind2 || len1 < len2) { |
| 12847 | _Py_INCREF_UNICODE_EMPTY(); |
| 12848 | if (!unicode_empty) |
| 12849 | out = NULL; |
| 12850 | else { |
| 12851 | out = PyTuple_Pack(3, str_obj, unicode_empty, unicode_empty); |
| 12852 | Py_DECREF(unicode_empty); |
| 12853 | } |
| 12854 | Py_DECREF(sep_obj); |
| 12855 | Py_DECREF(str_obj); |
| 12856 | return out; |
| 12857 | } |
| 12858 | buf1 = PyUnicode_DATA(str_obj); |
| 12859 | buf2 = PyUnicode_DATA(sep_obj); |
| 12860 | if (kind2 != kind1) { |
| 12861 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12862 | if (!buf2) |
| 12863 | goto onError; |
| 12864 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12865 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12866 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12867 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12868 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12869 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12870 | else |
| 12871 | 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] | 12872 | break; |
| 12873 | case PyUnicode_2BYTE_KIND: |
| 12874 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12875 | break; |
| 12876 | case PyUnicode_4BYTE_KIND: |
| 12877 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12878 | break; |
| 12879 | default: |
| 12880 | assert(0); |
| 12881 | out = 0; |
| 12882 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12883 | |
| 12884 | Py_DECREF(sep_obj); |
| 12885 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12886 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12887 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12888 | |
| 12889 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12890 | onError: |
| 12891 | Py_DECREF(sep_obj); |
| 12892 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12893 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12894 | PyMem_Free(buf2); |
| 12895 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12896 | } |
| 12897 | |
| 12898 | |
| 12899 | PyObject * |
| 12900 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12901 | { |
| 12902 | PyObject* str_obj; |
| 12903 | PyObject* sep_obj; |
| 12904 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12905 | int kind1, kind2; |
| 12906 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12907 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12908 | |
| 12909 | str_obj = PyUnicode_FromObject(str_in); |
| 12910 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12911 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12912 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12913 | if (!sep_obj) { |
| 12914 | Py_DECREF(str_obj); |
| 12915 | return NULL; |
| 12916 | } |
| 12917 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12918 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12919 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12920 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12921 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12922 | if (kind1 < kind2 || len1 < len2) { |
| 12923 | _Py_INCREF_UNICODE_EMPTY(); |
| 12924 | if (!unicode_empty) |
| 12925 | out = NULL; |
| 12926 | else { |
| 12927 | out = PyTuple_Pack(3, unicode_empty, unicode_empty, str_obj); |
| 12928 | Py_DECREF(unicode_empty); |
| 12929 | } |
| 12930 | Py_DECREF(sep_obj); |
| 12931 | Py_DECREF(str_obj); |
| 12932 | return out; |
| 12933 | } |
| 12934 | buf1 = PyUnicode_DATA(str_obj); |
| 12935 | buf2 = PyUnicode_DATA(sep_obj); |
| 12936 | if (kind2 != kind1) { |
| 12937 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12938 | if (!buf2) |
| 12939 | goto onError; |
| 12940 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12941 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12942 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12943 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12944 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12945 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12946 | else |
| 12947 | 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] | 12948 | break; |
| 12949 | case PyUnicode_2BYTE_KIND: |
| 12950 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12951 | break; |
| 12952 | case PyUnicode_4BYTE_KIND: |
| 12953 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12954 | break; |
| 12955 | default: |
| 12956 | assert(0); |
| 12957 | out = 0; |
| 12958 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12959 | |
| 12960 | Py_DECREF(sep_obj); |
| 12961 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12962 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12963 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12964 | |
| 12965 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12966 | onError: |
| 12967 | Py_DECREF(sep_obj); |
| 12968 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12969 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12970 | PyMem_Free(buf2); |
| 12971 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12972 | } |
| 12973 | |
| 12974 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12975 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12976 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12977 | 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] | 12978 | 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] | 12979 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12980 | |
| 12981 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12982 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12983 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12984 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12985 | } |
| 12986 | |
| 12987 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12988 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12989 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12990 | 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] | 12991 | 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] | 12992 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12993 | |
| 12994 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12995 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12996 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12997 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12998 | } |
| 12999 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13000 | PyObject * |
| 13001 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13002 | { |
| 13003 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13004 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13005 | s = PyUnicode_FromObject(s); |
| 13006 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13007 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13008 | if (sep != NULL) { |
| 13009 | sep = PyUnicode_FromObject(sep); |
| 13010 | if (sep == NULL) { |
| 13011 | Py_DECREF(s); |
| 13012 | return NULL; |
| 13013 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13014 | } |
| 13015 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13016 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13017 | |
| 13018 | Py_DECREF(s); |
| 13019 | Py_XDECREF(sep); |
| 13020 | return result; |
| 13021 | } |
| 13022 | |
| 13023 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13024 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13025 | \n\ |
| 13026 | Return a list of the words in S, using sep as the\n\ |
| 13027 | delimiter string, starting at the end of the string and\n\ |
| 13028 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 13029 | splits are done. If sep is not specified, any whitespace string\n\ |
| 13030 | is a separator."); |
| 13031 | |
| 13032 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13033 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13034 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13035 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13036 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13037 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13038 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13039 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 13040 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13041 | return NULL; |
| 13042 | |
| 13043 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13044 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13045 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13046 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13047 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13048 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 13049 | } |
| 13050 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13051 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13052 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13053 | \n\ |
| 13054 | 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] | 13055 | 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] | 13056 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13057 | |
| 13058 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13059 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13060 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13061 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 13062 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13063 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 13064 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 13065 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13066 | return NULL; |
| 13067 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13068 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13069 | } |
| 13070 | |
| 13071 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 13072 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13073 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13074 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13075 | } |
| 13076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13077 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13078 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13079 | \n\ |
| 13080 | 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] | 13081 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13082 | |
| 13083 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13084 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13085 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 13086 | if (PyUnicode_READY(self) == -1) |
| 13087 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13088 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13089 | } |
| 13090 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 13091 | /*[clinic input] |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13092 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13093 | @staticmethod |
| 13094 | str.maketrans as unicode_maketrans |
| 13095 | |
| 13096 | x: object |
| 13097 | |
| 13098 | y: unicode=NULL |
| 13099 | |
| 13100 | z: unicode=NULL |
| 13101 | |
| 13102 | / |
| 13103 | |
| 13104 | Return a translation table usable for str.translate(). |
| 13105 | |
| 13106 | If there is only one argument, it must be a dictionary mapping Unicode |
| 13107 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 13108 | Character keys will be then converted to ordinals. |
| 13109 | If there are two arguments, they must be strings of equal length, and |
| 13110 | in the resulting dictionary, each character in x will be mapped to the |
| 13111 | character at the same position in y. If there is a third argument, it |
| 13112 | 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] | 13113 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13114 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13115 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 13116 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 13117 | /*[clinic end generated code: output=a925c89452bd5881 input=7bfbf529a293c6c5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13118 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13119 | PyObject *new = NULL, *key, *value; |
| 13120 | Py_ssize_t i = 0; |
| 13121 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13122 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13123 | new = PyDict_New(); |
| 13124 | if (!new) |
| 13125 | return NULL; |
| 13126 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13127 | int x_kind, y_kind, z_kind; |
| 13128 | void *x_data, *y_data, *z_data; |
| 13129 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13130 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13131 | if (!PyUnicode_Check(x)) { |
| 13132 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 13133 | "be a string if there is a second argument"); |
| 13134 | goto err; |
| 13135 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13136 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13137 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 13138 | "arguments must have equal length"); |
| 13139 | goto err; |
| 13140 | } |
| 13141 | /* 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] | 13142 | x_kind = PyUnicode_KIND(x); |
| 13143 | y_kind = PyUnicode_KIND(y); |
| 13144 | x_data = PyUnicode_DATA(x); |
| 13145 | y_data = PyUnicode_DATA(y); |
| 13146 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 13147 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 13148 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13149 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 13150 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 13151 | if (!value) { |
| 13152 | Py_DECREF(key); |
| 13153 | goto err; |
| 13154 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13155 | res = PyDict_SetItem(new, key, value); |
| 13156 | Py_DECREF(key); |
| 13157 | Py_DECREF(value); |
| 13158 | if (res < 0) |
| 13159 | goto err; |
| 13160 | } |
| 13161 | /* create entries for deleting chars in z */ |
| 13162 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13163 | z_kind = PyUnicode_KIND(z); |
| 13164 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13165 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13166 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13167 | if (!key) |
| 13168 | goto err; |
| 13169 | res = PyDict_SetItem(new, key, Py_None); |
| 13170 | Py_DECREF(key); |
| 13171 | if (res < 0) |
| 13172 | goto err; |
| 13173 | } |
| 13174 | } |
| 13175 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13176 | int kind; |
| 13177 | void *data; |
| 13178 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13179 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 13180 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13181 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 13182 | "to maketrans it must be a dict"); |
| 13183 | goto err; |
| 13184 | } |
| 13185 | /* copy entries into the new dict, converting string keys to int keys */ |
| 13186 | while (PyDict_Next(x, &i, &key, &value)) { |
| 13187 | if (PyUnicode_Check(key)) { |
| 13188 | /* convert string keys to integer keys */ |
| 13189 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13190 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13191 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13192 | "table must be of length 1"); |
| 13193 | goto err; |
| 13194 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13195 | kind = PyUnicode_KIND(key); |
| 13196 | data = PyUnicode_DATA(key); |
| 13197 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13198 | if (!newkey) |
| 13199 | goto err; |
| 13200 | res = PyDict_SetItem(new, newkey, value); |
| 13201 | Py_DECREF(newkey); |
| 13202 | if (res < 0) |
| 13203 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13204 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13205 | /* just keep integer keys */ |
| 13206 | if (PyDict_SetItem(new, key, value) < 0) |
| 13207 | goto err; |
| 13208 | } else { |
| 13209 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13210 | "be strings or integers"); |
| 13211 | goto err; |
| 13212 | } |
| 13213 | } |
| 13214 | } |
| 13215 | return new; |
| 13216 | err: |
| 13217 | Py_DECREF(new); |
| 13218 | return NULL; |
| 13219 | } |
| 13220 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13221 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13222 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13223 | \n\ |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 13224 | Return a copy of the string S in which each character has been mapped\n\ |
| 13225 | through the given translation table. The table must implement\n\ |
| 13226 | lookup/indexing via __getitem__, for instance a dictionary or list,\n\ |
| 13227 | mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n\ |
| 13228 | this operation raises LookupError, the character is left untouched.\n\ |
| 13229 | Characters mapped to None are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13230 | |
| 13231 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13232 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13233 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13234 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13235 | } |
| 13236 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13237 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13238 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13239 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13240 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13241 | |
| 13242 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13243 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13244 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13245 | if (PyUnicode_READY(self) == -1) |
| 13246 | return NULL; |
| 13247 | if (PyUnicode_IS_ASCII(self)) |
| 13248 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13249 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13250 | } |
| 13251 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13252 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13253 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13254 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13255 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13256 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13257 | |
| 13258 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13259 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13260 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13261 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13262 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13263 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13264 | int kind; |
| 13265 | void *data; |
| 13266 | Py_UCS4 chr; |
| 13267 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13268 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13269 | return NULL; |
| 13270 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13271 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13272 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13273 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13274 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13275 | return unicode_result_unchanged(self); |
| 13276 | |
| 13277 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13278 | |
| 13279 | u = pad(self, fill, 0, '0'); |
| 13280 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13281 | if (u == NULL) |
| 13282 | return NULL; |
| 13283 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13284 | kind = PyUnicode_KIND(u); |
| 13285 | data = PyUnicode_DATA(u); |
| 13286 | chr = PyUnicode_READ(kind, data, fill); |
| 13287 | |
| 13288 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13289 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13290 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13291 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13292 | } |
| 13293 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13294 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13295 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13296 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13297 | |
| 13298 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13299 | static PyObject * |
| 13300 | unicode__decimal2ascii(PyObject *self) |
| 13301 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13302 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13303 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13304 | #endif |
| 13305 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13306 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13307 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13308 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13309 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13310 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13311 | With optional end, stop comparing S at that position.\n\ |
| 13312 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13313 | |
| 13314 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13315 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13316 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13317 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13318 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13319 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13320 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13321 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13322 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13323 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13324 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13325 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13326 | if (PyTuple_Check(subobj)) { |
| 13327 | Py_ssize_t i; |
| 13328 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13329 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13330 | if (substring == NULL) |
| 13331 | return NULL; |
| 13332 | result = tailmatch(self, substring, start, end, -1); |
| 13333 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13334 | if (result == -1) |
| 13335 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13336 | if (result) { |
| 13337 | Py_RETURN_TRUE; |
| 13338 | } |
| 13339 | } |
| 13340 | /* nothing matched */ |
| 13341 | Py_RETURN_FALSE; |
| 13342 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13343 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13344 | if (substring == NULL) { |
| 13345 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13346 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13347 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13348 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13349 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13350 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13351 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13352 | if (result == -1) |
| 13353 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13354 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13355 | } |
| 13356 | |
| 13357 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13358 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13359 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13360 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13361 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13362 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13363 | With optional end, stop comparing S at that position.\n\ |
| 13364 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13365 | |
| 13366 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13367 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13368 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13369 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13370 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13371 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13372 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13373 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13374 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13375 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13376 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13377 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13378 | if (PyTuple_Check(subobj)) { |
| 13379 | Py_ssize_t i; |
| 13380 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13381 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13382 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13383 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13384 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13385 | result = tailmatch(self, substring, start, end, +1); |
| 13386 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13387 | if (result == -1) |
| 13388 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13389 | if (result) { |
| 13390 | Py_RETURN_TRUE; |
| 13391 | } |
| 13392 | } |
| 13393 | Py_RETURN_FALSE; |
| 13394 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13395 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13396 | if (substring == NULL) { |
| 13397 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13398 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13399 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13400 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13401 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13402 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13403 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13404 | if (result == -1) |
| 13405 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13406 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13407 | } |
| 13408 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13409 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13410 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13411 | { |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13412 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13413 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13414 | |
| 13415 | if (!writer->readonly) { |
| 13416 | writer->kind = PyUnicode_KIND(writer->buffer); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13417 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13418 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13419 | else { |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13420 | /* use a value smaller than PyUnicode_1BYTE_KIND() so |
| 13421 | _PyUnicodeWriter_PrepareKind() will copy the buffer. */ |
| 13422 | writer->kind = PyUnicode_WCHAR_KIND; |
| 13423 | assert(writer->kind <= PyUnicode_1BYTE_KIND); |
| 13424 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13425 | /* Copy-on-write mode: set buffer size to 0 so |
| 13426 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13427 | * next write. */ |
| 13428 | writer->size = 0; |
| 13429 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13430 | } |
| 13431 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13432 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13433 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13434 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13435 | memset(writer, 0, sizeof(*writer)); |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13436 | |
| 13437 | /* ASCII is the bare minimum */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13438 | writer->min_char = 127; |
Victor Stinner | eb36fda | 2015-10-03 01:55:51 +0200 | [diff] [blame] | 13439 | |
| 13440 | /* use a value smaller than PyUnicode_1BYTE_KIND() so |
| 13441 | _PyUnicodeWriter_PrepareKind() will copy the buffer. */ |
| 13442 | writer->kind = PyUnicode_WCHAR_KIND; |
| 13443 | assert(writer->kind <= PyUnicode_1BYTE_KIND); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13444 | } |
| 13445 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13446 | int |
| 13447 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13448 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13449 | { |
| 13450 | Py_ssize_t newlen; |
| 13451 | PyObject *newbuffer; |
| 13452 | |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 13453 | /* ensure that the _PyUnicodeWriter_Prepare macro was used */ |
Victor Stinner | 6174474 | 2015-09-22 01:01:17 +0200 | [diff] [blame] | 13454 | assert((maxchar > writer->maxchar && length >= 0) |
| 13455 | || length > 0); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13456 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13457 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13458 | PyErr_NoMemory(); |
| 13459 | return -1; |
| 13460 | } |
| 13461 | newlen = writer->pos + length; |
| 13462 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13463 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13464 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13465 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13466 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13467 | if (writer->overallocate |
| 13468 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13469 | /* overallocate to limit the number of realloc() */ |
| 13470 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13471 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13472 | if (newlen < writer->min_length) |
| 13473 | newlen = writer->min_length; |
| 13474 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13475 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13476 | if (writer->buffer == NULL) |
| 13477 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13478 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13479 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13480 | if (writer->overallocate |
| 13481 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13482 | /* overallocate to limit the number of realloc() */ |
| 13483 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13484 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13485 | if (newlen < writer->min_length) |
| 13486 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13487 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13488 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13489 | /* resize + widen */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 13490 | maxchar = Py_MAX(maxchar, writer->maxchar); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13491 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13492 | if (newbuffer == NULL) |
| 13493 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13494 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13495 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13496 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13497 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13498 | } |
| 13499 | else { |
| 13500 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13501 | if (newbuffer == NULL) |
| 13502 | return -1; |
| 13503 | } |
| 13504 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13505 | } |
| 13506 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13507 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13508 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13509 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13510 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13511 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13512 | writer->buffer, 0, writer->pos); |
| 13513 | Py_DECREF(writer->buffer); |
| 13514 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13515 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13516 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13517 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13518 | |
| 13519 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13520 | } |
| 13521 | |
Victor Stinner | ca9381e | 2015-09-22 00:58:32 +0200 | [diff] [blame] | 13522 | int |
| 13523 | _PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer, |
| 13524 | enum PyUnicode_Kind kind) |
| 13525 | { |
| 13526 | Py_UCS4 maxchar; |
| 13527 | |
| 13528 | /* ensure that the _PyUnicodeWriter_PrepareKind macro was used */ |
| 13529 | assert(writer->kind < kind); |
| 13530 | |
| 13531 | switch (kind) |
| 13532 | { |
| 13533 | case PyUnicode_1BYTE_KIND: maxchar = 0xff; break; |
| 13534 | case PyUnicode_2BYTE_KIND: maxchar = 0xffff; break; |
| 13535 | case PyUnicode_4BYTE_KIND: maxchar = 0x10ffff; break; |
| 13536 | default: |
| 13537 | assert(0 && "invalid kind"); |
| 13538 | return -1; |
| 13539 | } |
| 13540 | |
| 13541 | return _PyUnicodeWriter_PrepareInternal(writer, 0, maxchar); |
| 13542 | } |
| 13543 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13544 | Py_LOCAL_INLINE(int) |
| 13545 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13546 | { |
| 13547 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13548 | return -1; |
| 13549 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13550 | writer->pos++; |
| 13551 | return 0; |
| 13552 | } |
| 13553 | |
| 13554 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13555 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13556 | { |
| 13557 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13558 | } |
| 13559 | |
| 13560 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13561 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13562 | { |
| 13563 | Py_UCS4 maxchar; |
| 13564 | Py_ssize_t len; |
| 13565 | |
| 13566 | if (PyUnicode_READY(str) == -1) |
| 13567 | return -1; |
| 13568 | len = PyUnicode_GET_LENGTH(str); |
| 13569 | if (len == 0) |
| 13570 | return 0; |
| 13571 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13572 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13573 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 1912b39 | 2015-03-26 09:37:23 +0100 | [diff] [blame] | 13574 | assert(_PyUnicode_CheckConsistency(str, 1)); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13575 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13576 | Py_INCREF(str); |
| 13577 | writer->buffer = str; |
| 13578 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13579 | writer->pos += len; |
| 13580 | return 0; |
| 13581 | } |
| 13582 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13583 | return -1; |
| 13584 | } |
| 13585 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13586 | str, 0, len); |
| 13587 | writer->pos += len; |
| 13588 | return 0; |
| 13589 | } |
| 13590 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13591 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13592 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13593 | Py_ssize_t start, Py_ssize_t end) |
| 13594 | { |
| 13595 | Py_UCS4 maxchar; |
| 13596 | Py_ssize_t len; |
| 13597 | |
| 13598 | if (PyUnicode_READY(str) == -1) |
| 13599 | return -1; |
| 13600 | |
| 13601 | assert(0 <= start); |
| 13602 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13603 | assert(start <= end); |
| 13604 | |
| 13605 | if (end == 0) |
| 13606 | return 0; |
| 13607 | |
| 13608 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13609 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13610 | |
| 13611 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13612 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13613 | else |
| 13614 | maxchar = writer->maxchar; |
| 13615 | len = end - start; |
| 13616 | |
| 13617 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13618 | return -1; |
| 13619 | |
| 13620 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13621 | str, start, len); |
| 13622 | writer->pos += len; |
| 13623 | return 0; |
| 13624 | } |
| 13625 | |
| 13626 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13627 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13628 | const char *ascii, Py_ssize_t len) |
| 13629 | { |
| 13630 | if (len == -1) |
| 13631 | len = strlen(ascii); |
| 13632 | |
| 13633 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13634 | |
| 13635 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13636 | PyObject *str; |
| 13637 | |
| 13638 | str = _PyUnicode_FromASCII(ascii, len); |
| 13639 | if (str == NULL) |
| 13640 | return -1; |
| 13641 | |
| 13642 | writer->readonly = 1; |
| 13643 | writer->buffer = str; |
| 13644 | _PyUnicodeWriter_Update(writer); |
| 13645 | writer->pos += len; |
| 13646 | return 0; |
| 13647 | } |
| 13648 | |
| 13649 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13650 | return -1; |
| 13651 | |
| 13652 | switch (writer->kind) |
| 13653 | { |
| 13654 | case PyUnicode_1BYTE_KIND: |
| 13655 | { |
| 13656 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13657 | Py_UCS1 *data = writer->data; |
| 13658 | |
| 13659 | Py_MEMCPY(data + writer->pos, str, len); |
| 13660 | break; |
| 13661 | } |
| 13662 | case PyUnicode_2BYTE_KIND: |
| 13663 | { |
| 13664 | _PyUnicode_CONVERT_BYTES( |
| 13665 | Py_UCS1, Py_UCS2, |
| 13666 | ascii, ascii + len, |
| 13667 | (Py_UCS2 *)writer->data + writer->pos); |
| 13668 | break; |
| 13669 | } |
| 13670 | case PyUnicode_4BYTE_KIND: |
| 13671 | { |
| 13672 | _PyUnicode_CONVERT_BYTES( |
| 13673 | Py_UCS1, Py_UCS4, |
| 13674 | ascii, ascii + len, |
| 13675 | (Py_UCS4 *)writer->data + writer->pos); |
| 13676 | break; |
| 13677 | } |
| 13678 | default: |
| 13679 | assert(0); |
| 13680 | } |
| 13681 | |
| 13682 | writer->pos += len; |
| 13683 | return 0; |
| 13684 | } |
| 13685 | |
| 13686 | int |
| 13687 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13688 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13689 | { |
| 13690 | Py_UCS4 maxchar; |
| 13691 | |
| 13692 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13693 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13694 | return -1; |
| 13695 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13696 | writer->pos += len; |
| 13697 | return 0; |
| 13698 | } |
| 13699 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13700 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13701 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13702 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13703 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13704 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13705 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13706 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13707 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13708 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13709 | str = writer->buffer; |
| 13710 | writer->buffer = NULL; |
| 13711 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13712 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13713 | } |
| 13714 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13715 | PyObject *newbuffer; |
| 13716 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13717 | if (newbuffer == NULL) { |
Serhiy Storchaka | dfe98a1 | 2014-02-09 13:46:20 +0200 | [diff] [blame] | 13718 | Py_CLEAR(writer->buffer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13719 | return NULL; |
| 13720 | } |
| 13721 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13722 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13723 | str = writer->buffer; |
| 13724 | writer->buffer = NULL; |
| 13725 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13726 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13727 | } |
| 13728 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13729 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13730 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13731 | { |
| 13732 | Py_CLEAR(writer->buffer); |
| 13733 | } |
| 13734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13735 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13736 | |
| 13737 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13738 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13739 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13740 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13741 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13742 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13743 | PyDoc_STRVAR(format_map__doc__, |
| 13744 | "S.format_map(mapping) -> str\n\ |
| 13745 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13746 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13747 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13748 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13749 | static PyObject * |
| 13750 | unicode__format__(PyObject* self, PyObject* args) |
| 13751 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13752 | PyObject *format_spec; |
| 13753 | _PyUnicodeWriter writer; |
| 13754 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13755 | |
| 13756 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13757 | return NULL; |
| 13758 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13759 | if (PyUnicode_READY(self) == -1) |
| 13760 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13761 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13762 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13763 | self, format_spec, 0, |
| 13764 | PyUnicode_GET_LENGTH(format_spec)); |
| 13765 | if (ret == -1) { |
| 13766 | _PyUnicodeWriter_Dealloc(&writer); |
| 13767 | return NULL; |
| 13768 | } |
| 13769 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13770 | } |
| 13771 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13772 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13773 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13774 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13775 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13776 | |
| 13777 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13778 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13779 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13780 | Py_ssize_t size; |
| 13781 | |
| 13782 | /* If it's a compact object, account for base structure + |
| 13783 | character data. */ |
| 13784 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13785 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13786 | else if (PyUnicode_IS_COMPACT(v)) |
| 13787 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13788 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13789 | else { |
| 13790 | /* If it is a two-block object, account for base object, and |
| 13791 | for character block if present. */ |
| 13792 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13793 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13794 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13795 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13796 | } |
| 13797 | /* 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] | 13798 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13799 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13800 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13801 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13802 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13803 | |
| 13804 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13805 | } |
| 13806 | |
| 13807 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13808 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13809 | |
| 13810 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13811 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13812 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13813 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13814 | if (!copy) |
| 13815 | return NULL; |
| 13816 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13817 | } |
| 13818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13819 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13820 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13821 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13822 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13823 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13824 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13825 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13826 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13827 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13828 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13829 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13830 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13831 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13832 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13833 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13834 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13835 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13836 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13837 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13838 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13839 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13840 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13841 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13842 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13843 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13844 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13845 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13846 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13847 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13848 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13849 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13850 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13851 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13852 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13853 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13854 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13855 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13856 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13857 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13858 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13859 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13860 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13861 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13862 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13863 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13864 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13865 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13866 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13867 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13868 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13869 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13870 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13871 | #endif |
| 13872 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13873 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13874 | {NULL, NULL} |
| 13875 | }; |
| 13876 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13877 | static PyObject * |
| 13878 | unicode_mod(PyObject *v, PyObject *w) |
| 13879 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13880 | if (!PyUnicode_Check(v)) |
| 13881 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13882 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13883 | } |
| 13884 | |
| 13885 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13886 | 0, /*nb_add*/ |
| 13887 | 0, /*nb_subtract*/ |
| 13888 | 0, /*nb_multiply*/ |
| 13889 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13890 | }; |
| 13891 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13892 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13893 | (lenfunc) unicode_length, /* sq_length */ |
| 13894 | PyUnicode_Concat, /* sq_concat */ |
| 13895 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13896 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13897 | 0, /* sq_slice */ |
| 13898 | 0, /* sq_ass_item */ |
| 13899 | 0, /* sq_ass_slice */ |
| 13900 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13901 | }; |
| 13902 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13903 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13904 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13905 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13906 | if (PyUnicode_READY(self) == -1) |
| 13907 | return NULL; |
| 13908 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13909 | if (PyIndex_Check(item)) { |
| 13910 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13911 | if (i == -1 && PyErr_Occurred()) |
| 13912 | return NULL; |
| 13913 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13914 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13915 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13916 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13917 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13918 | PyObject *result; |
| 13919 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13920 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13921 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13922 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13923 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13924 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13925 | return NULL; |
| 13926 | } |
| 13927 | |
| 13928 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13929 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13930 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13931 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13932 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13933 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13934 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13935 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13936 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13937 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13938 | src_kind = PyUnicode_KIND(self); |
| 13939 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13940 | if (!PyUnicode_IS_ASCII(self)) { |
| 13941 | kind_limit = kind_maxchar_limit(src_kind); |
| 13942 | max_char = 0; |
| 13943 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13944 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13945 | if (ch > max_char) { |
| 13946 | max_char = ch; |
| 13947 | if (max_char >= kind_limit) |
| 13948 | break; |
| 13949 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13950 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13951 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13952 | else |
| 13953 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13954 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13955 | if (result == NULL) |
| 13956 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13957 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13958 | dest_data = PyUnicode_DATA(result); |
| 13959 | |
| 13960 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13961 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13962 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13963 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13964 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13965 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13966 | } else { |
| 13967 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13968 | return NULL; |
| 13969 | } |
| 13970 | } |
| 13971 | |
| 13972 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13973 | (lenfunc)unicode_length, /* mp_length */ |
| 13974 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13975 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13976 | }; |
| 13977 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13978 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13979 | /* Helpers for PyUnicode_Format() */ |
| 13980 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13981 | struct unicode_formatter_t { |
| 13982 | PyObject *args; |
| 13983 | int args_owned; |
| 13984 | Py_ssize_t arglen, argidx; |
| 13985 | PyObject *dict; |
| 13986 | |
| 13987 | enum PyUnicode_Kind fmtkind; |
| 13988 | Py_ssize_t fmtcnt, fmtpos; |
| 13989 | void *fmtdata; |
| 13990 | PyObject *fmtstr; |
| 13991 | |
| 13992 | _PyUnicodeWriter writer; |
| 13993 | }; |
| 13994 | |
| 13995 | struct unicode_format_arg_t { |
| 13996 | Py_UCS4 ch; |
| 13997 | int flags; |
| 13998 | Py_ssize_t width; |
| 13999 | int prec; |
| 14000 | int sign; |
| 14001 | }; |
| 14002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14003 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14004 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14005 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14006 | Py_ssize_t argidx = ctx->argidx; |
| 14007 | |
| 14008 | if (argidx < ctx->arglen) { |
| 14009 | ctx->argidx++; |
| 14010 | if (ctx->arglen < 0) |
| 14011 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14012 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14013 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14014 | } |
| 14015 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14016 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14017 | return NULL; |
| 14018 | } |
| 14019 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 14020 | /* 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] | 14021 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14022 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 14023 | otherwise. |
| 14024 | |
| 14025 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14026 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14027 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 14028 | PyObject **p_output, |
| 14029 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14030 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 14031 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14032 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14033 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14034 | int prec; |
| 14035 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14037 | x = PyFloat_AsDouble(v); |
| 14038 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14039 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 14040 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14041 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14042 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14043 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 14044 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14045 | if (arg->flags & F_ALT) |
| 14046 | dtoa_flags = Py_DTSF_ALT; |
| 14047 | else |
| 14048 | dtoa_flags = 0; |
| 14049 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 14050 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14051 | return -1; |
| 14052 | len = strlen(p); |
| 14053 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 14054 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 14055 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14056 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 14057 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14058 | } |
| 14059 | else |
| 14060 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 14061 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14062 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14063 | } |
| 14064 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14065 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 14066 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 14067 | * Python's regular ints. |
| 14068 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 14069 | * The output string is of the form |
| 14070 | * "-"? ("0x" | "0X")? digit+ |
| 14071 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 14072 | * set in flags. The case of hex digits will be correct, |
| 14073 | * There will be at least prec digits, zero-filled on the left if |
| 14074 | * necessary to get that many. |
| 14075 | * val object to be converted |
| 14076 | * flags bitmask of format flags; only F_ALT is looked at |
| 14077 | * prec minimum number of digits; 0-fill on left if needed |
| 14078 | * type a character in [duoxX]; u acts the same as d |
| 14079 | * |
| 14080 | * CAUTION: o, x and X conversions on regular ints can never |
| 14081 | * produce a '-' sign, but can for Python's unbounded ints. |
| 14082 | */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14083 | PyObject * |
| 14084 | _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14085 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14086 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14087 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14088 | Py_ssize_t i; |
| 14089 | int sign; /* 1 if '-', else 0 */ |
| 14090 | int len; /* number of characters */ |
| 14091 | Py_ssize_t llen; |
| 14092 | int numdigits; /* len == numnondigits + numdigits */ |
| 14093 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14094 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14095 | /* Avoid exceeding SSIZE_T_MAX */ |
| 14096 | if (prec > INT_MAX-3) { |
| 14097 | PyErr_SetString(PyExc_OverflowError, |
| 14098 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14099 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14100 | } |
| 14101 | |
| 14102 | assert(PyLong_Check(val)); |
| 14103 | |
| 14104 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14105 | default: |
| 14106 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14107 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14108 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14109 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 14110 | /* int and int subclasses should print numerically when a numeric */ |
| 14111 | /* format code is used (see issue18780) */ |
| 14112 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14113 | break; |
| 14114 | case 'o': |
| 14115 | numnondigits = 2; |
| 14116 | result = PyNumber_ToBase(val, 8); |
| 14117 | break; |
| 14118 | case 'x': |
| 14119 | case 'X': |
| 14120 | numnondigits = 2; |
| 14121 | result = PyNumber_ToBase(val, 16); |
| 14122 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14123 | } |
| 14124 | if (!result) |
| 14125 | return NULL; |
| 14126 | |
| 14127 | assert(unicode_modifiable(result)); |
| 14128 | assert(PyUnicode_IS_READY(result)); |
| 14129 | assert(PyUnicode_IS_ASCII(result)); |
| 14130 | |
| 14131 | /* To modify the string in-place, there can only be one reference. */ |
| 14132 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 14133 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14134 | PyErr_BadInternalCall(); |
| 14135 | return NULL; |
| 14136 | } |
| 14137 | buf = PyUnicode_DATA(result); |
| 14138 | llen = PyUnicode_GET_LENGTH(result); |
| 14139 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 14140 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14141 | PyErr_SetString(PyExc_ValueError, |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14142 | "string too large in _PyUnicode_FormatLong"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14143 | return NULL; |
| 14144 | } |
| 14145 | len = (int)llen; |
| 14146 | sign = buf[0] == '-'; |
| 14147 | numnondigits += sign; |
| 14148 | numdigits = len - numnondigits; |
| 14149 | assert(numdigits > 0); |
| 14150 | |
| 14151 | /* Get rid of base marker unless F_ALT */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14152 | if (((alt) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14153 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 14154 | assert(buf[sign] == '0'); |
| 14155 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 14156 | buf[sign+1] == 'o'); |
| 14157 | numnondigits -= 2; |
| 14158 | buf += 2; |
| 14159 | len -= 2; |
| 14160 | if (sign) |
| 14161 | buf[0] = '-'; |
| 14162 | assert(len == numnondigits + numdigits); |
| 14163 | assert(numdigits > 0); |
| 14164 | } |
| 14165 | |
| 14166 | /* Fill with leading zeroes to meet minimum width. */ |
| 14167 | if (prec > numdigits) { |
| 14168 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 14169 | numnondigits + prec); |
| 14170 | char *b1; |
| 14171 | if (!r1) { |
| 14172 | Py_DECREF(result); |
| 14173 | return NULL; |
| 14174 | } |
| 14175 | b1 = PyBytes_AS_STRING(r1); |
| 14176 | for (i = 0; i < numnondigits; ++i) |
| 14177 | *b1++ = *buf++; |
| 14178 | for (i = 0; i < prec - numdigits; i++) |
| 14179 | *b1++ = '0'; |
| 14180 | for (i = 0; i < numdigits; i++) |
| 14181 | *b1++ = *buf++; |
| 14182 | *b1 = '\0'; |
| 14183 | Py_DECREF(result); |
| 14184 | result = r1; |
| 14185 | buf = PyBytes_AS_STRING(result); |
| 14186 | len = numnondigits + prec; |
| 14187 | } |
| 14188 | |
| 14189 | /* Fix up case for hex conversions. */ |
| 14190 | if (type == 'X') { |
| 14191 | /* Need to convert all lower case letters to upper case. |
| 14192 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 14193 | for (i = 0; i < len; i++) |
| 14194 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 14195 | buf[i] -= 'a'-'A'; |
| 14196 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14197 | if (!PyUnicode_Check(result) |
| 14198 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14199 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14200 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14201 | Py_DECREF(result); |
| 14202 | result = unicode; |
| 14203 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14204 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 14205 | if (PyUnicode_Resize(&result, len) < 0) |
| 14206 | Py_CLEAR(result); |
| 14207 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14208 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14209 | } |
| 14210 | |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14211 | /* Format an integer or a float as an integer. |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14212 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14213 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14214 | * -1 and raise an exception on error */ |
| 14215 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14216 | mainformatlong(PyObject *v, |
| 14217 | struct unicode_format_arg_t *arg, |
| 14218 | PyObject **p_output, |
| 14219 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14220 | { |
| 14221 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14222 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14223 | |
| 14224 | if (!PyNumber_Check(v)) |
| 14225 | goto wrongtype; |
| 14226 | |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14227 | /* 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] | 14228 | if (!PyLong_Check(v)) { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14229 | if (type == 'o' || type == 'x' || type == 'X') { |
| 14230 | iobj = PyNumber_Index(v); |
| 14231 | if (iobj == NULL) { |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14232 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14233 | goto wrongtype; |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14234 | return -1; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14235 | } |
| 14236 | } |
| 14237 | else { |
| 14238 | iobj = PyNumber_Long(v); |
| 14239 | if (iobj == NULL ) { |
| 14240 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14241 | goto wrongtype; |
| 14242 | return -1; |
| 14243 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14244 | } |
| 14245 | assert(PyLong_Check(iobj)); |
| 14246 | } |
| 14247 | else { |
| 14248 | iobj = v; |
| 14249 | Py_INCREF(iobj); |
| 14250 | } |
| 14251 | |
| 14252 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14253 | && arg->width == -1 && arg->prec == -1 |
| 14254 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14255 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14256 | { |
| 14257 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14258 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14259 | int base; |
| 14260 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14261 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14262 | { |
| 14263 | default: |
| 14264 | assert(0 && "'type' not in [diuoxX]"); |
| 14265 | case 'd': |
| 14266 | case 'i': |
| 14267 | case 'u': |
| 14268 | base = 10; |
| 14269 | break; |
| 14270 | case 'o': |
| 14271 | base = 8; |
| 14272 | break; |
| 14273 | case 'x': |
| 14274 | case 'X': |
| 14275 | base = 16; |
| 14276 | break; |
| 14277 | } |
| 14278 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14279 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14280 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14281 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14282 | } |
| 14283 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14284 | return 1; |
| 14285 | } |
| 14286 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14287 | res = _PyUnicode_FormatLong(iobj, arg->flags & F_ALT, arg->prec, type); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14288 | Py_DECREF(iobj); |
| 14289 | if (res == NULL) |
| 14290 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14291 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14292 | return 0; |
| 14293 | |
| 14294 | wrongtype: |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14295 | switch(type) |
| 14296 | { |
| 14297 | case 'o': |
| 14298 | case 'x': |
| 14299 | case 'X': |
| 14300 | PyErr_Format(PyExc_TypeError, |
| 14301 | "%%%c format: an integer is required, " |
| 14302 | "not %.200s", |
| 14303 | type, Py_TYPE(v)->tp_name); |
| 14304 | break; |
| 14305 | default: |
| 14306 | PyErr_Format(PyExc_TypeError, |
| 14307 | "%%%c format: a number is required, " |
| 14308 | "not %.200s", |
| 14309 | type, Py_TYPE(v)->tp_name); |
| 14310 | break; |
| 14311 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14312 | return -1; |
| 14313 | } |
| 14314 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14315 | static Py_UCS4 |
| 14316 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14317 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14318 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14319 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14320 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14321 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14322 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14323 | goto onError; |
| 14324 | } |
| 14325 | else { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14326 | PyObject *iobj; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14327 | long x; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14328 | /* make sure number is a type of integer */ |
| 14329 | if (!PyLong_Check(v)) { |
| 14330 | iobj = PyNumber_Index(v); |
| 14331 | if (iobj == NULL) { |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14332 | goto onError; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14333 | } |
| 14334 | v = iobj; |
| 14335 | Py_DECREF(iobj); |
| 14336 | } |
| 14337 | /* Integer input truncated to a character */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14338 | x = PyLong_AsLong(v); |
| 14339 | if (x == -1 && PyErr_Occurred()) |
| 14340 | goto onError; |
| 14341 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14342 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14343 | PyErr_SetString(PyExc_OverflowError, |
| 14344 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14345 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14346 | } |
| 14347 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14348 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14349 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14350 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14351 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14352 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14353 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14354 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14355 | } |
| 14356 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14357 | /* Parse options of an argument: flags, width, precision. |
| 14358 | Handle also "%(name)" syntax. |
| 14359 | |
| 14360 | Return 0 if the argument has been formatted into arg->str. |
| 14361 | Return 1 if the argument has been written into ctx->writer, |
| 14362 | Raise an exception and return -1 on error. */ |
| 14363 | static int |
| 14364 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14365 | struct unicode_format_arg_t *arg) |
| 14366 | { |
| 14367 | #define FORMAT_READ(ctx) \ |
| 14368 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14369 | |
| 14370 | PyObject *v; |
| 14371 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14372 | if (arg->ch == '(') { |
| 14373 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14374 | Py_ssize_t keystart; |
| 14375 | Py_ssize_t keylen; |
| 14376 | PyObject *key; |
| 14377 | int pcount = 1; |
| 14378 | |
| 14379 | if (ctx->dict == NULL) { |
| 14380 | PyErr_SetString(PyExc_TypeError, |
| 14381 | "format requires a mapping"); |
| 14382 | return -1; |
| 14383 | } |
| 14384 | ++ctx->fmtpos; |
| 14385 | --ctx->fmtcnt; |
| 14386 | keystart = ctx->fmtpos; |
| 14387 | /* Skip over balanced parentheses */ |
| 14388 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14389 | arg->ch = FORMAT_READ(ctx); |
| 14390 | if (arg->ch == ')') |
| 14391 | --pcount; |
| 14392 | else if (arg->ch == '(') |
| 14393 | ++pcount; |
| 14394 | ctx->fmtpos++; |
| 14395 | } |
| 14396 | keylen = ctx->fmtpos - keystart - 1; |
| 14397 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14398 | PyErr_SetString(PyExc_ValueError, |
| 14399 | "incomplete format key"); |
| 14400 | return -1; |
| 14401 | } |
| 14402 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14403 | keystart, keystart + keylen); |
| 14404 | if (key == NULL) |
| 14405 | return -1; |
| 14406 | if (ctx->args_owned) { |
| 14407 | Py_DECREF(ctx->args); |
| 14408 | ctx->args_owned = 0; |
| 14409 | } |
| 14410 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14411 | Py_DECREF(key); |
| 14412 | if (ctx->args == NULL) |
| 14413 | return -1; |
| 14414 | ctx->args_owned = 1; |
| 14415 | ctx->arglen = -1; |
| 14416 | ctx->argidx = -2; |
| 14417 | } |
| 14418 | |
| 14419 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14420 | while (--ctx->fmtcnt >= 0) { |
| 14421 | arg->ch = FORMAT_READ(ctx); |
| 14422 | ctx->fmtpos++; |
| 14423 | switch (arg->ch) { |
| 14424 | case '-': arg->flags |= F_LJUST; continue; |
| 14425 | case '+': arg->flags |= F_SIGN; continue; |
| 14426 | case ' ': arg->flags |= F_BLANK; continue; |
| 14427 | case '#': arg->flags |= F_ALT; continue; |
| 14428 | case '0': arg->flags |= F_ZERO; continue; |
| 14429 | } |
| 14430 | break; |
| 14431 | } |
| 14432 | |
| 14433 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14434 | if (arg->ch == '*') { |
| 14435 | v = unicode_format_getnextarg(ctx); |
| 14436 | if (v == NULL) |
| 14437 | return -1; |
| 14438 | if (!PyLong_Check(v)) { |
| 14439 | PyErr_SetString(PyExc_TypeError, |
| 14440 | "* wants int"); |
| 14441 | return -1; |
| 14442 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14443 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14444 | if (arg->width == -1 && PyErr_Occurred()) |
| 14445 | return -1; |
| 14446 | if (arg->width < 0) { |
| 14447 | arg->flags |= F_LJUST; |
| 14448 | arg->width = -arg->width; |
| 14449 | } |
| 14450 | if (--ctx->fmtcnt >= 0) { |
| 14451 | arg->ch = FORMAT_READ(ctx); |
| 14452 | ctx->fmtpos++; |
| 14453 | } |
| 14454 | } |
| 14455 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14456 | arg->width = arg->ch - '0'; |
| 14457 | while (--ctx->fmtcnt >= 0) { |
| 14458 | arg->ch = FORMAT_READ(ctx); |
| 14459 | ctx->fmtpos++; |
| 14460 | if (arg->ch < '0' || arg->ch > '9') |
| 14461 | break; |
| 14462 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14463 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14464 | '0' and '9', casting to int is safe. */ |
| 14465 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14466 | PyErr_SetString(PyExc_ValueError, |
| 14467 | "width too big"); |
| 14468 | return -1; |
| 14469 | } |
| 14470 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14471 | } |
| 14472 | } |
| 14473 | |
| 14474 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14475 | if (arg->ch == '.') { |
| 14476 | arg->prec = 0; |
| 14477 | if (--ctx->fmtcnt >= 0) { |
| 14478 | arg->ch = FORMAT_READ(ctx); |
| 14479 | ctx->fmtpos++; |
| 14480 | } |
| 14481 | if (arg->ch == '*') { |
| 14482 | v = unicode_format_getnextarg(ctx); |
| 14483 | if (v == NULL) |
| 14484 | return -1; |
| 14485 | if (!PyLong_Check(v)) { |
| 14486 | PyErr_SetString(PyExc_TypeError, |
| 14487 | "* wants int"); |
| 14488 | return -1; |
| 14489 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14490 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14491 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14492 | return -1; |
| 14493 | if (arg->prec < 0) |
| 14494 | arg->prec = 0; |
| 14495 | if (--ctx->fmtcnt >= 0) { |
| 14496 | arg->ch = FORMAT_READ(ctx); |
| 14497 | ctx->fmtpos++; |
| 14498 | } |
| 14499 | } |
| 14500 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14501 | arg->prec = arg->ch - '0'; |
| 14502 | while (--ctx->fmtcnt >= 0) { |
| 14503 | arg->ch = FORMAT_READ(ctx); |
| 14504 | ctx->fmtpos++; |
| 14505 | if (arg->ch < '0' || arg->ch > '9') |
| 14506 | break; |
| 14507 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14508 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14509 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14510 | return -1; |
| 14511 | } |
| 14512 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14513 | } |
| 14514 | } |
| 14515 | } |
| 14516 | |
| 14517 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14518 | if (ctx->fmtcnt >= 0) { |
| 14519 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14520 | if (--ctx->fmtcnt >= 0) { |
| 14521 | arg->ch = FORMAT_READ(ctx); |
| 14522 | ctx->fmtpos++; |
| 14523 | } |
| 14524 | } |
| 14525 | } |
| 14526 | if (ctx->fmtcnt < 0) { |
| 14527 | PyErr_SetString(PyExc_ValueError, |
| 14528 | "incomplete format"); |
| 14529 | return -1; |
| 14530 | } |
| 14531 | return 0; |
| 14532 | |
| 14533 | #undef FORMAT_READ |
| 14534 | } |
| 14535 | |
| 14536 | /* Format one argument. Supported conversion specifiers: |
| 14537 | |
| 14538 | - "s", "r", "a": any type |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14539 | - "i", "d", "u": int or float |
| 14540 | - "o", "x", "X": int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14541 | - "e", "E", "f", "F", "g", "G": float |
| 14542 | - "c": int or str (1 character) |
| 14543 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14544 | When possible, the output is written directly into the Unicode writer |
| 14545 | (ctx->writer). A string is created when padding is required. |
| 14546 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14547 | Return 0 if the argument has been formatted into *p_str, |
| 14548 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14549 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14550 | static int |
| 14551 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14552 | struct unicode_format_arg_t *arg, |
| 14553 | PyObject **p_str) |
| 14554 | { |
| 14555 | PyObject *v; |
| 14556 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14557 | |
| 14558 | if (ctx->fmtcnt == 0) |
| 14559 | ctx->writer.overallocate = 0; |
| 14560 | |
| 14561 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14562 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14563 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14564 | return 1; |
| 14565 | } |
| 14566 | |
| 14567 | v = unicode_format_getnextarg(ctx); |
| 14568 | if (v == NULL) |
| 14569 | return -1; |
| 14570 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14571 | |
| 14572 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14573 | case 's': |
| 14574 | case 'r': |
| 14575 | case 'a': |
| 14576 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14577 | /* Fast path */ |
| 14578 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14579 | return -1; |
| 14580 | return 1; |
| 14581 | } |
| 14582 | |
| 14583 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14584 | *p_str = v; |
| 14585 | Py_INCREF(*p_str); |
| 14586 | } |
| 14587 | else { |
| 14588 | if (arg->ch == 's') |
| 14589 | *p_str = PyObject_Str(v); |
| 14590 | else if (arg->ch == 'r') |
| 14591 | *p_str = PyObject_Repr(v); |
| 14592 | else |
| 14593 | *p_str = PyObject_ASCII(v); |
| 14594 | } |
| 14595 | break; |
| 14596 | |
| 14597 | case 'i': |
| 14598 | case 'd': |
| 14599 | case 'u': |
| 14600 | case 'o': |
| 14601 | case 'x': |
| 14602 | case 'X': |
| 14603 | { |
| 14604 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14605 | if (ret != 0) |
| 14606 | return ret; |
| 14607 | arg->sign = 1; |
| 14608 | break; |
| 14609 | } |
| 14610 | |
| 14611 | case 'e': |
| 14612 | case 'E': |
| 14613 | case 'f': |
| 14614 | case 'F': |
| 14615 | case 'g': |
| 14616 | case 'G': |
| 14617 | if (arg->width == -1 && arg->prec == -1 |
| 14618 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14619 | { |
| 14620 | /* Fast path */ |
| 14621 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14622 | return -1; |
| 14623 | return 1; |
| 14624 | } |
| 14625 | |
| 14626 | arg->sign = 1; |
| 14627 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14628 | return -1; |
| 14629 | break; |
| 14630 | |
| 14631 | case 'c': |
| 14632 | { |
| 14633 | Py_UCS4 ch = formatchar(v); |
| 14634 | if (ch == (Py_UCS4) -1) |
| 14635 | return -1; |
| 14636 | if (arg->width == -1 && arg->prec == -1) { |
| 14637 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14638 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14639 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14640 | return 1; |
| 14641 | } |
| 14642 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14643 | break; |
| 14644 | } |
| 14645 | |
| 14646 | default: |
| 14647 | PyErr_Format(PyExc_ValueError, |
| 14648 | "unsupported format character '%c' (0x%x) " |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 14649 | "at index %zd", |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14650 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14651 | (int)arg->ch, |
| 14652 | ctx->fmtpos - 1); |
| 14653 | return -1; |
| 14654 | } |
| 14655 | if (*p_str == NULL) |
| 14656 | return -1; |
| 14657 | assert (PyUnicode_Check(*p_str)); |
| 14658 | return 0; |
| 14659 | } |
| 14660 | |
| 14661 | static int |
| 14662 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14663 | struct unicode_format_arg_t *arg, |
| 14664 | PyObject *str) |
| 14665 | { |
| 14666 | Py_ssize_t len; |
| 14667 | enum PyUnicode_Kind kind; |
| 14668 | void *pbuf; |
| 14669 | Py_ssize_t pindex; |
| 14670 | Py_UCS4 signchar; |
| 14671 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14672 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14673 | Py_ssize_t sublen; |
| 14674 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14675 | Py_UCS4 fill; |
| 14676 | |
| 14677 | fill = ' '; |
| 14678 | if (arg->sign && arg->flags & F_ZERO) |
| 14679 | fill = '0'; |
| 14680 | |
| 14681 | if (PyUnicode_READY(str) == -1) |
| 14682 | return -1; |
| 14683 | |
| 14684 | len = PyUnicode_GET_LENGTH(str); |
| 14685 | if ((arg->width == -1 || arg->width <= len) |
| 14686 | && (arg->prec == -1 || arg->prec >= len) |
| 14687 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14688 | { |
| 14689 | /* Fast path */ |
| 14690 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14691 | return -1; |
| 14692 | return 0; |
| 14693 | } |
| 14694 | |
| 14695 | /* Truncate the string for "s", "r" and "a" formats |
| 14696 | if the precision is set */ |
| 14697 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14698 | if (arg->prec >= 0 && len > arg->prec) |
| 14699 | len = arg->prec; |
| 14700 | } |
| 14701 | |
| 14702 | /* Adjust sign and width */ |
| 14703 | kind = PyUnicode_KIND(str); |
| 14704 | pbuf = PyUnicode_DATA(str); |
| 14705 | pindex = 0; |
| 14706 | signchar = '\0'; |
| 14707 | if (arg->sign) { |
| 14708 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14709 | if (ch == '-' || ch == '+') { |
| 14710 | signchar = ch; |
| 14711 | len--; |
| 14712 | pindex++; |
| 14713 | } |
| 14714 | else if (arg->flags & F_SIGN) |
| 14715 | signchar = '+'; |
| 14716 | else if (arg->flags & F_BLANK) |
| 14717 | signchar = ' '; |
| 14718 | else |
| 14719 | arg->sign = 0; |
| 14720 | } |
| 14721 | if (arg->width < len) |
| 14722 | arg->width = len; |
| 14723 | |
| 14724 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14725 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14726 | if (!(arg->flags & F_LJUST)) { |
| 14727 | if (arg->sign) { |
| 14728 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14729 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14730 | } |
| 14731 | else { |
| 14732 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14733 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14734 | } |
| 14735 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14736 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14737 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14738 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14739 | } |
| 14740 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14741 | buflen = arg->width; |
| 14742 | if (arg->sign && len == arg->width) |
| 14743 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14744 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14745 | return -1; |
| 14746 | |
| 14747 | /* Write the sign if needed */ |
| 14748 | if (arg->sign) { |
| 14749 | if (fill != ' ') { |
| 14750 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14751 | writer->pos += 1; |
| 14752 | } |
| 14753 | if (arg->width > len) |
| 14754 | arg->width--; |
| 14755 | } |
| 14756 | |
| 14757 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14758 | if the alternate form is used. |
| 14759 | For example, write "0x" for the "%#x" format. */ |
| 14760 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14761 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14762 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14763 | if (fill != ' ') { |
| 14764 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14765 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14766 | writer->pos += 2; |
| 14767 | pindex += 2; |
| 14768 | } |
| 14769 | arg->width -= 2; |
| 14770 | if (arg->width < 0) |
| 14771 | arg->width = 0; |
| 14772 | len -= 2; |
| 14773 | } |
| 14774 | |
| 14775 | /* Pad left with the fill character if needed */ |
| 14776 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14777 | sublen = arg->width - len; |
| 14778 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14779 | writer->pos += sublen; |
| 14780 | arg->width = len; |
| 14781 | } |
| 14782 | |
| 14783 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14784 | the alternate form is used */ |
| 14785 | if (fill == ' ') { |
| 14786 | if (arg->sign) { |
| 14787 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14788 | writer->pos += 1; |
| 14789 | } |
| 14790 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14791 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14792 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14793 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14794 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14795 | writer->pos += 2; |
| 14796 | pindex += 2; |
| 14797 | } |
| 14798 | } |
| 14799 | |
| 14800 | /* Write characters */ |
| 14801 | if (len) { |
| 14802 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14803 | str, pindex, len); |
| 14804 | writer->pos += len; |
| 14805 | } |
| 14806 | |
| 14807 | /* Pad right with the fill character if needed */ |
| 14808 | if (arg->width > len) { |
| 14809 | sublen = arg->width - len; |
| 14810 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14811 | writer->pos += sublen; |
| 14812 | } |
| 14813 | return 0; |
| 14814 | } |
| 14815 | |
| 14816 | /* Helper of PyUnicode_Format(): format one arg. |
| 14817 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14818 | static int |
| 14819 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14820 | { |
| 14821 | struct unicode_format_arg_t arg; |
| 14822 | PyObject *str; |
| 14823 | int ret; |
| 14824 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14825 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14826 | arg.flags = 0; |
| 14827 | arg.width = -1; |
| 14828 | arg.prec = -1; |
| 14829 | arg.sign = 0; |
| 14830 | str = NULL; |
| 14831 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14832 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14833 | if (ret == -1) |
| 14834 | return -1; |
| 14835 | |
| 14836 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14837 | if (ret == -1) |
| 14838 | return -1; |
| 14839 | |
| 14840 | if (ret != 1) { |
| 14841 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14842 | Py_DECREF(str); |
| 14843 | if (ret == -1) |
| 14844 | return -1; |
| 14845 | } |
| 14846 | |
| 14847 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14848 | PyErr_SetString(PyExc_TypeError, |
| 14849 | "not all arguments converted during string formatting"); |
| 14850 | return -1; |
| 14851 | } |
| 14852 | return 0; |
| 14853 | } |
| 14854 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14855 | PyObject * |
| 14856 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14857 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14858 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14860 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14861 | PyErr_BadInternalCall(); |
| 14862 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14863 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14864 | |
| 14865 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14866 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14867 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14868 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14869 | Py_DECREF(ctx.fmtstr); |
| 14870 | return NULL; |
| 14871 | } |
| 14872 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14873 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14874 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14875 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14876 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14877 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14878 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14879 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14881 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14882 | ctx.arglen = PyTuple_Size(args); |
| 14883 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14884 | } |
| 14885 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14886 | ctx.arglen = -1; |
| 14887 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14888 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14889 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14890 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14891 | ctx.dict = args; |
| 14892 | else |
| 14893 | ctx.dict = NULL; |
| 14894 | ctx.args = 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 | while (--ctx.fmtcnt >= 0) { |
| 14897 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14898 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14899 | |
| 14900 | nonfmtpos = ctx.fmtpos++; |
| 14901 | while (ctx.fmtcnt >= 0 && |
| 14902 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14903 | ctx.fmtpos++; |
| 14904 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14905 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14906 | if (ctx.fmtcnt < 0) { |
| 14907 | ctx.fmtpos--; |
| 14908 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14909 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14910 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14911 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14912 | nonfmtpos, ctx.fmtpos) < 0) |
| 14913 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14914 | } |
| 14915 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14916 | ctx.fmtpos++; |
| 14917 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14918 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14919 | } |
| 14920 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14921 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14922 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14923 | PyErr_SetString(PyExc_TypeError, |
| 14924 | "not all arguments converted during string formatting"); |
| 14925 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14926 | } |
| 14927 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14928 | if (ctx.args_owned) { |
| 14929 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14930 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14931 | Py_DECREF(ctx.fmtstr); |
| 14932 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14933 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14934 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14935 | Py_DECREF(ctx.fmtstr); |
| 14936 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14937 | if (ctx.args_owned) { |
| 14938 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14939 | } |
| 14940 | return NULL; |
| 14941 | } |
| 14942 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14943 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14944 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14945 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14946 | static PyObject * |
| 14947 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14948 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14949 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14950 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14951 | char *encoding = NULL; |
| 14952 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14953 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14954 | if (type != &PyUnicode_Type) |
| 14955 | return unicode_subtype_new(type, args, kwds); |
| 14956 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14957 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14958 | return NULL; |
| 14959 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14960 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14961 | if (encoding == NULL && errors == NULL) |
| 14962 | return PyObject_Str(x); |
| 14963 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14964 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14965 | } |
| 14966 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14967 | static PyObject * |
| 14968 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14969 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14970 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14971 | Py_ssize_t length, char_size; |
| 14972 | int share_wstr, share_utf8; |
| 14973 | unsigned int kind; |
| 14974 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14975 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14976 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14977 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14978 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14979 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14980 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14981 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14982 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14983 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14984 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14985 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14986 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14987 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14988 | if (self == NULL) { |
| 14989 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14990 | return NULL; |
| 14991 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14992 | kind = PyUnicode_KIND(unicode); |
| 14993 | length = PyUnicode_GET_LENGTH(unicode); |
| 14994 | |
| 14995 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14996 | #ifdef Py_DEBUG |
| 14997 | _PyUnicode_HASH(self) = -1; |
| 14998 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14999 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 15000 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15001 | _PyUnicode_STATE(self).interned = 0; |
| 15002 | _PyUnicode_STATE(self).kind = kind; |
| 15003 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 15004 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15005 | _PyUnicode_STATE(self).ready = 1; |
| 15006 | _PyUnicode_WSTR(self) = NULL; |
| 15007 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 15008 | _PyUnicode_UTF8(self) = NULL; |
| 15009 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 15010 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15011 | |
| 15012 | share_utf8 = 0; |
| 15013 | share_wstr = 0; |
| 15014 | if (kind == PyUnicode_1BYTE_KIND) { |
| 15015 | char_size = 1; |
| 15016 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 15017 | share_utf8 = 1; |
| 15018 | } |
| 15019 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 15020 | char_size = 2; |
| 15021 | if (sizeof(wchar_t) == 2) |
| 15022 | share_wstr = 1; |
| 15023 | } |
| 15024 | else { |
| 15025 | assert(kind == PyUnicode_4BYTE_KIND); |
| 15026 | char_size = 4; |
| 15027 | if (sizeof(wchar_t) == 4) |
| 15028 | share_wstr = 1; |
| 15029 | } |
| 15030 | |
| 15031 | /* Ensure we won't overflow the length. */ |
| 15032 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 15033 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15034 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15035 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15036 | data = PyObject_MALLOC((length + 1) * char_size); |
| 15037 | if (data == NULL) { |
| 15038 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15039 | goto onError; |
| 15040 | } |
| 15041 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 15042 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15043 | if (share_utf8) { |
| 15044 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 15045 | _PyUnicode_UTF8(self) = data; |
| 15046 | } |
| 15047 | if (share_wstr) { |
| 15048 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 15049 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 15050 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15051 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15052 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 15053 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 15054 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 15055 | #ifdef Py_DEBUG |
| 15056 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 15057 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 15058 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15059 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 15060 | |
| 15061 | onError: |
| 15062 | Py_DECREF(unicode); |
| 15063 | Py_DECREF(self); |
| 15064 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 15065 | } |
| 15066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 15067 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 15068 | "str(object='') -> str\n\ |
| 15069 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 15070 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 15071 | Create a new string object from the given object. If encoding or\n\ |
| 15072 | errors is specified, then the object must expose a data buffer\n\ |
| 15073 | that will be decoded using the given encoding and error handler.\n\ |
| 15074 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 15075 | or repr(object).\n\ |
| 15076 | encoding defaults to sys.getdefaultencoding().\n\ |
| 15077 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 15078 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15079 | static PyObject *unicode_iter(PyObject *seq); |
| 15080 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15081 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 15082 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15083 | "str", /* tp_name */ |
| 15084 | sizeof(PyUnicodeObject), /* tp_size */ |
| 15085 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15086 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15087 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 15088 | 0, /* tp_print */ |
| 15089 | 0, /* tp_getattr */ |
| 15090 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15091 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15092 | unicode_repr, /* tp_repr */ |
| 15093 | &unicode_as_number, /* tp_as_number */ |
| 15094 | &unicode_as_sequence, /* tp_as_sequence */ |
| 15095 | &unicode_as_mapping, /* tp_as_mapping */ |
| 15096 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 15097 | 0, /* tp_call*/ |
| 15098 | (reprfunc) unicode_str, /* tp_str */ |
| 15099 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15100 | 0, /* tp_setattro */ |
| 15101 | 0, /* tp_as_buffer */ |
| 15102 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15103 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15104 | unicode_doc, /* tp_doc */ |
| 15105 | 0, /* tp_traverse */ |
| 15106 | 0, /* tp_clear */ |
| 15107 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 15108 | 0, /* tp_weaklistoffset */ |
| 15109 | unicode_iter, /* tp_iter */ |
| 15110 | 0, /* tp_iternext */ |
| 15111 | unicode_methods, /* tp_methods */ |
| 15112 | 0, /* tp_members */ |
| 15113 | 0, /* tp_getset */ |
| 15114 | &PyBaseObject_Type, /* tp_base */ |
| 15115 | 0, /* tp_dict */ |
| 15116 | 0, /* tp_descr_get */ |
| 15117 | 0, /* tp_descr_set */ |
| 15118 | 0, /* tp_dictoffset */ |
| 15119 | 0, /* tp_init */ |
| 15120 | 0, /* tp_alloc */ |
| 15121 | unicode_new, /* tp_new */ |
| 15122 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15123 | }; |
| 15124 | |
| 15125 | /* Initialize the Unicode implementation */ |
| 15126 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15127 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15128 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15129 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15130 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15131 | 0x000A, /* LINE FEED */ |
| 15132 | 0x000D, /* CARRIAGE RETURN */ |
| 15133 | 0x001C, /* FILE SEPARATOR */ |
| 15134 | 0x001D, /* GROUP SEPARATOR */ |
| 15135 | 0x001E, /* RECORD SEPARATOR */ |
| 15136 | 0x0085, /* NEXT LINE */ |
| 15137 | 0x2028, /* LINE SEPARATOR */ |
| 15138 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 15139 | }; |
| 15140 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 15141 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 15142 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 15143 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15144 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 15145 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 15146 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 15147 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15148 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15149 | |
| 15150 | /* initialize the linebreak bloom filter */ |
| 15151 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15152 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 15153 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 15154 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 15155 | if (PyType_Ready(&EncodingMapType) < 0) |
| 15156 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15157 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 15158 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 15159 | Py_FatalError("Can't initialize field name iterator type"); |
| 15160 | |
| 15161 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 15162 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 15163 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15164 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15165 | } |
| 15166 | |
| 15167 | /* Finalize the Unicode implementation */ |
| 15168 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15169 | int |
| 15170 | PyUnicode_ClearFreeList(void) |
| 15171 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15172 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15173 | } |
| 15174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15175 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 15176 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15177 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 15178 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15179 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15180 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 15181 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15182 | for (i = 0; i < 256; i++) |
| 15183 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 15184 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15185 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15186 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 15187 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15188 | void |
| 15189 | PyUnicode_InternInPlace(PyObject **p) |
| 15190 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15191 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15192 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15193 | #ifdef Py_DEBUG |
| 15194 | assert(s != NULL); |
| 15195 | assert(_PyUnicode_CHECK(s)); |
| 15196 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15197 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15198 | return; |
| 15199 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15200 | /* If it's a subclass, we don't really know what putting |
| 15201 | it in the interned dict might do. */ |
| 15202 | if (!PyUnicode_CheckExact(s)) |
| 15203 | return; |
| 15204 | if (PyUnicode_CHECK_INTERNED(s)) |
| 15205 | return; |
| 15206 | if (interned == NULL) { |
| 15207 | interned = PyDict_New(); |
| 15208 | if (interned == NULL) { |
| 15209 | PyErr_Clear(); /* Don't leave an exception */ |
| 15210 | return; |
| 15211 | } |
| 15212 | } |
| 15213 | /* It might be that the GetItem call fails even |
| 15214 | though the key is present in the dictionary, |
| 15215 | namely when this happens during a stack overflow. */ |
| 15216 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15217 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15218 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15219 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15220 | if (t) { |
| 15221 | Py_INCREF(t); |
| 15222 | Py_DECREF(*p); |
| 15223 | *p = t; |
| 15224 | return; |
| 15225 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15226 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15227 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15228 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15229 | PyErr_Clear(); |
| 15230 | PyThreadState_GET()->recursion_critical = 0; |
| 15231 | return; |
| 15232 | } |
| 15233 | PyThreadState_GET()->recursion_critical = 0; |
| 15234 | /* The two references in interned are not counted by refcnt. |
| 15235 | The deallocator will take care of this */ |
| 15236 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15237 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15238 | } |
| 15239 | |
| 15240 | void |
| 15241 | PyUnicode_InternImmortal(PyObject **p) |
| 15242 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15243 | PyUnicode_InternInPlace(p); |
| 15244 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 15245 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15246 | Py_INCREF(*p); |
| 15247 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15248 | } |
| 15249 | |
| 15250 | PyObject * |
| 15251 | PyUnicode_InternFromString(const char *cp) |
| 15252 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15253 | PyObject *s = PyUnicode_FromString(cp); |
| 15254 | if (s == NULL) |
| 15255 | return NULL; |
| 15256 | PyUnicode_InternInPlace(&s); |
| 15257 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15258 | } |
| 15259 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15260 | void |
| 15261 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15262 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15263 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15264 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15265 | Py_ssize_t i, n; |
| 15266 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15267 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15268 | if (interned == NULL || !PyDict_Check(interned)) |
| 15269 | return; |
| 15270 | keys = PyDict_Keys(interned); |
| 15271 | if (keys == NULL || !PyList_Check(keys)) { |
| 15272 | PyErr_Clear(); |
| 15273 | return; |
| 15274 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15275 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15276 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15277 | detector, interned unicode strings are not forcibly deallocated; |
| 15278 | rather, we give them their stolen references back, and then clear |
| 15279 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15280 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15281 | n = PyList_GET_SIZE(keys); |
| 15282 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15283 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15284 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15285 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15286 | if (PyUnicode_READY(s) == -1) { |
| 15287 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15288 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15289 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15290 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15291 | case SSTATE_NOT_INTERNED: |
| 15292 | /* XXX Shouldn't happen */ |
| 15293 | break; |
| 15294 | case SSTATE_INTERNED_IMMORTAL: |
| 15295 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15296 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15297 | break; |
| 15298 | case SSTATE_INTERNED_MORTAL: |
| 15299 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15300 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15301 | break; |
| 15302 | default: |
| 15303 | Py_FatalError("Inconsistent interned string state."); |
| 15304 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15305 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15306 | } |
| 15307 | fprintf(stderr, "total size of all interned strings: " |
| 15308 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15309 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15310 | Py_DECREF(keys); |
| 15311 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15312 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15313 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15314 | |
| 15315 | |
| 15316 | /********************* Unicode Iterator **************************/ |
| 15317 | |
| 15318 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15319 | PyObject_HEAD |
| 15320 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15321 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15322 | } unicodeiterobject; |
| 15323 | |
| 15324 | static void |
| 15325 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15326 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15327 | _PyObject_GC_UNTRACK(it); |
| 15328 | Py_XDECREF(it->it_seq); |
| 15329 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15330 | } |
| 15331 | |
| 15332 | static int |
| 15333 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15334 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15335 | Py_VISIT(it->it_seq); |
| 15336 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15337 | } |
| 15338 | |
| 15339 | static PyObject * |
| 15340 | unicodeiter_next(unicodeiterobject *it) |
| 15341 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15342 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15343 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15344 | assert(it != NULL); |
| 15345 | seq = it->it_seq; |
| 15346 | if (seq == NULL) |
| 15347 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15348 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15349 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15350 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15351 | int kind = PyUnicode_KIND(seq); |
| 15352 | void *data = PyUnicode_DATA(seq); |
| 15353 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15354 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15355 | if (item != NULL) |
| 15356 | ++it->it_index; |
| 15357 | return item; |
| 15358 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15359 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15360 | Py_DECREF(seq); |
| 15361 | it->it_seq = NULL; |
| 15362 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15363 | } |
| 15364 | |
| 15365 | static PyObject * |
| 15366 | unicodeiter_len(unicodeiterobject *it) |
| 15367 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15368 | Py_ssize_t len = 0; |
| 15369 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15370 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15371 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15372 | } |
| 15373 | |
| 15374 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15375 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15376 | static PyObject * |
| 15377 | unicodeiter_reduce(unicodeiterobject *it) |
| 15378 | { |
| 15379 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15380 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15381 | it->it_seq, it->it_index); |
| 15382 | } else { |
| 15383 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15384 | if (u == NULL) |
| 15385 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15386 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15387 | } |
| 15388 | } |
| 15389 | |
| 15390 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15391 | |
| 15392 | static PyObject * |
| 15393 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15394 | { |
| 15395 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15396 | if (index == -1 && PyErr_Occurred()) |
| 15397 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 15398 | if (it->it_seq != NULL) { |
| 15399 | if (index < 0) |
| 15400 | index = 0; |
| 15401 | else if (index > PyUnicode_GET_LENGTH(it->it_seq)) |
| 15402 | index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */ |
| 15403 | it->it_index = index; |
| 15404 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15405 | Py_RETURN_NONE; |
| 15406 | } |
| 15407 | |
| 15408 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15409 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15410 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15411 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15412 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15413 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15414 | reduce_doc}, |
| 15415 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15416 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15417 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15418 | }; |
| 15419 | |
| 15420 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15421 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15422 | "str_iterator", /* tp_name */ |
| 15423 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15424 | 0, /* tp_itemsize */ |
| 15425 | /* methods */ |
| 15426 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15427 | 0, /* tp_print */ |
| 15428 | 0, /* tp_getattr */ |
| 15429 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15430 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15431 | 0, /* tp_repr */ |
| 15432 | 0, /* tp_as_number */ |
| 15433 | 0, /* tp_as_sequence */ |
| 15434 | 0, /* tp_as_mapping */ |
| 15435 | 0, /* tp_hash */ |
| 15436 | 0, /* tp_call */ |
| 15437 | 0, /* tp_str */ |
| 15438 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15439 | 0, /* tp_setattro */ |
| 15440 | 0, /* tp_as_buffer */ |
| 15441 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15442 | 0, /* tp_doc */ |
| 15443 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15444 | 0, /* tp_clear */ |
| 15445 | 0, /* tp_richcompare */ |
| 15446 | 0, /* tp_weaklistoffset */ |
| 15447 | PyObject_SelfIter, /* tp_iter */ |
| 15448 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15449 | unicodeiter_methods, /* tp_methods */ |
| 15450 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15451 | }; |
| 15452 | |
| 15453 | static PyObject * |
| 15454 | unicode_iter(PyObject *seq) |
| 15455 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15456 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15457 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15458 | if (!PyUnicode_Check(seq)) { |
| 15459 | PyErr_BadInternalCall(); |
| 15460 | return NULL; |
| 15461 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15462 | if (PyUnicode_READY(seq) == -1) |
| 15463 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15464 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15465 | if (it == NULL) |
| 15466 | return NULL; |
| 15467 | it->it_index = 0; |
| 15468 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15469 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15470 | _PyObject_GC_TRACK(it); |
| 15471 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15472 | } |
| 15473 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15474 | |
| 15475 | size_t |
| 15476 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15477 | { |
| 15478 | int res = 0; |
| 15479 | while(*u++) |
| 15480 | res++; |
| 15481 | return res; |
| 15482 | } |
| 15483 | |
| 15484 | Py_UNICODE* |
| 15485 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15486 | { |
| 15487 | Py_UNICODE *u = s1; |
| 15488 | while ((*u++ = *s2++)); |
| 15489 | return s1; |
| 15490 | } |
| 15491 | |
| 15492 | Py_UNICODE* |
| 15493 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15494 | { |
| 15495 | Py_UNICODE *u = s1; |
| 15496 | while ((*u++ = *s2++)) |
| 15497 | if (n-- == 0) |
| 15498 | break; |
| 15499 | return s1; |
| 15500 | } |
| 15501 | |
| 15502 | Py_UNICODE* |
| 15503 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15504 | { |
| 15505 | Py_UNICODE *u1 = s1; |
| 15506 | u1 += Py_UNICODE_strlen(u1); |
| 15507 | Py_UNICODE_strcpy(u1, s2); |
| 15508 | return s1; |
| 15509 | } |
| 15510 | |
| 15511 | int |
| 15512 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15513 | { |
| 15514 | while (*s1 && *s2 && *s1 == *s2) |
| 15515 | s1++, s2++; |
| 15516 | if (*s1 && *s2) |
| 15517 | return (*s1 < *s2) ? -1 : +1; |
| 15518 | if (*s1) |
| 15519 | return 1; |
| 15520 | if (*s2) |
| 15521 | return -1; |
| 15522 | return 0; |
| 15523 | } |
| 15524 | |
| 15525 | int |
| 15526 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15527 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15528 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15529 | for (; n != 0; n--) { |
| 15530 | u1 = *s1; |
| 15531 | u2 = *s2; |
| 15532 | if (u1 != u2) |
| 15533 | return (u1 < u2) ? -1 : +1; |
| 15534 | if (u1 == '\0') |
| 15535 | return 0; |
| 15536 | s1++; |
| 15537 | s2++; |
| 15538 | } |
| 15539 | return 0; |
| 15540 | } |
| 15541 | |
| 15542 | Py_UNICODE* |
| 15543 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15544 | { |
| 15545 | const Py_UNICODE *p; |
| 15546 | for (p = s; *p; p++) |
| 15547 | if (*p == c) |
| 15548 | return (Py_UNICODE*)p; |
| 15549 | return NULL; |
| 15550 | } |
| 15551 | |
| 15552 | Py_UNICODE* |
| 15553 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15554 | { |
| 15555 | const Py_UNICODE *p; |
| 15556 | p = s + Py_UNICODE_strlen(s); |
| 15557 | while (p != s) { |
| 15558 | p--; |
| 15559 | if (*p == c) |
| 15560 | return (Py_UNICODE*)p; |
| 15561 | } |
| 15562 | return NULL; |
| 15563 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15564 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15565 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15566 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15567 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15568 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15569 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15570 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15571 | if (!PyUnicode_Check(unicode)) { |
| 15572 | PyErr_BadArgument(); |
| 15573 | return NULL; |
| 15574 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15575 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15576 | if (u == NULL) |
| 15577 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15578 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 15579 | 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] | 15580 | PyErr_NoMemory(); |
| 15581 | return NULL; |
| 15582 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15583 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15584 | size *= sizeof(Py_UNICODE); |
| 15585 | copy = PyMem_Malloc(size); |
| 15586 | if (copy == NULL) { |
| 15587 | PyErr_NoMemory(); |
| 15588 | return NULL; |
| 15589 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15590 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15591 | return copy; |
| 15592 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15593 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15594 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15595 | to the string.Formatter class implemented in Python. */ |
| 15596 | |
| 15597 | static PyMethodDef _string_methods[] = { |
| 15598 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15599 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15600 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15601 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15602 | {NULL, NULL} |
| 15603 | }; |
| 15604 | |
| 15605 | static struct PyModuleDef _string_module = { |
| 15606 | PyModuleDef_HEAD_INIT, |
| 15607 | "_string", |
| 15608 | PyDoc_STR("string helper module"), |
| 15609 | 0, |
| 15610 | _string_methods, |
| 15611 | NULL, |
| 15612 | NULL, |
| 15613 | NULL, |
| 15614 | NULL |
| 15615 | }; |
| 15616 | |
| 15617 | PyMODINIT_FUNC |
| 15618 | PyInit__string(void) |
| 15619 | { |
| 15620 | return PyModule_Create(&_string_module); |
| 15621 | } |
| 15622 | |
| 15623 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15624 | #ifdef __cplusplus |
| 15625 | } |
| 15626 | #endif |