Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Benjamin Peterson | 31616ea | 2011-10-01 00:11:09 -0400 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com>. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | Major speed upgrades to the method implementations at the Reykjavik |
| 7 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 8 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 11 | -------------------------------------------------------------------- |
| 12 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14 | Copyright (c) 1999 by Secret Labs AB |
| 15 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 16 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 17 | By obtaining, using, and/or copying this software and/or its |
| 18 | associated documentation, you agree that you have read, understood, |
| 19 | and will comply with the following terms and conditions: |
| 20 | |
| 21 | Permission to use, copy, modify, and distribute this software and its |
| 22 | associated documentation for any purpose and without fee is hereby |
| 23 | granted, provided that the above copyright notice appears in all |
| 24 | copies, and that both that copyright notice and this permission notice |
| 25 | appear in supporting documentation, and that the name of Secret Labs |
| 26 | AB or the author not be used in advertising or publicity pertaining to |
| 27 | distribution of the software without specific, written prior |
| 28 | permission. |
| 29 | |
| 30 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 31 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 32 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 33 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 34 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 35 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 36 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 37 | -------------------------------------------------------------------- |
| 38 | |
| 39 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "Python.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> |
| 48 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 50 | /*[clinic input] |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 51 | class str "PyUnicodeObject *" "&PyUnicode_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 52 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 53 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=604e916854800fa8]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 54 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 55 | /* --- Globals ------------------------------------------------------------ |
| 56 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 57 | NOTE: In the interpreter's initialization phase, some globals are currently |
| 58 | initialized dynamically as needed. In the process Unicode objects may |
| 59 | be created before the Unicode type is ready. |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 60 | |
| 61 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 63 | |
| 64 | #ifdef __cplusplus |
| 65 | extern "C" { |
| 66 | #endif |
| 67 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 68 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 69 | #define MAX_UNICODE 0x10ffff |
| 70 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 71 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 72 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 73 | #else |
| 74 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 75 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 76 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 77 | #define _PyUnicode_UTF8(op) \ |
| 78 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 79 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 80 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 81 | assert(PyUnicode_IS_READY(op)), \ |
| 82 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 83 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 84 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 85 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 86 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 87 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 88 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 89 | assert(PyUnicode_IS_READY(op)), \ |
| 90 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 91 | ((PyASCIIObject*)(op))->length : \ |
| 92 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 93 | #define _PyUnicode_WSTR(op) \ |
| 94 | (((PyASCIIObject*)(op))->wstr) |
| 95 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 96 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 97 | #define _PyUnicode_LENGTH(op) \ |
| 98 | (((PyASCIIObject *)(op))->length) |
| 99 | #define _PyUnicode_STATE(op) \ |
| 100 | (((PyASCIIObject *)(op))->state) |
| 101 | #define _PyUnicode_HASH(op) \ |
| 102 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 103 | #define _PyUnicode_KIND(op) \ |
| 104 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 105 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 106 | #define _PyUnicode_GET_LENGTH(op) \ |
| 107 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 108 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 109 | #define _PyUnicode_DATA_ANY(op) \ |
| 110 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 111 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 112 | #undef PyUnicode_READY |
| 113 | #define PyUnicode_READY(op) \ |
| 114 | (assert(_PyUnicode_CHECK(op)), \ |
| 115 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 116 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 117 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 118 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 119 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 120 | (assert(_PyUnicode_CHECK(op)), \ |
| 121 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 122 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 123 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 124 | (assert(_PyUnicode_CHECK(op)), \ |
| 125 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 126 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 127 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 128 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 129 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 130 | ((!PyUnicode_IS_COMPACT_ASCII(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 131 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 132 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 133 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 134 | /* true if the Unicode object has an allocated wstr memory block |
| 135 | (not shared with other data) */ |
| 136 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 137 | ((_PyUnicode_WSTR(op) && \ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 138 | (!PyUnicode_IS_READY(op) || \ |
| 139 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 140 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 141 | /* Generic helper macro to convert characters of different types. |
| 142 | from_type and to_type have to be valid type names, begin and end |
| 143 | are pointers to the source characters which should be of type |
| 144 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 145 | buffer where the result characters are written to. */ |
| 146 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 147 | do { \ |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 148 | to_type *_to = (to_type *)(to); \ |
| 149 | const from_type *_iter = (from_type *)(begin); \ |
| 150 | const from_type *_end = (from_type *)(end); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 151 | Py_ssize_t n = (_end) - (_iter); \ |
| 152 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 153 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 154 | while (_iter < (_unrolled_end)) { \ |
| 155 | _to[0] = (to_type) _iter[0]; \ |
| 156 | _to[1] = (to_type) _iter[1]; \ |
| 157 | _to[2] = (to_type) _iter[2]; \ |
| 158 | _to[3] = (to_type) _iter[3]; \ |
| 159 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 160 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 161 | while (_iter < (_end)) \ |
| 162 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 163 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 164 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 165 | /* This dictionary holds all interned unicode strings. Note that references |
| 166 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 167 | When the interned string reaches a refcnt of 0 the string deallocation |
| 168 | function will delete the reference from this dictionary. |
| 169 | |
| 170 | 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] | 171 | 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] | 172 | */ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 173 | static PyObject *interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 174 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 175 | /* The empty Unicode object is shared to improve performance. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 176 | static PyObject *unicode_empty = NULL; |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 177 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 178 | #define _Py_INCREF_UNICODE_EMPTY() \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 179 | do { \ |
| 180 | if (unicode_empty != NULL) \ |
| 181 | Py_INCREF(unicode_empty); \ |
| 182 | else { \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 183 | unicode_empty = PyUnicode_New(0, 0); \ |
| 184 | if (unicode_empty != NULL) { \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 185 | Py_INCREF(unicode_empty); \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 186 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); \ |
| 187 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 188 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 189 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 190 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 191 | #define _Py_RETURN_UNICODE_EMPTY() \ |
| 192 | do { \ |
| 193 | _Py_INCREF_UNICODE_EMPTY(); \ |
| 194 | return unicode_empty; \ |
| 195 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 196 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 197 | /* Forward declaration */ |
| 198 | Py_LOCAL_INLINE(int) |
| 199 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch); |
| 200 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 201 | /* List of static strings. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 202 | static _Py_Identifier *static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 203 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 204 | /* Single character Unicode strings in the Latin-1 range are being |
| 205 | shared as well. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 206 | static PyObject *unicode_latin1[256] = {NULL}; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 207 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 208 | /* Fast detection of the most frequent whitespace characters */ |
| 209 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 211 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 212 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 213 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 214 | /* case 0x000C: * FORM FEED */ |
| 215 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 216 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 217 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 218 | /* case 0x001C: * FILE SEPARATOR */ |
| 219 | /* case 0x001D: * GROUP SEPARATOR */ |
| 220 | /* case 0x001E: * RECORD SEPARATOR */ |
| 221 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 222 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 223 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 224 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 225 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 226 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 227 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 228 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 229 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 230 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 231 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 232 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 233 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 234 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 | }; |
| 238 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 239 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 240 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 241 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 242 | static int unicode_modifiable(PyObject *unicode); |
| 243 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 245 | static PyObject * |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 246 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 247 | static PyObject * |
| 248 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 249 | static PyObject * |
| 250 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 251 | |
| 252 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 253 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 254 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 255 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 256 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 257 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 258 | static void |
| 259 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 260 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 261 | PyObject *unicode, |
| 262 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 263 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 264 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 265 | /* Same for linebreaks */ |
| 266 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 267 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 268 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 269 | /* 0x000B, * LINE TABULATION */ |
| 270 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 271 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 272 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 273 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 274 | /* 0x001C, * FILE SEPARATOR */ |
| 275 | /* 0x001D, * GROUP SEPARATOR */ |
| 276 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 277 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 278 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 279 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 280 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 281 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 282 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 283 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 284 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 286 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 287 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 288 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 | }; |
| 292 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 293 | #include "clinic/unicodeobject.c.h" |
| 294 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 295 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 296 | 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] | 297 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 298 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 299 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 300 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 301 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 302 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 303 | /* This is actually an illegal character, so it should |
| 304 | not be passed to unichr. */ |
| 305 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 306 | #endif |
| 307 | } |
| 308 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 309 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 310 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 311 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 312 | { |
| 313 | PyASCIIObject *ascii; |
| 314 | unsigned int kind; |
| 315 | |
| 316 | assert(PyUnicode_Check(op)); |
| 317 | |
| 318 | ascii = (PyASCIIObject *)op; |
| 319 | kind = ascii->state.kind; |
| 320 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 321 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 322 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 323 | assert(ascii->state.ready == 1); |
| 324 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 325 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 326 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 327 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 328 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 329 | if (ascii->state.compact == 1) { |
| 330 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 331 | assert(kind == PyUnicode_1BYTE_KIND |
| 332 | || kind == PyUnicode_2BYTE_KIND |
| 333 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 334 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 335 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 336 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 337 | } |
| 338 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 339 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 340 | |
| 341 | data = unicode->data.any; |
| 342 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 343 | assert(ascii->length == 0); |
| 344 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 345 | assert(ascii->state.compact == 0); |
| 346 | assert(ascii->state.ascii == 0); |
| 347 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 348 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 349 | assert(ascii->wstr != NULL); |
| 350 | assert(data == NULL); |
| 351 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 352 | } |
| 353 | else { |
| 354 | assert(kind == PyUnicode_1BYTE_KIND |
| 355 | || kind == PyUnicode_2BYTE_KIND |
| 356 | || kind == PyUnicode_4BYTE_KIND); |
| 357 | assert(ascii->state.compact == 0); |
| 358 | assert(ascii->state.ready == 1); |
| 359 | assert(data != NULL); |
| 360 | if (ascii->state.ascii) { |
| 361 | assert (compact->utf8 == data); |
| 362 | assert (compact->utf8_length == ascii->length); |
| 363 | } |
| 364 | else |
| 365 | assert (compact->utf8 != data); |
| 366 | } |
| 367 | } |
| 368 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 369 | if ( |
| 370 | #if SIZEOF_WCHAR_T == 2 |
| 371 | kind == PyUnicode_2BYTE_KIND |
| 372 | #else |
| 373 | kind == PyUnicode_4BYTE_KIND |
| 374 | #endif |
| 375 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 376 | { |
| 377 | assert(ascii->wstr == data); |
| 378 | assert(compact->wstr_length == ascii->length); |
| 379 | } else |
| 380 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 381 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 382 | |
| 383 | if (compact->utf8 == NULL) |
| 384 | assert(compact->utf8_length == 0); |
| 385 | if (ascii->wstr == NULL) |
| 386 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 387 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 388 | /* check that the best kind is used */ |
| 389 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 390 | { |
| 391 | Py_ssize_t i; |
| 392 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 393 | void *data; |
| 394 | Py_UCS4 ch; |
| 395 | |
| 396 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 397 | for (i=0; i < ascii->length; i++) |
| 398 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 399 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 400 | if (ch > maxchar) |
| 401 | maxchar = ch; |
| 402 | } |
| 403 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 404 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 405 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 406 | assert(maxchar <= 255); |
| 407 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 408 | else |
| 409 | assert(maxchar < 128); |
| 410 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 411 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 412 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 413 | assert(maxchar <= 0xFFFF); |
| 414 | } |
| 415 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 416 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 417 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 418 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 419 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 420 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 421 | return 1; |
| 422 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 423 | #endif |
| 424 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 425 | static PyObject* |
| 426 | unicode_result_wchar(PyObject *unicode) |
| 427 | { |
| 428 | #ifndef Py_DEBUG |
| 429 | Py_ssize_t len; |
| 430 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 431 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 432 | if (len == 0) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 433 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 434 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (len == 1) { |
| 438 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 439 | if ((Py_UCS4)ch < 256) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 440 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 441 | Py_DECREF(unicode); |
| 442 | return latin1_char; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (_PyUnicode_Ready(unicode) < 0) { |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 447 | Py_DECREF(unicode); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 448 | return NULL; |
| 449 | } |
| 450 | #else |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 451 | assert(Py_REFCNT(unicode) == 1); |
| 452 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 453 | /* don't make the result ready in debug mode to ensure that the caller |
| 454 | makes the string ready before using it */ |
| 455 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 456 | #endif |
| 457 | return unicode; |
| 458 | } |
| 459 | |
| 460 | static PyObject* |
| 461 | unicode_result_ready(PyObject *unicode) |
| 462 | { |
| 463 | Py_ssize_t length; |
| 464 | |
| 465 | length = PyUnicode_GET_LENGTH(unicode); |
| 466 | if (length == 0) { |
| 467 | if (unicode != unicode_empty) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 468 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 469 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 470 | } |
| 471 | return unicode_empty; |
| 472 | } |
| 473 | |
| 474 | if (length == 1) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 475 | void *data = PyUnicode_DATA(unicode); |
| 476 | int kind = PyUnicode_KIND(unicode); |
| 477 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 478 | if (ch < 256) { |
| 479 | PyObject *latin1_char = unicode_latin1[ch]; |
| 480 | if (latin1_char != NULL) { |
| 481 | if (unicode != latin1_char) { |
| 482 | Py_INCREF(latin1_char); |
| 483 | Py_DECREF(unicode); |
| 484 | } |
| 485 | return latin1_char; |
| 486 | } |
| 487 | else { |
| 488 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 489 | Py_INCREF(unicode); |
| 490 | unicode_latin1[ch] = unicode; |
| 491 | return unicode; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 497 | return unicode; |
| 498 | } |
| 499 | |
| 500 | static PyObject* |
| 501 | unicode_result(PyObject *unicode) |
| 502 | { |
| 503 | assert(_PyUnicode_CHECK(unicode)); |
| 504 | if (PyUnicode_IS_READY(unicode)) |
| 505 | return unicode_result_ready(unicode); |
| 506 | else |
| 507 | return unicode_result_wchar(unicode); |
| 508 | } |
| 509 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 510 | static PyObject* |
| 511 | unicode_result_unchanged(PyObject *unicode) |
| 512 | { |
| 513 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 514 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 515 | return NULL; |
| 516 | Py_INCREF(unicode); |
| 517 | return unicode; |
| 518 | } |
| 519 | else |
| 520 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 521 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 524 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 525 | |
| 526 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 527 | to keep things simple, we use a single bitmask, using the least 5 |
| 528 | bits from each unicode characters as the bit index. */ |
| 529 | |
| 530 | /* the linebreak mask is set up by Unicode_Init below */ |
| 531 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 532 | #if LONG_BIT >= 128 |
| 533 | #define BLOOM_WIDTH 128 |
| 534 | #elif LONG_BIT >= 64 |
| 535 | #define BLOOM_WIDTH 64 |
| 536 | #elif LONG_BIT >= 32 |
| 537 | #define BLOOM_WIDTH 32 |
| 538 | #else |
| 539 | #error "LONG_BIT is smaller than 32" |
| 540 | #endif |
| 541 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 542 | #define BLOOM_MASK unsigned long |
| 543 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 544 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 546 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 548 | #define BLOOM_LINEBREAK(ch) \ |
| 549 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 550 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 551 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 552 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 553 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 554 | { |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 555 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN) \ |
| 556 | do { \ |
| 557 | TYPE *data = (TYPE *)PTR; \ |
| 558 | TYPE *end = data + LEN; \ |
| 559 | Py_UCS4 ch; \ |
| 560 | for (; data != end; data++) { \ |
| 561 | ch = *data; \ |
| 562 | MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ |
| 563 | } \ |
| 564 | break; \ |
| 565 | } while (0) |
| 566 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 567 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 568 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 569 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 570 | |
| 571 | mask = 0; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 572 | switch (kind) { |
| 573 | case PyUnicode_1BYTE_KIND: |
| 574 | BLOOM_UPDATE(Py_UCS1, mask, ptr, len); |
| 575 | break; |
| 576 | case PyUnicode_2BYTE_KIND: |
| 577 | BLOOM_UPDATE(Py_UCS2, mask, ptr, len); |
| 578 | break; |
| 579 | case PyUnicode_4BYTE_KIND: |
| 580 | BLOOM_UPDATE(Py_UCS4, mask, ptr, len); |
| 581 | break; |
| 582 | default: |
| 583 | assert(0); |
| 584 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 585 | return mask; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 586 | |
| 587 | #undef BLOOM_UPDATE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 590 | /* Compilation of templated routines */ |
| 591 | |
| 592 | #include "stringlib/asciilib.h" |
| 593 | #include "stringlib/fastsearch.h" |
| 594 | #include "stringlib/partition.h" |
| 595 | #include "stringlib/split.h" |
| 596 | #include "stringlib/count.h" |
| 597 | #include "stringlib/find.h" |
| 598 | #include "stringlib/find_max_char.h" |
| 599 | #include "stringlib/localeutil.h" |
| 600 | #include "stringlib/undef.h" |
| 601 | |
| 602 | #include "stringlib/ucs1lib.h" |
| 603 | #include "stringlib/fastsearch.h" |
| 604 | #include "stringlib/partition.h" |
| 605 | #include "stringlib/split.h" |
| 606 | #include "stringlib/count.h" |
| 607 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 608 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 609 | #include "stringlib/find_max_char.h" |
| 610 | #include "stringlib/localeutil.h" |
| 611 | #include "stringlib/undef.h" |
| 612 | |
| 613 | #include "stringlib/ucs2lib.h" |
| 614 | #include "stringlib/fastsearch.h" |
| 615 | #include "stringlib/partition.h" |
| 616 | #include "stringlib/split.h" |
| 617 | #include "stringlib/count.h" |
| 618 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 619 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 620 | #include "stringlib/find_max_char.h" |
| 621 | #include "stringlib/localeutil.h" |
| 622 | #include "stringlib/undef.h" |
| 623 | |
| 624 | #include "stringlib/ucs4lib.h" |
| 625 | #include "stringlib/fastsearch.h" |
| 626 | #include "stringlib/partition.h" |
| 627 | #include "stringlib/split.h" |
| 628 | #include "stringlib/count.h" |
| 629 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 630 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 631 | #include "stringlib/find_max_char.h" |
| 632 | #include "stringlib/localeutil.h" |
| 633 | #include "stringlib/undef.h" |
| 634 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 635 | #include "stringlib/unicodedefs.h" |
| 636 | #include "stringlib/fastsearch.h" |
| 637 | #include "stringlib/count.h" |
| 638 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 639 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 640 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 641 | /* --- Unicode Object ----------------------------------------------------- */ |
| 642 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 643 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 644 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 645 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 646 | Py_LOCAL_INLINE(Py_ssize_t) findchar(const void *s, int kind, |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 647 | Py_ssize_t size, Py_UCS4 ch, |
| 648 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 649 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 650 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 651 | |
| 652 | switch (kind) { |
| 653 | case PyUnicode_1BYTE_KIND: |
| 654 | { |
| 655 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 656 | if (ch1 == ch) |
| 657 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 658 | else |
| 659 | return -1; |
| 660 | } |
| 661 | case PyUnicode_2BYTE_KIND: |
| 662 | { |
| 663 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 664 | if (ch2 == ch) |
| 665 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 666 | else |
| 667 | return -1; |
| 668 | } |
| 669 | case PyUnicode_4BYTE_KIND: |
| 670 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 671 | default: |
| 672 | assert(0); |
| 673 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 674 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 677 | #ifdef Py_DEBUG |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 678 | /* Fill the data of a Unicode string with invalid characters to detect bugs |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 679 | earlier. |
| 680 | |
| 681 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 682 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 683 | invalid character in Unicode 6.0. */ |
| 684 | static void |
| 685 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 686 | { |
| 687 | int kind = PyUnicode_KIND(unicode); |
| 688 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 689 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 690 | if (length <= old_length) |
| 691 | return; |
| 692 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 693 | } |
| 694 | #endif |
| 695 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 696 | static PyObject* |
| 697 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 698 | { |
| 699 | Py_ssize_t char_size; |
| 700 | Py_ssize_t struct_size; |
| 701 | Py_ssize_t new_size; |
| 702 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 703 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 704 | #ifdef Py_DEBUG |
| 705 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 706 | #endif |
| 707 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 708 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 709 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 710 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 711 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 712 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 713 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 714 | struct_size = sizeof(PyASCIIObject); |
| 715 | else |
| 716 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 717 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 718 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 719 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 720 | PyErr_NoMemory(); |
| 721 | return NULL; |
| 722 | } |
| 723 | new_size = (struct_size + (length + 1) * char_size); |
| 724 | |
Serhiy Storchaka | 7aa6908 | 2015-12-03 01:02:03 +0200 | [diff] [blame] | 725 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) { |
| 726 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 727 | _PyUnicode_UTF8(unicode) = NULL; |
| 728 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 729 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 730 | _Py_DEC_REFTOTAL; |
| 731 | _Py_ForgetReference(unicode); |
| 732 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 733 | new_unicode = (PyObject *)PyObject_REALLOC(unicode, new_size); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 734 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 735 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | PyErr_NoMemory(); |
| 737 | return NULL; |
| 738 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 739 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 740 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 741 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 742 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 743 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 744 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 745 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 746 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 747 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 748 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 749 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 750 | _PyUnicode_WSTR(unicode) = NULL; |
Victor Stinner | 5bc03a6 | 2016-01-27 16:56:53 +0100 | [diff] [blame] | 751 | if (!PyUnicode_IS_ASCII(unicode)) |
| 752 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 753 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 754 | #ifdef Py_DEBUG |
| 755 | unicode_fill_invalid(unicode, old_length); |
| 756 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 757 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 758 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 759 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 760 | return unicode; |
| 761 | } |
| 762 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 763 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 764 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 765 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 766 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 767 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 768 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 769 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 770 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 771 | if (PyUnicode_IS_READY(unicode)) { |
| 772 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 773 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 774 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 775 | #ifdef Py_DEBUG |
| 776 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 777 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 778 | |
| 779 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 780 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 781 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 782 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 783 | |
| 784 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 785 | PyErr_NoMemory(); |
| 786 | return -1; |
| 787 | } |
| 788 | new_size = (length + 1) * char_size; |
| 789 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 790 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 791 | { |
| 792 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 793 | _PyUnicode_UTF8(unicode) = NULL; |
| 794 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 795 | } |
| 796 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 797 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 798 | if (data == NULL) { |
| 799 | PyErr_NoMemory(); |
| 800 | return -1; |
| 801 | } |
| 802 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 803 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 804 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 805 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 806 | } |
| 807 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 808 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 809 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 810 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 811 | _PyUnicode_LENGTH(unicode) = length; |
| 812 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 813 | #ifdef Py_DEBUG |
| 814 | unicode_fill_invalid(unicode, old_length); |
| 815 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 816 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 817 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 818 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 819 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 820 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 821 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 822 | |
| 823 | /* check for integer overflow */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 824 | 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] | 825 | PyErr_NoMemory(); |
| 826 | return -1; |
| 827 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 828 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 829 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 830 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 831 | if (!wstr) { |
| 832 | PyErr_NoMemory(); |
| 833 | return -1; |
| 834 | } |
| 835 | _PyUnicode_WSTR(unicode) = wstr; |
| 836 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 837 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 838 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 842 | static PyObject* |
| 843 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 844 | { |
| 845 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 846 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 847 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 848 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 849 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 850 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 851 | |
| 852 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 853 | if (copy == NULL) |
| 854 | return NULL; |
| 855 | |
| 856 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 857 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 858 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 859 | } |
| 860 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 861 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 862 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 863 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 864 | if (w == NULL) |
| 865 | return NULL; |
| 866 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 867 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 868 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 869 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 870 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 874 | /* 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] | 875 | Ux0000 terminated; some code (e.g. new_identifier) |
| 876 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 877 | |
| 878 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 879 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 880 | |
| 881 | */ |
| 882 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 883 | static PyUnicodeObject * |
| 884 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 885 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 886 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 887 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 888 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 889 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 890 | if (length == 0 && unicode_empty != NULL) { |
| 891 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 892 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 895 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 896 | 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] | 897 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 898 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 899 | if (length < 0) { |
| 900 | PyErr_SetString(PyExc_SystemError, |
| 901 | "Negative size passed to _PyUnicode_New"); |
| 902 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 905 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 906 | if (unicode == NULL) |
| 907 | return NULL; |
| 908 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 909 | |
| 910 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 911 | _PyUnicode_HASH(unicode) = -1; |
| 912 | _PyUnicode_STATE(unicode).interned = 0; |
| 913 | _PyUnicode_STATE(unicode).kind = 0; |
| 914 | _PyUnicode_STATE(unicode).compact = 0; |
| 915 | _PyUnicode_STATE(unicode).ready = 0; |
| 916 | _PyUnicode_STATE(unicode).ascii = 0; |
| 917 | _PyUnicode_DATA_ANY(unicode) = NULL; |
| 918 | _PyUnicode_LENGTH(unicode) = 0; |
| 919 | _PyUnicode_UTF8(unicode) = NULL; |
| 920 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 921 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 922 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 923 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 924 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 925 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 926 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 927 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 928 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 929 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 930 | * the caller fails before initializing str -- unicode_resize() |
| 931 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 932 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 933 | * We don't want unicode_resize to read uninitialized memory in |
| 934 | * that case. |
| 935 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 936 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 937 | _PyUnicode_WSTR(unicode)[length] = 0; |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 938 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 939 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 940 | return unicode; |
| 941 | } |
| 942 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 943 | static const char* |
| 944 | unicode_kind_name(PyObject *unicode) |
| 945 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 946 | /* don't check consistency: unicode_kind_name() is called from |
| 947 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 948 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 949 | { |
| 950 | if (!PyUnicode_IS_READY(unicode)) |
| 951 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 952 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 953 | { |
| 954 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 955 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 956 | return "legacy ascii"; |
| 957 | else |
| 958 | return "legacy latin1"; |
| 959 | case PyUnicode_2BYTE_KIND: |
| 960 | return "legacy UCS2"; |
| 961 | case PyUnicode_4BYTE_KIND: |
| 962 | return "legacy UCS4"; |
| 963 | default: |
| 964 | return "<legacy invalid kind>"; |
| 965 | } |
| 966 | } |
| 967 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 968 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 969 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 970 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 971 | return "ascii"; |
| 972 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 973 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 974 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 975 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 976 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 977 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 978 | default: |
| 979 | return "<invalid compact kind>"; |
| 980 | } |
| 981 | } |
| 982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 983 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 984 | /* Functions wrapping macros for use in debugger */ |
| 985 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 986 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | void *_PyUnicode_compact_data(void *unicode) { |
| 990 | return _PyUnicode_COMPACT_DATA(unicode); |
| 991 | } |
| 992 | void *_PyUnicode_data(void *unicode){ |
| 993 | printf("obj %p\n", unicode); |
| 994 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 995 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 996 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 997 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 998 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 999 | return PyUnicode_DATA(unicode); |
| 1000 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1001 | |
| 1002 | void |
| 1003 | _PyUnicode_Dump(PyObject *op) |
| 1004 | { |
| 1005 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1006 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 1007 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 1008 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1009 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1010 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1011 | { |
| 1012 | if (ascii->state.ascii) |
| 1013 | data = (ascii + 1); |
| 1014 | else |
| 1015 | data = (compact + 1); |
| 1016 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1017 | else |
| 1018 | data = unicode->data.any; |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1019 | printf("%s: len=%" PY_FORMAT_SIZE_T "u, ", |
| 1020 | unicode_kind_name(op), ascii->length); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1021 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1022 | if (ascii->wstr == data) |
| 1023 | printf("shared "); |
| 1024 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1025 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1026 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1027 | printf(" (%" PY_FORMAT_SIZE_T "u), ", compact->wstr_length); |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1028 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1029 | printf("shared "); |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1030 | printf("utf8=%p (%" PY_FORMAT_SIZE_T "u)", |
| 1031 | compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1032 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1033 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1034 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1035 | #endif |
| 1036 | |
| 1037 | PyObject * |
| 1038 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1039 | { |
| 1040 | PyObject *obj; |
| 1041 | PyCompactUnicodeObject *unicode; |
| 1042 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1043 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1044 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1045 | Py_ssize_t char_size; |
| 1046 | Py_ssize_t struct_size; |
| 1047 | |
| 1048 | /* Optimization for empty strings */ |
| 1049 | if (size == 0 && unicode_empty != NULL) { |
| 1050 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1051 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1054 | is_ascii = 0; |
| 1055 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1056 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1057 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1058 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1059 | char_size = 1; |
| 1060 | is_ascii = 1; |
| 1061 | struct_size = sizeof(PyASCIIObject); |
| 1062 | } |
| 1063 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1064 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1065 | char_size = 1; |
| 1066 | } |
| 1067 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1068 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1069 | char_size = 2; |
| 1070 | if (sizeof(wchar_t) == 2) |
| 1071 | is_sharing = 1; |
| 1072 | } |
| 1073 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1074 | if (maxchar > MAX_UNICODE) { |
| 1075 | PyErr_SetString(PyExc_SystemError, |
| 1076 | "invalid maximum character passed to PyUnicode_New"); |
| 1077 | return NULL; |
| 1078 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1079 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1080 | char_size = 4; |
| 1081 | if (sizeof(wchar_t) == 4) |
| 1082 | is_sharing = 1; |
| 1083 | } |
| 1084 | |
| 1085 | /* Ensure we won't overflow the size. */ |
| 1086 | if (size < 0) { |
| 1087 | PyErr_SetString(PyExc_SystemError, |
| 1088 | "Negative size passed to PyUnicode_New"); |
| 1089 | return NULL; |
| 1090 | } |
| 1091 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1092 | return PyErr_NoMemory(); |
| 1093 | |
| 1094 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1095 | * PyObject_New() so we are able to allocate space for the object and |
| 1096 | * it's data buffer. |
| 1097 | */ |
| 1098 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1099 | if (obj == NULL) |
| 1100 | return PyErr_NoMemory(); |
| 1101 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1102 | if (obj == NULL) |
| 1103 | return NULL; |
| 1104 | |
| 1105 | unicode = (PyCompactUnicodeObject *)obj; |
| 1106 | if (is_ascii) |
| 1107 | data = ((PyASCIIObject*)obj) + 1; |
| 1108 | else |
| 1109 | data = unicode + 1; |
| 1110 | _PyUnicode_LENGTH(unicode) = size; |
| 1111 | _PyUnicode_HASH(unicode) = -1; |
| 1112 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1113 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1114 | _PyUnicode_STATE(unicode).compact = 1; |
| 1115 | _PyUnicode_STATE(unicode).ready = 1; |
| 1116 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1117 | if (is_ascii) { |
| 1118 | ((char*)data)[size] = 0; |
| 1119 | _PyUnicode_WSTR(unicode) = NULL; |
| 1120 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1121 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1122 | ((char*)data)[size] = 0; |
| 1123 | _PyUnicode_WSTR(unicode) = NULL; |
| 1124 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1125 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1126 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1127 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1128 | else { |
| 1129 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1130 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1131 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1132 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1133 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1134 | ((Py_UCS4*)data)[size] = 0; |
| 1135 | if (is_sharing) { |
| 1136 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1137 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1138 | } |
| 1139 | else { |
| 1140 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1141 | _PyUnicode_WSTR(unicode) = NULL; |
| 1142 | } |
| 1143 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1144 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1145 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1146 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1147 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | return obj; |
| 1149 | } |
| 1150 | |
| 1151 | #if SIZEOF_WCHAR_T == 2 |
| 1152 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1153 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1154 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1155 | |
| 1156 | This function assumes that unicode can hold one more code point than wstr |
| 1157 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1158 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1159 | 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] | 1160 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1161 | { |
| 1162 | const wchar_t *iter; |
| 1163 | Py_UCS4 *ucs4_out; |
| 1164 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1165 | assert(unicode != NULL); |
| 1166 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1167 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1168 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1169 | |
| 1170 | for (iter = begin; iter < end; ) { |
| 1171 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1172 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1173 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1174 | && (iter+1) < end |
| 1175 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1176 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1177 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1178 | iter += 2; |
| 1179 | } |
| 1180 | else { |
| 1181 | *ucs4_out++ = *iter; |
| 1182 | iter++; |
| 1183 | } |
| 1184 | } |
| 1185 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1186 | _PyUnicode_GET_LENGTH(unicode))); |
| 1187 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1188 | } |
| 1189 | #endif |
| 1190 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1191 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1192 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1193 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1194 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1195 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1196 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1197 | return -1; |
| 1198 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1202 | static int |
| 1203 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1204 | PyObject *from, Py_ssize_t from_start, |
| 1205 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1206 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1207 | unsigned int from_kind, to_kind; |
| 1208 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1209 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1210 | assert(0 <= how_many); |
| 1211 | assert(0 <= from_start); |
| 1212 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1213 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1214 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1215 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1217 | assert(PyUnicode_Check(to)); |
| 1218 | assert(PyUnicode_IS_READY(to)); |
| 1219 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1220 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1221 | if (how_many == 0) |
| 1222 | return 0; |
| 1223 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1224 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1225 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1226 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1227 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1228 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1229 | #ifdef Py_DEBUG |
| 1230 | if (!check_maxchar |
| 1231 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1232 | { |
| 1233 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1234 | Py_UCS4 ch; |
| 1235 | Py_ssize_t i; |
| 1236 | for (i=0; i < how_many; i++) { |
| 1237 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1238 | assert(ch <= to_maxchar); |
| 1239 | } |
| 1240 | } |
| 1241 | #endif |
| 1242 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1243 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1244 | if (check_maxchar |
| 1245 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1246 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1247 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1248 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1249 | Py_UCS4 max_char; |
| 1250 | max_char = ucs1lib_find_max_char(from_data, |
| 1251 | (Py_UCS1*)from_data + how_many); |
| 1252 | if (max_char >= 128) |
| 1253 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1254 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1255 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1256 | (char*)from_data + from_kind * from_start, |
| 1257 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1258 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1259 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1260 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1261 | { |
| 1262 | _PyUnicode_CONVERT_BYTES( |
| 1263 | Py_UCS1, Py_UCS2, |
| 1264 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1265 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1266 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1267 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1268 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1269 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1270 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1271 | { |
| 1272 | _PyUnicode_CONVERT_BYTES( |
| 1273 | Py_UCS1, Py_UCS4, |
| 1274 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1275 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1276 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1277 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1278 | } |
| 1279 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1280 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1281 | { |
| 1282 | _PyUnicode_CONVERT_BYTES( |
| 1283 | Py_UCS2, Py_UCS4, |
| 1284 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1285 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1286 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1287 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1288 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1289 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1290 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1291 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1292 | if (!check_maxchar) { |
| 1293 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1294 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1295 | { |
| 1296 | _PyUnicode_CONVERT_BYTES( |
| 1297 | Py_UCS2, Py_UCS1, |
| 1298 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1299 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1300 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1301 | ); |
| 1302 | } |
| 1303 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1304 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1305 | { |
| 1306 | _PyUnicode_CONVERT_BYTES( |
| 1307 | Py_UCS4, Py_UCS1, |
| 1308 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1309 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1310 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1311 | ); |
| 1312 | } |
| 1313 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1314 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1315 | { |
| 1316 | _PyUnicode_CONVERT_BYTES( |
| 1317 | Py_UCS4, Py_UCS2, |
| 1318 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1319 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1320 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1321 | ); |
| 1322 | } |
| 1323 | else { |
| 1324 | assert(0); |
| 1325 | return -1; |
| 1326 | } |
| 1327 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1328 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1329 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1330 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1331 | Py_ssize_t i; |
| 1332 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1333 | for (i=0; i < how_many; i++) { |
| 1334 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1335 | if (ch > to_maxchar) |
| 1336 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1337 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1338 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1339 | } |
| 1340 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1341 | return 0; |
| 1342 | } |
| 1343 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1344 | void |
| 1345 | _PyUnicode_FastCopyCharacters( |
| 1346 | PyObject *to, Py_ssize_t to_start, |
| 1347 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1348 | { |
| 1349 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1350 | } |
| 1351 | |
| 1352 | Py_ssize_t |
| 1353 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1354 | PyObject *from, Py_ssize_t from_start, |
| 1355 | Py_ssize_t how_many) |
| 1356 | { |
| 1357 | int err; |
| 1358 | |
| 1359 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1360 | PyErr_BadInternalCall(); |
| 1361 | return -1; |
| 1362 | } |
| 1363 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1364 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1365 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1366 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1367 | return -1; |
| 1368 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1369 | if (from_start < 0) { |
| 1370 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1371 | return -1; |
| 1372 | } |
| 1373 | if (to_start < 0) { |
| 1374 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1375 | return -1; |
| 1376 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1377 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1378 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1379 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 1380 | "Cannot write %zi characters at %zi " |
| 1381 | "in a string of %zi characters", |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1382 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1383 | return -1; |
| 1384 | } |
| 1385 | |
| 1386 | if (how_many == 0) |
| 1387 | return 0; |
| 1388 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1389 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1390 | return -1; |
| 1391 | |
| 1392 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1393 | if (err) { |
| 1394 | PyErr_Format(PyExc_SystemError, |
| 1395 | "Cannot copy %s characters " |
| 1396 | "into a string of %s characters", |
| 1397 | unicode_kind_name(from), |
| 1398 | unicode_kind_name(to)); |
| 1399 | return -1; |
| 1400 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1401 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1404 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1405 | correct string length can be computed before converting a string to UCS4. |
| 1406 | This function counts single surrogates as a character and not as a pair. |
| 1407 | |
| 1408 | Return 0 on success, or -1 on error. */ |
| 1409 | static int |
| 1410 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1411 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1412 | { |
| 1413 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1414 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1415 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1416 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1417 | *num_surrogates = 0; |
| 1418 | *maxchar = 0; |
| 1419 | |
| 1420 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1421 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1422 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1423 | && (iter+1) < end |
| 1424 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1425 | { |
| 1426 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1427 | ++(*num_surrogates); |
| 1428 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1429 | } |
| 1430 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1431 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1432 | { |
| 1433 | ch = *iter; |
| 1434 | iter++; |
| 1435 | } |
| 1436 | if (ch > *maxchar) { |
| 1437 | *maxchar = ch; |
| 1438 | if (*maxchar > MAX_UNICODE) { |
| 1439 | PyErr_Format(PyExc_ValueError, |
| 1440 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1441 | ch); |
| 1442 | return -1; |
| 1443 | } |
| 1444 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1445 | } |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1449 | int |
| 1450 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1451 | { |
| 1452 | wchar_t *end; |
| 1453 | Py_UCS4 maxchar = 0; |
| 1454 | Py_ssize_t num_surrogates; |
| 1455 | #if SIZEOF_WCHAR_T == 2 |
| 1456 | Py_ssize_t length_wo_surrogates; |
| 1457 | #endif |
| 1458 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1459 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1460 | strings were created using _PyObject_New() and where no canonical |
| 1461 | representation (the str field) has been set yet aka strings |
| 1462 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1463 | assert(_PyUnicode_CHECK(unicode)); |
| 1464 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1466 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1467 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1468 | /* Actually, it should neither be interned nor be anything else: */ |
| 1469 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1470 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1471 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1472 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1473 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1474 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1475 | |
| 1476 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1477 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1478 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1479 | PyErr_NoMemory(); |
| 1480 | return -1; |
| 1481 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1482 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1483 | _PyUnicode_WSTR(unicode), end, |
| 1484 | PyUnicode_1BYTE_DATA(unicode)); |
| 1485 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1486 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1487 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1488 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1489 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1490 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1491 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1492 | } |
| 1493 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1494 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1495 | _PyUnicode_UTF8(unicode) = NULL; |
| 1496 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1497 | } |
| 1498 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1499 | _PyUnicode_WSTR(unicode) = NULL; |
| 1500 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1501 | } |
| 1502 | /* In this case we might have to convert down from 4-byte native |
| 1503 | wchar_t to 2-byte unicode. */ |
| 1504 | else if (maxchar < 65536) { |
| 1505 | assert(num_surrogates == 0 && |
| 1506 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1507 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1508 | #if SIZEOF_WCHAR_T == 2 |
| 1509 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1510 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1511 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1512 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1513 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1514 | _PyUnicode_UTF8(unicode) = NULL; |
| 1515 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1516 | #else |
| 1517 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1518 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1519 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1520 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1521 | PyErr_NoMemory(); |
| 1522 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1523 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1524 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1525 | _PyUnicode_WSTR(unicode), end, |
| 1526 | PyUnicode_2BYTE_DATA(unicode)); |
| 1527 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1528 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1529 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1530 | _PyUnicode_UTF8(unicode) = NULL; |
| 1531 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1532 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1533 | _PyUnicode_WSTR(unicode) = NULL; |
| 1534 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1535 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1536 | } |
| 1537 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1538 | else { |
| 1539 | #if SIZEOF_WCHAR_T == 2 |
| 1540 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1541 | new normalized 4-byte version. */ |
| 1542 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 1543 | if (length_wo_surrogates > PY_SSIZE_T_MAX / 4 - 1) { |
| 1544 | PyErr_NoMemory(); |
| 1545 | return -1; |
| 1546 | } |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1547 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1548 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1549 | PyErr_NoMemory(); |
| 1550 | return -1; |
| 1551 | } |
| 1552 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1553 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1554 | _PyUnicode_UTF8(unicode) = NULL; |
| 1555 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1556 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1557 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1558 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1559 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1560 | _PyUnicode_WSTR(unicode) = NULL; |
| 1561 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1562 | #else |
| 1563 | assert(num_surrogates == 0); |
| 1564 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1565 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1566 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1567 | _PyUnicode_UTF8(unicode) = NULL; |
| 1568 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1569 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1570 | #endif |
| 1571 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1572 | } |
| 1573 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1574 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1575 | return 0; |
| 1576 | } |
| 1577 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1578 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1579 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1580 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1581 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1582 | case SSTATE_NOT_INTERNED: |
| 1583 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1584 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1585 | case SSTATE_INTERNED_MORTAL: |
| 1586 | /* revive dead object temporarily for DelItem */ |
| 1587 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1588 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1589 | Py_FatalError( |
| 1590 | "deletion of interned string failed"); |
| 1591 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1592 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1593 | case SSTATE_INTERNED_IMMORTAL: |
| 1594 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1595 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1596 | default: |
| 1597 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1600 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1601 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1602 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1603 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1604 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1605 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1606 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1607 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1610 | #ifdef Py_DEBUG |
| 1611 | static int |
| 1612 | unicode_is_singleton(PyObject *unicode) |
| 1613 | { |
| 1614 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1615 | if (unicode == unicode_empty) |
| 1616 | return 1; |
| 1617 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1618 | { |
| 1619 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1620 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1621 | return 1; |
| 1622 | } |
| 1623 | return 0; |
| 1624 | } |
| 1625 | #endif |
| 1626 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1627 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1628 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1629 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1630 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1631 | if (Py_REFCNT(unicode) != 1) |
| 1632 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1633 | if (_PyUnicode_HASH(unicode) != -1) |
| 1634 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1635 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1636 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1637 | if (!PyUnicode_CheckExact(unicode)) |
| 1638 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1639 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1640 | /* singleton refcount is greater than 1 */ |
| 1641 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1642 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1643 | return 1; |
| 1644 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1645 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1646 | static int |
| 1647 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1648 | { |
| 1649 | PyObject *unicode; |
| 1650 | Py_ssize_t old_length; |
| 1651 | |
| 1652 | assert(p_unicode != NULL); |
| 1653 | unicode = *p_unicode; |
| 1654 | |
| 1655 | assert(unicode != NULL); |
| 1656 | assert(PyUnicode_Check(unicode)); |
| 1657 | assert(0 <= length); |
| 1658 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1659 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1660 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1661 | else |
| 1662 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1663 | if (old_length == length) |
| 1664 | return 0; |
| 1665 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1666 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1667 | _Py_INCREF_UNICODE_EMPTY(); |
| 1668 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1669 | return -1; |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 1670 | Py_SETREF(*p_unicode, unicode_empty); |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1671 | return 0; |
| 1672 | } |
| 1673 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1674 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1675 | PyObject *copy = resize_copy(unicode, length); |
| 1676 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1677 | return -1; |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 1678 | Py_SETREF(*p_unicode, copy); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1679 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1682 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1683 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1684 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1685 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1686 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1687 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1688 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1689 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1692 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1693 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1694 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1695 | PyObject *unicode; |
| 1696 | if (p_unicode == NULL) { |
| 1697 | PyErr_BadInternalCall(); |
| 1698 | return -1; |
| 1699 | } |
| 1700 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1701 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1702 | { |
| 1703 | PyErr_BadInternalCall(); |
| 1704 | return -1; |
| 1705 | } |
| 1706 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1707 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1708 | |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 1709 | /* Copy an ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1710 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1711 | WARNING: The function doesn't copy the terminating null character and |
| 1712 | doesn't check the maximum character (may write a latin1 character in an |
| 1713 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1714 | static void |
| 1715 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1716 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1717 | { |
| 1718 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1719 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1720 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1721 | |
| 1722 | switch (kind) { |
| 1723 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1724 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1725 | #ifdef Py_DEBUG |
| 1726 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1727 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1728 | (const Py_UCS1*)str, |
| 1729 | (const Py_UCS1*)str + len); |
| 1730 | assert(maxchar < 128); |
| 1731 | } |
| 1732 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1733 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1734 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1735 | } |
| 1736 | case PyUnicode_2BYTE_KIND: { |
| 1737 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1738 | Py_UCS2 *ucs2 = start; |
| 1739 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1740 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1741 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1742 | *ucs2 = (Py_UCS2)*str; |
| 1743 | |
| 1744 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1745 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1746 | } |
| 1747 | default: { |
| 1748 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1749 | Py_UCS4 *ucs4 = start; |
| 1750 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1751 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1752 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1753 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1754 | *ucs4 = (Py_UCS4)*str; |
| 1755 | |
| 1756 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1761 | static PyObject* |
| 1762 | get_latin1_char(unsigned char ch) |
| 1763 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1764 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1765 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1766 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1767 | if (!unicode) |
| 1768 | return NULL; |
| 1769 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1770 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1771 | unicode_latin1[ch] = unicode; |
| 1772 | } |
| 1773 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1774 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1775 | } |
| 1776 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1777 | static PyObject* |
| 1778 | unicode_char(Py_UCS4 ch) |
| 1779 | { |
| 1780 | PyObject *unicode; |
| 1781 | |
| 1782 | assert(ch <= MAX_UNICODE); |
| 1783 | |
Victor Stinner | f3b46b4 | 2014-01-03 13:16:00 +0100 | [diff] [blame] | 1784 | if (ch < 256) |
| 1785 | return get_latin1_char(ch); |
| 1786 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1787 | unicode = PyUnicode_New(1, ch); |
| 1788 | if (unicode == NULL) |
| 1789 | return NULL; |
| 1790 | switch (PyUnicode_KIND(unicode)) { |
| 1791 | case PyUnicode_1BYTE_KIND: |
| 1792 | PyUnicode_1BYTE_DATA(unicode)[0] = (Py_UCS1)ch; |
| 1793 | break; |
| 1794 | case PyUnicode_2BYTE_KIND: |
| 1795 | PyUnicode_2BYTE_DATA(unicode)[0] = (Py_UCS2)ch; |
| 1796 | break; |
| 1797 | default: |
| 1798 | assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1799 | PyUnicode_4BYTE_DATA(unicode)[0] = ch; |
| 1800 | } |
| 1801 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1802 | return unicode; |
| 1803 | } |
| 1804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1805 | PyObject * |
| 1806 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1807 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1808 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1809 | Py_UCS4 maxchar = 0; |
| 1810 | Py_ssize_t num_surrogates; |
| 1811 | |
| 1812 | if (u == NULL) |
| 1813 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1814 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1815 | /* If the Unicode data is known at construction time, we can apply |
| 1816 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1818 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1819 | if (size == 0) |
| 1820 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1821 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1822 | /* Single character Unicode objects in the Latin-1 range are |
| 1823 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1824 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1825 | return get_latin1_char((unsigned char)*u); |
| 1826 | |
| 1827 | /* If not empty and not single character, copy the Unicode data |
| 1828 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1829 | if (find_maxchar_surrogates(u, u + size, |
| 1830 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1831 | return NULL; |
| 1832 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1833 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1834 | if (!unicode) |
| 1835 | return NULL; |
| 1836 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1837 | switch (PyUnicode_KIND(unicode)) { |
| 1838 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1839 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1840 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1841 | break; |
| 1842 | case PyUnicode_2BYTE_KIND: |
| 1843 | #if Py_UNICODE_SIZE == 2 |
| 1844 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1845 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1846 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1847 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1848 | #endif |
| 1849 | break; |
| 1850 | case PyUnicode_4BYTE_KIND: |
| 1851 | #if SIZEOF_WCHAR_T == 2 |
| 1852 | /* This is the only case which has to process surrogates, thus |
| 1853 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1854 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1855 | #else |
| 1856 | assert(num_surrogates == 0); |
| 1857 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1858 | #endif |
| 1859 | break; |
| 1860 | default: |
| 1861 | assert(0 && "Impossible state"); |
| 1862 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1863 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1864 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1867 | PyObject * |
| 1868 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1869 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1870 | if (size < 0) { |
| 1871 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1872 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1873 | return NULL; |
| 1874 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1875 | if (u != NULL) |
| 1876 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1877 | else |
| 1878 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1881 | PyObject * |
| 1882 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1883 | { |
| 1884 | size_t size = strlen(u); |
| 1885 | if (size > PY_SSIZE_T_MAX) { |
| 1886 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1887 | return NULL; |
| 1888 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1889 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1892 | PyObject * |
| 1893 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1894 | { |
| 1895 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1896 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1897 | strlen(id->string), |
| 1898 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1899 | if (!id->object) |
| 1900 | return NULL; |
| 1901 | PyUnicode_InternInPlace(&id->object); |
| 1902 | assert(!id->next); |
| 1903 | id->next = static_strings; |
| 1904 | static_strings = id; |
| 1905 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1906 | return id->object; |
| 1907 | } |
| 1908 | |
| 1909 | void |
| 1910 | _PyUnicode_ClearStaticStrings() |
| 1911 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1912 | _Py_Identifier *tmp, *s = static_strings; |
| 1913 | while (s) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 1914 | Py_CLEAR(s->object); |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1915 | tmp = s->next; |
| 1916 | s->next = NULL; |
| 1917 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1918 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1919 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1920 | } |
| 1921 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1922 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1923 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1924 | PyObject* |
| 1925 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1926 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1927 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1928 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1929 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1930 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1931 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1932 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1933 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1934 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1935 | unicode = PyUnicode_New(size, 127); |
| 1936 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1937 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1938 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1939 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1940 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1943 | static Py_UCS4 |
| 1944 | kind_maxchar_limit(unsigned int kind) |
| 1945 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1946 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1947 | case PyUnicode_1BYTE_KIND: |
| 1948 | return 0x80; |
| 1949 | case PyUnicode_2BYTE_KIND: |
| 1950 | return 0x100; |
| 1951 | case PyUnicode_4BYTE_KIND: |
| 1952 | return 0x10000; |
| 1953 | default: |
| 1954 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1955 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1959 | Py_LOCAL_INLINE(Py_UCS4) |
| 1960 | align_maxchar(Py_UCS4 maxchar) |
| 1961 | { |
| 1962 | if (maxchar <= 127) |
| 1963 | return 127; |
| 1964 | else if (maxchar <= 255) |
| 1965 | return 255; |
| 1966 | else if (maxchar <= 65535) |
| 1967 | return 65535; |
| 1968 | else |
| 1969 | return MAX_UNICODE; |
| 1970 | } |
| 1971 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1972 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1973 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1974 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1975 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1976 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1977 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1978 | if (size == 0) |
| 1979 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1980 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1981 | if (size == 1) |
| 1982 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1983 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1984 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1985 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1986 | if (!res) |
| 1987 | return NULL; |
| 1988 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1989 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1990 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1993 | static PyObject* |
| 1994 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1995 | { |
| 1996 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1997 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1998 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1999 | if (size == 0) |
| 2000 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2001 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2002 | if (size == 1) |
| 2003 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2004 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2005 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2006 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2007 | if (!res) |
| 2008 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2009 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2010 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2011 | else { |
| 2012 | _PyUnicode_CONVERT_BYTES( |
| 2013 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 2014 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2015 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2016 | return res; |
| 2017 | } |
| 2018 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2019 | static PyObject* |
| 2020 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2021 | { |
| 2022 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2023 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2024 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2025 | if (size == 0) |
| 2026 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2027 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2028 | if (size == 1) |
| 2029 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2030 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2031 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2032 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2033 | if (!res) |
| 2034 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2035 | if (max_char < 256) |
| 2036 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2037 | PyUnicode_1BYTE_DATA(res)); |
| 2038 | else if (max_char < 0x10000) |
| 2039 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2040 | PyUnicode_2BYTE_DATA(res)); |
| 2041 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2042 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2043 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2044 | return res; |
| 2045 | } |
| 2046 | |
| 2047 | PyObject* |
| 2048 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2049 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2050 | if (size < 0) { |
| 2051 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2052 | return NULL; |
| 2053 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2054 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2055 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2056 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2057 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2058 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2059 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2060 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2061 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2062 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2063 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2064 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2065 | } |
| 2066 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2067 | Py_UCS4 |
| 2068 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2069 | { |
| 2070 | enum PyUnicode_Kind kind; |
| 2071 | void *startptr, *endptr; |
| 2072 | |
| 2073 | assert(PyUnicode_IS_READY(unicode)); |
| 2074 | assert(0 <= start); |
| 2075 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2076 | assert(start <= end); |
| 2077 | |
| 2078 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2079 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2080 | |
| 2081 | if (start == end) |
| 2082 | return 127; |
| 2083 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2084 | if (PyUnicode_IS_ASCII(unicode)) |
| 2085 | return 127; |
| 2086 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2087 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2088 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2089 | endptr = (char *)startptr + end * kind; |
| 2090 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2091 | switch(kind) { |
| 2092 | case PyUnicode_1BYTE_KIND: |
| 2093 | return ucs1lib_find_max_char(startptr, endptr); |
| 2094 | case PyUnicode_2BYTE_KIND: |
| 2095 | return ucs2lib_find_max_char(startptr, endptr); |
| 2096 | case PyUnicode_4BYTE_KIND: |
| 2097 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2098 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2099 | assert(0); |
| 2100 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2104 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2105 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2106 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2107 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2108 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2109 | { |
| 2110 | PyObject *unicode, *copy; |
| 2111 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2112 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2113 | unsigned int kind; |
| 2114 | |
| 2115 | assert(p_unicode != NULL); |
| 2116 | unicode = *p_unicode; |
| 2117 | assert(PyUnicode_IS_READY(unicode)); |
| 2118 | if (PyUnicode_IS_ASCII(unicode)) |
| 2119 | return; |
| 2120 | |
| 2121 | len = PyUnicode_GET_LENGTH(unicode); |
| 2122 | kind = PyUnicode_KIND(unicode); |
| 2123 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2124 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2125 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2126 | if (max_char >= 128) |
| 2127 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2128 | } |
| 2129 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2130 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2131 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2132 | if (max_char >= 256) |
| 2133 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2134 | } |
| 2135 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2136 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2137 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2138 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2139 | if (max_char >= 0x10000) |
| 2140 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2141 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2142 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2143 | if (copy != NULL) |
| 2144 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2145 | Py_DECREF(unicode); |
| 2146 | *p_unicode = copy; |
| 2147 | } |
| 2148 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2149 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2150 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2151 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2152 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2153 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2154 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2155 | if (!PyUnicode_Check(unicode)) { |
| 2156 | PyErr_BadInternalCall(); |
| 2157 | return NULL; |
| 2158 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2159 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2160 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2161 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2162 | length = PyUnicode_GET_LENGTH(unicode); |
| 2163 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2164 | if (!copy) |
| 2165 | return NULL; |
| 2166 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2167 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2168 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2169 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2170 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2171 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2172 | } |
| 2173 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2174 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2175 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2176 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2177 | |
| 2178 | void* |
| 2179 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2180 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2181 | Py_ssize_t len; |
| 2182 | void *result; |
| 2183 | unsigned int skind; |
| 2184 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2185 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2186 | return NULL; |
| 2187 | |
| 2188 | len = PyUnicode_GET_LENGTH(s); |
| 2189 | skind = PyUnicode_KIND(s); |
| 2190 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2191 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2192 | return NULL; |
| 2193 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2194 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2195 | case PyUnicode_2BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2196 | result = PyMem_New(Py_UCS2, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2197 | if (!result) |
| 2198 | return PyErr_NoMemory(); |
| 2199 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2200 | _PyUnicode_CONVERT_BYTES( |
| 2201 | Py_UCS1, Py_UCS2, |
| 2202 | PyUnicode_1BYTE_DATA(s), |
| 2203 | PyUnicode_1BYTE_DATA(s) + len, |
| 2204 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2205 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2206 | case PyUnicode_4BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2207 | result = PyMem_New(Py_UCS4, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2208 | if (!result) |
| 2209 | return PyErr_NoMemory(); |
| 2210 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2211 | _PyUnicode_CONVERT_BYTES( |
| 2212 | Py_UCS2, Py_UCS4, |
| 2213 | PyUnicode_2BYTE_DATA(s), |
| 2214 | PyUnicode_2BYTE_DATA(s) + len, |
| 2215 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2216 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2217 | else { |
| 2218 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2219 | _PyUnicode_CONVERT_BYTES( |
| 2220 | Py_UCS1, Py_UCS4, |
| 2221 | PyUnicode_1BYTE_DATA(s), |
| 2222 | PyUnicode_1BYTE_DATA(s) + len, |
| 2223 | result); |
| 2224 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2225 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2226 | default: |
| 2227 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2228 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2229 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2230 | return NULL; |
| 2231 | } |
| 2232 | |
| 2233 | static Py_UCS4* |
| 2234 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2235 | int copy_null) |
| 2236 | { |
| 2237 | int kind; |
| 2238 | void *data; |
| 2239 | Py_ssize_t len, targetlen; |
| 2240 | if (PyUnicode_READY(string) == -1) |
| 2241 | return NULL; |
| 2242 | kind = PyUnicode_KIND(string); |
| 2243 | data = PyUnicode_DATA(string); |
| 2244 | len = PyUnicode_GET_LENGTH(string); |
| 2245 | targetlen = len; |
| 2246 | if (copy_null) |
| 2247 | targetlen++; |
| 2248 | if (!target) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2249 | target = PyMem_New(Py_UCS4, targetlen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2250 | if (!target) { |
| 2251 | PyErr_NoMemory(); |
| 2252 | return NULL; |
| 2253 | } |
| 2254 | } |
| 2255 | else { |
| 2256 | if (targetsize < targetlen) { |
| 2257 | PyErr_Format(PyExc_SystemError, |
| 2258 | "string is longer than the buffer"); |
| 2259 | if (copy_null && 0 < targetsize) |
| 2260 | target[0] = 0; |
| 2261 | return NULL; |
| 2262 | } |
| 2263 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2264 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2265 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2266 | _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] | 2267 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2268 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2269 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2270 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2271 | } |
| 2272 | else { |
| 2273 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2274 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2275 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2276 | if (copy_null) |
| 2277 | target[len] = 0; |
| 2278 | return target; |
| 2279 | } |
| 2280 | |
| 2281 | Py_UCS4* |
| 2282 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2283 | int copy_null) |
| 2284 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2285 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2286 | PyErr_BadInternalCall(); |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | return as_ucs4(string, target, targetsize, copy_null); |
| 2290 | } |
| 2291 | |
| 2292 | Py_UCS4* |
| 2293 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2294 | { |
| 2295 | return as_ucs4(string, NULL, 0, 1); |
| 2296 | } |
| 2297 | |
| 2298 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2299 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2300 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2301 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2302 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2303 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2304 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2305 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2306 | PyErr_BadInternalCall(); |
| 2307 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2310 | if (size == -1) { |
| 2311 | size = wcslen(w); |
| 2312 | } |
| 2313 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2314 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2317 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2318 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2319 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2320 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2321 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2322 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2323 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2324 | static int |
| 2325 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2326 | Py_ssize_t width, Py_ssize_t precision) |
| 2327 | { |
| 2328 | Py_ssize_t length, fill, arglen; |
| 2329 | Py_UCS4 maxchar; |
| 2330 | |
| 2331 | if (PyUnicode_READY(str) == -1) |
| 2332 | return -1; |
| 2333 | |
| 2334 | length = PyUnicode_GET_LENGTH(str); |
| 2335 | if ((precision == -1 || precision >= length) |
| 2336 | && width <= length) |
| 2337 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2338 | |
| 2339 | if (precision != -1) |
| 2340 | length = Py_MIN(precision, length); |
| 2341 | |
| 2342 | arglen = Py_MAX(length, width); |
| 2343 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2344 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2345 | else |
| 2346 | maxchar = writer->maxchar; |
| 2347 | |
| 2348 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2349 | return -1; |
| 2350 | |
| 2351 | if (width > length) { |
| 2352 | fill = width - length; |
| 2353 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2354 | return -1; |
| 2355 | writer->pos += fill; |
| 2356 | } |
| 2357 | |
| 2358 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2359 | str, 0, length); |
| 2360 | writer->pos += length; |
| 2361 | return 0; |
| 2362 | } |
| 2363 | |
| 2364 | static int |
| 2365 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2366 | Py_ssize_t width, Py_ssize_t precision) |
| 2367 | { |
| 2368 | /* UTF-8 */ |
| 2369 | Py_ssize_t length; |
| 2370 | PyObject *unicode; |
| 2371 | int res; |
| 2372 | |
| 2373 | length = strlen(str); |
| 2374 | if (precision != -1) |
| 2375 | length = Py_MIN(length, precision); |
| 2376 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2377 | if (unicode == NULL) |
| 2378 | return -1; |
| 2379 | |
| 2380 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2381 | Py_DECREF(unicode); |
| 2382 | return res; |
| 2383 | } |
| 2384 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2385 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2386 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2387 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2388 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2389 | const char *p; |
| 2390 | Py_ssize_t len; |
| 2391 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2392 | Py_ssize_t width; |
| 2393 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2394 | int longflag; |
| 2395 | int longlongflag; |
| 2396 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2397 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2398 | |
| 2399 | p = f; |
| 2400 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2401 | zeropad = 0; |
| 2402 | if (*f == '0') { |
| 2403 | zeropad = 1; |
| 2404 | f++; |
| 2405 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2406 | |
| 2407 | /* 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] | 2408 | width = -1; |
| 2409 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2410 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2411 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2412 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2413 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2414 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2415 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2416 | return NULL; |
| 2417 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2418 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2419 | f++; |
| 2420 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2421 | } |
| 2422 | precision = -1; |
| 2423 | if (*f == '.') { |
| 2424 | f++; |
| 2425 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2426 | precision = (*f - '0'); |
| 2427 | f++; |
| 2428 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2429 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2430 | PyErr_SetString(PyExc_ValueError, |
| 2431 | "precision too big"); |
| 2432 | return NULL; |
| 2433 | } |
| 2434 | precision = (precision * 10) + (*f - '0'); |
| 2435 | f++; |
| 2436 | } |
| 2437 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2438 | if (*f == '%') { |
| 2439 | /* "%.3%s" => f points to "3" */ |
| 2440 | f--; |
| 2441 | } |
| 2442 | } |
| 2443 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2444 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2445 | f--; |
| 2446 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2447 | |
| 2448 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2449 | longflag = 0; |
| 2450 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2451 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2452 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2453 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2454 | longflag = 1; |
| 2455 | ++f; |
| 2456 | } |
| 2457 | #ifdef HAVE_LONG_LONG |
| 2458 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2459 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2460 | longlongflag = 1; |
| 2461 | f += 2; |
| 2462 | } |
| 2463 | #endif |
| 2464 | } |
| 2465 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2466 | 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] | 2467 | size_tflag = 1; |
| 2468 | ++f; |
| 2469 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2470 | |
| 2471 | if (f[1] == '\0') |
| 2472 | writer->overallocate = 0; |
| 2473 | |
| 2474 | switch (*f) { |
| 2475 | case 'c': |
| 2476 | { |
| 2477 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2478 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2479 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2480 | "character argument not in range(0x110000)"); |
| 2481 | return NULL; |
| 2482 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2483 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2484 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2485 | break; |
| 2486 | } |
| 2487 | |
| 2488 | case 'i': |
| 2489 | case 'd': |
| 2490 | case 'u': |
| 2491 | case 'x': |
| 2492 | { |
| 2493 | /* used by sprintf */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2494 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2495 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2496 | |
| 2497 | if (*f == 'u') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2498 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2499 | len = sprintf(buffer, "%lu", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2500 | va_arg(*vargs, unsigned long)); |
| 2501 | #ifdef HAVE_LONG_LONG |
| 2502 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2503 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2504 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2505 | #endif |
| 2506 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2507 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2508 | va_arg(*vargs, size_t)); |
| 2509 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2510 | len = sprintf(buffer, "%u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2511 | va_arg(*vargs, unsigned int)); |
| 2512 | } |
| 2513 | else if (*f == 'x') { |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2514 | len = sprintf(buffer, "%x", va_arg(*vargs, int)); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2515 | } |
| 2516 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2517 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2518 | len = sprintf(buffer, "%li", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2519 | va_arg(*vargs, long)); |
| 2520 | #ifdef HAVE_LONG_LONG |
| 2521 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2522 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2523 | va_arg(*vargs, PY_LONG_LONG)); |
| 2524 | #endif |
| 2525 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2526 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2527 | va_arg(*vargs, Py_ssize_t)); |
| 2528 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2529 | len = sprintf(buffer, "%i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2530 | va_arg(*vargs, int)); |
| 2531 | } |
| 2532 | assert(len >= 0); |
| 2533 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2534 | if (precision < len) |
| 2535 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2536 | |
| 2537 | arglen = Py_MAX(precision, width); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2538 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2539 | return NULL; |
| 2540 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2541 | if (width > precision) { |
| 2542 | Py_UCS4 fillchar; |
| 2543 | fill = width - precision; |
| 2544 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2545 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2546 | return NULL; |
| 2547 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2548 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2549 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2550 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2551 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2552 | return NULL; |
| 2553 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2554 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2555 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2556 | if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) |
| 2557 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2558 | break; |
| 2559 | } |
| 2560 | |
| 2561 | case 'p': |
| 2562 | { |
| 2563 | char number[MAX_LONG_LONG_CHARS]; |
| 2564 | |
| 2565 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2566 | assert(len >= 0); |
| 2567 | |
| 2568 | /* %p is ill-defined: ensure leading 0x. */ |
| 2569 | if (number[1] == 'X') |
| 2570 | number[1] = 'x'; |
| 2571 | else if (number[1] != 'x') { |
| 2572 | memmove(number + 2, number, |
| 2573 | strlen(number) + 1); |
| 2574 | number[0] = '0'; |
| 2575 | number[1] = 'x'; |
| 2576 | len += 2; |
| 2577 | } |
| 2578 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2579 | if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2580 | return NULL; |
| 2581 | break; |
| 2582 | } |
| 2583 | |
| 2584 | case 's': |
| 2585 | { |
| 2586 | /* UTF-8 */ |
| 2587 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2588 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2589 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | } |
| 2592 | |
| 2593 | case 'U': |
| 2594 | { |
| 2595 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2596 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2597 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2598 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2599 | return NULL; |
| 2600 | break; |
| 2601 | } |
| 2602 | |
| 2603 | case 'V': |
| 2604 | { |
| 2605 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2606 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2607 | if (obj) { |
| 2608 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2609 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2610 | return NULL; |
| 2611 | } |
| 2612 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2613 | assert(str != NULL); |
| 2614 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2615 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2616 | } |
| 2617 | break; |
| 2618 | } |
| 2619 | |
| 2620 | case 'S': |
| 2621 | { |
| 2622 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2623 | PyObject *str; |
| 2624 | assert(obj); |
| 2625 | str = PyObject_Str(obj); |
| 2626 | if (!str) |
| 2627 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2628 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2629 | Py_DECREF(str); |
| 2630 | return NULL; |
| 2631 | } |
| 2632 | Py_DECREF(str); |
| 2633 | break; |
| 2634 | } |
| 2635 | |
| 2636 | case 'R': |
| 2637 | { |
| 2638 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2639 | PyObject *repr; |
| 2640 | assert(obj); |
| 2641 | repr = PyObject_Repr(obj); |
| 2642 | if (!repr) |
| 2643 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2644 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2645 | Py_DECREF(repr); |
| 2646 | return NULL; |
| 2647 | } |
| 2648 | Py_DECREF(repr); |
| 2649 | break; |
| 2650 | } |
| 2651 | |
| 2652 | case 'A': |
| 2653 | { |
| 2654 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2655 | PyObject *ascii; |
| 2656 | assert(obj); |
| 2657 | ascii = PyObject_ASCII(obj); |
| 2658 | if (!ascii) |
| 2659 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2660 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2661 | Py_DECREF(ascii); |
| 2662 | return NULL; |
| 2663 | } |
| 2664 | Py_DECREF(ascii); |
| 2665 | break; |
| 2666 | } |
| 2667 | |
| 2668 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2669 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2670 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2671 | break; |
| 2672 | |
| 2673 | default: |
| 2674 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2675 | of the format string to the output string. (we cannot just |
| 2676 | skip the code, since there's no way to know what's in the |
| 2677 | argument list) */ |
| 2678 | len = strlen(p); |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2679 | if (_PyUnicodeWriter_WriteLatin1String(writer, p, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2680 | return NULL; |
| 2681 | f = p+len; |
| 2682 | return f; |
| 2683 | } |
| 2684 | |
| 2685 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2686 | return f; |
| 2687 | } |
| 2688 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2689 | PyObject * |
| 2690 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2691 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2692 | va_list vargs2; |
| 2693 | const char *f; |
| 2694 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2695 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2696 | _PyUnicodeWriter_Init(&writer); |
| 2697 | writer.min_length = strlen(format) + 100; |
| 2698 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2699 | |
| 2700 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2701 | Copy it to be able to pass a reference to a subfunction. */ |
| 2702 | Py_VA_COPY(vargs2, vargs); |
| 2703 | |
| 2704 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2705 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2706 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2707 | if (f == NULL) |
| 2708 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2709 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2710 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2711 | const char *p; |
| 2712 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2713 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2714 | p = f; |
| 2715 | do |
| 2716 | { |
| 2717 | if ((unsigned char)*p > 127) { |
| 2718 | PyErr_Format(PyExc_ValueError, |
| 2719 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2720 | "string, got a non-ASCII byte: 0x%02x", |
| 2721 | (unsigned char)*p); |
| 2722 | return NULL; |
| 2723 | } |
| 2724 | p++; |
| 2725 | } |
| 2726 | while (*p != '\0' && *p != '%'); |
| 2727 | len = p - f; |
| 2728 | |
| 2729 | if (*p == '\0') |
| 2730 | writer.overallocate = 0; |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2731 | |
| 2732 | if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2733 | goto fail; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2734 | |
| 2735 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2736 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2737 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2738 | return _PyUnicodeWriter_Finish(&writer); |
| 2739 | |
| 2740 | fail: |
| 2741 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2742 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2745 | PyObject * |
| 2746 | PyUnicode_FromFormat(const char *format, ...) |
| 2747 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2748 | PyObject* ret; |
| 2749 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2750 | |
| 2751 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2752 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2753 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2754 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2755 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2756 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2757 | va_end(vargs); |
| 2758 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2761 | #ifdef HAVE_WCHAR_H |
| 2762 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2763 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2764 | convert a Unicode object to a wide character string. |
| 2765 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2766 | - 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] | 2767 | character) required to convert the unicode object. Ignore size argument. |
| 2768 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2769 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2770 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2771 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2772 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2773 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2774 | wchar_t *w, |
| 2775 | Py_ssize_t size) |
| 2776 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2777 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2778 | const wchar_t *wstr; |
| 2779 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2780 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2781 | if (wstr == NULL) |
| 2782 | return -1; |
| 2783 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2784 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2785 | if (size > res) |
| 2786 | size = res + 1; |
| 2787 | else |
| 2788 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2789 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2790 | return res; |
| 2791 | } |
| 2792 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2793 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2797 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2798 | wchar_t *w, |
| 2799 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2800 | { |
| 2801 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2802 | PyErr_BadInternalCall(); |
| 2803 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2804 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2805 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2808 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2809 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2810 | Py_ssize_t *size) |
| 2811 | { |
| 2812 | wchar_t* buffer; |
| 2813 | Py_ssize_t buflen; |
| 2814 | |
| 2815 | if (unicode == NULL) { |
| 2816 | PyErr_BadInternalCall(); |
| 2817 | return NULL; |
| 2818 | } |
| 2819 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2820 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2821 | if (buflen == -1) |
| 2822 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2823 | buffer = PyMem_NEW(wchar_t, buflen); |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2824 | if (buffer == NULL) { |
| 2825 | PyErr_NoMemory(); |
| 2826 | return NULL; |
| 2827 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2828 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2829 | if (buflen == -1) { |
| 2830 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2831 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2832 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2833 | if (size != NULL) |
| 2834 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2835 | return buffer; |
| 2836 | } |
| 2837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2838 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2840 | PyObject * |
| 2841 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2842 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2843 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2844 | PyErr_SetString(PyExc_ValueError, |
| 2845 | "chr() arg not in range(0x110000)"); |
| 2846 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2847 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2848 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2849 | return unicode_char((Py_UCS4)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2852 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2853 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2854 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2855 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2856 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2857 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2858 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2859 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2860 | Py_INCREF(obj); |
| 2861 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2862 | } |
| 2863 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2864 | /* For a Unicode subtype that's not a Unicode object, |
| 2865 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2866 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2867 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2868 | PyErr_Format(PyExc_TypeError, |
| 2869 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2870 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2871 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2874 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2875 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2876 | const char *encoding, |
| 2877 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2878 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2879 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2880 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2881 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2882 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2883 | PyErr_BadInternalCall(); |
| 2884 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2885 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2886 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2887 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2888 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2889 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2890 | _Py_RETURN_UNICODE_EMPTY(); |
| 2891 | v = PyUnicode_Decode( |
| 2892 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2893 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2894 | return v; |
| 2895 | } |
| 2896 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2897 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2898 | PyErr_SetString(PyExc_TypeError, |
| 2899 | "decoding str is not supported"); |
| 2900 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2901 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2902 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2903 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2904 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2905 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 2906 | "coercing to str: need a bytes-like object, %.80s found", |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2907 | Py_TYPE(obj)->tp_name); |
| 2908 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2909 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2910 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2911 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2912 | PyBuffer_Release(&buffer); |
| 2913 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2914 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2915 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2916 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2917 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2918 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2921 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2922 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2923 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2924 | int |
| 2925 | _Py_normalize_encoding(const char *encoding, |
| 2926 | char *lower, |
| 2927 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2928 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2929 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2930 | char *l; |
| 2931 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2932 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2933 | if (encoding == NULL) { |
Victor Stinner | 66b3270 | 2013-11-07 23:12:23 +0100 | [diff] [blame] | 2934 | /* 6 == strlen("utf-8") + 1 */ |
Victor Stinner | df23e30 | 2013-11-07 13:33:36 +0100 | [diff] [blame] | 2935 | if (lower_len < 6) |
| 2936 | return 0; |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2937 | strcpy(lower, "utf-8"); |
| 2938 | return 1; |
| 2939 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2940 | e = encoding; |
| 2941 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2942 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2943 | while (*e) { |
| 2944 | if (l == l_end) |
| 2945 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2946 | if (Py_ISUPPER(*e)) { |
| 2947 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2948 | } |
| 2949 | else if (*e == '_') { |
| 2950 | *l++ = '-'; |
| 2951 | e++; |
| 2952 | } |
| 2953 | else { |
| 2954 | *l++ = *e++; |
| 2955 | } |
| 2956 | } |
| 2957 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2958 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2961 | PyObject * |
| 2962 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2963 | Py_ssize_t size, |
| 2964 | const char *encoding, |
| 2965 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2966 | { |
| 2967 | PyObject *buffer = NULL, *unicode; |
| 2968 | Py_buffer info; |
| 2969 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2970 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2971 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2972 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2973 | if ((strcmp(lower, "utf-8") == 0) || |
| 2974 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2975 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2976 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2977 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 2978 | (strcmp(lower, "iso-8859-1") == 0) || |
| 2979 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2980 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2981 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2982 | else if (strcmp(lower, "mbcs") == 0) |
| 2983 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2984 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2985 | else if (strcmp(lower, "ascii") == 0) |
| 2986 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2987 | else if (strcmp(lower, "utf-16") == 0) |
| 2988 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2989 | else if (strcmp(lower, "utf-32") == 0) |
| 2990 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2991 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2992 | |
| 2993 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2994 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2995 | 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] | 2996 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2997 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | if (buffer == NULL) |
| 2999 | goto onError; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3000 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3001 | if (unicode == NULL) |
| 3002 | goto onError; |
| 3003 | if (!PyUnicode_Check(unicode)) { |
| 3004 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3005 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3006 | "use codecs.decode() to decode to arbitrary types", |
| 3007 | encoding, |
| 3008 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3009 | Py_DECREF(unicode); |
| 3010 | goto onError; |
| 3011 | } |
| 3012 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3013 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3015 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3016 | Py_XDECREF(buffer); |
| 3017 | return NULL; |
| 3018 | } |
| 3019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3020 | PyObject * |
| 3021 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3022 | const char *encoding, |
| 3023 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3024 | { |
| 3025 | PyObject *v; |
| 3026 | |
| 3027 | if (!PyUnicode_Check(unicode)) { |
| 3028 | PyErr_BadArgument(); |
| 3029 | goto onError; |
| 3030 | } |
| 3031 | |
| 3032 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3033 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3034 | |
| 3035 | /* Decode via the codec registry */ |
| 3036 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3037 | if (v == NULL) |
| 3038 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3039 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3040 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3041 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3042 | return NULL; |
| 3043 | } |
| 3044 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3045 | PyObject * |
| 3046 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3047 | const char *encoding, |
| 3048 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3049 | { |
| 3050 | PyObject *v; |
| 3051 | |
| 3052 | if (!PyUnicode_Check(unicode)) { |
| 3053 | PyErr_BadArgument(); |
| 3054 | goto onError; |
| 3055 | } |
| 3056 | |
| 3057 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3058 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3059 | |
| 3060 | /* Decode via the codec registry */ |
| 3061 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3062 | if (v == NULL) |
| 3063 | goto onError; |
| 3064 | if (!PyUnicode_Check(v)) { |
| 3065 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3066 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3067 | "use codecs.decode() to decode to arbitrary types", |
| 3068 | encoding, |
| 3069 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3070 | Py_DECREF(v); |
| 3071 | goto onError; |
| 3072 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3073 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3074 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3075 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3076 | return NULL; |
| 3077 | } |
| 3078 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3079 | PyObject * |
| 3080 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3081 | Py_ssize_t size, |
| 3082 | const char *encoding, |
| 3083 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3084 | { |
| 3085 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3087 | unicode = PyUnicode_FromUnicode(s, size); |
| 3088 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3089 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3091 | Py_DECREF(unicode); |
| 3092 | return v; |
| 3093 | } |
| 3094 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3095 | PyObject * |
| 3096 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3097 | const char *encoding, |
| 3098 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3099 | { |
| 3100 | PyObject *v; |
| 3101 | |
| 3102 | if (!PyUnicode_Check(unicode)) { |
| 3103 | PyErr_BadArgument(); |
| 3104 | goto onError; |
| 3105 | } |
| 3106 | |
| 3107 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3108 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3109 | |
| 3110 | /* Encode via the codec registry */ |
| 3111 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3112 | if (v == NULL) |
| 3113 | goto onError; |
| 3114 | return v; |
| 3115 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3116 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3117 | return NULL; |
| 3118 | } |
| 3119 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3120 | static size_t |
| 3121 | wcstombs_errorpos(const wchar_t *wstr) |
| 3122 | { |
| 3123 | size_t len; |
| 3124 | #if SIZEOF_WCHAR_T == 2 |
| 3125 | wchar_t buf[3]; |
| 3126 | #else |
| 3127 | wchar_t buf[2]; |
| 3128 | #endif |
| 3129 | char outbuf[MB_LEN_MAX]; |
| 3130 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3131 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3132 | #if SIZEOF_WCHAR_T == 2 |
| 3133 | buf[2] = 0; |
| 3134 | #else |
| 3135 | buf[1] = 0; |
| 3136 | #endif |
| 3137 | start = wstr; |
| 3138 | while (*wstr != L'\0') |
| 3139 | { |
| 3140 | previous = wstr; |
| 3141 | #if SIZEOF_WCHAR_T == 2 |
| 3142 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3143 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3144 | { |
| 3145 | buf[0] = wstr[0]; |
| 3146 | buf[1] = wstr[1]; |
| 3147 | wstr += 2; |
| 3148 | } |
| 3149 | else { |
| 3150 | buf[0] = *wstr; |
| 3151 | buf[1] = 0; |
| 3152 | wstr++; |
| 3153 | } |
| 3154 | #else |
| 3155 | buf[0] = *wstr; |
| 3156 | wstr++; |
| 3157 | #endif |
| 3158 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3159 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3160 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3164 | return 0; |
| 3165 | } |
| 3166 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3167 | static int |
| 3168 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3169 | { |
| 3170 | if (errors == NULL) { |
| 3171 | *surrogateescape = 0; |
| 3172 | return 0; |
| 3173 | } |
| 3174 | |
| 3175 | if (strcmp(errors, "strict") == 0) { |
| 3176 | *surrogateescape = 0; |
| 3177 | return 0; |
| 3178 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3179 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3180 | *surrogateescape = 1; |
| 3181 | return 0; |
| 3182 | } |
| 3183 | PyErr_Format(PyExc_ValueError, |
| 3184 | "only 'strict' and 'surrogateescape' error handlers " |
| 3185 | "are supported, not '%s'", |
| 3186 | errors); |
| 3187 | return -1; |
| 3188 | } |
| 3189 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3190 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3191 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3192 | { |
| 3193 | Py_ssize_t wlen, wlen2; |
| 3194 | wchar_t *wstr; |
| 3195 | PyObject *bytes = NULL; |
| 3196 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3197 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3198 | PyObject *exc; |
| 3199 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3200 | int surrogateescape; |
| 3201 | |
| 3202 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3203 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3204 | |
| 3205 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3206 | if (wstr == NULL) |
| 3207 | return NULL; |
| 3208 | |
| 3209 | wlen2 = wcslen(wstr); |
| 3210 | if (wlen2 != wlen) { |
| 3211 | PyMem_Free(wstr); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3212 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3213 | return NULL; |
| 3214 | } |
| 3215 | |
| 3216 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3217 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3218 | char *str; |
| 3219 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3220 | str = Py_EncodeLocale(wstr, &error_pos); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3221 | if (str == NULL) { |
| 3222 | if (error_pos == (size_t)-1) { |
| 3223 | PyErr_NoMemory(); |
| 3224 | PyMem_Free(wstr); |
| 3225 | return NULL; |
| 3226 | } |
| 3227 | else { |
| 3228 | goto encode_error; |
| 3229 | } |
| 3230 | } |
| 3231 | PyMem_Free(wstr); |
| 3232 | |
| 3233 | bytes = PyBytes_FromString(str); |
| 3234 | PyMem_Free(str); |
| 3235 | } |
| 3236 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3237 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3238 | size_t len, len2; |
| 3239 | |
| 3240 | len = wcstombs(NULL, wstr, 0); |
| 3241 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3242 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3243 | goto encode_error; |
| 3244 | } |
| 3245 | |
| 3246 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3247 | if (bytes == NULL) { |
| 3248 | PyMem_Free(wstr); |
| 3249 | return NULL; |
| 3250 | } |
| 3251 | |
| 3252 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3253 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3254 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3255 | goto encode_error; |
| 3256 | } |
| 3257 | PyMem_Free(wstr); |
| 3258 | } |
| 3259 | return bytes; |
| 3260 | |
| 3261 | encode_error: |
| 3262 | errmsg = strerror(errno); |
| 3263 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3264 | |
| 3265 | if (error_pos == (size_t)-1) |
| 3266 | error_pos = wcstombs_errorpos(wstr); |
| 3267 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3268 | PyMem_Free(wstr); |
| 3269 | Py_XDECREF(bytes); |
| 3270 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3271 | if (errmsg != NULL) { |
| 3272 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3273 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3274 | if (wstr != NULL) { |
| 3275 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3276 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3277 | } else |
| 3278 | errmsg = NULL; |
| 3279 | } |
| 3280 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3281 | reason = PyUnicode_FromString( |
| 3282 | "wcstombs() encountered an unencodable " |
| 3283 | "wide character"); |
| 3284 | if (reason == NULL) |
| 3285 | return NULL; |
| 3286 | |
| 3287 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3288 | "locale", unicode, |
| 3289 | (Py_ssize_t)error_pos, |
| 3290 | (Py_ssize_t)(error_pos+1), |
| 3291 | reason); |
| 3292 | Py_DECREF(reason); |
| 3293 | if (exc != NULL) { |
| 3294 | PyCodec_StrictErrors(exc); |
| 3295 | Py_XDECREF(exc); |
| 3296 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3297 | return NULL; |
| 3298 | } |
| 3299 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3300 | PyObject * |
| 3301 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3302 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3303 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3304 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3305 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3306 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3307 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3308 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3309 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3310 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3311 | the Python codec requires to encode at least its own filename. Use the C |
| 3312 | version of the locale codec until the codec registry is initialized and |
| 3313 | the Python codec is loaded. |
| 3314 | |
| 3315 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3316 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3317 | subinterpreters. */ |
| 3318 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3319 | return PyUnicode_AsEncodedString(unicode, |
| 3320 | Py_FileSystemDefaultEncoding, |
| 3321 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3322 | } |
| 3323 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3324 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3325 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3326 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3329 | PyObject * |
| 3330 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3331 | const char *encoding, |
| 3332 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3333 | { |
| 3334 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3335 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3337 | if (!PyUnicode_Check(unicode)) { |
| 3338 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3339 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3340 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3341 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3342 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3343 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3344 | if ((strcmp(lower, "utf-8") == 0) || |
| 3345 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3346 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3347 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3348 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3349 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3350 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3351 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3352 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3353 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3354 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3355 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3356 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3357 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3358 | else if (strcmp(lower, "mbcs") == 0) |
| 3359 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3360 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3361 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3362 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3363 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3364 | |
| 3365 | /* Encode via the codec registry */ |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3366 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3367 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3368 | return NULL; |
| 3369 | |
| 3370 | /* The normal path */ |
| 3371 | if (PyBytes_Check(v)) |
| 3372 | return v; |
| 3373 | |
| 3374 | /* 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] | 3375 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3376 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3377 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3378 | |
| 3379 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3380 | "encoder %s returned bytearray instead of bytes; " |
| 3381 | "use codecs.encode() to encode to arbitrary types", |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3382 | encoding); |
| 3383 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3384 | Py_DECREF(v); |
| 3385 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3386 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3387 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3388 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3389 | Py_DECREF(v); |
| 3390 | return b; |
| 3391 | } |
| 3392 | |
| 3393 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3394 | "'%.400s' encoder returned '%.400s' instead of 'bytes'; " |
| 3395 | "use codecs.encode() to encode to arbitrary types", |
| 3396 | encoding, |
| 3397 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3398 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3399 | return NULL; |
| 3400 | } |
| 3401 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3402 | PyObject * |
| 3403 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3404 | const char *encoding, |
| 3405 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3406 | { |
| 3407 | PyObject *v; |
| 3408 | |
| 3409 | if (!PyUnicode_Check(unicode)) { |
| 3410 | PyErr_BadArgument(); |
| 3411 | goto onError; |
| 3412 | } |
| 3413 | |
| 3414 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3415 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3416 | |
| 3417 | /* Encode via the codec registry */ |
| 3418 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3419 | if (v == NULL) |
| 3420 | goto onError; |
| 3421 | if (!PyUnicode_Check(v)) { |
| 3422 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3423 | "'%.400s' encoder returned '%.400s' instead of 'str'; " |
| 3424 | "use codecs.encode() to encode to arbitrary types", |
| 3425 | encoding, |
| 3426 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3427 | Py_DECREF(v); |
| 3428 | goto onError; |
| 3429 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3430 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3431 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3432 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3433 | return NULL; |
| 3434 | } |
| 3435 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3436 | static size_t |
| 3437 | mbstowcs_errorpos(const char *str, size_t len) |
| 3438 | { |
| 3439 | #ifdef HAVE_MBRTOWC |
| 3440 | const char *start = str; |
| 3441 | mbstate_t mbs; |
| 3442 | size_t converted; |
| 3443 | wchar_t ch; |
| 3444 | |
| 3445 | memset(&mbs, 0, sizeof mbs); |
| 3446 | while (len) |
| 3447 | { |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3448 | converted = mbrtowc(&ch, str, len, &mbs); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3449 | if (converted == 0) |
| 3450 | /* Reached end of string */ |
| 3451 | break; |
| 3452 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3453 | /* Conversion error or incomplete character */ |
| 3454 | return str - start; |
| 3455 | } |
| 3456 | else { |
| 3457 | str += converted; |
| 3458 | len -= converted; |
| 3459 | } |
| 3460 | } |
| 3461 | /* failed to find the undecodable byte sequence */ |
| 3462 | return 0; |
| 3463 | #endif |
| 3464 | return 0; |
| 3465 | } |
| 3466 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3467 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3468 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3469 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3470 | { |
| 3471 | wchar_t smallbuf[256]; |
| 3472 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3473 | wchar_t *wstr; |
| 3474 | size_t wlen, wlen2; |
| 3475 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3476 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3477 | size_t error_pos; |
| 3478 | char *errmsg; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 3479 | PyObject *reason = NULL; /* initialize to prevent gcc warning */ |
| 3480 | PyObject *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3481 | |
| 3482 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3483 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3484 | |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3485 | if (str[len] != '\0' || (size_t)len != strlen(str)) { |
| 3486 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3487 | return NULL; |
| 3488 | } |
| 3489 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3490 | if (surrogateescape) { |
| 3491 | /* "surrogateescape" error handler */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3492 | wstr = Py_DecodeLocale(str, &wlen); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3493 | if (wstr == NULL) { |
| 3494 | if (wlen == (size_t)-1) |
| 3495 | PyErr_NoMemory(); |
| 3496 | else |
| 3497 | PyErr_SetFromErrno(PyExc_OSError); |
| 3498 | return NULL; |
| 3499 | } |
| 3500 | |
| 3501 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3502 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3503 | } |
| 3504 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3505 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3506 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3507 | wlen = mbstowcs(NULL, str, 0); |
| 3508 | #else |
| 3509 | wlen = len; |
| 3510 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3511 | if (wlen == (size_t)-1) |
| 3512 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3513 | if (wlen+1 <= smallbuf_len) { |
| 3514 | wstr = smallbuf; |
| 3515 | } |
| 3516 | else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3517 | wstr = PyMem_New(wchar_t, wlen+1); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3518 | if (!wstr) |
| 3519 | return PyErr_NoMemory(); |
| 3520 | } |
| 3521 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3522 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3523 | if (wlen2 == (size_t)-1) { |
| 3524 | if (wstr != smallbuf) |
| 3525 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3526 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3527 | } |
| 3528 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3529 | assert(wlen2 == wlen); |
| 3530 | #endif |
| 3531 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3532 | if (wstr != smallbuf) |
| 3533 | PyMem_Free(wstr); |
| 3534 | } |
| 3535 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3536 | |
| 3537 | decode_error: |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3538 | reason = NULL; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3539 | errmsg = strerror(errno); |
| 3540 | assert(errmsg != NULL); |
| 3541 | |
| 3542 | error_pos = mbstowcs_errorpos(str, len); |
| 3543 | if (errmsg != NULL) { |
| 3544 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3545 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3546 | if (wstr != NULL) { |
| 3547 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3548 | PyMem_RawFree(wstr); |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3549 | } |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3550 | } |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3551 | if (reason == NULL) |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3552 | reason = PyUnicode_FromString( |
| 3553 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3554 | if (reason == NULL) |
| 3555 | return NULL; |
| 3556 | |
| 3557 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3558 | "locale", str, len, |
| 3559 | (Py_ssize_t)error_pos, |
| 3560 | (Py_ssize_t)(error_pos+1), |
| 3561 | reason); |
| 3562 | Py_DECREF(reason); |
| 3563 | if (exc != NULL) { |
| 3564 | PyCodec_StrictErrors(exc); |
| 3565 | Py_XDECREF(exc); |
| 3566 | } |
| 3567 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3571 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3572 | { |
| 3573 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3574 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | |
| 3578 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3579 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3580 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3581 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3582 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3583 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3584 | PyObject* |
| 3585 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3586 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3587 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3588 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3589 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3590 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3591 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3592 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3593 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3594 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3595 | the Python codec requires to encode at least its own filename. Use the C |
| 3596 | version of the locale codec until the codec registry is initialized and |
| 3597 | the Python codec is loaded. |
| 3598 | |
| 3599 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3600 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3601 | subinterpreters. */ |
| 3602 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3603 | return PyUnicode_Decode(s, size, |
| 3604 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3605 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3606 | } |
| 3607 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3608 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3609 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3610 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3613 | |
| 3614 | int |
| 3615 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3616 | { |
| 3617 | PyObject *output = NULL; |
| 3618 | Py_ssize_t size; |
| 3619 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3620 | if (arg == NULL) { |
| 3621 | Py_DECREF(*(PyObject**)addr); |
Benjamin Peterson | a4d33b3 | 2015-11-15 21:57:39 -0800 | [diff] [blame] | 3622 | *(PyObject**)addr = NULL; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3623 | return 1; |
| 3624 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3625 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3626 | output = arg; |
| 3627 | Py_INCREF(output); |
| 3628 | } |
| 3629 | else { |
| 3630 | arg = PyUnicode_FromObject(arg); |
| 3631 | if (!arg) |
| 3632 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3633 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3634 | Py_DECREF(arg); |
| 3635 | if (!output) |
| 3636 | return 0; |
| 3637 | if (!PyBytes_Check(output)) { |
| 3638 | Py_DECREF(output); |
| 3639 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3640 | return 0; |
| 3641 | } |
| 3642 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3643 | size = PyBytes_GET_SIZE(output); |
| 3644 | data = PyBytes_AS_STRING(output); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 3645 | if ((size_t)size != strlen(data)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3646 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3647 | Py_DECREF(output); |
| 3648 | return 0; |
| 3649 | } |
| 3650 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3651 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3655 | int |
| 3656 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3657 | { |
| 3658 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3659 | if (arg == NULL) { |
| 3660 | Py_DECREF(*(PyObject**)addr); |
| 3661 | return 1; |
| 3662 | } |
| 3663 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3664 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3665 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3666 | output = arg; |
| 3667 | Py_INCREF(output); |
| 3668 | } |
Serhiy Storchaka | 9305d83 | 2016-06-18 13:53:36 +0300 | [diff] [blame] | 3669 | else if (PyObject_CheckBuffer(arg)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3670 | arg = PyBytes_FromObject(arg); |
| 3671 | if (!arg) |
| 3672 | return 0; |
| 3673 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3674 | PyBytes_GET_SIZE(arg)); |
| 3675 | Py_DECREF(arg); |
| 3676 | if (!output) |
| 3677 | return 0; |
| 3678 | if (!PyUnicode_Check(output)) { |
| 3679 | Py_DECREF(output); |
| 3680 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3681 | return 0; |
| 3682 | } |
| 3683 | } |
Serhiy Storchaka | 9305d83 | 2016-06-18 13:53:36 +0300 | [diff] [blame] | 3684 | else { |
| 3685 | PyErr_Format(PyExc_TypeError, |
| 3686 | "path should be string or bytes, not %.200s", |
| 3687 | Py_TYPE(arg)->tp_name); |
| 3688 | return 0; |
| 3689 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3690 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3691 | Py_DECREF(output); |
| 3692 | return 0; |
| 3693 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3694 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3695 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3696 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3697 | Py_DECREF(output); |
| 3698 | return 0; |
| 3699 | } |
| 3700 | *(PyObject**)addr = output; |
| 3701 | return Py_CLEANUP_SUPPORTED; |
| 3702 | } |
| 3703 | |
| 3704 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3705 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3706 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3707 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3708 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3709 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3710 | if (!PyUnicode_Check(unicode)) { |
| 3711 | PyErr_BadArgument(); |
| 3712 | return NULL; |
| 3713 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3714 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3715 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3716 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3717 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3718 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3719 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3720 | if (bytes == NULL) |
| 3721 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3722 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3723 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3724 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3725 | Py_DECREF(bytes); |
| 3726 | return NULL; |
| 3727 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3728 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3729 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3730 | PyBytes_AS_STRING(bytes), |
| 3731 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3732 | Py_DECREF(bytes); |
| 3733 | } |
| 3734 | |
| 3735 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3736 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3737 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3741 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3742 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3743 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3744 | } |
| 3745 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3746 | Py_UNICODE * |
| 3747 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3748 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3749 | const unsigned char *one_byte; |
| 3750 | #if SIZEOF_WCHAR_T == 4 |
| 3751 | const Py_UCS2 *two_bytes; |
| 3752 | #else |
| 3753 | const Py_UCS4 *four_bytes; |
| 3754 | const Py_UCS4 *ucs4_end; |
| 3755 | Py_ssize_t num_surrogates; |
| 3756 | #endif |
| 3757 | wchar_t *w; |
| 3758 | wchar_t *wchar_end; |
| 3759 | |
| 3760 | if (!PyUnicode_Check(unicode)) { |
| 3761 | PyErr_BadArgument(); |
| 3762 | return NULL; |
| 3763 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3764 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3765 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3766 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3767 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3768 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3769 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3770 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3771 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3772 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3773 | num_surrogates = 0; |
| 3774 | |
| 3775 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3776 | if (*four_bytes > 0xFFFF) |
| 3777 | ++num_surrogates; |
| 3778 | } |
| 3779 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3780 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3781 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3782 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3783 | PyErr_NoMemory(); |
| 3784 | return NULL; |
| 3785 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3786 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3787 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3788 | w = _PyUnicode_WSTR(unicode); |
| 3789 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3790 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3791 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3792 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3793 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3794 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3795 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3796 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3797 | } |
| 3798 | else |
| 3799 | *w = *four_bytes; |
| 3800 | |
| 3801 | if (w > wchar_end) { |
| 3802 | assert(0 && "Miscalculated string end"); |
| 3803 | } |
| 3804 | } |
| 3805 | *w = 0; |
| 3806 | #else |
| 3807 | /* sizeof(wchar_t) == 4 */ |
| 3808 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3809 | "should share memory already."); |
| 3810 | return NULL; |
| 3811 | #endif |
| 3812 | } |
| 3813 | else { |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 3814 | if ((size_t)_PyUnicode_LENGTH(unicode) > |
| 3815 | PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 3816 | PyErr_NoMemory(); |
| 3817 | return NULL; |
| 3818 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3819 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3820 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3821 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3822 | PyErr_NoMemory(); |
| 3823 | return NULL; |
| 3824 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3825 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3826 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3827 | w = _PyUnicode_WSTR(unicode); |
| 3828 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3829 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3830 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3831 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3832 | for (; w < wchar_end; ++one_byte, ++w) |
| 3833 | *w = *one_byte; |
| 3834 | /* null-terminate the wstr */ |
| 3835 | *w = 0; |
| 3836 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3837 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3838 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3839 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3840 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3841 | *w = *two_bytes; |
| 3842 | /* null-terminate the wstr */ |
| 3843 | *w = 0; |
| 3844 | #else |
| 3845 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3846 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3847 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3848 | Py_FatalError("Impossible unicode object state, wstr " |
| 3849 | "and str should share memory already."); |
| 3850 | return NULL; |
| 3851 | #endif |
| 3852 | } |
| 3853 | else { |
| 3854 | assert(0 && "This should never happen."); |
| 3855 | } |
| 3856 | } |
| 3857 | } |
| 3858 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3859 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3860 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3863 | Py_UNICODE * |
| 3864 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3865 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3866 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3869 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3870 | Py_ssize_t |
| 3871 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3872 | { |
| 3873 | if (!PyUnicode_Check(unicode)) { |
| 3874 | PyErr_BadArgument(); |
| 3875 | goto onError; |
| 3876 | } |
| 3877 | return PyUnicode_GET_SIZE(unicode); |
| 3878 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3879 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3880 | return -1; |
| 3881 | } |
| 3882 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3883 | Py_ssize_t |
| 3884 | PyUnicode_GetLength(PyObject *unicode) |
| 3885 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3886 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3887 | PyErr_BadArgument(); |
| 3888 | return -1; |
| 3889 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3890 | if (PyUnicode_READY(unicode) == -1) |
| 3891 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3892 | return PyUnicode_GET_LENGTH(unicode); |
| 3893 | } |
| 3894 | |
| 3895 | Py_UCS4 |
| 3896 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3897 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3898 | void *data; |
| 3899 | int kind; |
| 3900 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3901 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3902 | PyErr_BadArgument(); |
| 3903 | return (Py_UCS4)-1; |
| 3904 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3905 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3906 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3907 | return (Py_UCS4)-1; |
| 3908 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3909 | data = PyUnicode_DATA(unicode); |
| 3910 | kind = PyUnicode_KIND(unicode); |
| 3911 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | int |
| 3915 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3916 | { |
| 3917 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3918 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3919 | return -1; |
| 3920 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3921 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3922 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3923 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3924 | return -1; |
| 3925 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3926 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3927 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3928 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3929 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3930 | return -1; |
| 3931 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3932 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3933 | index, ch); |
| 3934 | return 0; |
| 3935 | } |
| 3936 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3937 | const char * |
| 3938 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3939 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3940 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3943 | /* create or adjust a UnicodeDecodeError */ |
| 3944 | static void |
| 3945 | make_decode_exception(PyObject **exceptionObject, |
| 3946 | const char *encoding, |
| 3947 | const char *input, Py_ssize_t length, |
| 3948 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3949 | const char *reason) |
| 3950 | { |
| 3951 | if (*exceptionObject == NULL) { |
| 3952 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3953 | encoding, input, length, startpos, endpos, reason); |
| 3954 | } |
| 3955 | else { |
| 3956 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3957 | goto onError; |
| 3958 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3959 | goto onError; |
| 3960 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3961 | goto onError; |
| 3962 | } |
| 3963 | return; |
| 3964 | |
| 3965 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3966 | Py_CLEAR(*exceptionObject); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3969 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3970 | /* error handling callback helper: |
| 3971 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3972 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3973 | and adjust various state variables. |
| 3974 | return 0 on success, -1 on error |
| 3975 | */ |
| 3976 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3977 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3978 | unicode_decode_call_errorhandler_wchar( |
| 3979 | const char *errors, PyObject **errorHandler, |
| 3980 | const char *encoding, const char *reason, |
| 3981 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3982 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3983 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3984 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3985 | 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] | 3986 | |
| 3987 | PyObject *restuple = NULL; |
| 3988 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3989 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3990 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3991 | Py_ssize_t requiredsize; |
| 3992 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3993 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3994 | wchar_t *repwstr; |
| 3995 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3996 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3997 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 3998 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3999 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4000 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4001 | *errorHandler = PyCodec_LookupError(errors); |
| 4002 | if (*errorHandler == NULL) |
| 4003 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4006 | make_decode_exception(exceptionObject, |
| 4007 | encoding, |
| 4008 | *input, *inend - *input, |
| 4009 | *startinpos, *endinpos, |
| 4010 | reason); |
| 4011 | if (*exceptionObject == NULL) |
| 4012 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4013 | |
| 4014 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4015 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4016 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4017 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4018 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4019 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4020 | } |
| 4021 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4022 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4023 | |
| 4024 | /* Copy back the bytes variables, which might have been modified by the |
| 4025 | callback */ |
| 4026 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4027 | if (!inputobj) |
| 4028 | goto onError; |
| 4029 | if (!PyBytes_Check(inputobj)) { |
| 4030 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4031 | } |
| 4032 | *input = PyBytes_AS_STRING(inputobj); |
| 4033 | insize = PyBytes_GET_SIZE(inputobj); |
| 4034 | *inend = *input + insize; |
| 4035 | /* we can DECREF safely, as the exception has another reference, |
| 4036 | so the object won't go away. */ |
| 4037 | Py_DECREF(inputobj); |
| 4038 | |
| 4039 | if (newpos<0) |
| 4040 | newpos = insize+newpos; |
| 4041 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4042 | 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] | 4043 | goto onError; |
| 4044 | } |
| 4045 | |
| 4046 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4047 | if (repwstr == NULL) |
| 4048 | goto onError; |
| 4049 | /* need more space? (at least enough for what we |
| 4050 | have+the replacement+the rest of the string (starting |
| 4051 | at the new input position), so we won't have to check space |
| 4052 | when there are no errors in the rest of the string) */ |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4053 | requiredsize = *outpos; |
| 4054 | if (requiredsize > PY_SSIZE_T_MAX - repwlen) |
| 4055 | goto overflow; |
| 4056 | requiredsize += repwlen; |
| 4057 | if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) |
| 4058 | goto overflow; |
| 4059 | requiredsize += insize - newpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4060 | if (requiredsize > outsize) { |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4061 | if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4062 | requiredsize = 2*outsize; |
| 4063 | if (unicode_resize(output, requiredsize) < 0) |
| 4064 | goto onError; |
| 4065 | } |
| 4066 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4067 | *outpos += repwlen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4068 | *endinpos = newpos; |
| 4069 | *inptr = *input + newpos; |
| 4070 | |
| 4071 | /* we made it! */ |
| 4072 | Py_XDECREF(restuple); |
| 4073 | return 0; |
| 4074 | |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4075 | overflow: |
| 4076 | PyErr_SetString(PyExc_OverflowError, |
| 4077 | "decoded result is too long for a Python string"); |
| 4078 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4079 | onError: |
| 4080 | Py_XDECREF(restuple); |
| 4081 | return -1; |
| 4082 | } |
| 4083 | #endif /* HAVE_MBCS */ |
| 4084 | |
| 4085 | static int |
| 4086 | unicode_decode_call_errorhandler_writer( |
| 4087 | const char *errors, PyObject **errorHandler, |
| 4088 | const char *encoding, const char *reason, |
| 4089 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4090 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4091 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4092 | { |
| 4093 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4094 | |
| 4095 | PyObject *restuple = NULL; |
| 4096 | PyObject *repunicode = NULL; |
| 4097 | Py_ssize_t insize; |
| 4098 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4099 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4100 | PyObject *inputobj = NULL; |
| 4101 | |
| 4102 | if (*errorHandler == NULL) { |
| 4103 | *errorHandler = PyCodec_LookupError(errors); |
| 4104 | if (*errorHandler == NULL) |
| 4105 | goto onError; |
| 4106 | } |
| 4107 | |
| 4108 | make_decode_exception(exceptionObject, |
| 4109 | encoding, |
| 4110 | *input, *inend - *input, |
| 4111 | *startinpos, *endinpos, |
| 4112 | reason); |
| 4113 | if (*exceptionObject == NULL) |
| 4114 | goto onError; |
| 4115 | |
| 4116 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4117 | if (restuple == NULL) |
| 4118 | goto onError; |
| 4119 | if (!PyTuple_Check(restuple)) { |
| 4120 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4121 | goto onError; |
| 4122 | } |
| 4123 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4124 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4125 | |
| 4126 | /* Copy back the bytes variables, which might have been modified by the |
| 4127 | callback */ |
| 4128 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4129 | if (!inputobj) |
| 4130 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4131 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4132 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4133 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4134 | *input = PyBytes_AS_STRING(inputobj); |
| 4135 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4136 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4137 | /* we can DECREF safely, as the exception has another reference, |
| 4138 | so the object won't go away. */ |
| 4139 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4140 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4141 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4142 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4143 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4144 | 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] | 4145 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4146 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4147 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4148 | if (PyUnicode_READY(repunicode) < 0) |
| 4149 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4150 | replen = PyUnicode_GET_LENGTH(repunicode); |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4151 | if (replen > 1) { |
| 4152 | writer->min_length += replen - 1; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4153 | writer->overallocate = 1; |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4154 | if (_PyUnicodeWriter_Prepare(writer, writer->min_length, |
| 4155 | PyUnicode_MAX_CHAR_VALUE(repunicode)) == -1) |
| 4156 | goto onError; |
| 4157 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4158 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4159 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4160 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4161 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4162 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4163 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4164 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4165 | Py_XDECREF(restuple); |
| 4166 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4167 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4168 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4169 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4170 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4173 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4174 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4175 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4176 | |
| 4177 | /* Three simple macros defining base-64. */ |
| 4178 | |
| 4179 | /* Is c a base-64 character? */ |
| 4180 | |
| 4181 | #define IS_BASE64(c) \ |
| 4182 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4183 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4184 | ((c) >= '0' && (c) <= '9') || \ |
| 4185 | (c) == '+' || (c) == '/') |
| 4186 | |
| 4187 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4188 | |
| 4189 | #define FROM_BASE64(c) \ |
| 4190 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4191 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4192 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4193 | (c) == '+' ? 62 : 63) |
| 4194 | |
| 4195 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4196 | |
| 4197 | #define TO_BASE64(n) \ |
| 4198 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4199 | |
| 4200 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4201 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4202 | * byte not decoding to itself is the + which begins a base64 |
| 4203 | * string. */ |
| 4204 | |
| 4205 | #define DECODE_DIRECT(c) \ |
| 4206 | ((c) <= 127 && (c) != '+') |
| 4207 | |
| 4208 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4209 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4210 | * the above). See RFC2152. This array identifies these different |
| 4211 | * sets: |
| 4212 | * 0 : "Set D" |
| 4213 | * alphanumeric and '(),-./:? |
| 4214 | * 1 : "Set O" |
| 4215 | * !"#$%&*;<=>@[]^_`{|} |
| 4216 | * 2 : "whitespace" |
| 4217 | * ht nl cr sp |
| 4218 | * 3 : special (must be base64 encoded) |
| 4219 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4220 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4221 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4222 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4223 | char utf7_category[128] = { |
| 4224 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4225 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4226 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4227 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4228 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4229 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4230 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4231 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4232 | /* @ A B C D E F G H I J K L M N O */ |
| 4233 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4234 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4235 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4236 | /* ` a b c d e f g h i j k l m n o */ |
| 4237 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4238 | /* p q r s t u v w x y z { | } ~ del */ |
| 4239 | 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] | 4240 | }; |
| 4241 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4242 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4243 | * answer depends on whether we are encoding set O as itself, and also |
| 4244 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4245 | * clear that the answers to these questions vary between |
| 4246 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4247 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4248 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4249 | ((c) < 128 && (c) > 0 && \ |
| 4250 | ((utf7_category[(c)] == 0) || \ |
| 4251 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4252 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4253 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4254 | PyObject * |
| 4255 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4256 | Py_ssize_t size, |
| 4257 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4258 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4259 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4260 | } |
| 4261 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4262 | /* The decoder. The only state we preserve is our read position, |
| 4263 | * i.e. how many characters we have consumed. So if we end in the |
| 4264 | * middle of a shift sequence we have to back off the read position |
| 4265 | * and the output to the beginning of the sequence, otherwise we lose |
| 4266 | * all the shift state (seen bits, number of bits seen, high |
| 4267 | * surrogate). */ |
| 4268 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4269 | PyObject * |
| 4270 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4271 | Py_ssize_t size, |
| 4272 | const char *errors, |
| 4273 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4274 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4275 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4276 | Py_ssize_t startinpos; |
| 4277 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4278 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4279 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4280 | const char *errmsg = ""; |
| 4281 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4282 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4283 | unsigned int base64bits = 0; |
| 4284 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4285 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4286 | PyObject *errorHandler = NULL; |
| 4287 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4288 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4289 | if (size == 0) { |
| 4290 | if (consumed) |
| 4291 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4292 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4293 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4294 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4295 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4296 | _PyUnicodeWriter_Init(&writer); |
| 4297 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4298 | |
| 4299 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4300 | e = s + size; |
| 4301 | |
| 4302 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4303 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4304 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4305 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4306 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4307 | if (inShift) { /* in a base-64 section */ |
| 4308 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4309 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4310 | base64bits += 6; |
| 4311 | s++; |
| 4312 | if (base64bits >= 16) { |
| 4313 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4314 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4315 | base64bits -= 16; |
| 4316 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4317 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4318 | if (surrogate) { |
| 4319 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4320 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4321 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4322 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4323 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4324 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4325 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4326 | } |
| 4327 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4328 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4329 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4330 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4331 | } |
| 4332 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4333 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4334 | /* first surrogate */ |
| 4335 | surrogate = outCh; |
| 4336 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4337 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4338 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4339 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4340 | } |
| 4341 | } |
| 4342 | } |
| 4343 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4344 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4345 | if (base64bits > 0) { /* left-over bits */ |
| 4346 | if (base64bits >= 6) { |
| 4347 | /* We've seen at least one base-64 character */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4348 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4349 | errmsg = "partial character in shift sequence"; |
| 4350 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4351 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4352 | else { |
| 4353 | /* Some bits remain; they should be zero */ |
| 4354 | if (base64buffer != 0) { |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4355 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4356 | errmsg = "non-zero padding bits in shift sequence"; |
| 4357 | goto utf7Error; |
| 4358 | } |
| 4359 | } |
| 4360 | } |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4361 | if (surrogate && DECODE_DIRECT(ch)) { |
| 4362 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
| 4363 | goto onError; |
| 4364 | } |
| 4365 | surrogate = 0; |
| 4366 | if (ch == '-') { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4367 | /* '-' is absorbed; other terminating |
| 4368 | characters are preserved */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4369 | s++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4370 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4371 | } |
| 4372 | } |
| 4373 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4374 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4375 | s++; /* consume '+' */ |
| 4376 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4377 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4378 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4379 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4380 | } |
| 4381 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4382 | inShift = 1; |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4383 | surrogate = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4384 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4385 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4386 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4387 | } |
| 4388 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4389 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4390 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4391 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4392 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4393 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4394 | else { |
| 4395 | startinpos = s-starts; |
| 4396 | s++; |
| 4397 | errmsg = "unexpected special character"; |
| 4398 | goto utf7Error; |
| 4399 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4400 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4401 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4402 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4403 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4404 | errors, &errorHandler, |
| 4405 | "utf7", errmsg, |
| 4406 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4407 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4408 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4411 | /* end of string */ |
| 4412 | |
| 4413 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4414 | /* if we're in an inconsistent state, that's an error */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4415 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4416 | if (surrogate || |
| 4417 | (base64bits >= 6) || |
| 4418 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4419 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4420 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4421 | errors, &errorHandler, |
| 4422 | "utf7", "unterminated shift sequence", |
| 4423 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4424 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4425 | goto onError; |
| 4426 | if (s < e) |
| 4427 | goto restart; |
| 4428 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4429 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4430 | |
| 4431 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4432 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | if (inShift) { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4434 | *consumed = startinpos; |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4435 | if (writer.pos != shiftOutStart && writer.maxchar > 127) { |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4436 | PyObject *result = PyUnicode_FromKindAndData( |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4437 | writer.kind, writer.data, shiftOutStart); |
| 4438 | Py_XDECREF(errorHandler); |
| 4439 | Py_XDECREF(exc); |
| 4440 | _PyUnicodeWriter_Dealloc(&writer); |
| 4441 | return result; |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4442 | } |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4443 | writer.pos = shiftOutStart; /* back off output */ |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4444 | } |
| 4445 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4446 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4447 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4448 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4449 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4450 | Py_XDECREF(errorHandler); |
| 4451 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4452 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4453 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4454 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4455 | Py_XDECREF(errorHandler); |
| 4456 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4457 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4458 | return NULL; |
| 4459 | } |
| 4460 | |
| 4461 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4462 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4463 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4464 | int base64SetO, |
| 4465 | int base64WhiteSpace, |
| 4466 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4467 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4468 | int kind; |
| 4469 | void *data; |
| 4470 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4471 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4472 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4473 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4474 | unsigned int base64bits = 0; |
| 4475 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4476 | char * out; |
| 4477 | char * start; |
| 4478 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4479 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4480 | return NULL; |
| 4481 | kind = PyUnicode_KIND(str); |
| 4482 | data = PyUnicode_DATA(str); |
| 4483 | len = PyUnicode_GET_LENGTH(str); |
| 4484 | |
| 4485 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4486 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4487 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4488 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4489 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4490 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4491 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4492 | if (v == NULL) |
| 4493 | return NULL; |
| 4494 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4495 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4496 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4497 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4498 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4499 | if (inShift) { |
| 4500 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4501 | /* shifting out */ |
| 4502 | if (base64bits) { /* output remaining bits */ |
| 4503 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4504 | base64buffer = 0; |
| 4505 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4506 | } |
| 4507 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4508 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4509 | so no '-' is required, except if the character is itself a '-' */ |
| 4510 | if (IS_BASE64(ch) || ch == '-') { |
| 4511 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4512 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4513 | *out++ = (char) ch; |
| 4514 | } |
| 4515 | else { |
| 4516 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4517 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4518 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4519 | else { /* not in a shift sequence */ |
| 4520 | if (ch == '+') { |
| 4521 | *out++ = '+'; |
| 4522 | *out++ = '-'; |
| 4523 | } |
| 4524 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4525 | *out++ = (char) ch; |
| 4526 | } |
| 4527 | else { |
| 4528 | *out++ = '+'; |
| 4529 | inShift = 1; |
| 4530 | goto encode_char; |
| 4531 | } |
| 4532 | } |
| 4533 | continue; |
| 4534 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4535 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4536 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4537 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4538 | /* code first surrogate */ |
| 4539 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4540 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4541 | while (base64bits >= 6) { |
| 4542 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4543 | base64bits -= 6; |
| 4544 | } |
| 4545 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4546 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4547 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4548 | base64bits += 16; |
| 4549 | base64buffer = (base64buffer << 16) | ch; |
| 4550 | while (base64bits >= 6) { |
| 4551 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4552 | base64bits -= 6; |
| 4553 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4554 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4555 | if (base64bits) |
| 4556 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4557 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4558 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4559 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4560 | return NULL; |
| 4561 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4562 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4563 | PyObject * |
| 4564 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4565 | Py_ssize_t size, |
| 4566 | int base64SetO, |
| 4567 | int base64WhiteSpace, |
| 4568 | const char *errors) |
| 4569 | { |
| 4570 | PyObject *result; |
| 4571 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4572 | if (tmp == NULL) |
| 4573 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4574 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4575 | base64WhiteSpace, errors); |
| 4576 | Py_DECREF(tmp); |
| 4577 | return result; |
| 4578 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4579 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4580 | #undef IS_BASE64 |
| 4581 | #undef FROM_BASE64 |
| 4582 | #undef TO_BASE64 |
| 4583 | #undef DECODE_DIRECT |
| 4584 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4585 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4586 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4587 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4588 | PyObject * |
| 4589 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4590 | Py_ssize_t size, |
| 4591 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4592 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4593 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4594 | } |
| 4595 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4596 | #include "stringlib/asciilib.h" |
| 4597 | #include "stringlib/codecs.h" |
| 4598 | #include "stringlib/undef.h" |
| 4599 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4600 | #include "stringlib/ucs1lib.h" |
| 4601 | #include "stringlib/codecs.h" |
| 4602 | #include "stringlib/undef.h" |
| 4603 | |
| 4604 | #include "stringlib/ucs2lib.h" |
| 4605 | #include "stringlib/codecs.h" |
| 4606 | #include "stringlib/undef.h" |
| 4607 | |
| 4608 | #include "stringlib/ucs4lib.h" |
| 4609 | #include "stringlib/codecs.h" |
| 4610 | #include "stringlib/undef.h" |
| 4611 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4612 | /* Mask to quickly check whether a C 'long' contains a |
| 4613 | non-ASCII, UTF8-encoded char. */ |
| 4614 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4615 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4616 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4617 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4618 | #else |
| 4619 | # error C 'long' size should be either 4 or 8! |
| 4620 | #endif |
| 4621 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4622 | static Py_ssize_t |
| 4623 | 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] | 4624 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4625 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4626 | 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] | 4627 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4628 | /* |
| 4629 | * Issue #17237: m68k is a bit different from most architectures in |
| 4630 | * that objects do not use "natural alignment" - for example, int and |
| 4631 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4632 | * won't work; also, tests have shown that skipping the "optimised |
| 4633 | * version" will even speed up m68k. |
| 4634 | */ |
| 4635 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4636 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4637 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4638 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4639 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4640 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4641 | /* Help allocation */ |
| 4642 | const char *_p = p; |
| 4643 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4644 | while (_p < aligned_end) { |
| 4645 | unsigned long value = *(const unsigned long *) _p; |
| 4646 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4647 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4648 | *((unsigned long *)q) = value; |
| 4649 | _p += SIZEOF_LONG; |
| 4650 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4651 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4652 | p = _p; |
| 4653 | while (p < end) { |
| 4654 | if ((unsigned char)*p & 0x80) |
| 4655 | break; |
| 4656 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4657 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4658 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4659 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4660 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4661 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4662 | while (p < end) { |
| 4663 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4664 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4665 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4666 | /* Help allocation */ |
| 4667 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4668 | while (_p < aligned_end) { |
| 4669 | unsigned long value = *(unsigned long *) _p; |
| 4670 | if (value & ASCII_CHAR_MASK) |
| 4671 | break; |
| 4672 | _p += SIZEOF_LONG; |
| 4673 | } |
| 4674 | p = _p; |
| 4675 | if (_p == end) |
| 4676 | break; |
| 4677 | } |
| 4678 | if ((unsigned char)*p & 0x80) |
| 4679 | break; |
| 4680 | ++p; |
| 4681 | } |
| 4682 | memcpy(dest, start, p - start); |
| 4683 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4684 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4685 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4686 | PyObject * |
| 4687 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4688 | Py_ssize_t size, |
| 4689 | const char *errors, |
| 4690 | Py_ssize_t *consumed) |
| 4691 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4692 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4693 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4694 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4695 | |
| 4696 | Py_ssize_t startinpos; |
| 4697 | Py_ssize_t endinpos; |
| 4698 | const char *errmsg = ""; |
| 4699 | PyObject *errorHandler = NULL; |
| 4700 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4701 | |
| 4702 | if (size == 0) { |
| 4703 | if (consumed) |
| 4704 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4705 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4706 | } |
| 4707 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4708 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4709 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4710 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4711 | *consumed = 1; |
| 4712 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4713 | } |
| 4714 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4715 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4716 | writer.min_length = size; |
| 4717 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4718 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4719 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4720 | writer.pos = ascii_decode(s, end, writer.data); |
| 4721 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4722 | while (s < end) { |
| 4723 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4724 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4725 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4726 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4727 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4728 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4729 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4730 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4731 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4732 | } else { |
| 4733 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4734 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | switch (ch) { |
| 4738 | case 0: |
| 4739 | if (s == end || consumed) |
| 4740 | goto End; |
| 4741 | errmsg = "unexpected end of data"; |
| 4742 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4743 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4744 | break; |
| 4745 | case 1: |
| 4746 | errmsg = "invalid start byte"; |
| 4747 | startinpos = s - starts; |
| 4748 | endinpos = startinpos + 1; |
| 4749 | break; |
| 4750 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4751 | case 3: |
| 4752 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4753 | errmsg = "invalid continuation byte"; |
| 4754 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4755 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4756 | break; |
| 4757 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4758 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4759 | goto onError; |
| 4760 | continue; |
| 4761 | } |
| 4762 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4763 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4764 | errors, &errorHandler, |
| 4765 | "utf-8", errmsg, |
| 4766 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4767 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4768 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4769 | } |
| 4770 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4771 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4772 | if (consumed) |
| 4773 | *consumed = s - starts; |
| 4774 | |
| 4775 | Py_XDECREF(errorHandler); |
| 4776 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4777 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4778 | |
| 4779 | onError: |
| 4780 | Py_XDECREF(errorHandler); |
| 4781 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4782 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4783 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4784 | } |
| 4785 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4786 | #ifdef __APPLE__ |
| 4787 | |
| 4788 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4789 | used to decode the command line arguments on Mac OS X. |
| 4790 | |
| 4791 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4792 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4793 | |
| 4794 | wchar_t* |
| 4795 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4796 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4797 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4798 | wchar_t *unicode; |
| 4799 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4800 | |
| 4801 | /* Note: size will always be longer than the resulting Unicode |
| 4802 | character count */ |
Victor Stinner | f50e187 | 2015-03-20 11:32:24 +0100 | [diff] [blame] | 4803 | 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] | 4804 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4805 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4806 | if (!unicode) |
| 4807 | return NULL; |
| 4808 | |
| 4809 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4810 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4811 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4812 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4813 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4814 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4815 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4816 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4817 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4818 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4819 | if (ch > 0xFF) { |
| 4820 | #if SIZEOF_WCHAR_T == 4 |
| 4821 | assert(0); |
| 4822 | #else |
| 4823 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4824 | /* compute and append the two surrogates: */ |
| 4825 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4826 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4827 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4828 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4829 | else { |
| 4830 | if (!ch && s == e) |
| 4831 | break; |
| 4832 | /* surrogateescape */ |
| 4833 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4834 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4835 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4836 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4837 | return unicode; |
| 4838 | } |
| 4839 | |
| 4840 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4841 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4842 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4843 | |
| 4844 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4845 | and allocate exactly as much space needed at the end. Else allocate the |
| 4846 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4847 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4848 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4849 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4850 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4851 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4852 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4853 | void *data; |
| 4854 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4855 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4856 | if (!PyUnicode_Check(unicode)) { |
| 4857 | PyErr_BadArgument(); |
| 4858 | return NULL; |
| 4859 | } |
| 4860 | |
| 4861 | if (PyUnicode_READY(unicode) == -1) |
| 4862 | return NULL; |
| 4863 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4864 | if (PyUnicode_UTF8(unicode)) |
| 4865 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4866 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4867 | |
| 4868 | kind = PyUnicode_KIND(unicode); |
| 4869 | data = PyUnicode_DATA(unicode); |
| 4870 | size = PyUnicode_GET_LENGTH(unicode); |
| 4871 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4872 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4873 | default: |
| 4874 | assert(0); |
| 4875 | case PyUnicode_1BYTE_KIND: |
| 4876 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4877 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4878 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4879 | case PyUnicode_2BYTE_KIND: |
| 4880 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4881 | case PyUnicode_4BYTE_KIND: |
| 4882 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4883 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4886 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4887 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4888 | Py_ssize_t size, |
| 4889 | const char *errors) |
| 4890 | { |
| 4891 | PyObject *v, *unicode; |
| 4892 | |
| 4893 | unicode = PyUnicode_FromUnicode(s, size); |
| 4894 | if (unicode == NULL) |
| 4895 | return NULL; |
| 4896 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4897 | Py_DECREF(unicode); |
| 4898 | return v; |
| 4899 | } |
| 4900 | |
| 4901 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4902 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4903 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4904 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4907 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4908 | |
| 4909 | PyObject * |
| 4910 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4911 | Py_ssize_t size, |
| 4912 | const char *errors, |
| 4913 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4914 | { |
| 4915 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4916 | } |
| 4917 | |
| 4918 | PyObject * |
| 4919 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4920 | Py_ssize_t size, |
| 4921 | const char *errors, |
| 4922 | int *byteorder, |
| 4923 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4924 | { |
| 4925 | const char *starts = s; |
| 4926 | Py_ssize_t startinpos; |
| 4927 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4928 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4929 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4930 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4931 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4932 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4933 | PyObject *errorHandler = NULL; |
| 4934 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4935 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4936 | q = (unsigned char *)s; |
| 4937 | e = q + size; |
| 4938 | |
| 4939 | if (byteorder) |
| 4940 | bo = *byteorder; |
| 4941 | |
| 4942 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4943 | byte order setting accordingly. In native mode, the leading BOM |
| 4944 | mark is skipped, in all other modes, it is copied to the output |
| 4945 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4946 | if (bo == 0 && size >= 4) { |
| 4947 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4948 | if (bom == 0x0000FEFF) { |
| 4949 | bo = -1; |
| 4950 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4951 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4952 | else if (bom == 0xFFFE0000) { |
| 4953 | bo = 1; |
| 4954 | q += 4; |
| 4955 | } |
| 4956 | if (byteorder) |
| 4957 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4960 | if (q == e) { |
| 4961 | if (consumed) |
| 4962 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4963 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4964 | } |
| 4965 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4966 | #ifdef WORDS_BIGENDIAN |
| 4967 | le = bo < 0; |
| 4968 | #else |
| 4969 | le = bo <= 0; |
| 4970 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4971 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4972 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4973 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4974 | writer.min_length = (e - q + 3) / 4; |
| 4975 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4976 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4977 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4978 | while (1) { |
| 4979 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4980 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4981 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4982 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4983 | enum PyUnicode_Kind kind = writer.kind; |
| 4984 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4985 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4986 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4987 | if (le) { |
| 4988 | do { |
| 4989 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4990 | if (ch > maxch) |
| 4991 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4992 | if (kind != PyUnicode_1BYTE_KIND && |
| 4993 | Py_UNICODE_IS_SURROGATE(ch)) |
| 4994 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4995 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4996 | q += 4; |
| 4997 | } while (q <= last); |
| 4998 | } |
| 4999 | else { |
| 5000 | do { |
| 5001 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5002 | if (ch > maxch) |
| 5003 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5004 | if (kind != PyUnicode_1BYTE_KIND && |
| 5005 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5006 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5007 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5008 | q += 4; |
| 5009 | } while (q <= last); |
| 5010 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5011 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5012 | } |
| 5013 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5014 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5015 | errmsg = "code point in surrogate code point range(0xd800, 0xe000)"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5016 | startinpos = ((const char *)q) - starts; |
| 5017 | endinpos = startinpos + 4; |
| 5018 | } |
| 5019 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5020 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5021 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5022 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5023 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5024 | startinpos = ((const char *)q) - starts; |
| 5025 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5026 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5027 | else { |
| 5028 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5029 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5030 | goto onError; |
| 5031 | q += 4; |
| 5032 | continue; |
| 5033 | } |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5034 | errmsg = "code point not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5035 | startinpos = ((const char *)q) - starts; |
| 5036 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5037 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5038 | |
| 5039 | /* The remaining input chars are ignored if the callback |
| 5040 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5041 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5042 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5043 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5044 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5045 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5046 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5049 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5050 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5051 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5052 | Py_XDECREF(errorHandler); |
| 5053 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5054 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5055 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5056 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5057 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5058 | Py_XDECREF(errorHandler); |
| 5059 | Py_XDECREF(exc); |
| 5060 | return NULL; |
| 5061 | } |
| 5062 | |
| 5063 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5064 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5065 | const char *errors, |
| 5066 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5067 | { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5068 | enum PyUnicode_Kind kind; |
| 5069 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5070 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5071 | PyObject *v; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5072 | PY_UINT32_T *out; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5073 | #if PY_LITTLE_ENDIAN |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5074 | int native_ordering = byteorder <= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5075 | #else |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5076 | int native_ordering = byteorder >= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5077 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5078 | const char *encoding; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5079 | Py_ssize_t nsize, pos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5080 | PyObject *errorHandler = NULL; |
| 5081 | PyObject *exc = NULL; |
| 5082 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5083 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5084 | if (!PyUnicode_Check(str)) { |
| 5085 | PyErr_BadArgument(); |
| 5086 | return NULL; |
| 5087 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5088 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5089 | return NULL; |
| 5090 | kind = PyUnicode_KIND(str); |
| 5091 | data = PyUnicode_DATA(str); |
| 5092 | len = PyUnicode_GET_LENGTH(str); |
| 5093 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5094 | if (len > PY_SSIZE_T_MAX / 4 - (byteorder == 0)) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5095 | return PyErr_NoMemory(); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5096 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5097 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5098 | if (v == NULL) |
| 5099 | return NULL; |
| 5100 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5101 | /* output buffer is 4-bytes aligned */ |
| 5102 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 4)); |
| 5103 | out = (PY_UINT32_T *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5104 | if (byteorder == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5105 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5106 | if (len == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5107 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5108 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5109 | if (byteorder == -1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5110 | encoding = "utf-32-le"; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5111 | else if (byteorder == 1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5112 | encoding = "utf-32-be"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5113 | else |
| 5114 | encoding = "utf-32"; |
| 5115 | |
| 5116 | if (kind == PyUnicode_1BYTE_KIND) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5117 | ucs1lib_utf32_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5118 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5121 | pos = 0; |
| 5122 | while (pos < len) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5123 | Py_ssize_t repsize, moreunits; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5124 | |
| 5125 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5126 | pos += ucs2lib_utf32_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5127 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5128 | } |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5129 | else { |
| 5130 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5131 | pos += ucs4lib_utf32_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5132 | &out, native_ordering); |
| 5133 | } |
| 5134 | if (pos == len) |
| 5135 | break; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5136 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5137 | rep = unicode_encode_call_errorhandler( |
| 5138 | errors, &errorHandler, |
| 5139 | encoding, "surrogates not allowed", |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5140 | str, &exc, pos, pos + 1, &pos); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5141 | if (!rep) |
| 5142 | goto error; |
| 5143 | |
| 5144 | if (PyBytes_Check(rep)) { |
| 5145 | repsize = PyBytes_GET_SIZE(rep); |
| 5146 | if (repsize & 3) { |
| 5147 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5148 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5149 | "surrogates not allowed"); |
| 5150 | goto error; |
| 5151 | } |
| 5152 | moreunits = repsize / 4; |
| 5153 | } |
| 5154 | else { |
| 5155 | assert(PyUnicode_Check(rep)); |
| 5156 | if (PyUnicode_READY(rep) < 0) |
| 5157 | goto error; |
| 5158 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5159 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5160 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5161 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5162 | "surrogates not allowed"); |
| 5163 | goto error; |
| 5164 | } |
| 5165 | } |
| 5166 | |
| 5167 | /* four bytes are reserved for each surrogate */ |
| 5168 | if (moreunits > 1) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5169 | Py_ssize_t outpos = out - (PY_UINT32_T*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5170 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5171 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5172 | /* integer overflow */ |
| 5173 | PyErr_NoMemory(); |
| 5174 | goto error; |
| 5175 | } |
| 5176 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5177 | goto error; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5178 | out = (PY_UINT32_T*) PyBytes_AS_STRING(v) + outpos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | if (PyBytes_Check(rep)) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5182 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5183 | out += moreunits; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5184 | } else /* rep is unicode */ { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5185 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5186 | ucs1lib_utf32_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5187 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5188 | } |
| 5189 | |
| 5190 | Py_CLEAR(rep); |
| 5191 | } |
| 5192 | |
| 5193 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5194 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5195 | handler is used. */ |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5196 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5197 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5198 | _PyBytes_Resize(&v, nsize); |
| 5199 | Py_XDECREF(errorHandler); |
| 5200 | Py_XDECREF(exc); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5201 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5202 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5203 | error: |
| 5204 | Py_XDECREF(rep); |
| 5205 | Py_XDECREF(errorHandler); |
| 5206 | Py_XDECREF(exc); |
| 5207 | Py_XDECREF(v); |
| 5208 | return NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5211 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5212 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5213 | Py_ssize_t size, |
| 5214 | const char *errors, |
| 5215 | int byteorder) |
| 5216 | { |
| 5217 | PyObject *result; |
| 5218 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5219 | if (tmp == NULL) |
| 5220 | return NULL; |
| 5221 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5222 | Py_DECREF(tmp); |
| 5223 | return result; |
| 5224 | } |
| 5225 | |
| 5226 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5227 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5228 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5229 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5232 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5233 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5234 | PyObject * |
| 5235 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5236 | Py_ssize_t size, |
| 5237 | const char *errors, |
| 5238 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5239 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5240 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5241 | } |
| 5242 | |
| 5243 | PyObject * |
| 5244 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5245 | Py_ssize_t size, |
| 5246 | const char *errors, |
| 5247 | int *byteorder, |
| 5248 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5249 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5250 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5251 | Py_ssize_t startinpos; |
| 5252 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5253 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5254 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5255 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5256 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5257 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5258 | PyObject *errorHandler = NULL; |
| 5259 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5260 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5261 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5262 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5263 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5264 | |
| 5265 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5266 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5267 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5268 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5269 | byte order setting accordingly. In native mode, the leading BOM |
| 5270 | mark is skipped, in all other modes, it is copied to the output |
| 5271 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5272 | if (bo == 0 && size >= 2) { |
| 5273 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5274 | if (bom == 0xFEFF) { |
| 5275 | q += 2; |
| 5276 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5277 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5278 | else if (bom == 0xFFFE) { |
| 5279 | q += 2; |
| 5280 | bo = 1; |
| 5281 | } |
| 5282 | if (byteorder) |
| 5283 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5284 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5285 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5286 | if (q == e) { |
| 5287 | if (consumed) |
| 5288 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5289 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5290 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5291 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5292 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5293 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5294 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5295 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5296 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5297 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5298 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5299 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5300 | /* Note: size will always be longer than the resulting Unicode |
| 5301 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5302 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5303 | writer.min_length = (e - q + 1) / 2; |
| 5304 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5305 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5306 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5307 | while (1) { |
| 5308 | Py_UCS4 ch = 0; |
| 5309 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5310 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5311 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5312 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5313 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5314 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5315 | native_ordering); |
| 5316 | else |
| 5317 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5318 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5319 | native_ordering); |
| 5320 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5321 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5322 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5323 | native_ordering); |
| 5324 | } else { |
| 5325 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5326 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5327 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5328 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5329 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5330 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5331 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5332 | switch (ch) |
| 5333 | { |
| 5334 | case 0: |
| 5335 | /* remaining byte at the end? (size should be even) */ |
| 5336 | if (q == e || consumed) |
| 5337 | goto End; |
| 5338 | errmsg = "truncated data"; |
| 5339 | startinpos = ((const char *)q) - starts; |
| 5340 | endinpos = ((const char *)e) - starts; |
| 5341 | break; |
| 5342 | /* The remaining input chars are ignored if the callback |
| 5343 | chooses to skip the input */ |
| 5344 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5345 | q -= 2; |
| 5346 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5347 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5348 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5349 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5350 | endinpos = ((const char *)e) - starts; |
| 5351 | break; |
| 5352 | case 2: |
| 5353 | errmsg = "illegal encoding"; |
| 5354 | startinpos = ((const char *)q) - 2 - starts; |
| 5355 | endinpos = startinpos + 2; |
| 5356 | break; |
| 5357 | case 3: |
| 5358 | errmsg = "illegal UTF-16 surrogate"; |
| 5359 | startinpos = ((const char *)q) - 4 - starts; |
| 5360 | endinpos = startinpos + 2; |
| 5361 | break; |
| 5362 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5363 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5364 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5365 | continue; |
| 5366 | } |
| 5367 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5368 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5369 | errors, |
| 5370 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5371 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5372 | &starts, |
| 5373 | (const char **)&e, |
| 5374 | &startinpos, |
| 5375 | &endinpos, |
| 5376 | &exc, |
| 5377 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5378 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5379 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5382 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5383 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5384 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5385 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5386 | Py_XDECREF(errorHandler); |
| 5387 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5388 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5390 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5391 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5392 | Py_XDECREF(errorHandler); |
| 5393 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5394 | return NULL; |
| 5395 | } |
| 5396 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5397 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5398 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5399 | const char *errors, |
| 5400 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5401 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5402 | enum PyUnicode_Kind kind; |
| 5403 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5404 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5405 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5406 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5407 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5408 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5409 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5410 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5411 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5412 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5413 | const char *encoding; |
| 5414 | Py_ssize_t nsize, pos; |
| 5415 | PyObject *errorHandler = NULL; |
| 5416 | PyObject *exc = NULL; |
| 5417 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5418 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5419 | if (!PyUnicode_Check(str)) { |
| 5420 | PyErr_BadArgument(); |
| 5421 | return NULL; |
| 5422 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5423 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5424 | return NULL; |
| 5425 | kind = PyUnicode_KIND(str); |
| 5426 | data = PyUnicode_DATA(str); |
| 5427 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5428 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5429 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5430 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5431 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5432 | const Py_UCS4 *end = in + len; |
| 5433 | while (in < end) |
| 5434 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5435 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5436 | } |
| 5437 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5438 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5439 | nsize = len + pairs + (byteorder == 0); |
| 5440 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | if (v == NULL) |
| 5442 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5444 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5445 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5446 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5448 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5449 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5450 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5451 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5452 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5453 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5454 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5455 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5456 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5457 | if (byteorder < 0) |
| 5458 | encoding = "utf-16-le"; |
| 5459 | else if (byteorder > 0) |
| 5460 | encoding = "utf-16-be"; |
| 5461 | else |
| 5462 | encoding = "utf-16"; |
| 5463 | |
| 5464 | pos = 0; |
| 5465 | while (pos < len) { |
| 5466 | Py_ssize_t repsize, moreunits; |
| 5467 | |
| 5468 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5469 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5470 | &out, native_ordering); |
| 5471 | } |
| 5472 | else { |
| 5473 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5474 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5475 | &out, native_ordering); |
| 5476 | } |
| 5477 | if (pos == len) |
| 5478 | break; |
| 5479 | |
| 5480 | rep = unicode_encode_call_errorhandler( |
| 5481 | errors, &errorHandler, |
| 5482 | encoding, "surrogates not allowed", |
| 5483 | str, &exc, pos, pos + 1, &pos); |
| 5484 | if (!rep) |
| 5485 | goto error; |
| 5486 | |
| 5487 | if (PyBytes_Check(rep)) { |
| 5488 | repsize = PyBytes_GET_SIZE(rep); |
| 5489 | if (repsize & 1) { |
| 5490 | raise_encode_exception(&exc, encoding, |
| 5491 | str, pos - 1, pos, |
| 5492 | "surrogates not allowed"); |
| 5493 | goto error; |
| 5494 | } |
| 5495 | moreunits = repsize / 2; |
| 5496 | } |
| 5497 | else { |
| 5498 | assert(PyUnicode_Check(rep)); |
| 5499 | if (PyUnicode_READY(rep) < 0) |
| 5500 | goto error; |
| 5501 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5502 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5503 | raise_encode_exception(&exc, encoding, |
| 5504 | str, pos - 1, pos, |
| 5505 | "surrogates not allowed"); |
| 5506 | goto error; |
| 5507 | } |
| 5508 | } |
| 5509 | |
| 5510 | /* two bytes are reserved for each surrogate */ |
| 5511 | if (moreunits > 1) { |
| 5512 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5513 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5514 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5515 | /* integer overflow */ |
| 5516 | PyErr_NoMemory(); |
| 5517 | goto error; |
| 5518 | } |
| 5519 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5520 | goto error; |
| 5521 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5522 | } |
| 5523 | |
| 5524 | if (PyBytes_Check(rep)) { |
| 5525 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5526 | out += moreunits; |
| 5527 | } else /* rep is unicode */ { |
| 5528 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5529 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5530 | &out, native_ordering); |
| 5531 | } |
| 5532 | |
| 5533 | Py_CLEAR(rep); |
| 5534 | } |
| 5535 | |
| 5536 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5537 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5538 | is used. */ |
| 5539 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5540 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5541 | _PyBytes_Resize(&v, nsize); |
| 5542 | Py_XDECREF(errorHandler); |
| 5543 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5544 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5545 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5546 | error: |
| 5547 | Py_XDECREF(rep); |
| 5548 | Py_XDECREF(errorHandler); |
| 5549 | Py_XDECREF(exc); |
| 5550 | Py_XDECREF(v); |
| 5551 | return NULL; |
| 5552 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5555 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5556 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5557 | Py_ssize_t size, |
| 5558 | const char *errors, |
| 5559 | int byteorder) |
| 5560 | { |
| 5561 | PyObject *result; |
| 5562 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5563 | if (tmp == NULL) |
| 5564 | return NULL; |
| 5565 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5566 | Py_DECREF(tmp); |
| 5567 | return result; |
| 5568 | } |
| 5569 | |
| 5570 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5571 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5572 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5573 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | } |
| 5575 | |
| 5576 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5577 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5578 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5579 | if all the escapes in the string make it still a valid ASCII string. |
| 5580 | Returns -1 if any escapes were found which cause the string to |
| 5581 | pop out of ASCII range. Otherwise returns the length of the |
| 5582 | required buffer to hold the string. |
| 5583 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5584 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5585 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5586 | { |
| 5587 | const unsigned char *p = (const unsigned char *)s; |
| 5588 | const unsigned char *end = p + size; |
| 5589 | Py_ssize_t length = 0; |
| 5590 | |
| 5591 | if (size < 0) |
| 5592 | return -1; |
| 5593 | |
| 5594 | for (; p < end; ++p) { |
| 5595 | if (*p > 127) { |
| 5596 | /* Non-ASCII */ |
| 5597 | return -1; |
| 5598 | } |
| 5599 | else if (*p != '\\') { |
| 5600 | /* Normal character */ |
| 5601 | ++length; |
| 5602 | } |
| 5603 | else { |
| 5604 | /* Backslash-escape, check next char */ |
| 5605 | ++p; |
| 5606 | /* Escape sequence reaches till end of string or |
| 5607 | non-ASCII follow-up. */ |
| 5608 | if (p >= end || *p > 127) |
| 5609 | return -1; |
| 5610 | switch (*p) { |
| 5611 | case '\n': |
| 5612 | /* backslash + \n result in zero characters */ |
| 5613 | break; |
| 5614 | case '\\': case '\'': case '\"': |
| 5615 | case 'b': case 'f': case 't': |
| 5616 | case 'n': case 'r': case 'v': case 'a': |
| 5617 | ++length; |
| 5618 | break; |
| 5619 | case '0': case '1': case '2': case '3': |
| 5620 | case '4': case '5': case '6': case '7': |
| 5621 | case 'x': case 'u': case 'U': case 'N': |
| 5622 | /* these do not guarantee ASCII characters */ |
| 5623 | return -1; |
| 5624 | default: |
| 5625 | /* count the backslash + the other character */ |
| 5626 | length += 2; |
| 5627 | } |
| 5628 | } |
| 5629 | } |
| 5630 | return length; |
| 5631 | } |
| 5632 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5633 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5634 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5635 | PyObject * |
| 5636 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5637 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5638 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5640 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5641 | Py_ssize_t startinpos; |
| 5642 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5643 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5644 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5645 | char* message; |
| 5646 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5647 | PyObject *errorHandler = NULL; |
| 5648 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5649 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5650 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5651 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5652 | if (len == 0) |
| 5653 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5654 | |
| 5655 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5656 | either the string is pure ASCII with named escapes like \n, etc. |
| 5657 | and we determined it's exact size (common case) |
| 5658 | or it contains \x, \u, ... escape sequences. then we create a |
| 5659 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5660 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5661 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5662 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5663 | } |
| 5664 | else { |
| 5665 | /* Escaped strings will always be longer than the resulting |
| 5666 | Unicode string, so we start with size here and then reduce the |
| 5667 | length after conversion to the true value. |
| 5668 | (but if the error callback returns a long replacement string |
| 5669 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5670 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5671 | } |
| 5672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5674 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | while (s < end) { |
| 5678 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5679 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5680 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5681 | |
| 5682 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5683 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5684 | x = (unsigned char)*s; |
| 5685 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5686 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5687 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5688 | continue; |
| 5689 | } |
| 5690 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5691 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5692 | /* \ - Escapes */ |
| 5693 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5694 | c = *s++; |
| 5695 | if (s > end) |
| 5696 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5697 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5698 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5700 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5701 | #define WRITECHAR(ch) \ |
| 5702 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5703 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5704 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5705 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5706 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5707 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5708 | case '\\': WRITECHAR('\\'); break; |
| 5709 | case '\'': WRITECHAR('\''); break; |
| 5710 | case '\"': WRITECHAR('\"'); break; |
| 5711 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5712 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5713 | case 'f': WRITECHAR('\014'); break; |
| 5714 | case 't': WRITECHAR('\t'); break; |
| 5715 | case 'n': WRITECHAR('\n'); break; |
| 5716 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5717 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5718 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5719 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5720 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5722 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | case '0': case '1': case '2': case '3': |
| 5724 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5725 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5726 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5727 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5728 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5729 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5730 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5731 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | break; |
| 5733 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5734 | /* hex escapes */ |
| 5735 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5737 | digits = 2; |
| 5738 | message = "truncated \\xXX escape"; |
| 5739 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5741 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5743 | digits = 4; |
| 5744 | message = "truncated \\uXXXX escape"; |
| 5745 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5747 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5748 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5749 | digits = 8; |
| 5750 | message = "truncated \\UXXXXXXXX escape"; |
| 5751 | hexescape: |
| 5752 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5753 | if (end - s < digits) { |
| 5754 | /* count only hex digits */ |
| 5755 | for (; s < end; ++s) { |
| 5756 | c = (unsigned char)*s; |
| 5757 | if (!Py_ISXDIGIT(c)) |
| 5758 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5759 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5760 | goto error; |
| 5761 | } |
| 5762 | for (; digits--; ++s) { |
| 5763 | c = (unsigned char)*s; |
| 5764 | if (!Py_ISXDIGIT(c)) |
| 5765 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5766 | chr = (chr<<4) & ~0xF; |
| 5767 | if (c >= '0' && c <= '9') |
| 5768 | chr += c - '0'; |
| 5769 | else if (c >= 'a' && c <= 'f') |
| 5770 | chr += 10 + c - 'a'; |
| 5771 | else |
| 5772 | chr += 10 + c - 'A'; |
| 5773 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5774 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5775 | /* _decoding_error will have already written into the |
| 5776 | target buffer. */ |
| 5777 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5778 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5779 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5780 | message = "illegal Unicode character"; |
| 5781 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5782 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5783 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5784 | break; |
| 5785 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5786 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5787 | case 'N': |
| 5788 | message = "malformed \\N character escape"; |
| 5789 | if (ucnhash_CAPI == NULL) { |
| 5790 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5791 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5792 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5793 | if (ucnhash_CAPI == NULL) |
| 5794 | goto ucnhashError; |
| 5795 | } |
| 5796 | if (*s == '{') { |
| 5797 | const char *start = s+1; |
| 5798 | /* look for the closing brace */ |
| 5799 | while (*s != '}' && s < end) |
| 5800 | s++; |
| 5801 | if (s > start && s < end && *s == '}') { |
| 5802 | /* found a name. look it up in the unicode database */ |
| 5803 | message = "unknown Unicode character name"; |
| 5804 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5805 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5806 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5807 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5808 | goto store; |
| 5809 | } |
| 5810 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5811 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5812 | |
| 5813 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5814 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5815 | message = "\\ at end of string"; |
| 5816 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5817 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5818 | } |
| 5819 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5820 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5821 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5822 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5823 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5824 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5825 | continue; |
| 5826 | |
| 5827 | error: |
| 5828 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5829 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5830 | errors, &errorHandler, |
| 5831 | "unicodeescape", message, |
| 5832 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5833 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5834 | goto onError; |
| 5835 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5837 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5838 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5839 | Py_XDECREF(errorHandler); |
| 5840 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5841 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5842 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5843 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5844 | PyErr_SetString( |
| 5845 | PyExc_UnicodeError, |
| 5846 | "\\N escapes not supported (can't load unicodedata module)" |
| 5847 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5848 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5849 | Py_XDECREF(errorHandler); |
| 5850 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5851 | return NULL; |
| 5852 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5853 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5854 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5855 | Py_XDECREF(errorHandler); |
| 5856 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5857 | return NULL; |
| 5858 | } |
| 5859 | |
| 5860 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5861 | |
| 5862 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5863 | appropriate. |
| 5864 | |
| 5865 | */ |
| 5866 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5867 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5868 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5870 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5871 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5873 | int kind; |
| 5874 | void *data; |
| 5875 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5877 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5878 | escape. |
| 5879 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5880 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5881 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5882 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5883 | */ |
| 5884 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5885 | if (!PyUnicode_Check(unicode)) { |
| 5886 | PyErr_BadArgument(); |
| 5887 | return NULL; |
| 5888 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5889 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5890 | return NULL; |
| 5891 | len = PyUnicode_GET_LENGTH(unicode); |
| 5892 | kind = PyUnicode_KIND(unicode); |
| 5893 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5894 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5895 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5896 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5897 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5898 | } |
| 5899 | |
| 5900 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5901 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5902 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5903 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5904 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5905 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5906 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5907 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5908 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5909 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | if (repr == NULL) |
| 5911 | return NULL; |
| 5912 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5913 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5915 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5916 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5917 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5918 | /* Escape backslashes */ |
| 5919 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | *p++ = '\\'; |
| 5921 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5922 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5923 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5924 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5925 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5926 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5927 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5928 | *p++ = '\\'; |
| 5929 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5930 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5931 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5932 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5933 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5934 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5935 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5936 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5937 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5938 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5939 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5940 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5941 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5942 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5943 | *p++ = '\\'; |
| 5944 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5945 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5946 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5947 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5948 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5949 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5950 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5951 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5952 | else if (ch == '\t') { |
| 5953 | *p++ = '\\'; |
| 5954 | *p++ = 't'; |
| 5955 | } |
| 5956 | else if (ch == '\n') { |
| 5957 | *p++ = '\\'; |
| 5958 | *p++ = 'n'; |
| 5959 | } |
| 5960 | else if (ch == '\r') { |
| 5961 | *p++ = '\\'; |
| 5962 | *p++ = 'r'; |
| 5963 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5964 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5965 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5966 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5968 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5969 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5970 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5971 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | /* Copy everything else as-is */ |
| 5974 | else |
| 5975 | *p++ = (char) ch; |
| 5976 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5977 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5978 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5979 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5980 | return NULL; |
| 5981 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5984 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5985 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5986 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5988 | PyObject *result; |
| 5989 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5990 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5992 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5993 | Py_DECREF(tmp); |
| 5994 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5998 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5999 | PyObject * |
| 6000 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6001 | Py_ssize_t size, |
| 6002 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6004 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6005 | Py_ssize_t startinpos; |
| 6006 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6007 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | const char *end; |
| 6009 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6010 | PyObject *errorHandler = NULL; |
| 6011 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6012 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6013 | if (size == 0) |
| 6014 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | /* Escaped strings will always be longer than the resulting |
| 6017 | 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] | 6018 | length after conversion to the true value. (But decoding error |
| 6019 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6020 | _PyUnicodeWriter_Init(&writer); |
| 6021 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6022 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | end = s + size; |
| 6024 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6025 | unsigned char c; |
| 6026 | Py_UCS4 x; |
| 6027 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6028 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6029 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6030 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6031 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6032 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6033 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6034 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6035 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6036 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6037 | startinpos = s-starts; |
| 6038 | |
| 6039 | /* \u-escapes are only interpreted iff the number of leading |
| 6040 | backslashes if odd */ |
| 6041 | bs = s; |
| 6042 | for (;s < end;) { |
| 6043 | if (*s != '\\') |
| 6044 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6045 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6046 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6047 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6048 | } |
| 6049 | if (((s - bs) & 1) == 0 || |
| 6050 | s >= end || |
| 6051 | (*s != 'u' && *s != 'U')) { |
| 6052 | continue; |
| 6053 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6054 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6055 | count = *s=='u' ? 4 : 8; |
| 6056 | s++; |
| 6057 | |
| 6058 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6059 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6060 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6061 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6062 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6063 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6064 | errors, &errorHandler, |
| 6065 | "rawunicodeescape", "truncated \\uXXXX", |
| 6066 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6067 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6068 | goto onError; |
| 6069 | goto nextByte; |
| 6070 | } |
| 6071 | x = (x<<4) & ~0xF; |
| 6072 | if (c >= '0' && c <= '9') |
| 6073 | x += c - '0'; |
| 6074 | else if (c >= 'a' && c <= 'f') |
| 6075 | x += 10 + c - 'a'; |
| 6076 | else |
| 6077 | x += 10 + c - 'A'; |
| 6078 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6079 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6080 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6081 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6082 | } |
| 6083 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6084 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6085 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6086 | errors, &errorHandler, |
| 6087 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6088 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6089 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6090 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6091 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6092 | nextByte: |
| 6093 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6094 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6095 | Py_XDECREF(errorHandler); |
| 6096 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6097 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6098 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6099 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6100 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6101 | Py_XDECREF(errorHandler); |
| 6102 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | return NULL; |
| 6104 | } |
| 6105 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6106 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6107 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6108 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6110 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | char *p; |
| 6112 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6113 | Py_ssize_t expandsize, pos; |
| 6114 | int kind; |
| 6115 | void *data; |
| 6116 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6117 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6118 | if (!PyUnicode_Check(unicode)) { |
| 6119 | PyErr_BadArgument(); |
| 6120 | return NULL; |
| 6121 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6122 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6123 | return NULL; |
| 6124 | kind = PyUnicode_KIND(unicode); |
| 6125 | data = PyUnicode_DATA(unicode); |
| 6126 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6127 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6128 | bytes, and 1 byte characters 4. */ |
| 6129 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6130 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6131 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6132 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6133 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6134 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6135 | if (repr == NULL) |
| 6136 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6137 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6138 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6140 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6141 | for (pos = 0; pos < len; pos++) { |
| 6142 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6143 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6144 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6145 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6146 | *p++ = '\\'; |
| 6147 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6148 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6149 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6150 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6151 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6152 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6153 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6154 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6155 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6156 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6157 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6158 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | *p++ = '\\'; |
| 6160 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6161 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6162 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6163 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6164 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6166 | /* Copy everything else as-is */ |
| 6167 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | *p++ = (char) ch; |
| 6169 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6170 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6171 | assert(p > q); |
| 6172 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6173 | return NULL; |
| 6174 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6177 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6178 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6179 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6181 | PyObject *result; |
| 6182 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6183 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6184 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6185 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6186 | Py_DECREF(tmp); |
| 6187 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | } |
| 6189 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6190 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6191 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6192 | PyObject * |
| 6193 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6194 | Py_ssize_t size, |
| 6195 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6196 | { |
| 6197 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6198 | Py_ssize_t startinpos; |
| 6199 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6200 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6201 | const char *end; |
| 6202 | const char *reason; |
| 6203 | PyObject *errorHandler = NULL; |
| 6204 | PyObject *exc = NULL; |
| 6205 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6206 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6207 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6208 | 1)) |
| 6209 | return NULL; |
| 6210 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6211 | if (size == 0) |
| 6212 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6213 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6214 | _PyUnicodeWriter_Init(&writer); |
| 6215 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6216 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6217 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6218 | } |
| 6219 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6220 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6221 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6222 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6223 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6224 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6225 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6226 | endinpos = end-starts; |
| 6227 | reason = "truncated input"; |
| 6228 | goto error; |
| 6229 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6230 | /* We copy the raw representation one byte at a time because the |
| 6231 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6232 | ((char *) &uch)[0] = s[0]; |
| 6233 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6234 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6235 | ((char *) &uch)[2] = s[2]; |
| 6236 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6237 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6238 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6239 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6240 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6241 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6242 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6243 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6244 | reason = "illegal code point (> 0x10FFFF)"; |
| 6245 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6246 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6247 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6248 | s += Py_UNICODE_SIZE; |
| 6249 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6250 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6251 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6252 | Py_UNICODE uch2; |
| 6253 | ((char *) &uch2)[0] = s[0]; |
| 6254 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6255 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6256 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6257 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6258 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6259 | } |
| 6260 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6261 | #endif |
| 6262 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6263 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6264 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6265 | continue; |
| 6266 | |
| 6267 | error: |
| 6268 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6269 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6270 | errors, &errorHandler, |
| 6271 | "unicode_internal", reason, |
| 6272 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6273 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6274 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6277 | Py_XDECREF(errorHandler); |
| 6278 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6279 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6280 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6281 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6282 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6283 | Py_XDECREF(errorHandler); |
| 6284 | Py_XDECREF(exc); |
| 6285 | return NULL; |
| 6286 | } |
| 6287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6289 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6290 | PyObject * |
| 6291 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6292 | Py_ssize_t size, |
| 6293 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6294 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6295 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6296 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6297 | } |
| 6298 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6299 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6300 | static void |
| 6301 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6302 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6303 | PyObject *unicode, |
| 6304 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6305 | const char *reason) |
| 6306 | { |
| 6307 | if (*exceptionObject == NULL) { |
| 6308 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6309 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6310 | encoding, unicode, startpos, endpos, reason); |
| 6311 | } |
| 6312 | else { |
| 6313 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6314 | goto onError; |
| 6315 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6316 | goto onError; |
| 6317 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6318 | goto onError; |
| 6319 | return; |
| 6320 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 6321 | Py_CLEAR(*exceptionObject); |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6322 | } |
| 6323 | } |
| 6324 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6325 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6326 | static void |
| 6327 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6328 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6329 | PyObject *unicode, |
| 6330 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6331 | const char *reason) |
| 6332 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6333 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6334 | encoding, unicode, startpos, endpos, reason); |
| 6335 | if (*exceptionObject != NULL) |
| 6336 | PyCodec_StrictErrors(*exceptionObject); |
| 6337 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6338 | |
| 6339 | /* error handling callback helper: |
| 6340 | build arguments, call the callback and check the arguments, |
| 6341 | put the result into newpos and return the replacement string, which |
| 6342 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6343 | static PyObject * |
| 6344 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6345 | PyObject **errorHandler, |
| 6346 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6347 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6348 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6349 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6350 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6351 | 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] | 6352 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6353 | PyObject *restuple; |
| 6354 | PyObject *resunicode; |
| 6355 | |
| 6356 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6357 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6358 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6359 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6362 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6363 | return NULL; |
| 6364 | len = PyUnicode_GET_LENGTH(unicode); |
| 6365 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6366 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6367 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6368 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6369 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6370 | |
| 6371 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6372 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6373 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6374 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6375 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6376 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6377 | Py_DECREF(restuple); |
| 6378 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6379 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6380 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6381 | &resunicode, newpos)) { |
| 6382 | Py_DECREF(restuple); |
| 6383 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6384 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6385 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6386 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6387 | Py_DECREF(restuple); |
| 6388 | return NULL; |
| 6389 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6390 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6391 | *newpos = len + *newpos; |
| 6392 | if (*newpos<0 || *newpos>len) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 6393 | 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] | 6394 | Py_DECREF(restuple); |
| 6395 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6396 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6397 | Py_INCREF(resunicode); |
| 6398 | Py_DECREF(restuple); |
| 6399 | return resunicode; |
| 6400 | } |
| 6401 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6402 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6403 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6404 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6405 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6406 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6407 | /* input state */ |
| 6408 | Py_ssize_t pos=0, size; |
| 6409 | int kind; |
| 6410 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6411 | /* output object */ |
| 6412 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6413 | /* pointer into the output */ |
| 6414 | char *str; |
| 6415 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6416 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6417 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6418 | const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6419 | PyObject *errorHandler = NULL; |
| 6420 | PyObject *exc = NULL; |
| 6421 | /* the following variable is used for caching string comparisons |
| 6422 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6423 | int known_errorHandler = -1; |
| 6424 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6425 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6426 | return NULL; |
| 6427 | size = PyUnicode_GET_LENGTH(unicode); |
| 6428 | kind = PyUnicode_KIND(unicode); |
| 6429 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6430 | /* allocate enough for a simple encoding without |
| 6431 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6432 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6433 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6434 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6435 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6436 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6437 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6438 | ressize = size; |
| 6439 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6440 | while (pos < size) { |
| 6441 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6442 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 | /* can we encode this? */ |
| 6444 | if (c<limit) { |
| 6445 | /* no overflow check, because we know that the space is enough */ |
| 6446 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6447 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6448 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6449 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6450 | Py_ssize_t requiredsize; |
| 6451 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6452 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6453 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6454 | Py_ssize_t collstart = pos; |
| 6455 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6456 | /* find all unecodable characters */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6457 | while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6458 | ++collend; |
| 6459 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6460 | if (known_errorHandler==-1) { |
| 6461 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6462 | known_errorHandler = 1; |
| 6463 | else if (!strcmp(errors, "replace")) |
| 6464 | known_errorHandler = 2; |
| 6465 | else if (!strcmp(errors, "ignore")) |
| 6466 | known_errorHandler = 3; |
| 6467 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6468 | known_errorHandler = 4; |
| 6469 | else |
| 6470 | known_errorHandler = 0; |
| 6471 | } |
| 6472 | switch (known_errorHandler) { |
| 6473 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6474 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6475 | goto onError; |
| 6476 | case 2: /* replace */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6477 | while (collstart++ < collend) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6478 | *str++ = '?'; /* fall through */ |
| 6479 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6480 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | break; |
| 6482 | case 4: /* xmlcharrefreplace */ |
| 6483 | respos = str - PyBytes_AS_STRING(res); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6484 | requiredsize = respos; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6485 | /* determine replacement size */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6486 | for (i = collstart; i < collend; ++i) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6487 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6488 | Py_ssize_t incr; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6489 | if (ch < 10) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6490 | incr = 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6491 | else if (ch < 100) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6492 | incr = 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6493 | else if (ch < 1000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6494 | incr = 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6495 | else if (ch < 10000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6496 | incr = 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6497 | else if (ch < 100000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6498 | incr = 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6499 | else if (ch < 1000000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6500 | incr = 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6501 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6502 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6503 | incr = 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6504 | } |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6505 | if (requiredsize > PY_SSIZE_T_MAX - incr) |
| 6506 | goto overflow; |
| 6507 | requiredsize += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6508 | } |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6509 | if (requiredsize > PY_SSIZE_T_MAX - (size - collend)) |
| 6510 | goto overflow; |
| 6511 | requiredsize += size - collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6512 | if (requiredsize > ressize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6513 | if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6514 | requiredsize = 2*ressize; |
| 6515 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6516 | goto onError; |
| 6517 | str = PyBytes_AS_STRING(res) + respos; |
| 6518 | ressize = requiredsize; |
| 6519 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6520 | /* generate replacement */ |
| 6521 | for (i = collstart; i < collend; ++i) { |
| 6522 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6523 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6524 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6525 | break; |
| 6526 | default: |
| 6527 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6528 | encoding, reason, unicode, &exc, |
| 6529 | collstart, collend, &newpos); |
| 6530 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6531 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6532 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6533 | if (PyBytes_Check(repunicode)) { |
| 6534 | /* Directly copy bytes result to output. */ |
| 6535 | repsize = PyBytes_Size(repunicode); |
| 6536 | if (repsize > 1) { |
| 6537 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6538 | respos = str - PyBytes_AS_STRING(res); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6539 | if (ressize > PY_SSIZE_T_MAX - repsize - 1) { |
| 6540 | Py_DECREF(repunicode); |
| 6541 | goto overflow; |
| 6542 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6543 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6544 | Py_DECREF(repunicode); |
| 6545 | goto onError; |
| 6546 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6547 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6548 | ressize += repsize-1; |
| 6549 | } |
| 6550 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6551 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6552 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6553 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6554 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6555 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6556 | /* need more space? (at least enough for what we |
| 6557 | have+the replacement+the rest of the string, so |
| 6558 | we won't have to check space for encodable characters) */ |
| 6559 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6560 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6561 | requiredsize = respos; |
| 6562 | if (requiredsize > PY_SSIZE_T_MAX - repsize) |
| 6563 | goto overflow; |
| 6564 | requiredsize += repsize; |
| 6565 | if (requiredsize > PY_SSIZE_T_MAX - (size - collend)) |
| 6566 | goto overflow; |
| 6567 | requiredsize += size - collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6568 | if (requiredsize > ressize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6569 | if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6570 | requiredsize = 2*ressize; |
| 6571 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6572 | Py_DECREF(repunicode); |
| 6573 | goto onError; |
| 6574 | } |
| 6575 | str = PyBytes_AS_STRING(res) + respos; |
| 6576 | ressize = requiredsize; |
| 6577 | } |
| 6578 | /* check if there is anything unencodable in the replacement |
| 6579 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6580 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6581 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6582 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6583 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6584 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6585 | Py_DECREF(repunicode); |
| 6586 | goto onError; |
| 6587 | } |
| 6588 | *str = (char)c; |
| 6589 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6590 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6591 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6592 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6593 | } |
| 6594 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6595 | /* Resize if we allocated to much */ |
| 6596 | size = str - PyBytes_AS_STRING(res); |
| 6597 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6598 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6599 | if (_PyBytes_Resize(&res, size) < 0) |
| 6600 | goto onError; |
| 6601 | } |
| 6602 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6603 | Py_XDECREF(errorHandler); |
| 6604 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6605 | return res; |
| 6606 | |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6607 | overflow: |
| 6608 | PyErr_SetString(PyExc_OverflowError, |
| 6609 | "encoded result is too long for a Python string"); |
| 6610 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6611 | onError: |
| 6612 | Py_XDECREF(res); |
| 6613 | Py_XDECREF(errorHandler); |
| 6614 | Py_XDECREF(exc); |
| 6615 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6618 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6619 | PyObject * |
| 6620 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6621 | Py_ssize_t size, |
| 6622 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6624 | PyObject *result; |
| 6625 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6626 | if (unicode == NULL) |
| 6627 | return NULL; |
| 6628 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6629 | Py_DECREF(unicode); |
| 6630 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6631 | } |
| 6632 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6633 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6634 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6635 | { |
| 6636 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6637 | PyErr_BadArgument(); |
| 6638 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6639 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6640 | if (PyUnicode_READY(unicode) == -1) |
| 6641 | return NULL; |
| 6642 | /* Fast path: if it is a one-byte string, construct |
| 6643 | bytes object directly. */ |
| 6644 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6645 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6646 | PyUnicode_GET_LENGTH(unicode)); |
| 6647 | /* Non-Latin-1 characters present. Defer to above function to |
| 6648 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6649 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6650 | } |
| 6651 | |
| 6652 | PyObject* |
| 6653 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6654 | { |
| 6655 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6656 | } |
| 6657 | |
| 6658 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6659 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6660 | PyObject * |
| 6661 | PyUnicode_DecodeASCII(const char *s, |
| 6662 | Py_ssize_t size, |
| 6663 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6664 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6665 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6666 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6667 | int kind; |
| 6668 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6669 | Py_ssize_t startinpos; |
| 6670 | Py_ssize_t endinpos; |
| 6671 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6672 | const char *e; |
| 6673 | PyObject *errorHandler = NULL; |
| 6674 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6675 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6676 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6677 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6678 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6679 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6680 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6681 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6682 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6683 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6684 | writer.min_length = size; |
| 6685 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6686 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6687 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6688 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6689 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6690 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6691 | writer.pos = outpos; |
| 6692 | if (writer.pos == size) |
| 6693 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6694 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6695 | s += writer.pos; |
| 6696 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6697 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6698 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6699 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6700 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6701 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6702 | ++s; |
| 6703 | } |
| 6704 | else { |
| 6705 | startinpos = s-starts; |
| 6706 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6707 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6708 | errors, &errorHandler, |
| 6709 | "ascii", "ordinal not in range(128)", |
| 6710 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6711 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6712 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6713 | kind = writer.kind; |
| 6714 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6715 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6716 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6717 | Py_XDECREF(errorHandler); |
| 6718 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6719 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6720 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6721 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6722 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6723 | Py_XDECREF(errorHandler); |
| 6724 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | return NULL; |
| 6726 | } |
| 6727 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6728 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6729 | PyObject * |
| 6730 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6731 | Py_ssize_t size, |
| 6732 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6733 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6734 | PyObject *result; |
| 6735 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6736 | if (unicode == NULL) |
| 6737 | return NULL; |
| 6738 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6739 | Py_DECREF(unicode); |
| 6740 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | } |
| 6742 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6743 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6744 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6745 | { |
| 6746 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6747 | PyErr_BadArgument(); |
| 6748 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6749 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6750 | if (PyUnicode_READY(unicode) == -1) |
| 6751 | return NULL; |
| 6752 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6753 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6754 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6755 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6756 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6757 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6758 | } |
| 6759 | |
| 6760 | PyObject * |
| 6761 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6762 | { |
| 6763 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6766 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6767 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6768 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6769 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6770 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6771 | #define NEED_RETRY |
| 6772 | #endif |
| 6773 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6774 | #ifndef WC_ERR_INVALID_CHARS |
| 6775 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6776 | #endif |
| 6777 | |
| 6778 | static char* |
| 6779 | code_page_name(UINT code_page, PyObject **obj) |
| 6780 | { |
| 6781 | *obj = NULL; |
| 6782 | if (code_page == CP_ACP) |
| 6783 | return "mbcs"; |
| 6784 | if (code_page == CP_UTF7) |
| 6785 | return "CP_UTF7"; |
| 6786 | if (code_page == CP_UTF8) |
| 6787 | return "CP_UTF8"; |
| 6788 | |
| 6789 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6790 | if (*obj == NULL) |
| 6791 | return NULL; |
| 6792 | return PyBytes_AS_STRING(*obj); |
| 6793 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6794 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6795 | static DWORD |
| 6796 | decode_code_page_flags(UINT code_page) |
| 6797 | { |
| 6798 | if (code_page == CP_UTF7) { |
| 6799 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6800 | return 0; |
| 6801 | } |
| 6802 | else |
| 6803 | return MB_ERR_INVALID_CHARS; |
| 6804 | } |
| 6805 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6806 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6807 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6808 | * mode. |
| 6809 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6810 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6811 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6812 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6813 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6814 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6815 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6816 | const char *in, |
| 6817 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6818 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6819 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6820 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6821 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6822 | |
| 6823 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6824 | assert(insize > 0); |
| 6825 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6826 | if (outsize <= 0) |
| 6827 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6828 | |
| 6829 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6830 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6831 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6832 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6833 | if (*v == NULL) |
| 6834 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6835 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6836 | } |
| 6837 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6838 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6839 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6840 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6841 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6842 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6846 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6847 | if (outsize <= 0) |
| 6848 | goto error; |
| 6849 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6850 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6851 | error: |
| 6852 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6853 | return -2; |
| 6854 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6855 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6858 | /* |
| 6859 | * Decode a byte string from a code page into unicode object with an error |
| 6860 | * handler. |
| 6861 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6862 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6863 | * UnicodeDecodeError exception and returns -1 on error. |
| 6864 | */ |
| 6865 | static int |
| 6866 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6867 | PyObject **v, |
| 6868 | const char *in, const int size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 6869 | const char *errors, int final) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6870 | { |
| 6871 | const char *startin = in; |
| 6872 | const char *endin = in + size; |
| 6873 | const DWORD flags = decode_code_page_flags(code_page); |
| 6874 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6875 | 2000 English version of the message. */ |
| 6876 | const char *reason = "No mapping for the Unicode character exists " |
| 6877 | "in the target code page."; |
| 6878 | /* each step cannot decode more than 1 character, but a character can be |
| 6879 | represented as a surrogate pair */ |
| 6880 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6881 | int insize; |
| 6882 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6883 | PyObject *errorHandler = NULL; |
| 6884 | PyObject *exc = NULL; |
| 6885 | PyObject *encoding_obj = NULL; |
| 6886 | char *encoding; |
| 6887 | DWORD err; |
| 6888 | int ret = -1; |
| 6889 | |
| 6890 | assert(size > 0); |
| 6891 | |
| 6892 | encoding = code_page_name(code_page, &encoding_obj); |
| 6893 | if (encoding == NULL) |
| 6894 | return -1; |
| 6895 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 6896 | if ((errors == NULL || strcmp(errors, "strict") == 0) && final) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6897 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6898 | UnicodeDecodeError. */ |
| 6899 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6900 | if (exc != NULL) { |
| 6901 | PyCodec_StrictErrors(exc); |
| 6902 | Py_CLEAR(exc); |
| 6903 | } |
| 6904 | goto error; |
| 6905 | } |
| 6906 | |
| 6907 | if (*v == NULL) { |
| 6908 | /* Create unicode object */ |
| 6909 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6910 | PyErr_NoMemory(); |
| 6911 | goto error; |
| 6912 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6913 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6914 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6915 | if (*v == NULL) |
| 6916 | goto error; |
| 6917 | startout = PyUnicode_AS_UNICODE(*v); |
| 6918 | } |
| 6919 | else { |
| 6920 | /* Extend unicode object */ |
| 6921 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6922 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6923 | PyErr_NoMemory(); |
| 6924 | goto error; |
| 6925 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6926 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6927 | goto error; |
| 6928 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6929 | } |
| 6930 | |
| 6931 | /* Decode the byte string character per character */ |
| 6932 | out = startout; |
| 6933 | while (in < endin) |
| 6934 | { |
| 6935 | /* Decode a character */ |
| 6936 | insize = 1; |
| 6937 | do |
| 6938 | { |
| 6939 | outsize = MultiByteToWideChar(code_page, flags, |
| 6940 | in, insize, |
| 6941 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6942 | if (outsize > 0) |
| 6943 | break; |
| 6944 | err = GetLastError(); |
| 6945 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6946 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6947 | { |
| 6948 | PyErr_SetFromWindowsErr(0); |
| 6949 | goto error; |
| 6950 | } |
| 6951 | insize++; |
| 6952 | } |
| 6953 | /* 4=maximum length of a UTF-8 sequence */ |
| 6954 | while (insize <= 4 && (in + insize) <= endin); |
| 6955 | |
| 6956 | if (outsize <= 0) { |
| 6957 | Py_ssize_t startinpos, endinpos, outpos; |
| 6958 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 6959 | /* last character in partial decode? */ |
| 6960 | if (in + insize >= endin && !final) |
| 6961 | break; |
| 6962 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6963 | startinpos = in - startin; |
| 6964 | endinpos = startinpos + 1; |
| 6965 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6966 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6967 | errors, &errorHandler, |
| 6968 | encoding, reason, |
| 6969 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6970 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6971 | { |
| 6972 | goto error; |
| 6973 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6974 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6975 | } |
| 6976 | else { |
| 6977 | in += insize; |
| 6978 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6979 | out += outsize; |
| 6980 | } |
| 6981 | } |
| 6982 | |
| 6983 | /* write a NUL character at the end */ |
| 6984 | *out = 0; |
| 6985 | |
| 6986 | /* Extend unicode object */ |
| 6987 | outsize = out - startout; |
| 6988 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6989 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6990 | goto error; |
Victor Stinner | e1f17c6 | 2014-07-25 14:03:03 +0200 | [diff] [blame] | 6991 | /* (in - startin) <= size and size is an int */ |
| 6992 | ret = Py_SAFE_DOWNCAST(in - startin, Py_ssize_t, int); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6993 | |
| 6994 | error: |
| 6995 | Py_XDECREF(encoding_obj); |
| 6996 | Py_XDECREF(errorHandler); |
| 6997 | Py_XDECREF(exc); |
| 6998 | return ret; |
| 6999 | } |
| 7000 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7001 | static PyObject * |
| 7002 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7003 | const char *s, Py_ssize_t size, |
| 7004 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7005 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7006 | PyObject *v = NULL; |
| 7007 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7008 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7009 | if (code_page < 0) { |
| 7010 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7011 | return NULL; |
| 7012 | } |
| 7013 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7014 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7015 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7016 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7017 | do |
| 7018 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7019 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7020 | if (size > INT_MAX) { |
| 7021 | chunk_size = INT_MAX; |
| 7022 | final = 0; |
| 7023 | done = 0; |
| 7024 | } |
| 7025 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7026 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7027 | { |
| 7028 | chunk_size = (int)size; |
| 7029 | final = (consumed == NULL); |
| 7030 | done = 1; |
| 7031 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7032 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7033 | if (chunk_size == 0 && done) { |
| 7034 | if (v != NULL) |
| 7035 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7036 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7037 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7038 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7039 | converted = decode_code_page_strict(code_page, &v, |
| 7040 | s, chunk_size); |
| 7041 | if (converted == -2) |
| 7042 | converted = decode_code_page_errors(code_page, &v, |
| 7043 | s, chunk_size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7044 | errors, final); |
| 7045 | assert(converted != 0 || done); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7046 | |
| 7047 | if (converted < 0) { |
| 7048 | Py_XDECREF(v); |
| 7049 | return NULL; |
| 7050 | } |
| 7051 | |
| 7052 | if (consumed) |
| 7053 | *consumed += converted; |
| 7054 | |
| 7055 | s += converted; |
| 7056 | size -= converted; |
| 7057 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7058 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7059 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7060 | } |
| 7061 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7062 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7063 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7064 | const char *s, |
| 7065 | Py_ssize_t size, |
| 7066 | const char *errors, |
| 7067 | Py_ssize_t *consumed) |
| 7068 | { |
| 7069 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7070 | } |
| 7071 | |
| 7072 | PyObject * |
| 7073 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7074 | Py_ssize_t size, |
| 7075 | const char *errors, |
| 7076 | Py_ssize_t *consumed) |
| 7077 | { |
| 7078 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7079 | } |
| 7080 | |
| 7081 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7082 | PyUnicode_DecodeMBCS(const char *s, |
| 7083 | Py_ssize_t size, |
| 7084 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7085 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7086 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7087 | } |
| 7088 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7089 | static DWORD |
| 7090 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7091 | { |
| 7092 | if (code_page == CP_UTF8) { |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 7093 | return WC_ERR_INVALID_CHARS; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7094 | } |
| 7095 | else if (code_page == CP_UTF7) { |
| 7096 | /* CP_UTF7 only supports flags=0 */ |
| 7097 | return 0; |
| 7098 | } |
| 7099 | else { |
| 7100 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7101 | return 0; |
| 7102 | else |
| 7103 | return WC_NO_BEST_FIT_CHARS; |
| 7104 | } |
| 7105 | } |
| 7106 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7107 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7108 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7109 | * mode. |
| 7110 | * |
| 7111 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7112 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7113 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7114 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7115 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7116 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7117 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7118 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7119 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7120 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7121 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7122 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7123 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7124 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7125 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7126 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7127 | /* Create a substring so that we can get the UTF-16 representation |
| 7128 | of just the slice under consideration. */ |
| 7129 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7130 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7131 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7132 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7133 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7134 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7135 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7136 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7137 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7138 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7139 | if (substring == NULL) |
| 7140 | return -1; |
| 7141 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7142 | if (p == NULL) { |
| 7143 | Py_DECREF(substring); |
| 7144 | return -1; |
| 7145 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7146 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7147 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7148 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7149 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7150 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7151 | NULL, 0, |
| 7152 | NULL, pusedDefaultChar); |
| 7153 | if (outsize <= 0) |
| 7154 | goto error; |
| 7155 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7156 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7157 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7158 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7159 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7160 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7161 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7162 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7163 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7164 | if (*outbytes == NULL) { |
| 7165 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7166 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7167 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7168 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7169 | } |
| 7170 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7171 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7172 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7173 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7174 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7175 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7176 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7177 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7178 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7179 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7180 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7181 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7182 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7183 | } |
| 7184 | |
| 7185 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7186 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7187 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7188 | out, outsize, |
| 7189 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7190 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7191 | if (outsize <= 0) |
| 7192 | goto error; |
| 7193 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7194 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7195 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7196 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7197 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7198 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7199 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7200 | return -2; |
| 7201 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7202 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7203 | } |
| 7204 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7205 | /* |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 7206 | * Encode a Unicode string to a Windows code page into a byte string using an |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7207 | * error handler. |
| 7208 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7209 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7210 | * -1 on other error. |
| 7211 | */ |
| 7212 | static int |
| 7213 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7214 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7215 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7216 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7217 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7218 | Py_ssize_t pos = unicode_offset; |
| 7219 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7220 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7221 | 2000 English version of the message. */ |
| 7222 | const char *reason = "invalid character"; |
| 7223 | /* 4=maximum length of a UTF-8 sequence */ |
| 7224 | char buffer[4]; |
| 7225 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7226 | Py_ssize_t outsize; |
| 7227 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7228 | PyObject *errorHandler = NULL; |
| 7229 | PyObject *exc = NULL; |
| 7230 | PyObject *encoding_obj = NULL; |
| 7231 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7232 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7233 | PyObject *rep; |
| 7234 | int ret = -1; |
| 7235 | |
| 7236 | assert(insize > 0); |
| 7237 | |
| 7238 | encoding = code_page_name(code_page, &encoding_obj); |
| 7239 | if (encoding == NULL) |
| 7240 | return -1; |
| 7241 | |
| 7242 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7243 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7244 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7245 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7246 | if (exc != NULL) { |
| 7247 | PyCodec_StrictErrors(exc); |
| 7248 | Py_DECREF(exc); |
| 7249 | } |
| 7250 | Py_XDECREF(encoding_obj); |
| 7251 | return -1; |
| 7252 | } |
| 7253 | |
| 7254 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7255 | pusedDefaultChar = &usedDefaultChar; |
| 7256 | else |
| 7257 | pusedDefaultChar = NULL; |
| 7258 | |
| 7259 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7260 | PyErr_NoMemory(); |
| 7261 | goto error; |
| 7262 | } |
| 7263 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7264 | |
| 7265 | if (*outbytes == NULL) { |
| 7266 | /* Create string object */ |
| 7267 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7268 | if (*outbytes == NULL) |
| 7269 | goto error; |
| 7270 | out = PyBytes_AS_STRING(*outbytes); |
| 7271 | } |
| 7272 | else { |
| 7273 | /* Extend string object */ |
| 7274 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7275 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7276 | PyErr_NoMemory(); |
| 7277 | goto error; |
| 7278 | } |
| 7279 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7280 | goto error; |
| 7281 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7282 | } |
| 7283 | |
| 7284 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7285 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7286 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7287 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7288 | wchar_t chars[2]; |
| 7289 | int charsize; |
| 7290 | if (ch < 0x10000) { |
| 7291 | chars[0] = (wchar_t)ch; |
| 7292 | charsize = 1; |
| 7293 | } |
| 7294 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7295 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7296 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7297 | charsize = 2; |
| 7298 | } |
| 7299 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7300 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7301 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7302 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7303 | NULL, pusedDefaultChar); |
| 7304 | if (outsize > 0) { |
| 7305 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7306 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7307 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7308 | memcpy(out, buffer, outsize); |
| 7309 | out += outsize; |
| 7310 | continue; |
| 7311 | } |
| 7312 | } |
| 7313 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7314 | PyErr_SetFromWindowsErr(0); |
| 7315 | goto error; |
| 7316 | } |
| 7317 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7318 | rep = unicode_encode_call_errorhandler( |
| 7319 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7320 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7321 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7322 | if (rep == NULL) |
| 7323 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7324 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7325 | |
| 7326 | if (PyBytes_Check(rep)) { |
| 7327 | outsize = PyBytes_GET_SIZE(rep); |
| 7328 | if (outsize != 1) { |
| 7329 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7330 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7331 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7332 | Py_DECREF(rep); |
| 7333 | goto error; |
| 7334 | } |
| 7335 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7336 | } |
| 7337 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7338 | out += outsize; |
| 7339 | } |
| 7340 | else { |
| 7341 | Py_ssize_t i; |
| 7342 | enum PyUnicode_Kind kind; |
| 7343 | void *data; |
| 7344 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7345 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7346 | Py_DECREF(rep); |
| 7347 | goto error; |
| 7348 | } |
| 7349 | |
| 7350 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7351 | if (outsize != 1) { |
| 7352 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7353 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7354 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7355 | Py_DECREF(rep); |
| 7356 | goto error; |
| 7357 | } |
| 7358 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7359 | } |
| 7360 | kind = PyUnicode_KIND(rep); |
| 7361 | data = PyUnicode_DATA(rep); |
| 7362 | for (i=0; i < outsize; i++) { |
| 7363 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7364 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7365 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7366 | encoding, unicode, |
| 7367 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7368 | "unable to encode error handler result to ASCII"); |
| 7369 | Py_DECREF(rep); |
| 7370 | goto error; |
| 7371 | } |
| 7372 | *out = (unsigned char)ch; |
| 7373 | out++; |
| 7374 | } |
| 7375 | } |
| 7376 | Py_DECREF(rep); |
| 7377 | } |
| 7378 | /* write a NUL byte */ |
| 7379 | *out = 0; |
| 7380 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7381 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7382 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7383 | goto error; |
| 7384 | ret = 0; |
| 7385 | |
| 7386 | error: |
| 7387 | Py_XDECREF(encoding_obj); |
| 7388 | Py_XDECREF(errorHandler); |
| 7389 | Py_XDECREF(exc); |
| 7390 | return ret; |
| 7391 | } |
| 7392 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7393 | static PyObject * |
| 7394 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7395 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7396 | const char *errors) |
| 7397 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7398 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7399 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7400 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7401 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7402 | |
Victor Stinner | 29dacf2 | 2015-01-26 16:41:32 +0100 | [diff] [blame] | 7403 | if (!PyUnicode_Check(unicode)) { |
| 7404 | PyErr_BadArgument(); |
| 7405 | return NULL; |
| 7406 | } |
| 7407 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7408 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7409 | return NULL; |
| 7410 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7411 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7412 | if (code_page < 0) { |
| 7413 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7414 | return NULL; |
| 7415 | } |
| 7416 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7417 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7418 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7419 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7420 | offset = 0; |
| 7421 | do |
| 7422 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7423 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7424 | /* 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] | 7425 | chunks. */ |
| 7426 | if (len > INT_MAX/2) { |
| 7427 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7428 | done = 0; |
| 7429 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7430 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7431 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7432 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7433 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7434 | done = 1; |
| 7435 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7436 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7437 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7438 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7439 | errors); |
| 7440 | if (ret == -2) |
| 7441 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7442 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7443 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7444 | if (ret < 0) { |
| 7445 | Py_XDECREF(outbytes); |
| 7446 | return NULL; |
| 7447 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7448 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7449 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7450 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7451 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7452 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7453 | return outbytes; |
| 7454 | } |
| 7455 | |
| 7456 | PyObject * |
| 7457 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7458 | Py_ssize_t size, |
| 7459 | const char *errors) |
| 7460 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7461 | PyObject *unicode, *res; |
| 7462 | unicode = PyUnicode_FromUnicode(p, size); |
| 7463 | if (unicode == NULL) |
| 7464 | return NULL; |
| 7465 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7466 | Py_DECREF(unicode); |
| 7467 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7468 | } |
| 7469 | |
| 7470 | PyObject * |
| 7471 | PyUnicode_EncodeCodePage(int code_page, |
| 7472 | PyObject *unicode, |
| 7473 | const char *errors) |
| 7474 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7475 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7476 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7477 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7478 | PyObject * |
| 7479 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7480 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7481 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7482 | } |
| 7483 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7484 | #undef NEED_RETRY |
| 7485 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7486 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7487 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7489 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7490 | static int |
| 7491 | charmap_decode_string(const char *s, |
| 7492 | Py_ssize_t size, |
| 7493 | PyObject *mapping, |
| 7494 | const char *errors, |
| 7495 | _PyUnicodeWriter *writer) |
| 7496 | { |
| 7497 | const char *starts = s; |
| 7498 | const char *e; |
| 7499 | Py_ssize_t startinpos, endinpos; |
| 7500 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7501 | Py_ssize_t maplen; |
| 7502 | enum PyUnicode_Kind mapkind; |
| 7503 | void *mapdata; |
| 7504 | Py_UCS4 x; |
| 7505 | unsigned char ch; |
| 7506 | |
| 7507 | if (PyUnicode_READY(mapping) == -1) |
| 7508 | return -1; |
| 7509 | |
| 7510 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7511 | mapdata = PyUnicode_DATA(mapping); |
| 7512 | mapkind = PyUnicode_KIND(mapping); |
| 7513 | |
| 7514 | e = s + size; |
| 7515 | |
| 7516 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7517 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7518 | * is disabled in encoding aliases, latin1 is preferred because |
| 7519 | * its implementation is faster. */ |
| 7520 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7521 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7522 | Py_UCS4 maxchar = writer->maxchar; |
| 7523 | |
| 7524 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7525 | while (s < e) { |
| 7526 | ch = *s; |
| 7527 | x = mapdata_ucs1[ch]; |
| 7528 | if (x > maxchar) { |
| 7529 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7530 | goto onError; |
| 7531 | maxchar = writer->maxchar; |
| 7532 | outdata = (Py_UCS1 *)writer->data; |
| 7533 | } |
| 7534 | outdata[writer->pos] = x; |
| 7535 | writer->pos++; |
| 7536 | ++s; |
| 7537 | } |
| 7538 | return 0; |
| 7539 | } |
| 7540 | |
| 7541 | while (s < e) { |
| 7542 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7543 | enum PyUnicode_Kind outkind = writer->kind; |
| 7544 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7545 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7546 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7547 | Py_UCS4 maxchar = writer->maxchar; |
| 7548 | while (s < e) { |
| 7549 | ch = *s; |
| 7550 | x = mapdata_ucs2[ch]; |
| 7551 | if (x > maxchar) |
| 7552 | goto Error; |
| 7553 | outdata[writer->pos] = x; |
| 7554 | writer->pos++; |
| 7555 | ++s; |
| 7556 | } |
| 7557 | break; |
| 7558 | } |
| 7559 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7560 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7561 | while (s < e) { |
| 7562 | ch = *s; |
| 7563 | x = mapdata_ucs2[ch]; |
| 7564 | if (x == 0xFFFE) |
| 7565 | goto Error; |
| 7566 | outdata[writer->pos] = x; |
| 7567 | writer->pos++; |
| 7568 | ++s; |
| 7569 | } |
| 7570 | break; |
| 7571 | } |
| 7572 | } |
| 7573 | ch = *s; |
| 7574 | |
| 7575 | if (ch < maplen) |
| 7576 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7577 | else |
| 7578 | x = 0xfffe; /* invalid value */ |
| 7579 | Error: |
| 7580 | if (x == 0xfffe) |
| 7581 | { |
| 7582 | /* undefined mapping */ |
| 7583 | startinpos = s-starts; |
| 7584 | endinpos = startinpos+1; |
| 7585 | if (unicode_decode_call_errorhandler_writer( |
| 7586 | errors, &errorHandler, |
| 7587 | "charmap", "character maps to <undefined>", |
| 7588 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7589 | writer)) { |
| 7590 | goto onError; |
| 7591 | } |
| 7592 | continue; |
| 7593 | } |
| 7594 | |
| 7595 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7596 | goto onError; |
| 7597 | ++s; |
| 7598 | } |
| 7599 | Py_XDECREF(errorHandler); |
| 7600 | Py_XDECREF(exc); |
| 7601 | return 0; |
| 7602 | |
| 7603 | onError: |
| 7604 | Py_XDECREF(errorHandler); |
| 7605 | Py_XDECREF(exc); |
| 7606 | return -1; |
| 7607 | } |
| 7608 | |
| 7609 | static int |
| 7610 | charmap_decode_mapping(const char *s, |
| 7611 | Py_ssize_t size, |
| 7612 | PyObject *mapping, |
| 7613 | const char *errors, |
| 7614 | _PyUnicodeWriter *writer) |
| 7615 | { |
| 7616 | const char *starts = s; |
| 7617 | const char *e; |
| 7618 | Py_ssize_t startinpos, endinpos; |
| 7619 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7620 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7621 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7622 | |
| 7623 | e = s + size; |
| 7624 | |
| 7625 | while (s < e) { |
| 7626 | ch = *s; |
| 7627 | |
| 7628 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7629 | key = PyLong_FromLong((long)ch); |
| 7630 | if (key == NULL) |
| 7631 | goto onError; |
| 7632 | |
| 7633 | item = PyObject_GetItem(mapping, key); |
| 7634 | Py_DECREF(key); |
| 7635 | if (item == NULL) { |
| 7636 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7637 | /* No mapping found means: mapping is undefined. */ |
| 7638 | PyErr_Clear(); |
| 7639 | goto Undefined; |
| 7640 | } else |
| 7641 | goto onError; |
| 7642 | } |
| 7643 | |
| 7644 | /* Apply mapping */ |
| 7645 | if (item == Py_None) |
| 7646 | goto Undefined; |
| 7647 | if (PyLong_Check(item)) { |
| 7648 | long value = PyLong_AS_LONG(item); |
| 7649 | if (value == 0xFFFE) |
| 7650 | goto Undefined; |
| 7651 | if (value < 0 || value > MAX_UNICODE) { |
| 7652 | PyErr_Format(PyExc_TypeError, |
| 7653 | "character mapping must be in range(0x%lx)", |
| 7654 | (unsigned long)MAX_UNICODE + 1); |
| 7655 | goto onError; |
| 7656 | } |
| 7657 | |
| 7658 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7659 | goto onError; |
| 7660 | } |
| 7661 | else if (PyUnicode_Check(item)) { |
| 7662 | if (PyUnicode_READY(item) == -1) |
| 7663 | goto onError; |
| 7664 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7665 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7666 | if (value == 0xFFFE) |
| 7667 | goto Undefined; |
| 7668 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7669 | goto onError; |
| 7670 | } |
| 7671 | else { |
| 7672 | writer->overallocate = 1; |
| 7673 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7674 | goto onError; |
| 7675 | } |
| 7676 | } |
| 7677 | else { |
| 7678 | /* wrong return value */ |
| 7679 | PyErr_SetString(PyExc_TypeError, |
| 7680 | "character mapping must return integer, None or str"); |
| 7681 | goto onError; |
| 7682 | } |
| 7683 | Py_CLEAR(item); |
| 7684 | ++s; |
| 7685 | continue; |
| 7686 | |
| 7687 | Undefined: |
| 7688 | /* undefined mapping */ |
| 7689 | Py_CLEAR(item); |
| 7690 | startinpos = s-starts; |
| 7691 | endinpos = startinpos+1; |
| 7692 | if (unicode_decode_call_errorhandler_writer( |
| 7693 | errors, &errorHandler, |
| 7694 | "charmap", "character maps to <undefined>", |
| 7695 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7696 | writer)) { |
| 7697 | goto onError; |
| 7698 | } |
| 7699 | } |
| 7700 | Py_XDECREF(errorHandler); |
| 7701 | Py_XDECREF(exc); |
| 7702 | return 0; |
| 7703 | |
| 7704 | onError: |
| 7705 | Py_XDECREF(item); |
| 7706 | Py_XDECREF(errorHandler); |
| 7707 | Py_XDECREF(exc); |
| 7708 | return -1; |
| 7709 | } |
| 7710 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7711 | PyObject * |
| 7712 | PyUnicode_DecodeCharmap(const char *s, |
| 7713 | Py_ssize_t size, |
| 7714 | PyObject *mapping, |
| 7715 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7716 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7717 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7719 | /* Default to Latin-1 */ |
| 7720 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7721 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7723 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7724 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7725 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7726 | writer.min_length = size; |
| 7727 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7729 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7730 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7731 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7732 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7733 | } |
| 7734 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7735 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7736 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7737 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7738 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7739 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7740 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7741 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7742 | return NULL; |
| 7743 | } |
| 7744 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7745 | /* Charmap encoding: the lookup table */ |
| 7746 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7747 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7748 | PyObject_HEAD |
| 7749 | unsigned char level1[32]; |
| 7750 | int count2, count3; |
| 7751 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7752 | }; |
| 7753 | |
| 7754 | static PyObject* |
| 7755 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7756 | { |
| 7757 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7758 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7759 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
| 7762 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7763 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7764 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7765 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7766 | }; |
| 7767 | |
| 7768 | static void |
| 7769 | encoding_map_dealloc(PyObject* o) |
| 7770 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7771 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7772 | } |
| 7773 | |
| 7774 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7775 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7776 | "EncodingMap", /*tp_name*/ |
| 7777 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7778 | 0, /*tp_itemsize*/ |
| 7779 | /* methods */ |
| 7780 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7781 | 0, /*tp_print*/ |
| 7782 | 0, /*tp_getattr*/ |
| 7783 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7784 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7785 | 0, /*tp_repr*/ |
| 7786 | 0, /*tp_as_number*/ |
| 7787 | 0, /*tp_as_sequence*/ |
| 7788 | 0, /*tp_as_mapping*/ |
| 7789 | 0, /*tp_hash*/ |
| 7790 | 0, /*tp_call*/ |
| 7791 | 0, /*tp_str*/ |
| 7792 | 0, /*tp_getattro*/ |
| 7793 | 0, /*tp_setattro*/ |
| 7794 | 0, /*tp_as_buffer*/ |
| 7795 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7796 | 0, /*tp_doc*/ |
| 7797 | 0, /*tp_traverse*/ |
| 7798 | 0, /*tp_clear*/ |
| 7799 | 0, /*tp_richcompare*/ |
| 7800 | 0, /*tp_weaklistoffset*/ |
| 7801 | 0, /*tp_iter*/ |
| 7802 | 0, /*tp_iternext*/ |
| 7803 | encoding_map_methods, /*tp_methods*/ |
| 7804 | 0, /*tp_members*/ |
| 7805 | 0, /*tp_getset*/ |
| 7806 | 0, /*tp_base*/ |
| 7807 | 0, /*tp_dict*/ |
| 7808 | 0, /*tp_descr_get*/ |
| 7809 | 0, /*tp_descr_set*/ |
| 7810 | 0, /*tp_dictoffset*/ |
| 7811 | 0, /*tp_init*/ |
| 7812 | 0, /*tp_alloc*/ |
| 7813 | 0, /*tp_new*/ |
| 7814 | 0, /*tp_free*/ |
| 7815 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7816 | }; |
| 7817 | |
| 7818 | PyObject* |
| 7819 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7820 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7821 | PyObject *result; |
| 7822 | struct encoding_map *mresult; |
| 7823 | int i; |
| 7824 | int need_dict = 0; |
| 7825 | unsigned char level1[32]; |
| 7826 | unsigned char level2[512]; |
| 7827 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7828 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7829 | int kind; |
| 7830 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7831 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7832 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7833 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7834 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7835 | PyErr_BadArgument(); |
| 7836 | return NULL; |
| 7837 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7838 | kind = PyUnicode_KIND(string); |
| 7839 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7840 | length = PyUnicode_GET_LENGTH(string); |
| 7841 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7842 | memset(level1, 0xFF, sizeof level1); |
| 7843 | memset(level2, 0xFF, sizeof level2); |
| 7844 | |
| 7845 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7846 | or if there are non-BMP characters, we need to use |
| 7847 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7848 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7849 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7850 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7851 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7852 | ch = PyUnicode_READ(kind, data, i); |
| 7853 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7854 | need_dict = 1; |
| 7855 | break; |
| 7856 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7857 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7858 | /* unmapped character */ |
| 7859 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7860 | l1 = ch >> 11; |
| 7861 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7862 | if (level1[l1] == 0xFF) |
| 7863 | level1[l1] = count2++; |
| 7864 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7865 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7866 | } |
| 7867 | |
| 7868 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7869 | need_dict = 1; |
| 7870 | |
| 7871 | if (need_dict) { |
| 7872 | PyObject *result = PyDict_New(); |
| 7873 | PyObject *key, *value; |
| 7874 | if (!result) |
| 7875 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7876 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7877 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7878 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7879 | if (!key || !value) |
| 7880 | goto failed1; |
| 7881 | if (PyDict_SetItem(result, key, value) == -1) |
| 7882 | goto failed1; |
| 7883 | Py_DECREF(key); |
| 7884 | Py_DECREF(value); |
| 7885 | } |
| 7886 | return result; |
| 7887 | failed1: |
| 7888 | Py_XDECREF(key); |
| 7889 | Py_XDECREF(value); |
| 7890 | Py_DECREF(result); |
| 7891 | return NULL; |
| 7892 | } |
| 7893 | |
| 7894 | /* Create a three-level trie */ |
| 7895 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7896 | 16*count2 + 128*count3 - 1); |
| 7897 | if (!result) |
| 7898 | return PyErr_NoMemory(); |
| 7899 | PyObject_Init(result, &EncodingMapType); |
| 7900 | mresult = (struct encoding_map*)result; |
| 7901 | mresult->count2 = count2; |
| 7902 | mresult->count3 = count3; |
| 7903 | mlevel1 = mresult->level1; |
| 7904 | mlevel2 = mresult->level23; |
| 7905 | mlevel3 = mresult->level23 + 16*count2; |
| 7906 | memcpy(mlevel1, level1, 32); |
| 7907 | memset(mlevel2, 0xFF, 16*count2); |
| 7908 | memset(mlevel3, 0, 128*count3); |
| 7909 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7910 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7911 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7912 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7913 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7914 | /* unmapped character */ |
| 7915 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7916 | o1 = ch>>11; |
| 7917 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7918 | i2 = 16*mlevel1[o1] + o2; |
| 7919 | if (mlevel2[i2] == 0xFF) |
| 7920 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7921 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7922 | i3 = 128*mlevel2[i2] + o3; |
| 7923 | mlevel3[i3] = i; |
| 7924 | } |
| 7925 | return result; |
| 7926 | } |
| 7927 | |
| 7928 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7929 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7930 | { |
| 7931 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7932 | int l1 = c>>11; |
| 7933 | int l2 = (c>>7) & 0xF; |
| 7934 | int l3 = c & 0x7F; |
| 7935 | int i; |
| 7936 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7937 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7938 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7939 | if (c == 0) |
| 7940 | return 0; |
| 7941 | /* level 1*/ |
| 7942 | i = map->level1[l1]; |
| 7943 | if (i == 0xFF) { |
| 7944 | return -1; |
| 7945 | } |
| 7946 | /* level 2*/ |
| 7947 | i = map->level23[16*i+l2]; |
| 7948 | if (i == 0xFF) { |
| 7949 | return -1; |
| 7950 | } |
| 7951 | /* level 3 */ |
| 7952 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7953 | if (i == 0) { |
| 7954 | return -1; |
| 7955 | } |
| 7956 | return i; |
| 7957 | } |
| 7958 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7959 | /* Lookup the character ch in the mapping. If the character |
| 7960 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7961 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7962 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7963 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7964 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7965 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7966 | PyObject *x; |
| 7967 | |
| 7968 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7969 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7970 | x = PyObject_GetItem(mapping, w); |
| 7971 | Py_DECREF(w); |
| 7972 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7973 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7974 | /* No mapping found means: mapping is undefined. */ |
| 7975 | PyErr_Clear(); |
| 7976 | x = Py_None; |
| 7977 | Py_INCREF(x); |
| 7978 | return x; |
| 7979 | } else |
| 7980 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7981 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7982 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7984 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | long value = PyLong_AS_LONG(x); |
| 7986 | if (value < 0 || value > 255) { |
| 7987 | PyErr_SetString(PyExc_TypeError, |
| 7988 | "character mapping must be in range(256)"); |
| 7989 | Py_DECREF(x); |
| 7990 | return NULL; |
| 7991 | } |
| 7992 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7993 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7994 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7996 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7997 | /* wrong return value */ |
| 7998 | PyErr_Format(PyExc_TypeError, |
| 7999 | "character mapping must return integer, bytes or None, not %.400s", |
| 8000 | x->ob_type->tp_name); |
| 8001 | Py_DECREF(x); |
| 8002 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8003 | } |
| 8004 | } |
| 8005 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8006 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8007 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8008 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8009 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8010 | /* exponentially overallocate to minimize reallocations */ |
| 8011 | if (requiredsize < 2*outsize) |
| 8012 | requiredsize = 2*outsize; |
| 8013 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8014 | return -1; |
| 8015 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8016 | } |
| 8017 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8018 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8019 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8020 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8021 | /* 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] | 8022 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8023 | space is available. Return a new reference to the object that |
| 8024 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8025 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8026 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8027 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8028 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8029 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8030 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8031 | PyObject *rep; |
| 8032 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8033 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8034 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8035 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8036 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8037 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8038 | if (res == -1) |
| 8039 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8040 | if (outsize<requiredsize) |
| 8041 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8042 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8043 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8044 | outstart[(*outpos)++] = (char)res; |
| 8045 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8046 | } |
| 8047 | |
| 8048 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8049 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8050 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8051 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 | Py_DECREF(rep); |
| 8053 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8054 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8055 | if (PyLong_Check(rep)) { |
| 8056 | Py_ssize_t requiredsize = *outpos+1; |
| 8057 | if (outsize<requiredsize) |
| 8058 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8059 | Py_DECREF(rep); |
| 8060 | return enc_EXCEPTION; |
| 8061 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8062 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8063 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8064 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8065 | else { |
| 8066 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8067 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8068 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8069 | if (outsize<requiredsize) |
| 8070 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8071 | Py_DECREF(rep); |
| 8072 | return enc_EXCEPTION; |
| 8073 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8074 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8075 | memcpy(outstart + *outpos, repchars, repsize); |
| 8076 | *outpos += repsize; |
| 8077 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8078 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8079 | Py_DECREF(rep); |
| 8080 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8081 | } |
| 8082 | |
| 8083 | /* handle an error in PyUnicode_EncodeCharmap |
| 8084 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8085 | static int |
| 8086 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8087 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8088 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8089 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8090 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8091 | { |
| 8092 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8093 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8094 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8095 | enum PyUnicode_Kind kind; |
| 8096 | void *data; |
| 8097 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8098 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8099 | Py_ssize_t collstartpos = *inpos; |
| 8100 | Py_ssize_t collendpos = *inpos+1; |
| 8101 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8102 | char *encoding = "charmap"; |
| 8103 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8104 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8105 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8106 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8107 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8108 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8109 | return -1; |
| 8110 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8111 | /* find all unencodable characters */ |
| 8112 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8113 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8114 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8115 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8116 | val = encoding_map_lookup(ch, mapping); |
| 8117 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8118 | break; |
| 8119 | ++collendpos; |
| 8120 | continue; |
| 8121 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8122 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8123 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8124 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8125 | if (rep==NULL) |
| 8126 | return -1; |
| 8127 | else if (rep!=Py_None) { |
| 8128 | Py_DECREF(rep); |
| 8129 | break; |
| 8130 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8131 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8132 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8133 | } |
| 8134 | /* cache callback name lookup |
| 8135 | * (if not done yet, i.e. it's the first error) */ |
| 8136 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8137 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8138 | *known_errorHandler = 1; |
| 8139 | else if (!strcmp(errors, "replace")) |
| 8140 | *known_errorHandler = 2; |
| 8141 | else if (!strcmp(errors, "ignore")) |
| 8142 | *known_errorHandler = 3; |
| 8143 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8144 | *known_errorHandler = 4; |
| 8145 | else |
| 8146 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8147 | } |
| 8148 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8149 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8150 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8151 | return -1; |
| 8152 | case 2: /* replace */ |
| 8153 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8154 | x = charmapencode_output('?', mapping, res, respos); |
| 8155 | if (x==enc_EXCEPTION) { |
| 8156 | return -1; |
| 8157 | } |
| 8158 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8159 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | return -1; |
| 8161 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8162 | } |
| 8163 | /* fall through */ |
| 8164 | case 3: /* ignore */ |
| 8165 | *inpos = collendpos; |
| 8166 | break; |
| 8167 | case 4: /* xmlcharrefreplace */ |
| 8168 | /* generate replacement (temporarily (mis)uses p) */ |
| 8169 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8170 | char buffer[2+29+1+1]; |
| 8171 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8172 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8173 | for (cp = buffer; *cp; ++cp) { |
| 8174 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8175 | if (x==enc_EXCEPTION) |
| 8176 | return -1; |
| 8177 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8178 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8179 | return -1; |
| 8180 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8181 | } |
| 8182 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8183 | *inpos = collendpos; |
| 8184 | break; |
| 8185 | default: |
| 8186 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8187 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8188 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8189 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8190 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8191 | if (PyBytes_Check(repunicode)) { |
| 8192 | /* Directly copy bytes result to output. */ |
| 8193 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8194 | Py_ssize_t requiredsize; |
| 8195 | repsize = PyBytes_Size(repunicode); |
| 8196 | requiredsize = *respos + repsize; |
| 8197 | if (requiredsize > outsize) |
| 8198 | /* Make room for all additional bytes. */ |
| 8199 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8200 | Py_DECREF(repunicode); |
| 8201 | return -1; |
| 8202 | } |
| 8203 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8204 | PyBytes_AsString(repunicode), repsize); |
| 8205 | *respos += repsize; |
| 8206 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8207 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8208 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8209 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8210 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8211 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8212 | Py_DECREF(repunicode); |
| 8213 | return -1; |
| 8214 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8215 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8216 | data = PyUnicode_DATA(repunicode); |
| 8217 | kind = PyUnicode_KIND(repunicode); |
| 8218 | for (index = 0; index < repsize; index++) { |
| 8219 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8220 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8221 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8222 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8223 | return -1; |
| 8224 | } |
| 8225 | else if (x==enc_FAILED) { |
| 8226 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8227 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8228 | return -1; |
| 8229 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8230 | } |
| 8231 | *inpos = newpos; |
| 8232 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8233 | } |
| 8234 | return 0; |
| 8235 | } |
| 8236 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8237 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8238 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8239 | PyObject *mapping, |
| 8240 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8241 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8242 | /* output object */ |
| 8243 | PyObject *res = NULL; |
| 8244 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8245 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8246 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8247 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8248 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8249 | PyObject *errorHandler = NULL; |
| 8250 | PyObject *exc = NULL; |
| 8251 | /* the following variable is used for caching string comparisons |
| 8252 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8253 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8254 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8255 | void *data; |
| 8256 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8258 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8259 | return NULL; |
| 8260 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8261 | data = PyUnicode_DATA(unicode); |
| 8262 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8263 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8264 | /* Default to Latin-1 */ |
| 8265 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8266 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8267 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8268 | /* allocate enough for a simple encoding without |
| 8269 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8270 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8271 | if (res == NULL) |
| 8272 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8273 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8274 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8276 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8277 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8278 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8279 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8280 | if (x==enc_EXCEPTION) /* error */ |
| 8281 | goto onError; |
| 8282 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8283 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8284 | &exc, |
| 8285 | &known_errorHandler, &errorHandler, errors, |
| 8286 | &res, &respos)) { |
| 8287 | goto onError; |
| 8288 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8289 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8290 | else |
| 8291 | /* done with this character => adjust input position */ |
| 8292 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8293 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8294 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8295 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8296 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8297 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8298 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8299 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8300 | Py_XDECREF(exc); |
| 8301 | Py_XDECREF(errorHandler); |
| 8302 | return res; |
| 8303 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8305 | Py_XDECREF(res); |
| 8306 | Py_XDECREF(exc); |
| 8307 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8308 | return NULL; |
| 8309 | } |
| 8310 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8311 | /* Deprecated */ |
| 8312 | PyObject * |
| 8313 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8314 | Py_ssize_t size, |
| 8315 | PyObject *mapping, |
| 8316 | const char *errors) |
| 8317 | { |
| 8318 | PyObject *result; |
| 8319 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8320 | if (unicode == NULL) |
| 8321 | return NULL; |
| 8322 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8323 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8324 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8325 | } |
| 8326 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8327 | PyObject * |
| 8328 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8329 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8330 | { |
| 8331 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8332 | PyErr_BadArgument(); |
| 8333 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8335 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8336 | } |
| 8337 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8338 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8339 | static void |
| 8340 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8341 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8342 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8343 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8345 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8346 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8347 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8348 | } |
| 8349 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8350 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8351 | goto onError; |
| 8352 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8353 | goto onError; |
| 8354 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8355 | goto onError; |
| 8356 | return; |
| 8357 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 8358 | Py_CLEAR(*exceptionObject); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8359 | } |
| 8360 | } |
| 8361 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8362 | /* error handling callback helper: |
| 8363 | build arguments, call the callback and check the arguments, |
| 8364 | put the result into newpos and return the replacement string, which |
| 8365 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8366 | static PyObject * |
| 8367 | unicode_translate_call_errorhandler(const char *errors, |
| 8368 | PyObject **errorHandler, |
| 8369 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8370 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8371 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8372 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8373 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8374 | 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] | 8375 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8376 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8377 | PyObject *restuple; |
| 8378 | PyObject *resunicode; |
| 8379 | |
| 8380 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8381 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8382 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8383 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8384 | } |
| 8385 | |
| 8386 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8387 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8388 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8389 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8390 | |
| 8391 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8392 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8393 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8394 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8395 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8396 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8397 | Py_DECREF(restuple); |
| 8398 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8399 | } |
| 8400 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8401 | &resunicode, &i_newpos)) { |
| 8402 | Py_DECREF(restuple); |
| 8403 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8404 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8405 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8406 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8407 | else |
| 8408 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8409 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 8410 | 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] | 8411 | Py_DECREF(restuple); |
| 8412 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8413 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8414 | Py_INCREF(resunicode); |
| 8415 | Py_DECREF(restuple); |
| 8416 | return resunicode; |
| 8417 | } |
| 8418 | |
| 8419 | /* Lookup the character ch in the mapping and put the result in result, |
| 8420 | which must be decrefed by the caller. |
| 8421 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8422 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8424 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8425 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8426 | PyObject *x; |
| 8427 | |
| 8428 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8429 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8430 | x = PyObject_GetItem(mapping, w); |
| 8431 | Py_DECREF(w); |
| 8432 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8433 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8434 | /* No mapping found means: use 1:1 mapping. */ |
| 8435 | PyErr_Clear(); |
| 8436 | *result = NULL; |
| 8437 | return 0; |
| 8438 | } else |
| 8439 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8440 | } |
| 8441 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8442 | *result = x; |
| 8443 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8444 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8445 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8446 | long value = PyLong_AS_LONG(x); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8447 | if (value < 0 || value > MAX_UNICODE) { |
| 8448 | PyErr_Format(PyExc_ValueError, |
| 8449 | "character mapping must be in range(0x%x)", |
| 8450 | MAX_UNICODE+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8451 | Py_DECREF(x); |
| 8452 | return -1; |
| 8453 | } |
| 8454 | *result = x; |
| 8455 | return 0; |
| 8456 | } |
| 8457 | else if (PyUnicode_Check(x)) { |
| 8458 | *result = x; |
| 8459 | return 0; |
| 8460 | } |
| 8461 | else { |
| 8462 | /* wrong return value */ |
| 8463 | PyErr_SetString(PyExc_TypeError, |
| 8464 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8465 | Py_DECREF(x); |
| 8466 | return -1; |
| 8467 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8468 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8469 | |
| 8470 | /* lookup the character, write the result into the writer. |
| 8471 | Return 1 if the result was written into the writer, return 0 if the mapping |
| 8472 | was undefined, raise an exception return -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8473 | static int |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8474 | charmaptranslate_output(Py_UCS4 ch, PyObject *mapping, |
| 8475 | _PyUnicodeWriter *writer) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8476 | { |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8477 | PyObject *item; |
| 8478 | |
| 8479 | if (charmaptranslate_lookup(ch, mapping, &item)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8480 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8481 | |
| 8482 | if (item == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8483 | /* not found => default to 1:1 mapping */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8484 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8485 | return -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8486 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8487 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8488 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8489 | |
| 8490 | if (item == Py_None) { |
| 8491 | Py_DECREF(item); |
| 8492 | return 0; |
| 8493 | } |
| 8494 | |
| 8495 | if (PyLong_Check(item)) { |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8496 | long ch = (Py_UCS4)PyLong_AS_LONG(item); |
| 8497 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8498 | used it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8499 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
| 8500 | Py_DECREF(item); |
| 8501 | return -1; |
| 8502 | } |
| 8503 | Py_DECREF(item); |
| 8504 | return 1; |
| 8505 | } |
| 8506 | |
| 8507 | if (!PyUnicode_Check(item)) { |
| 8508 | Py_DECREF(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8510 | } |
| 8511 | |
| 8512 | if (_PyUnicodeWriter_WriteStr(writer, item) < 0) { |
| 8513 | Py_DECREF(item); |
| 8514 | return -1; |
| 8515 | } |
| 8516 | |
| 8517 | Py_DECREF(item); |
| 8518 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8519 | } |
| 8520 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8521 | static int |
| 8522 | unicode_fast_translate_lookup(PyObject *mapping, Py_UCS1 ch, |
| 8523 | Py_UCS1 *translate) |
| 8524 | { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8525 | PyObject *item = NULL; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8526 | int ret = 0; |
| 8527 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8528 | if (charmaptranslate_lookup(ch, mapping, &item)) { |
| 8529 | return -1; |
| 8530 | } |
| 8531 | |
| 8532 | if (item == Py_None) { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8533 | /* deletion */ |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8534 | translate[ch] = 0xfe; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8535 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8536 | else if (item == NULL) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8537 | /* not found => default to 1:1 mapping */ |
| 8538 | translate[ch] = ch; |
| 8539 | return 1; |
| 8540 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8541 | else if (PyLong_Check(item)) { |
Victor Stinner | 4dd2525 | 2014-04-08 09:14:21 +0200 | [diff] [blame] | 8542 | long replace = PyLong_AS_LONG(item); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8543 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8544 | used it */ |
| 8545 | if (127 < replace) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8546 | /* invalid character or character outside ASCII: |
| 8547 | skip the fast translate */ |
| 8548 | goto exit; |
| 8549 | } |
| 8550 | translate[ch] = (Py_UCS1)replace; |
| 8551 | } |
| 8552 | else if (PyUnicode_Check(item)) { |
| 8553 | Py_UCS4 replace; |
| 8554 | |
| 8555 | if (PyUnicode_READY(item) == -1) { |
| 8556 | Py_DECREF(item); |
| 8557 | return -1; |
| 8558 | } |
| 8559 | if (PyUnicode_GET_LENGTH(item) != 1) |
| 8560 | goto exit; |
| 8561 | |
| 8562 | replace = PyUnicode_READ_CHAR(item, 0); |
| 8563 | if (replace > 127) |
| 8564 | goto exit; |
| 8565 | translate[ch] = (Py_UCS1)replace; |
| 8566 | } |
| 8567 | else { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8568 | /* not None, NULL, long or unicode */ |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8569 | goto exit; |
| 8570 | } |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8571 | ret = 1; |
| 8572 | |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8573 | exit: |
| 8574 | Py_DECREF(item); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8575 | return ret; |
| 8576 | } |
| 8577 | |
| 8578 | /* Fast path for ascii => ascii translation. Return 1 if the whole string |
| 8579 | was translated into writer, return 0 if the input string was partially |
| 8580 | translated into writer, raise an exception and return -1 on error. */ |
| 8581 | static int |
| 8582 | unicode_fast_translate(PyObject *input, PyObject *mapping, |
Victor Stinner | 6c9aa8f | 2016-03-01 21:30:30 +0100 | [diff] [blame] | 8583 | _PyUnicodeWriter *writer, int ignore, |
| 8584 | Py_ssize_t *input_pos) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8585 | { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8586 | Py_UCS1 ascii_table[128], ch, ch2; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8587 | Py_ssize_t len; |
| 8588 | Py_UCS1 *in, *end, *out; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8589 | int res = 0; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8590 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8591 | len = PyUnicode_GET_LENGTH(input); |
| 8592 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8593 | memset(ascii_table, 0xff, 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8594 | |
| 8595 | in = PyUnicode_1BYTE_DATA(input); |
| 8596 | end = in + len; |
| 8597 | |
| 8598 | assert(PyUnicode_IS_ASCII(writer->buffer)); |
| 8599 | assert(PyUnicode_GET_LENGTH(writer->buffer) == len); |
| 8600 | out = PyUnicode_1BYTE_DATA(writer->buffer); |
| 8601 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8602 | for (; in < end; in++) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8603 | ch = *in; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8604 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8605 | if (ch2 == 0xff) { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8606 | int translate = unicode_fast_translate_lookup(mapping, ch, |
| 8607 | ascii_table); |
| 8608 | if (translate < 0) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8609 | return -1; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8610 | if (translate == 0) |
| 8611 | goto exit; |
| 8612 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8613 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8614 | if (ch2 == 0xfe) { |
| 8615 | if (ignore) |
| 8616 | continue; |
| 8617 | goto exit; |
| 8618 | } |
| 8619 | assert(ch2 < 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8620 | *out = ch2; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8621 | out++; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8622 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8623 | res = 1; |
| 8624 | |
| 8625 | exit: |
| 8626 | writer->pos = out - PyUnicode_1BYTE_DATA(writer->buffer); |
Victor Stinner | 6c9aa8f | 2016-03-01 21:30:30 +0100 | [diff] [blame] | 8627 | *input_pos = in - PyUnicode_1BYTE_DATA(input); |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8628 | return res; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8629 | } |
| 8630 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8631 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8632 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8633 | PyObject *mapping, |
| 8634 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8635 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8636 | /* input object */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8637 | char *data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8638 | Py_ssize_t size, i; |
| 8639 | int kind; |
| 8640 | /* output buffer */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8641 | _PyUnicodeWriter writer; |
| 8642 | /* error handler */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8643 | char *reason = "character maps to <undefined>"; |
| 8644 | PyObject *errorHandler = NULL; |
| 8645 | PyObject *exc = NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8646 | int ignore; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8647 | int res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8649 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8650 | PyErr_BadArgument(); |
| 8651 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8652 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8653 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8654 | if (PyUnicode_READY(input) == -1) |
| 8655 | return NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8656 | data = (char*)PyUnicode_DATA(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8657 | kind = PyUnicode_KIND(input); |
| 8658 | size = PyUnicode_GET_LENGTH(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8659 | |
| 8660 | if (size == 0) { |
| 8661 | Py_INCREF(input); |
| 8662 | return input; |
| 8663 | } |
| 8664 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8665 | /* allocate enough for a simple 1:1 translation without |
| 8666 | replacements, if we need more, we'll resize */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8667 | _PyUnicodeWriter_Init(&writer); |
| 8668 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8669 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8670 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8671 | ignore = (errors != NULL && strcmp(errors, "ignore") == 0); |
| 8672 | |
Victor Stinner | 3379867 | 2016-03-01 21:59:58 +0100 | [diff] [blame] | 8673 | if (PyUnicode_READY(input) == -1) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8674 | return NULL; |
Victor Stinner | 3379867 | 2016-03-01 21:59:58 +0100 | [diff] [blame] | 8675 | if (PyUnicode_IS_ASCII(input)) { |
| 8676 | res = unicode_fast_translate(input, mapping, &writer, ignore, &i); |
| 8677 | if (res < 0) { |
| 8678 | _PyUnicodeWriter_Dealloc(&writer); |
| 8679 | return NULL; |
| 8680 | } |
| 8681 | if (res == 1) |
| 8682 | return _PyUnicodeWriter_Finish(&writer); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8683 | } |
Victor Stinner | 3379867 | 2016-03-01 21:59:58 +0100 | [diff] [blame] | 8684 | else { |
| 8685 | i = 0; |
| 8686 | } |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8687 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8688 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8689 | /* try to encode it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8690 | int translate; |
| 8691 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8692 | Py_ssize_t newpos; |
| 8693 | /* startpos for collecting untranslatable chars */ |
| 8694 | Py_ssize_t collstart; |
| 8695 | Py_ssize_t collend; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8696 | Py_UCS4 ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8697 | |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8698 | ch = PyUnicode_READ(kind, data, i); |
| 8699 | translate = charmaptranslate_output(ch, mapping, &writer); |
| 8700 | if (translate < 0) |
| 8701 | goto onError; |
| 8702 | |
| 8703 | if (translate != 0) { |
| 8704 | /* it worked => adjust input pointer */ |
| 8705 | ++i; |
| 8706 | continue; |
| 8707 | } |
| 8708 | |
| 8709 | /* untranslatable character */ |
| 8710 | collstart = i; |
| 8711 | collend = i+1; |
| 8712 | |
| 8713 | /* find all untranslatable characters */ |
| 8714 | while (collend < size) { |
| 8715 | PyObject *x; |
| 8716 | ch = PyUnicode_READ(kind, data, collend); |
| 8717 | if (charmaptranslate_lookup(ch, mapping, &x)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8718 | goto onError; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8719 | Py_XDECREF(x); |
| 8720 | if (x != Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8721 | break; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8722 | ++collend; |
| 8723 | } |
| 8724 | |
| 8725 | if (ignore) { |
| 8726 | i = collend; |
| 8727 | } |
| 8728 | else { |
| 8729 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 8730 | reason, input, &exc, |
| 8731 | collstart, collend, &newpos); |
| 8732 | if (repunicode == NULL) |
| 8733 | goto onError; |
| 8734 | if (_PyUnicodeWriter_WriteStr(&writer, repunicode) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8735 | Py_DECREF(repunicode); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8736 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8737 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8738 | Py_DECREF(repunicode); |
| 8739 | i = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8740 | } |
| 8741 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8742 | Py_XDECREF(exc); |
| 8743 | Py_XDECREF(errorHandler); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8744 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8745 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8746 | onError: |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8747 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8748 | Py_XDECREF(exc); |
| 8749 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8750 | return NULL; |
| 8751 | } |
| 8752 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8753 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8754 | PyObject * |
| 8755 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8756 | Py_ssize_t size, |
| 8757 | PyObject *mapping, |
| 8758 | const char *errors) |
| 8759 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8760 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8761 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8762 | if (!unicode) |
| 8763 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8764 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8765 | Py_DECREF(unicode); |
| 8766 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8767 | } |
| 8768 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8769 | PyObject * |
| 8770 | PyUnicode_Translate(PyObject *str, |
| 8771 | PyObject *mapping, |
| 8772 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8773 | { |
| 8774 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8776 | str = PyUnicode_FromObject(str); |
| 8777 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8778 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8779 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8780 | Py_DECREF(str); |
| 8781 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8783 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8785 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8786 | { |
| 8787 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8788 | called as a callback from fixup() which does it already. */ |
| 8789 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8790 | const int kind = PyUnicode_KIND(self); |
| 8791 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8792 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8793 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8794 | Py_ssize_t i; |
| 8795 | |
| 8796 | for (i = 0; i < len; ++i) { |
| 8797 | ch = PyUnicode_READ(kind, data, i); |
| 8798 | fixed = 0; |
| 8799 | if (ch > 127) { |
| 8800 | if (Py_UNICODE_ISSPACE(ch)) |
| 8801 | fixed = ' '; |
| 8802 | else { |
| 8803 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8804 | if (decimal >= 0) |
| 8805 | fixed = '0' + decimal; |
| 8806 | } |
| 8807 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8808 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8809 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8810 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8811 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8812 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8813 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8814 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8815 | } |
| 8816 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8817 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8818 | } |
| 8819 | |
| 8820 | PyObject * |
| 8821 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8822 | { |
| 8823 | if (!PyUnicode_Check(unicode)) { |
| 8824 | PyErr_BadInternalCall(); |
| 8825 | return NULL; |
| 8826 | } |
| 8827 | if (PyUnicode_READY(unicode) == -1) |
| 8828 | return NULL; |
| 8829 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8830 | /* If the string is already ASCII, just return the same string */ |
| 8831 | Py_INCREF(unicode); |
| 8832 | return unicode; |
| 8833 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8834 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8835 | } |
| 8836 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8837 | PyObject * |
| 8838 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8839 | Py_ssize_t length) |
| 8840 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8841 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8842 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8843 | Py_UCS4 maxchar; |
| 8844 | enum PyUnicode_Kind kind; |
| 8845 | void *data; |
| 8846 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8847 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8848 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 8849 | Py_UCS4 ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8850 | if (ch > 127) { |
| 8851 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8852 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8853 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8854 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8855 | } |
| 8856 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8857 | |
| 8858 | /* Copy to a new string */ |
| 8859 | decimal = PyUnicode_New(length, maxchar); |
| 8860 | if (decimal == NULL) |
| 8861 | return decimal; |
| 8862 | kind = PyUnicode_KIND(decimal); |
| 8863 | data = PyUnicode_DATA(decimal); |
| 8864 | /* Iterate over code points */ |
| 8865 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 8866 | Py_UCS4 ch = s[i]; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8867 | if (ch > 127) { |
| 8868 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8869 | if (decimal >= 0) |
| 8870 | ch = '0' + decimal; |
| 8871 | } |
| 8872 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8873 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8874 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8875 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8876 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8877 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8878 | int |
| 8879 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8880 | Py_ssize_t length, |
| 8881 | char *output, |
| 8882 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8883 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8884 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8885 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8886 | enum PyUnicode_Kind kind; |
| 8887 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8888 | |
| 8889 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8890 | PyErr_BadArgument(); |
| 8891 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8892 | } |
| 8893 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8894 | unicode = PyUnicode_FromUnicode(s, length); |
| 8895 | if (unicode == NULL) |
| 8896 | return -1; |
| 8897 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8898 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8899 | Py_DECREF(unicode); |
| 8900 | return -1; |
| 8901 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8902 | kind = PyUnicode_KIND(unicode); |
| 8903 | data = PyUnicode_DATA(unicode); |
| 8904 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8905 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8906 | PyObject *exc; |
| 8907 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8908 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8909 | Py_ssize_t startpos; |
| 8910 | |
| 8911 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8912 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8913 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8914 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8915 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8916 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8917 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8918 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8919 | if (decimal >= 0) { |
| 8920 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8921 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8922 | continue; |
| 8923 | } |
| 8924 | if (0 < ch && ch < 256) { |
| 8925 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8926 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8927 | continue; |
| 8928 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8929 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8930 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8931 | exc = NULL; |
| 8932 | raise_encode_exception(&exc, "decimal", unicode, |
| 8933 | startpos, startpos+1, |
| 8934 | "invalid decimal Unicode string"); |
| 8935 | Py_XDECREF(exc); |
| 8936 | Py_DECREF(unicode); |
| 8937 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8938 | } |
| 8939 | /* 0-terminate the output string */ |
| 8940 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8941 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8942 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8943 | } |
| 8944 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8945 | /* --- Helpers ------------------------------------------------------------ */ |
| 8946 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 8947 | /* helper macro to fixup start/end slice values */ |
| 8948 | #define ADJUST_INDICES(start, end, len) \ |
| 8949 | if (end > len) \ |
| 8950 | end = len; \ |
| 8951 | else if (end < 0) { \ |
| 8952 | end += len; \ |
| 8953 | if (end < 0) \ |
| 8954 | end = 0; \ |
| 8955 | } \ |
| 8956 | if (start < 0) { \ |
| 8957 | start += len; \ |
| 8958 | if (start < 0) \ |
| 8959 | start = 0; \ |
| 8960 | } |
| 8961 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8962 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8963 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8964 | Py_ssize_t start, |
| 8965 | Py_ssize_t end) |
| 8966 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 8967 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8968 | void *buf1, *buf2; |
| 8969 | Py_ssize_t len1, len2, result; |
| 8970 | |
| 8971 | kind1 = PyUnicode_KIND(s1); |
| 8972 | kind2 = PyUnicode_KIND(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 8973 | if (kind1 < kind2) |
| 8974 | return -1; |
| 8975 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8976 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8977 | len2 = PyUnicode_GET_LENGTH(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 8978 | ADJUST_INDICES(start, end, len1); |
| 8979 | if (end - start < len2) |
| 8980 | return -1; |
| 8981 | |
| 8982 | buf1 = PyUnicode_DATA(s1); |
| 8983 | buf2 = PyUnicode_DATA(s2); |
| 8984 | if (len2 == 1) { |
| 8985 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 8986 | result = findchar((const char *)buf1 + kind1*start, |
| 8987 | kind1, end - start, ch, direction); |
| 8988 | if (result == -1) |
| 8989 | return -1; |
| 8990 | else |
| 8991 | return start + result; |
| 8992 | } |
| 8993 | |
| 8994 | if (kind2 != kind1) { |
| 8995 | buf2 = _PyUnicode_AsKind(s2, kind1); |
| 8996 | if (!buf2) |
| 8997 | return -2; |
| 8998 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8999 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9000 | if (direction > 0) { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9001 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9002 | case PyUnicode_1BYTE_KIND: |
| 9003 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9004 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9005 | else |
| 9006 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9007 | break; |
| 9008 | case PyUnicode_2BYTE_KIND: |
| 9009 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9010 | break; |
| 9011 | case PyUnicode_4BYTE_KIND: |
| 9012 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9013 | break; |
| 9014 | default: |
| 9015 | assert(0); result = -2; |
| 9016 | } |
| 9017 | } |
| 9018 | else { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9019 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9020 | case PyUnicode_1BYTE_KIND: |
| 9021 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9022 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9023 | else |
| 9024 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9025 | break; |
| 9026 | case PyUnicode_2BYTE_KIND: |
| 9027 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9028 | break; |
| 9029 | case PyUnicode_4BYTE_KIND: |
| 9030 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9031 | break; |
| 9032 | default: |
| 9033 | assert(0); result = -2; |
| 9034 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9035 | } |
| 9036 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9037 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9038 | PyMem_Free(buf2); |
| 9039 | |
| 9040 | return result; |
| 9041 | } |
| 9042 | |
| 9043 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9044 | _PyUnicode_InsertThousandsGrouping( |
| 9045 | PyObject *unicode, Py_ssize_t index, |
| 9046 | Py_ssize_t n_buffer, |
| 9047 | void *digits, Py_ssize_t n_digits, |
| 9048 | Py_ssize_t min_width, |
| 9049 | const char *grouping, PyObject *thousands_sep, |
| 9050 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9051 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9052 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9053 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9054 | Py_ssize_t thousands_sep_len; |
| 9055 | Py_ssize_t len; |
| 9056 | |
| 9057 | if (unicode != NULL) { |
| 9058 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9059 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9060 | } |
| 9061 | else { |
| 9062 | kind = PyUnicode_1BYTE_KIND; |
| 9063 | data = NULL; |
| 9064 | } |
| 9065 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9066 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9067 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9068 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9069 | if (thousands_sep_kind < kind) { |
| 9070 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9071 | if (!thousands_sep_data) |
| 9072 | return -1; |
| 9073 | } |
| 9074 | else { |
| 9075 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9076 | if (!data) |
| 9077 | return -1; |
| 9078 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9079 | } |
| 9080 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9081 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9082 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9083 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9084 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9085 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9086 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9087 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9088 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9089 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9090 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9091 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9092 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9093 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9094 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9095 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9096 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9097 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9098 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9099 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9100 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9101 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9102 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9103 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9104 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9105 | break; |
| 9106 | default: |
| 9107 | assert(0); |
| 9108 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9109 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9110 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9111 | if (thousands_sep_kind < kind) |
| 9112 | PyMem_Free(thousands_sep_data); |
| 9113 | else |
| 9114 | PyMem_Free(data); |
| 9115 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9116 | if (unicode == NULL) { |
| 9117 | *maxchar = 127; |
| 9118 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9119 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9120 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9121 | } |
| 9122 | } |
| 9123 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9124 | } |
| 9125 | |
| 9126 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9127 | Py_ssize_t |
| 9128 | PyUnicode_Count(PyObject *str, |
| 9129 | PyObject *substr, |
| 9130 | Py_ssize_t start, |
| 9131 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9132 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9133 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9134 | PyObject* str_obj; |
| 9135 | PyObject* sub_obj; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9136 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9137 | void *buf1 = NULL, *buf2 = NULL; |
| 9138 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9139 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9140 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9141 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9142 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9143 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9144 | if (!sub_obj) { |
| 9145 | Py_DECREF(str_obj); |
| 9146 | return -1; |
| 9147 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9148 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9149 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9150 | Py_DECREF(str_obj); |
| 9151 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9152 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9153 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9154 | kind1 = PyUnicode_KIND(str_obj); |
| 9155 | kind2 = PyUnicode_KIND(sub_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9156 | if (kind1 < kind2) { |
| 9157 | Py_DECREF(sub_obj); |
| 9158 | Py_DECREF(str_obj); |
| 9159 | return 0; |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9160 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9161 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9162 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9163 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9164 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9165 | if (end - start < len2) { |
| 9166 | Py_DECREF(sub_obj); |
| 9167 | Py_DECREF(str_obj); |
| 9168 | return 0; |
| 9169 | } |
| 9170 | |
| 9171 | buf1 = PyUnicode_DATA(str_obj); |
| 9172 | buf2 = PyUnicode_DATA(sub_obj); |
| 9173 | if (kind2 != kind1) { |
| 9174 | buf2 = _PyUnicode_AsKind(sub_obj, kind1); |
| 9175 | if (!buf2) |
| 9176 | goto onError; |
| 9177 | } |
| 9178 | |
| 9179 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9180 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9181 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9182 | result = asciilib_count( |
| 9183 | ((Py_UCS1*)buf1) + start, end - start, |
| 9184 | buf2, len2, PY_SSIZE_T_MAX |
| 9185 | ); |
| 9186 | else |
| 9187 | result = ucs1lib_count( |
| 9188 | ((Py_UCS1*)buf1) + start, end - start, |
| 9189 | buf2, len2, PY_SSIZE_T_MAX |
| 9190 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9191 | break; |
| 9192 | case PyUnicode_2BYTE_KIND: |
| 9193 | result = ucs2lib_count( |
| 9194 | ((Py_UCS2*)buf1) + start, end - start, |
| 9195 | buf2, len2, PY_SSIZE_T_MAX |
| 9196 | ); |
| 9197 | break; |
| 9198 | case PyUnicode_4BYTE_KIND: |
| 9199 | result = ucs4lib_count( |
| 9200 | ((Py_UCS4*)buf1) + start, end - start, |
| 9201 | buf2, len2, PY_SSIZE_T_MAX |
| 9202 | ); |
| 9203 | break; |
| 9204 | default: |
| 9205 | assert(0); result = 0; |
| 9206 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9207 | |
| 9208 | Py_DECREF(sub_obj); |
| 9209 | Py_DECREF(str_obj); |
| 9210 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9211 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9212 | PyMem_Free(buf2); |
| 9213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9214 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9215 | onError: |
| 9216 | Py_DECREF(sub_obj); |
| 9217 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9218 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9219 | PyMem_Free(buf2); |
| 9220 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9221 | } |
| 9222 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9223 | Py_ssize_t |
| 9224 | PyUnicode_Find(PyObject *str, |
| 9225 | PyObject *sub, |
| 9226 | Py_ssize_t start, |
| 9227 | Py_ssize_t end, |
| 9228 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9229 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9230 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9232 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9233 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9234 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9235 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9236 | if (!sub) { |
| 9237 | Py_DECREF(str); |
| 9238 | return -2; |
| 9239 | } |
| 9240 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9241 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9242 | Py_DECREF(str); |
| 9243 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9244 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9245 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9246 | result = any_find_slice(direction, |
| 9247 | str, sub, start, end |
| 9248 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9249 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9250 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9251 | Py_DECREF(sub); |
| 9252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9253 | return result; |
| 9254 | } |
| 9255 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9256 | Py_ssize_t |
| 9257 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9258 | Py_ssize_t start, Py_ssize_t end, |
| 9259 | int direction) |
| 9260 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9261 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9262 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9263 | if (PyUnicode_READY(str) == -1) |
| 9264 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9265 | if (start < 0 || end < 0) { |
| 9266 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9267 | return -2; |
| 9268 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9269 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9270 | end = PyUnicode_GET_LENGTH(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9271 | if (start >= end) |
| 9272 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9273 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9274 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9275 | kind, end-start, ch, direction); |
| 9276 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9278 | else |
| 9279 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9280 | } |
| 9281 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9282 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9283 | tailmatch(PyObject *self, |
| 9284 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9285 | Py_ssize_t start, |
| 9286 | Py_ssize_t end, |
| 9287 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9288 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9289 | int kind_self; |
| 9290 | int kind_sub; |
| 9291 | void *data_self; |
| 9292 | void *data_sub; |
| 9293 | Py_ssize_t offset; |
| 9294 | Py_ssize_t i; |
| 9295 | Py_ssize_t end_sub; |
| 9296 | |
| 9297 | if (PyUnicode_READY(self) == -1 || |
| 9298 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9299 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9300 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9301 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9302 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9303 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9304 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9305 | |
Serhiy Storchaka | d4ea03c | 2015-05-31 09:15:51 +0300 | [diff] [blame] | 9306 | if (PyUnicode_GET_LENGTH(substring) == 0) |
| 9307 | return 1; |
| 9308 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9309 | kind_self = PyUnicode_KIND(self); |
| 9310 | data_self = PyUnicode_DATA(self); |
| 9311 | kind_sub = PyUnicode_KIND(substring); |
| 9312 | data_sub = PyUnicode_DATA(substring); |
| 9313 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9314 | |
| 9315 | if (direction > 0) |
| 9316 | offset = end; |
| 9317 | else |
| 9318 | offset = start; |
| 9319 | |
| 9320 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9321 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9322 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9323 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9324 | /* If both are of the same kind, memcmp is sufficient */ |
| 9325 | if (kind_self == kind_sub) { |
| 9326 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9327 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | data_sub, |
| 9329 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9330 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9331 | } |
Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 9332 | /* otherwise we have to compare each character by first accessing it */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9333 | else { |
| 9334 | /* We do not need to compare 0 and len(substring)-1 because |
| 9335 | the if statement above ensured already that they are equal |
| 9336 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9337 | for (i = 1; i < end_sub; ++i) { |
| 9338 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9339 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9340 | return 0; |
| 9341 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9342 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9343 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9344 | } |
| 9345 | |
| 9346 | return 0; |
| 9347 | } |
| 9348 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9349 | Py_ssize_t |
| 9350 | PyUnicode_Tailmatch(PyObject *str, |
| 9351 | PyObject *substr, |
| 9352 | Py_ssize_t start, |
| 9353 | Py_ssize_t end, |
| 9354 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9355 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9356 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9358 | str = PyUnicode_FromObject(str); |
| 9359 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9360 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9361 | substr = PyUnicode_FromObject(substr); |
| 9362 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9363 | Py_DECREF(str); |
| 9364 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9365 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9366 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9367 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9368 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9369 | Py_DECREF(str); |
| 9370 | Py_DECREF(substr); |
| 9371 | return result; |
| 9372 | } |
| 9373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9374 | /* Apply fixfct filter to the Unicode object self and return a |
| 9375 | reference to the modified object */ |
| 9376 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9377 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9378 | fixup(PyObject *self, |
| 9379 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9380 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9381 | PyObject *u; |
| 9382 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9383 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9384 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9385 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9386 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9387 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9388 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9389 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9390 | /* fix functions return the new maximum character in a string, |
| 9391 | if the kind of the resulting unicode object does not change, |
| 9392 | everything is fine. Otherwise we need to change the string kind |
| 9393 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9394 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9395 | |
| 9396 | if (maxchar_new == 0) { |
| 9397 | /* no changes */; |
| 9398 | if (PyUnicode_CheckExact(self)) { |
| 9399 | Py_DECREF(u); |
| 9400 | Py_INCREF(self); |
| 9401 | return self; |
| 9402 | } |
| 9403 | else |
| 9404 | return u; |
| 9405 | } |
| 9406 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9407 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9408 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9409 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9410 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9411 | |
| 9412 | /* In case the maximum character changed, we need to |
| 9413 | convert the string to the new category. */ |
| 9414 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9415 | if (v == NULL) { |
| 9416 | Py_DECREF(u); |
| 9417 | return NULL; |
| 9418 | } |
| 9419 | if (maxchar_new > maxchar_old) { |
| 9420 | /* If the maxchar increased so that the kind changed, not all |
| 9421 | characters are representable anymore and we need to fix the |
| 9422 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9423 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9424 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9425 | maxchar_old = fixfct(v); |
| 9426 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9427 | } |
| 9428 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9429 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9430 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9431 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9432 | Py_DECREF(u); |
| 9433 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9434 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9435 | } |
| 9436 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9437 | static PyObject * |
| 9438 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9439 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9440 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9441 | char *resdata, *data = PyUnicode_DATA(self); |
| 9442 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9443 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9444 | res = PyUnicode_New(len, 127); |
| 9445 | if (res == NULL) |
| 9446 | return NULL; |
| 9447 | resdata = PyUnicode_DATA(res); |
| 9448 | if (lower) |
| 9449 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9450 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9451 | _Py_bytes_upper(resdata, data, len); |
| 9452 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9453 | } |
| 9454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9455 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9456 | 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] | 9457 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9458 | Py_ssize_t j; |
| 9459 | int final_sigma; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 9460 | Py_UCS4 c = 0; /* initialize to prevent gcc warning */ |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9461 | /* 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] | 9462 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9463 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9464 | |
| 9465 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9466 | */ |
| 9467 | for (j = i - 1; j >= 0; j--) { |
| 9468 | c = PyUnicode_READ(kind, data, j); |
| 9469 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9470 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9471 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9472 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9473 | if (final_sigma) { |
| 9474 | for (j = i + 1; j < length; j++) { |
| 9475 | c = PyUnicode_READ(kind, data, j); |
| 9476 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9477 | break; |
| 9478 | } |
| 9479 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9480 | } |
| 9481 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9482 | } |
| 9483 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9484 | static int |
| 9485 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9486 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9487 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9488 | /* Obscure special case. */ |
| 9489 | if (c == 0x3A3) { |
| 9490 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9491 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9492 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9493 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9494 | } |
| 9495 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9496 | static Py_ssize_t |
| 9497 | 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] | 9498 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9499 | Py_ssize_t i, k = 0; |
| 9500 | int n_res, j; |
| 9501 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9502 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9503 | c = PyUnicode_READ(kind, data, 0); |
| 9504 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9505 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9506 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9507 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9508 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9509 | for (i = 1; i < length; i++) { |
| 9510 | c = PyUnicode_READ(kind, data, i); |
| 9511 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9512 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9513 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9514 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9515 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9516 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9517 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9518 | } |
| 9519 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9520 | static Py_ssize_t |
| 9521 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9522 | Py_ssize_t i, k = 0; |
| 9523 | |
| 9524 | for (i = 0; i < length; i++) { |
| 9525 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9526 | int n_res, j; |
| 9527 | if (Py_UNICODE_ISUPPER(c)) { |
| 9528 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9529 | } |
| 9530 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9531 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9532 | } |
| 9533 | else { |
| 9534 | n_res = 1; |
| 9535 | mapped[0] = c; |
| 9536 | } |
| 9537 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9538 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9539 | res[k++] = mapped[j]; |
| 9540 | } |
| 9541 | } |
| 9542 | return k; |
| 9543 | } |
| 9544 | |
| 9545 | static Py_ssize_t |
| 9546 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9547 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9548 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9549 | Py_ssize_t i, k = 0; |
| 9550 | |
| 9551 | for (i = 0; i < length; i++) { |
| 9552 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9553 | int n_res, j; |
| 9554 | if (lower) |
| 9555 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9556 | else |
| 9557 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9558 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9559 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9560 | res[k++] = mapped[j]; |
| 9561 | } |
| 9562 | } |
| 9563 | return k; |
| 9564 | } |
| 9565 | |
| 9566 | static Py_ssize_t |
| 9567 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9568 | { |
| 9569 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9570 | } |
| 9571 | |
| 9572 | static Py_ssize_t |
| 9573 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9574 | { |
| 9575 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9576 | } |
| 9577 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9578 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9579 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9580 | { |
| 9581 | Py_ssize_t i, k = 0; |
| 9582 | |
| 9583 | for (i = 0; i < length; i++) { |
| 9584 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9585 | Py_UCS4 mapped[3]; |
| 9586 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9587 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9588 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9589 | res[k++] = mapped[j]; |
| 9590 | } |
| 9591 | } |
| 9592 | return k; |
| 9593 | } |
| 9594 | |
| 9595 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9596 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9597 | { |
| 9598 | Py_ssize_t i, k = 0; |
| 9599 | int previous_is_cased; |
| 9600 | |
| 9601 | previous_is_cased = 0; |
| 9602 | for (i = 0; i < length; i++) { |
| 9603 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9604 | Py_UCS4 mapped[3]; |
| 9605 | int n_res, j; |
| 9606 | |
| 9607 | if (previous_is_cased) |
| 9608 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9609 | else |
| 9610 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9611 | |
| 9612 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9613 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9614 | res[k++] = mapped[j]; |
| 9615 | } |
| 9616 | |
| 9617 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9618 | } |
| 9619 | return k; |
| 9620 | } |
| 9621 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9622 | static PyObject * |
| 9623 | case_operation(PyObject *self, |
| 9624 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9625 | { |
| 9626 | PyObject *res = NULL; |
| 9627 | Py_ssize_t length, newlength = 0; |
| 9628 | int kind, outkind; |
| 9629 | void *data, *outdata; |
| 9630 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9631 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9632 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9633 | |
| 9634 | kind = PyUnicode_KIND(self); |
| 9635 | data = PyUnicode_DATA(self); |
| 9636 | length = PyUnicode_GET_LENGTH(self); |
Antoine Pitrou | 4e33424 | 2014-10-15 23:14:53 +0200 | [diff] [blame] | 9637 | if ((size_t) length > PY_SSIZE_T_MAX / (3 * sizeof(Py_UCS4))) { |
Benjamin Peterson | e1bd38c | 2014-10-15 11:47:36 -0400 | [diff] [blame] | 9638 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 9639 | return NULL; |
| 9640 | } |
Benjamin Peterson | 1e211ff | 2014-10-15 12:17:21 -0400 | [diff] [blame] | 9641 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9642 | if (tmp == NULL) |
| 9643 | return PyErr_NoMemory(); |
| 9644 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9645 | res = PyUnicode_New(newlength, maxchar); |
| 9646 | if (res == NULL) |
| 9647 | goto leave; |
| 9648 | tmpend = tmp + newlength; |
| 9649 | outdata = PyUnicode_DATA(res); |
| 9650 | outkind = PyUnicode_KIND(res); |
| 9651 | switch (outkind) { |
| 9652 | case PyUnicode_1BYTE_KIND: |
| 9653 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9654 | break; |
| 9655 | case PyUnicode_2BYTE_KIND: |
| 9656 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9657 | break; |
| 9658 | case PyUnicode_4BYTE_KIND: |
| 9659 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9660 | break; |
| 9661 | default: |
| 9662 | assert(0); |
| 9663 | break; |
| 9664 | } |
| 9665 | leave: |
| 9666 | PyMem_FREE(tmp); |
| 9667 | return res; |
| 9668 | } |
| 9669 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9670 | PyObject * |
| 9671 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9672 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9673 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9674 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9675 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9676 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9677 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9678 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9679 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9680 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9681 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9682 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9683 | int use_memcpy; |
| 9684 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9685 | PyObject *last_obj; |
| 9686 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9687 | |
Benjamin Peterson | 9743b2c | 2014-02-15 13:02:52 -0500 | [diff] [blame] | 9688 | fseq = PySequence_Fast(seq, "can only join an iterable"); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9689 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9690 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9691 | } |
| 9692 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9693 | /* NOTE: the following code can't call back into Python code, |
| 9694 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9695 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9696 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9697 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9698 | /* If empty sequence, return u"". */ |
| 9699 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9700 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9701 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9702 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9703 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9704 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9705 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9706 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9707 | if (seqlen == 1) { |
| 9708 | if (PyUnicode_CheckExact(items[0])) { |
| 9709 | res = items[0]; |
| 9710 | Py_INCREF(res); |
| 9711 | Py_DECREF(fseq); |
| 9712 | return res; |
| 9713 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9714 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9715 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9716 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9717 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9718 | /* Set up sep and seplen */ |
| 9719 | if (separator == NULL) { |
| 9720 | /* fall back to a blank space separator */ |
| 9721 | sep = PyUnicode_FromOrdinal(' '); |
| 9722 | if (!sep) |
| 9723 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9724 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9725 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9726 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9727 | else { |
| 9728 | if (!PyUnicode_Check(separator)) { |
| 9729 | PyErr_Format(PyExc_TypeError, |
| 9730 | "separator: expected str instance," |
| 9731 | " %.80s found", |
| 9732 | Py_TYPE(separator)->tp_name); |
| 9733 | goto onError; |
| 9734 | } |
| 9735 | if (PyUnicode_READY(separator)) |
| 9736 | goto onError; |
| 9737 | sep = separator; |
| 9738 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9739 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9740 | /* inc refcount to keep this code path symmetric with the |
| 9741 | above case of a blank separator */ |
| 9742 | Py_INCREF(sep); |
| 9743 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9744 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9745 | } |
| 9746 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9747 | /* There are at least two things to join, or else we have a subclass |
| 9748 | * of str in the sequence. |
| 9749 | * Do a pre-pass to figure out the total amount of space we'll |
| 9750 | * need (sz), and see whether all argument are strings. |
| 9751 | */ |
| 9752 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9753 | #ifdef Py_DEBUG |
| 9754 | use_memcpy = 0; |
| 9755 | #else |
| 9756 | use_memcpy = 1; |
| 9757 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9758 | for (i = 0; i < seqlen; i++) { |
| 9759 | const Py_ssize_t old_sz = sz; |
| 9760 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9761 | if (!PyUnicode_Check(item)) { |
| 9762 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 9763 | "sequence item %zd: expected str instance," |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9764 | " %.80s found", |
| 9765 | i, Py_TYPE(item)->tp_name); |
| 9766 | goto onError; |
| 9767 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9768 | if (PyUnicode_READY(item) == -1) |
| 9769 | goto onError; |
| 9770 | sz += PyUnicode_GET_LENGTH(item); |
| 9771 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9772 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9773 | if (i != 0) |
| 9774 | sz += seplen; |
| 9775 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9776 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9777 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9778 | goto onError; |
| 9779 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9780 | if (use_memcpy && last_obj != NULL) { |
| 9781 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9782 | use_memcpy = 0; |
| 9783 | } |
| 9784 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9785 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9786 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9787 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9788 | if (res == NULL) |
| 9789 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9790 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9791 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9792 | #ifdef Py_DEBUG |
| 9793 | use_memcpy = 0; |
| 9794 | #else |
| 9795 | if (use_memcpy) { |
| 9796 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9797 | kind = PyUnicode_KIND(res); |
| 9798 | if (seplen != 0) |
| 9799 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9800 | } |
| 9801 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9802 | if (use_memcpy) { |
| 9803 | for (i = 0; i < seqlen; ++i) { |
| 9804 | Py_ssize_t itemlen; |
| 9805 | item = items[i]; |
| 9806 | |
| 9807 | /* Copy item, and maybe the separator. */ |
| 9808 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9809 | Py_MEMCPY(res_data, |
| 9810 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9811 | kind * seplen); |
| 9812 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9813 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9814 | |
| 9815 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9816 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9817 | Py_MEMCPY(res_data, |
| 9818 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9819 | kind * itemlen); |
| 9820 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9821 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9822 | } |
| 9823 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9824 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9825 | } |
| 9826 | else { |
| 9827 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9828 | Py_ssize_t itemlen; |
| 9829 | item = items[i]; |
| 9830 | |
| 9831 | /* Copy item, and maybe the separator. */ |
| 9832 | if (i && seplen != 0) { |
| 9833 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9834 | res_offset += seplen; |
| 9835 | } |
| 9836 | |
| 9837 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9838 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9839 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9840 | res_offset += itemlen; |
| 9841 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9842 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9843 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9844 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9845 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9846 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9847 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9848 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9849 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9850 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9851 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9852 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9853 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9854 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9855 | return NULL; |
| 9856 | } |
| 9857 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9858 | #define FILL(kind, data, value, start, length) \ |
| 9859 | do { \ |
| 9860 | Py_ssize_t i_ = 0; \ |
| 9861 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9862 | switch ((kind)) { \ |
| 9863 | case PyUnicode_1BYTE_KIND: { \ |
| 9864 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9865 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9866 | break; \ |
| 9867 | } \ |
| 9868 | case PyUnicode_2BYTE_KIND: { \ |
| 9869 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9870 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9871 | break; \ |
| 9872 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9873 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9874 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9875 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9876 | break; \ |
| 9877 | } \ |
Serhiy Storchaka | 133b11b | 2014-12-01 18:56:28 +0200 | [diff] [blame] | 9878 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9879 | } \ |
| 9880 | } while (0) |
| 9881 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9882 | void |
| 9883 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9884 | Py_UCS4 fill_char) |
| 9885 | { |
| 9886 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9887 | const void *data = PyUnicode_DATA(unicode); |
| 9888 | assert(PyUnicode_IS_READY(unicode)); |
| 9889 | assert(unicode_modifiable(unicode)); |
| 9890 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9891 | assert(start >= 0); |
| 9892 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9893 | FILL(kind, data, fill_char, start, length); |
| 9894 | } |
| 9895 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9896 | Py_ssize_t |
| 9897 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9898 | Py_UCS4 fill_char) |
| 9899 | { |
| 9900 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9901 | |
| 9902 | if (!PyUnicode_Check(unicode)) { |
| 9903 | PyErr_BadInternalCall(); |
| 9904 | return -1; |
| 9905 | } |
| 9906 | if (PyUnicode_READY(unicode) == -1) |
| 9907 | return -1; |
| 9908 | if (unicode_check_modifiable(unicode)) |
| 9909 | return -1; |
| 9910 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9911 | if (start < 0) { |
| 9912 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9913 | return -1; |
| 9914 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9915 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9916 | PyErr_SetString(PyExc_ValueError, |
| 9917 | "fill character is bigger than " |
| 9918 | "the string maximum character"); |
| 9919 | return -1; |
| 9920 | } |
| 9921 | |
| 9922 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9923 | length = Py_MIN(maxlen, length); |
| 9924 | if (length <= 0) |
| 9925 | return 0; |
| 9926 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9927 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9928 | return length; |
| 9929 | } |
| 9930 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9931 | static PyObject * |
| 9932 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9933 | Py_ssize_t left, |
| 9934 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9935 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9936 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9937 | PyObject *u; |
| 9938 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9939 | int kind; |
| 9940 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9941 | |
| 9942 | if (left < 0) |
| 9943 | left = 0; |
| 9944 | if (right < 0) |
| 9945 | right = 0; |
| 9946 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9947 | if (left == 0 && right == 0) |
| 9948 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9950 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9951 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9952 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9953 | return NULL; |
| 9954 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9955 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9956 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9957 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9958 | if (!u) |
| 9959 | return NULL; |
| 9960 | |
| 9961 | kind = PyUnicode_KIND(u); |
| 9962 | data = PyUnicode_DATA(u); |
| 9963 | if (left) |
| 9964 | FILL(kind, data, fill, 0, left); |
| 9965 | if (right) |
| 9966 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9967 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9968 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9969 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9970 | } |
| 9971 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9972 | PyObject * |
| 9973 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9974 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9975 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9976 | |
| 9977 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9978 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9979 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9980 | if (PyUnicode_READY(string) == -1) { |
| 9981 | Py_DECREF(string); |
| 9982 | return NULL; |
| 9983 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9984 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9985 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9986 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9987 | if (PyUnicode_IS_ASCII(string)) |
| 9988 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9989 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9990 | PyUnicode_GET_LENGTH(string), keepends); |
| 9991 | else |
| 9992 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9993 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9994 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9995 | break; |
| 9996 | case PyUnicode_2BYTE_KIND: |
| 9997 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9998 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9999 | PyUnicode_GET_LENGTH(string), keepends); |
| 10000 | break; |
| 10001 | case PyUnicode_4BYTE_KIND: |
| 10002 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10003 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10004 | PyUnicode_GET_LENGTH(string), keepends); |
| 10005 | break; |
| 10006 | default: |
| 10007 | assert(0); |
| 10008 | list = 0; |
| 10009 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10010 | Py_DECREF(string); |
| 10011 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10012 | } |
| 10013 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10014 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10015 | split(PyObject *self, |
| 10016 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10017 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10018 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10019 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10020 | void *buf1, *buf2; |
| 10021 | Py_ssize_t len1, len2; |
| 10022 | PyObject* out; |
| 10023 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10024 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10025 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10027 | if (PyUnicode_READY(self) == -1) |
| 10028 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10029 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10030 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10031 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10032 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10033 | if (PyUnicode_IS_ASCII(self)) |
| 10034 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10035 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10036 | PyUnicode_GET_LENGTH(self), maxcount |
| 10037 | ); |
| 10038 | else |
| 10039 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10040 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10041 | PyUnicode_GET_LENGTH(self), maxcount |
| 10042 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10043 | case PyUnicode_2BYTE_KIND: |
| 10044 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10045 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10046 | PyUnicode_GET_LENGTH(self), maxcount |
| 10047 | ); |
| 10048 | case PyUnicode_4BYTE_KIND: |
| 10049 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10050 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | PyUnicode_GET_LENGTH(self), maxcount |
| 10052 | ); |
| 10053 | default: |
| 10054 | assert(0); |
| 10055 | return NULL; |
| 10056 | } |
| 10057 | |
| 10058 | if (PyUnicode_READY(substring) == -1) |
| 10059 | return NULL; |
| 10060 | |
| 10061 | kind1 = PyUnicode_KIND(self); |
| 10062 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | len1 = PyUnicode_GET_LENGTH(self); |
| 10064 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10065 | if (kind1 < kind2 || len1 < len2) { |
| 10066 | out = PyList_New(1); |
| 10067 | if (out == NULL) |
| 10068 | return NULL; |
| 10069 | Py_INCREF(self); |
| 10070 | PyList_SET_ITEM(out, 0, self); |
| 10071 | return out; |
| 10072 | } |
| 10073 | buf1 = PyUnicode_DATA(self); |
| 10074 | buf2 = PyUnicode_DATA(substring); |
| 10075 | if (kind2 != kind1) { |
| 10076 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10077 | if (!buf2) |
| 10078 | return NULL; |
| 10079 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10080 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10081 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10082 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10083 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10084 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10085 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10086 | else |
| 10087 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10088 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10089 | break; |
| 10090 | case PyUnicode_2BYTE_KIND: |
| 10091 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10092 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | break; |
| 10094 | case PyUnicode_4BYTE_KIND: |
| 10095 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10096 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10097 | break; |
| 10098 | default: |
| 10099 | out = NULL; |
| 10100 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10101 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 | PyMem_Free(buf2); |
| 10103 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10104 | } |
| 10105 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10106 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10107 | rsplit(PyObject *self, |
| 10108 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10109 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10110 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10111 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10112 | void *buf1, *buf2; |
| 10113 | Py_ssize_t len1, len2; |
| 10114 | PyObject* out; |
| 10115 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10116 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10117 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10118 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10119 | if (PyUnicode_READY(self) == -1) |
| 10120 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10121 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10122 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10123 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10124 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10125 | if (PyUnicode_IS_ASCII(self)) |
| 10126 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10127 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10128 | PyUnicode_GET_LENGTH(self), maxcount |
| 10129 | ); |
| 10130 | else |
| 10131 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10132 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10133 | PyUnicode_GET_LENGTH(self), maxcount |
| 10134 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10135 | case PyUnicode_2BYTE_KIND: |
| 10136 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10137 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 | PyUnicode_GET_LENGTH(self), maxcount |
| 10139 | ); |
| 10140 | case PyUnicode_4BYTE_KIND: |
| 10141 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10142 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | PyUnicode_GET_LENGTH(self), maxcount |
| 10144 | ); |
| 10145 | default: |
| 10146 | assert(0); |
| 10147 | return NULL; |
| 10148 | } |
| 10149 | |
| 10150 | if (PyUnicode_READY(substring) == -1) |
| 10151 | return NULL; |
| 10152 | |
| 10153 | kind1 = PyUnicode_KIND(self); |
| 10154 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10155 | len1 = PyUnicode_GET_LENGTH(self); |
| 10156 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10157 | if (kind1 < kind2 || len1 < len2) { |
| 10158 | out = PyList_New(1); |
| 10159 | if (out == NULL) |
| 10160 | return NULL; |
| 10161 | Py_INCREF(self); |
| 10162 | PyList_SET_ITEM(out, 0, self); |
| 10163 | return out; |
| 10164 | } |
| 10165 | buf1 = PyUnicode_DATA(self); |
| 10166 | buf2 = PyUnicode_DATA(substring); |
| 10167 | if (kind2 != kind1) { |
| 10168 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10169 | if (!buf2) |
| 10170 | return NULL; |
| 10171 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10172 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10173 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10174 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10175 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10176 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10177 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10178 | else |
| 10179 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10180 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10181 | break; |
| 10182 | case PyUnicode_2BYTE_KIND: |
| 10183 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10184 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10185 | break; |
| 10186 | case PyUnicode_4BYTE_KIND: |
| 10187 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10188 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10189 | break; |
| 10190 | default: |
| 10191 | out = NULL; |
| 10192 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10193 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10194 | PyMem_Free(buf2); |
| 10195 | return out; |
| 10196 | } |
| 10197 | |
| 10198 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10199 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10200 | 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] | 10201 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10202 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10203 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10204 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10205 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10206 | else |
| 10207 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | case PyUnicode_2BYTE_KIND: |
| 10209 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10210 | case PyUnicode_4BYTE_KIND: |
| 10211 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10212 | } |
| 10213 | assert(0); |
| 10214 | return -1; |
| 10215 | } |
| 10216 | |
| 10217 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10218 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10219 | 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] | 10220 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10221 | switch (kind) { |
| 10222 | case PyUnicode_1BYTE_KIND: |
| 10223 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10224 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10225 | else |
| 10226 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10227 | case PyUnicode_2BYTE_KIND: |
| 10228 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10229 | case PyUnicode_4BYTE_KIND: |
| 10230 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10231 | } |
| 10232 | assert(0); |
| 10233 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10234 | } |
| 10235 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10236 | static void |
| 10237 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10238 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10239 | { |
| 10240 | int kind = PyUnicode_KIND(u); |
| 10241 | void *data = PyUnicode_DATA(u); |
| 10242 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10243 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10244 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10245 | (Py_UCS1 *)data + len, |
| 10246 | u1, u2, maxcount); |
| 10247 | } |
| 10248 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10249 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10250 | (Py_UCS2 *)data + len, |
| 10251 | u1, u2, maxcount); |
| 10252 | } |
| 10253 | else { |
| 10254 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10255 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10256 | (Py_UCS4 *)data + len, |
| 10257 | u1, u2, maxcount); |
| 10258 | } |
| 10259 | } |
| 10260 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10261 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10262 | replace(PyObject *self, PyObject *str1, |
| 10263 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10264 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10265 | PyObject *u; |
| 10266 | char *sbuf = PyUnicode_DATA(self); |
| 10267 | char *buf1 = PyUnicode_DATA(str1); |
| 10268 | char *buf2 = PyUnicode_DATA(str2); |
| 10269 | int srelease = 0, release1 = 0, release2 = 0; |
| 10270 | int skind = PyUnicode_KIND(self); |
| 10271 | int kind1 = PyUnicode_KIND(str1); |
| 10272 | int kind2 = PyUnicode_KIND(str2); |
| 10273 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10274 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10275 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10276 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10277 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10278 | |
| 10279 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10280 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10281 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10282 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10283 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10284 | if (str1 == str2) |
| 10285 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10286 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10287 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10288 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10289 | if (maxchar < maxchar_str1) |
| 10290 | /* substring too wide to be present */ |
| 10291 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10292 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10293 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10294 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10295 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10296 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10297 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10298 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10299 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10300 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10301 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10302 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10303 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10304 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10305 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10306 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10307 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10308 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10309 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10310 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10311 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10312 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10313 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10314 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10315 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10316 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10317 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10318 | } |
| 10319 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10320 | int rkind = skind; |
| 10321 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10322 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10323 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10324 | if (kind1 < rkind) { |
| 10325 | /* widen substring */ |
| 10326 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10327 | if (!buf1) goto error; |
| 10328 | release1 = 1; |
| 10329 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10330 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10331 | if (i < 0) |
| 10332 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10333 | if (rkind > kind2) { |
| 10334 | /* widen replacement */ |
| 10335 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10336 | if (!buf2) goto error; |
| 10337 | release2 = 1; |
| 10338 | } |
| 10339 | else if (rkind < kind2) { |
| 10340 | /* widen self and buf1 */ |
| 10341 | rkind = kind2; |
| 10342 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10343 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10344 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10345 | if (!sbuf) goto error; |
| 10346 | srelease = 1; |
| 10347 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10348 | if (!buf1) goto error; |
| 10349 | release1 = 1; |
| 10350 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10351 | u = PyUnicode_New(slen, maxchar); |
| 10352 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10353 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10354 | assert(PyUnicode_KIND(u) == rkind); |
| 10355 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10356 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10357 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10358 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10359 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10360 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10361 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10362 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10363 | |
| 10364 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10365 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10366 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10367 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10368 | if (i == -1) |
| 10369 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10370 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10371 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10372 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10373 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10374 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10375 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10376 | } |
| 10377 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10378 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10379 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10380 | int rkind = skind; |
| 10381 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10382 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10384 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10385 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10386 | if (!buf1) goto error; |
| 10387 | release1 = 1; |
| 10388 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10389 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10390 | if (n == 0) |
| 10391 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10392 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10393 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10394 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10395 | if (!buf2) goto error; |
| 10396 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10397 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10398 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10399 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10400 | rkind = kind2; |
| 10401 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10402 | if (!sbuf) goto error; |
| 10403 | srelease = 1; |
| 10404 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10405 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10406 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10407 | if (!buf1) goto error; |
| 10408 | release1 = 1; |
| 10409 | } |
| 10410 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10411 | PyUnicode_GET_LENGTH(str1))); */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10412 | 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] | 10413 | PyErr_SetString(PyExc_OverflowError, |
| 10414 | "replace string is too long"); |
| 10415 | goto error; |
| 10416 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10417 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10418 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10419 | _Py_INCREF_UNICODE_EMPTY(); |
| 10420 | if (!unicode_empty) |
| 10421 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10422 | u = unicode_empty; |
| 10423 | goto done; |
| 10424 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10425 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10426 | PyErr_SetString(PyExc_OverflowError, |
| 10427 | "replace string is too long"); |
| 10428 | goto error; |
| 10429 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10430 | u = PyUnicode_New(new_size, maxchar); |
| 10431 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10432 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10433 | assert(PyUnicode_KIND(u) == rkind); |
| 10434 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10435 | ires = i = 0; |
| 10436 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10437 | while (n-- > 0) { |
| 10438 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10439 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10440 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10441 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10442 | if (j == -1) |
| 10443 | break; |
| 10444 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10445 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10446 | memcpy(res + rkind * ires, |
| 10447 | sbuf + rkind * i, |
| 10448 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10449 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10450 | } |
| 10451 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10452 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10453 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10454 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10455 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10456 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10457 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10458 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10459 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10460 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10461 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10462 | memcpy(res + rkind * ires, |
| 10463 | sbuf + rkind * i, |
| 10464 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10465 | } |
| 10466 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10467 | /* interleave */ |
| 10468 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10469 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10470 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10471 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10472 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10473 | if (--n <= 0) |
| 10474 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10475 | memcpy(res + rkind * ires, |
| 10476 | sbuf + rkind * i, |
| 10477 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10478 | ires++; |
| 10479 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10480 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10481 | memcpy(res + rkind * ires, |
| 10482 | sbuf + rkind * i, |
| 10483 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10484 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10485 | } |
| 10486 | |
| 10487 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10488 | unicode_adjust_maxchar(&u); |
| 10489 | if (u == NULL) |
| 10490 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10491 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10492 | |
| 10493 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | if (srelease) |
| 10495 | PyMem_FREE(sbuf); |
| 10496 | if (release1) |
| 10497 | PyMem_FREE(buf1); |
| 10498 | if (release2) |
| 10499 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10500 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10501 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10502 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10503 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10504 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10505 | if (srelease) |
| 10506 | PyMem_FREE(sbuf); |
| 10507 | if (release1) |
| 10508 | PyMem_FREE(buf1); |
| 10509 | if (release2) |
| 10510 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10511 | return unicode_result_unchanged(self); |
| 10512 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10513 | error: |
| 10514 | if (srelease && sbuf) |
| 10515 | PyMem_FREE(sbuf); |
| 10516 | if (release1 && buf1) |
| 10517 | PyMem_FREE(buf1); |
| 10518 | if (release2 && buf2) |
| 10519 | PyMem_FREE(buf2); |
| 10520 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10521 | } |
| 10522 | |
| 10523 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10524 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10525 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10526 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10527 | \n\ |
| 10528 | 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] | 10529 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | |
| 10531 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10532 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10533 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10534 | if (PyUnicode_READY(self) == -1) |
| 10535 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10536 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10537 | } |
| 10538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10539 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10540 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10541 | \n\ |
| 10542 | 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] | 10543 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10544 | |
| 10545 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10546 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10547 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10548 | if (PyUnicode_READY(self) == -1) |
| 10549 | return NULL; |
| 10550 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10551 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10552 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10553 | } |
| 10554 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10555 | PyDoc_STRVAR(casefold__doc__, |
| 10556 | "S.casefold() -> str\n\ |
| 10557 | \n\ |
| 10558 | Return a version of S suitable for caseless comparisons."); |
| 10559 | |
| 10560 | static PyObject * |
| 10561 | unicode_casefold(PyObject *self) |
| 10562 | { |
| 10563 | if (PyUnicode_READY(self) == -1) |
| 10564 | return NULL; |
| 10565 | if (PyUnicode_IS_ASCII(self)) |
| 10566 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10567 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10568 | } |
| 10569 | |
| 10570 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10571 | /* Argument converter. Coerces to a single unicode character */ |
| 10572 | |
| 10573 | static int |
| 10574 | convert_uc(PyObject *obj, void *addr) |
| 10575 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10576 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10577 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10578 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10579 | uniobj = PyUnicode_FromObject(obj); |
| 10580 | if (uniobj == NULL) { |
| 10581 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10582 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10583 | return 0; |
| 10584 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10585 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10586 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10587 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10588 | Py_DECREF(uniobj); |
| 10589 | return 0; |
| 10590 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10591 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10592 | Py_DECREF(uniobj); |
| 10593 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10594 | } |
| 10595 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10596 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10597 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10598 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10599 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10600 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10601 | |
| 10602 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10603 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10604 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10605 | Py_ssize_t marg, left; |
| 10606 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10607 | Py_UCS4 fillchar = ' '; |
| 10608 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10609 | 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] | 10610 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10611 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10612 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10613 | return NULL; |
| 10614 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10615 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10616 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10617 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10618 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10619 | left = marg / 2 + (marg & width & 1); |
| 10620 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10621 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10622 | } |
| 10623 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10624 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10625 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10626 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10627 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10628 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10629 | #define COMPARE(TYPE1, TYPE2) \ |
| 10630 | do { \ |
| 10631 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10632 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10633 | TYPE1* end = p1 + len; \ |
| 10634 | Py_UCS4 c1, c2; \ |
| 10635 | for (; p1 != end; p1++, p2++) { \ |
| 10636 | c1 = *p1; \ |
| 10637 | c2 = *p2; \ |
| 10638 | if (c1 != c2) \ |
| 10639 | return (c1 < c2) ? -1 : 1; \ |
| 10640 | } \ |
| 10641 | } \ |
| 10642 | while (0) |
| 10643 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10644 | int kind1, kind2; |
| 10645 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10646 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10647 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10648 | kind1 = PyUnicode_KIND(str1); |
| 10649 | kind2 = PyUnicode_KIND(str2); |
| 10650 | data1 = PyUnicode_DATA(str1); |
| 10651 | data2 = PyUnicode_DATA(str2); |
| 10652 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10653 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10654 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10655 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10656 | switch(kind1) { |
| 10657 | case PyUnicode_1BYTE_KIND: |
| 10658 | { |
| 10659 | switch(kind2) { |
| 10660 | case PyUnicode_1BYTE_KIND: |
| 10661 | { |
| 10662 | int cmp = memcmp(data1, data2, len); |
| 10663 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10664 | if (cmp < 0) |
| 10665 | return -1; |
| 10666 | if (cmp > 0) |
| 10667 | return 1; |
| 10668 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10669 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10670 | case PyUnicode_2BYTE_KIND: |
| 10671 | COMPARE(Py_UCS1, Py_UCS2); |
| 10672 | break; |
| 10673 | case PyUnicode_4BYTE_KIND: |
| 10674 | COMPARE(Py_UCS1, Py_UCS4); |
| 10675 | break; |
| 10676 | default: |
| 10677 | assert(0); |
| 10678 | } |
| 10679 | break; |
| 10680 | } |
| 10681 | case PyUnicode_2BYTE_KIND: |
| 10682 | { |
| 10683 | switch(kind2) { |
| 10684 | case PyUnicode_1BYTE_KIND: |
| 10685 | COMPARE(Py_UCS2, Py_UCS1); |
| 10686 | break; |
| 10687 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10688 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10689 | COMPARE(Py_UCS2, Py_UCS2); |
| 10690 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10691 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10692 | case PyUnicode_4BYTE_KIND: |
| 10693 | COMPARE(Py_UCS2, Py_UCS4); |
| 10694 | break; |
| 10695 | default: |
| 10696 | assert(0); |
| 10697 | } |
| 10698 | break; |
| 10699 | } |
| 10700 | case PyUnicode_4BYTE_KIND: |
| 10701 | { |
| 10702 | switch(kind2) { |
| 10703 | case PyUnicode_1BYTE_KIND: |
| 10704 | COMPARE(Py_UCS4, Py_UCS1); |
| 10705 | break; |
| 10706 | case PyUnicode_2BYTE_KIND: |
| 10707 | COMPARE(Py_UCS4, Py_UCS2); |
| 10708 | break; |
| 10709 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10710 | { |
| 10711 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10712 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10713 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10714 | if (cmp < 0) |
| 10715 | return -1; |
| 10716 | if (cmp > 0) |
| 10717 | return 1; |
| 10718 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10719 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10720 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10721 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10722 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10723 | default: |
| 10724 | assert(0); |
| 10725 | } |
| 10726 | break; |
| 10727 | } |
| 10728 | default: |
| 10729 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10730 | } |
| 10731 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10732 | if (len1 == len2) |
| 10733 | return 0; |
| 10734 | if (len1 < len2) |
| 10735 | return -1; |
| 10736 | else |
| 10737 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10738 | |
| 10739 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10740 | } |
| 10741 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10742 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10743 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10744 | { |
| 10745 | int kind; |
| 10746 | void *data1, *data2; |
| 10747 | Py_ssize_t len; |
| 10748 | int cmp; |
| 10749 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10750 | len = PyUnicode_GET_LENGTH(str1); |
| 10751 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10752 | return 0; |
| 10753 | kind = PyUnicode_KIND(str1); |
| 10754 | if (PyUnicode_KIND(str2) != kind) |
| 10755 | return 0; |
| 10756 | data1 = PyUnicode_DATA(str1); |
| 10757 | data2 = PyUnicode_DATA(str2); |
| 10758 | |
| 10759 | cmp = memcmp(data1, data2, len * kind); |
| 10760 | return (cmp == 0); |
| 10761 | } |
| 10762 | |
| 10763 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10764 | int |
| 10765 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10766 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10767 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10768 | if (PyUnicode_READY(left) == -1 || |
| 10769 | PyUnicode_READY(right) == -1) |
| 10770 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10771 | |
| 10772 | /* a string is equal to itself */ |
| 10773 | if (left == right) |
| 10774 | return 0; |
| 10775 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10776 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10777 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10778 | PyErr_Format(PyExc_TypeError, |
| 10779 | "Can't compare %.100s and %.100s", |
| 10780 | left->ob_type->tp_name, |
| 10781 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10782 | return -1; |
| 10783 | } |
| 10784 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10785 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10786 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10787 | { |
| 10788 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10789 | if (right_str == NULL) |
| 10790 | return -1; |
| 10791 | return PyUnicode_Compare(left, right_str); |
| 10792 | } |
| 10793 | |
| 10794 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10795 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10796 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10797 | Py_ssize_t i; |
| 10798 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10799 | Py_UCS4 chr; |
| 10800 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10801 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10802 | if (PyUnicode_READY(uni) == -1) |
| 10803 | return -1; |
| 10804 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10805 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10806 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10807 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10808 | size_t len, len2 = strlen(str); |
| 10809 | int cmp; |
| 10810 | |
| 10811 | len = Py_MIN(len1, len2); |
| 10812 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 10813 | if (cmp != 0) { |
| 10814 | if (cmp < 0) |
| 10815 | return -1; |
| 10816 | else |
| 10817 | return 1; |
| 10818 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10819 | if (len1 > len2) |
| 10820 | return 1; /* uni is longer */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10821 | if (len1 < len2) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10822 | return -1; /* str is longer */ |
| 10823 | return 0; |
| 10824 | } |
| 10825 | else { |
| 10826 | void *data = PyUnicode_DATA(uni); |
| 10827 | /* Compare Unicode string and source character set string */ |
| 10828 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 10829 | if (chr != (unsigned char)str[i]) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10830 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10831 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10832 | to C strings identical up to that point. */ |
| 10833 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 10834 | return 1; /* uni is longer */ |
| 10835 | if (str[i]) |
| 10836 | return -1; /* str is longer */ |
| 10837 | return 0; |
| 10838 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10839 | } |
| 10840 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10841 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10842 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10843 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10844 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10845 | PyObject * |
| 10846 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10847 | { |
| 10848 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10849 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10850 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10851 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10852 | Py_RETURN_NOTIMPLEMENTED; |
| 10853 | |
| 10854 | if (PyUnicode_READY(left) == -1 || |
| 10855 | PyUnicode_READY(right) == -1) |
| 10856 | return NULL; |
| 10857 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 10858 | if (left == right) { |
| 10859 | switch (op) { |
| 10860 | case Py_EQ: |
| 10861 | case Py_LE: |
| 10862 | case Py_GE: |
| 10863 | /* a string is equal to itself */ |
| 10864 | v = Py_True; |
| 10865 | break; |
| 10866 | case Py_NE: |
| 10867 | case Py_LT: |
| 10868 | case Py_GT: |
| 10869 | v = Py_False; |
| 10870 | break; |
| 10871 | default: |
| 10872 | PyErr_BadArgument(); |
| 10873 | return NULL; |
| 10874 | } |
| 10875 | } |
| 10876 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10877 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10878 | result ^= (op == Py_NE); |
| 10879 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10880 | } |
| 10881 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10882 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10883 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10884 | /* Convert the return value to a Boolean */ |
| 10885 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10886 | case Py_LE: |
| 10887 | v = TEST_COND(result <= 0); |
| 10888 | break; |
| 10889 | case Py_GE: |
| 10890 | v = TEST_COND(result >= 0); |
| 10891 | break; |
| 10892 | case Py_LT: |
| 10893 | v = TEST_COND(result == -1); |
| 10894 | break; |
| 10895 | case Py_GT: |
| 10896 | v = TEST_COND(result == 1); |
| 10897 | break; |
| 10898 | default: |
| 10899 | PyErr_BadArgument(); |
| 10900 | return NULL; |
| 10901 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10902 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10903 | Py_INCREF(v); |
| 10904 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10905 | } |
| 10906 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10907 | int |
| 10908 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10909 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10910 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10911 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10912 | void *buf1, *buf2; |
| 10913 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10914 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10915 | |
| 10916 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10917 | sub = PyUnicode_FromObject(element); |
| 10918 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10919 | PyErr_Format(PyExc_TypeError, |
| 10920 | "'in <string>' requires string as left operand, not %s", |
| 10921 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10922 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10923 | } |
| 10924 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10925 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10926 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10927 | Py_DECREF(sub); |
| 10928 | return -1; |
| 10929 | } |
| 10930 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10931 | kind1 = PyUnicode_KIND(str); |
| 10932 | kind2 = PyUnicode_KIND(sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10933 | if (kind1 < kind2) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10934 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10935 | Py_DECREF(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10936 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10937 | } |
| 10938 | len1 = PyUnicode_GET_LENGTH(str); |
| 10939 | len2 = PyUnicode_GET_LENGTH(sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10940 | if (len1 < len2) { |
| 10941 | Py_DECREF(sub); |
| 10942 | Py_DECREF(str); |
| 10943 | return 0; |
| 10944 | } |
| 10945 | buf1 = PyUnicode_DATA(str); |
| 10946 | buf2 = PyUnicode_DATA(sub); |
| 10947 | if (len2 == 1) { |
| 10948 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 10949 | result = findchar((const char *)buf1, kind1, len1, ch, 1) != -1; |
| 10950 | Py_DECREF(sub); |
| 10951 | Py_DECREF(str); |
| 10952 | return result; |
| 10953 | } |
| 10954 | if (kind2 != kind1) { |
| 10955 | buf2 = _PyUnicode_AsKind(sub, kind1); |
| 10956 | if (!buf2) { |
| 10957 | Py_DECREF(sub); |
| 10958 | Py_DECREF(str); |
| 10959 | return -1; |
| 10960 | } |
| 10961 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10962 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10963 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10964 | case PyUnicode_1BYTE_KIND: |
| 10965 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10966 | break; |
| 10967 | case PyUnicode_2BYTE_KIND: |
| 10968 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10969 | break; |
| 10970 | case PyUnicode_4BYTE_KIND: |
| 10971 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10972 | break; |
| 10973 | default: |
| 10974 | result = -1; |
| 10975 | assert(0); |
| 10976 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10977 | |
| 10978 | Py_DECREF(str); |
| 10979 | Py_DECREF(sub); |
| 10980 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10981 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10982 | PyMem_Free(buf2); |
| 10983 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10984 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10985 | } |
| 10986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10987 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10988 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10989 | PyObject * |
| 10990 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10991 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10992 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10993 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10994 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10995 | |
| 10996 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10997 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10999 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11000 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11001 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11002 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11003 | |
| 11004 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11005 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11006 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11007 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11009 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11010 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11011 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | } |
| 11013 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11014 | u_len = PyUnicode_GET_LENGTH(u); |
| 11015 | v_len = PyUnicode_GET_LENGTH(v); |
| 11016 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11017 | PyErr_SetString(PyExc_OverflowError, |
| 11018 | "strings are too large to concat"); |
| 11019 | goto onError; |
| 11020 | } |
| 11021 | new_len = u_len + v_len; |
| 11022 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11023 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11024 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11025 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11026 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11027 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11028 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11029 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11030 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11031 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 11032 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11033 | Py_DECREF(u); |
| 11034 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11035 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11036 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11038 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11039 | Py_XDECREF(u); |
| 11040 | Py_XDECREF(v); |
| 11041 | return NULL; |
| 11042 | } |
| 11043 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11044 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11045 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11046 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11047 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11048 | Py_UCS4 maxchar, maxchar2; |
| 11049 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11050 | |
| 11051 | if (p_left == NULL) { |
| 11052 | if (!PyErr_Occurred()) |
| 11053 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11054 | return; |
| 11055 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11056 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11057 | if (right == NULL || left == NULL |
| 11058 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11059 | if (!PyErr_Occurred()) |
| 11060 | PyErr_BadInternalCall(); |
| 11061 | goto error; |
| 11062 | } |
| 11063 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11064 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11065 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11066 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11067 | goto error; |
| 11068 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11069 | /* Shortcuts */ |
| 11070 | if (left == unicode_empty) { |
| 11071 | Py_DECREF(left); |
| 11072 | Py_INCREF(right); |
| 11073 | *p_left = right; |
| 11074 | return; |
| 11075 | } |
| 11076 | if (right == unicode_empty) |
| 11077 | return; |
| 11078 | |
| 11079 | left_len = PyUnicode_GET_LENGTH(left); |
| 11080 | right_len = PyUnicode_GET_LENGTH(right); |
| 11081 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11082 | PyErr_SetString(PyExc_OverflowError, |
| 11083 | "strings are too large to concat"); |
| 11084 | goto error; |
| 11085 | } |
| 11086 | new_len = left_len + right_len; |
| 11087 | |
| 11088 | if (unicode_modifiable(left) |
| 11089 | && PyUnicode_CheckExact(right) |
| 11090 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11091 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11092 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11093 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11094 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11095 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11096 | { |
| 11097 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11098 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11099 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11100 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11101 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11102 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11103 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11104 | else { |
| 11105 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11106 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11107 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11108 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11109 | /* Concat the two Unicode strings */ |
| 11110 | res = PyUnicode_New(new_len, maxchar); |
| 11111 | if (res == NULL) |
| 11112 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11113 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11114 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11115 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11116 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11117 | } |
| 11118 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11119 | return; |
| 11120 | |
| 11121 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11122 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11123 | } |
| 11124 | |
| 11125 | void |
| 11126 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11127 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11128 | PyUnicode_Append(pleft, right); |
| 11129 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11130 | } |
| 11131 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11132 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11133 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11134 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11135 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11136 | 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] | 11137 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11138 | |
| 11139 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11140 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11141 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11142 | PyObject *substring = NULL; /* initialize to fix a compiler warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11143 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11144 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11145 | PyObject *result; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11146 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11147 | void *buf1, *buf2; |
| 11148 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11149 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11150 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11151 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11152 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11153 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11154 | kind1 = PyUnicode_KIND(self); |
| 11155 | kind2 = PyUnicode_KIND(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11156 | if (kind1 < kind2) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11157 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11158 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11159 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11160 | len1 = PyUnicode_GET_LENGTH(self); |
| 11161 | len2 = PyUnicode_GET_LENGTH(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11162 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11163 | if (end - start < len2) { |
| 11164 | Py_DECREF(substring); |
| 11165 | return PyLong_FromLong(0); |
| 11166 | } |
| 11167 | buf1 = PyUnicode_DATA(self); |
| 11168 | buf2 = PyUnicode_DATA(substring); |
| 11169 | if (kind2 != kind1) { |
| 11170 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 11171 | if (!buf2) { |
| 11172 | Py_DECREF(substring); |
| 11173 | return NULL; |
| 11174 | } |
| 11175 | } |
| 11176 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | case PyUnicode_1BYTE_KIND: |
| 11178 | iresult = ucs1lib_count( |
| 11179 | ((Py_UCS1*)buf1) + start, end - start, |
| 11180 | buf2, len2, PY_SSIZE_T_MAX |
| 11181 | ); |
| 11182 | break; |
| 11183 | case PyUnicode_2BYTE_KIND: |
| 11184 | iresult = ucs2lib_count( |
| 11185 | ((Py_UCS2*)buf1) + start, end - start, |
| 11186 | buf2, len2, PY_SSIZE_T_MAX |
| 11187 | ); |
| 11188 | break; |
| 11189 | case PyUnicode_4BYTE_KIND: |
| 11190 | iresult = ucs4lib_count( |
| 11191 | ((Py_UCS4*)buf1) + start, end - start, |
| 11192 | buf2, len2, PY_SSIZE_T_MAX |
| 11193 | ); |
| 11194 | break; |
| 11195 | default: |
| 11196 | assert(0); iresult = 0; |
| 11197 | } |
| 11198 | |
| 11199 | result = PyLong_FromSsize_t(iresult); |
| 11200 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11201 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11202 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11203 | |
| 11204 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11205 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11206 | return result; |
| 11207 | } |
| 11208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11209 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11210 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11211 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11212 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11213 | 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] | 11214 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11215 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11216 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11217 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11218 | |
| 11219 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11220 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11221 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11222 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11223 | char *encoding = NULL; |
| 11224 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11225 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11226 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11227 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11228 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11229 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11230 | } |
| 11231 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11232 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11233 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11234 | \n\ |
| 11235 | 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] | 11236 | 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] | 11237 | |
| 11238 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11239 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11240 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11241 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11242 | Py_UCS4 ch; |
| 11243 | PyObject *u; |
| 11244 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11245 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11246 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11247 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11248 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11250 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11251 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11252 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11254 | if (PyUnicode_READY(self) == -1) |
| 11255 | return NULL; |
| 11256 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11257 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11258 | src_len = PyUnicode_GET_LENGTH(self); |
| 11259 | i = j = line_pos = 0; |
| 11260 | kind = PyUnicode_KIND(self); |
| 11261 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11262 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11263 | for (; i < src_len; i++) { |
| 11264 | ch = PyUnicode_READ(kind, src_data, i); |
| 11265 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11266 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11267 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11268 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11269 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11270 | goto overflow; |
| 11271 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11272 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11273 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11274 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11275 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11276 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11277 | goto overflow; |
| 11278 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11280 | if (ch == '\n' || ch == '\r') |
| 11281 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11282 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11283 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11284 | if (!found) |
| 11285 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11286 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11287 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11288 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11289 | if (!u) |
| 11290 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11291 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11292 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11293 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11295 | for (; i < src_len; i++) { |
| 11296 | ch = PyUnicode_READ(kind, src_data, i); |
| 11297 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11298 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11299 | incr = tabsize - (line_pos % tabsize); |
| 11300 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11301 | FILL(kind, dest_data, ' ', j, incr); |
| 11302 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11303 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11304 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11305 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11306 | line_pos++; |
| 11307 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11308 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11309 | if (ch == '\n' || ch == '\r') |
| 11310 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11311 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11312 | } |
| 11313 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11314 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11315 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11316 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11317 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11318 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11319 | } |
| 11320 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11321 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11322 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11323 | \n\ |
| 11324 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11325 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11326 | arguments start and end are interpreted as in slice notation.\n\ |
| 11327 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11328 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 | |
| 11330 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11331 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11332 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11333 | /* initialize variables to prevent gcc warning */ |
| 11334 | PyObject *substring = NULL; |
| 11335 | Py_ssize_t start = 0; |
| 11336 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11337 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11338 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11339 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11340 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11341 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11342 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11343 | if (PyUnicode_READY(self) == -1) { |
| 11344 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11345 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11346 | } |
| 11347 | if (PyUnicode_READY(substring) == -1) { |
| 11348 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11349 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11350 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11351 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11352 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11353 | |
| 11354 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11355 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11356 | if (result == -2) |
| 11357 | return NULL; |
| 11358 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11359 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11360 | } |
| 11361 | |
| 11362 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11363 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11364 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11365 | void *data; |
| 11366 | enum PyUnicode_Kind kind; |
| 11367 | Py_UCS4 ch; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11368 | |
| 11369 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11370 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11371 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11372 | } |
| 11373 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11374 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11375 | return NULL; |
| 11376 | } |
| 11377 | kind = PyUnicode_KIND(self); |
| 11378 | data = PyUnicode_DATA(self); |
| 11379 | ch = PyUnicode_READ(kind, data, index); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 11380 | return unicode_char(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11381 | } |
| 11382 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11383 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11384 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11385 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11386 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11387 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11388 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11389 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11390 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11391 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11392 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11393 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11394 | if (_PyUnicode_HASH(self) != -1) |
| 11395 | return _PyUnicode_HASH(self); |
| 11396 | if (PyUnicode_READY(self) == -1) |
| 11397 | return -1; |
| 11398 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11399 | /* |
| 11400 | We make the hash of the empty string be 0, rather than using |
| 11401 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11402 | */ |
| 11403 | if (len == 0) { |
| 11404 | _PyUnicode_HASH(self) = 0; |
| 11405 | return 0; |
| 11406 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11407 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11408 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11409 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11410 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11411 | } |
| 11412 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11413 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11414 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11415 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11416 | 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] | 11417 | |
| 11418 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11419 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11420 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11421 | /* initialize variables to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11422 | Py_ssize_t result; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11423 | PyObject *substring = NULL; |
| 11424 | Py_ssize_t start = 0; |
| 11425 | Py_ssize_t end = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11427 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11428 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11430 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11431 | if (PyUnicode_READY(self) == -1) { |
| 11432 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11433 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11434 | } |
| 11435 | if (PyUnicode_READY(substring) == -1) { |
| 11436 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11437 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11438 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11439 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11440 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11441 | |
| 11442 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11444 | if (result == -2) |
| 11445 | return NULL; |
| 11446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11447 | if (result < 0) { |
| 11448 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11449 | return NULL; |
| 11450 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11451 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11452 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11453 | } |
| 11454 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11455 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11456 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11457 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11458 | 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] | 11459 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | |
| 11461 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11462 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | Py_ssize_t i, length; |
| 11465 | int kind; |
| 11466 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | int cased; |
| 11468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11469 | if (PyUnicode_READY(self) == -1) |
| 11470 | return NULL; |
| 11471 | length = PyUnicode_GET_LENGTH(self); |
| 11472 | kind = PyUnicode_KIND(self); |
| 11473 | data = PyUnicode_DATA(self); |
| 11474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11476 | if (length == 1) |
| 11477 | return PyBool_FromLong( |
| 11478 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11479 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11480 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11481 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11482 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11484 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11485 | for (i = 0; i < length; i++) { |
| 11486 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11487 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11488 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11489 | return PyBool_FromLong(0); |
| 11490 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11491 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11492 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11493 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11494 | } |
| 11495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11496 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11497 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11499 | 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] | 11500 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11501 | |
| 11502 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11503 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 | Py_ssize_t i, length; |
| 11506 | int kind; |
| 11507 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11508 | int cased; |
| 11509 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11510 | if (PyUnicode_READY(self) == -1) |
| 11511 | return NULL; |
| 11512 | length = PyUnicode_GET_LENGTH(self); |
| 11513 | kind = PyUnicode_KIND(self); |
| 11514 | data = PyUnicode_DATA(self); |
| 11515 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11516 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11517 | if (length == 1) |
| 11518 | return PyBool_FromLong( |
| 11519 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11521 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11522 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11523 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11525 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11526 | for (i = 0; i < length; i++) { |
| 11527 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11529 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11530 | return PyBool_FromLong(0); |
| 11531 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11532 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11533 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11534 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11535 | } |
| 11536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11537 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11538 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11540 | Return True if S is a titlecased string and there is at least one\n\ |
| 11541 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11542 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11543 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11544 | |
| 11545 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11546 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11547 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11548 | Py_ssize_t i, length; |
| 11549 | int kind; |
| 11550 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11551 | int cased, previous_is_cased; |
| 11552 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11553 | if (PyUnicode_READY(self) == -1) |
| 11554 | return NULL; |
| 11555 | length = PyUnicode_GET_LENGTH(self); |
| 11556 | kind = PyUnicode_KIND(self); |
| 11557 | data = PyUnicode_DATA(self); |
| 11558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11560 | if (length == 1) { |
| 11561 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11562 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11563 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11564 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11565 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11566 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11567 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11568 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11570 | cased = 0; |
| 11571 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11572 | for (i = 0; i < length; i++) { |
| 11573 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11574 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11575 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11576 | if (previous_is_cased) |
| 11577 | return PyBool_FromLong(0); |
| 11578 | previous_is_cased = 1; |
| 11579 | cased = 1; |
| 11580 | } |
| 11581 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11582 | if (!previous_is_cased) |
| 11583 | return PyBool_FromLong(0); |
| 11584 | previous_is_cased = 1; |
| 11585 | cased = 1; |
| 11586 | } |
| 11587 | else |
| 11588 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11589 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11590 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11591 | } |
| 11592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11593 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11594 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11595 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11596 | Return True if all characters in S are whitespace\n\ |
| 11597 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11598 | |
| 11599 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11600 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11601 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11602 | Py_ssize_t i, length; |
| 11603 | int kind; |
| 11604 | void *data; |
| 11605 | |
| 11606 | if (PyUnicode_READY(self) == -1) |
| 11607 | return NULL; |
| 11608 | length = PyUnicode_GET_LENGTH(self); |
| 11609 | kind = PyUnicode_KIND(self); |
| 11610 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11611 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11612 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11613 | if (length == 1) |
| 11614 | return PyBool_FromLong( |
| 11615 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11616 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11617 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11618 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11619 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11620 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11621 | for (i = 0; i < length; i++) { |
| 11622 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11623 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11624 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11625 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11626 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11627 | } |
| 11628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11629 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11630 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11631 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11632 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11633 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11634 | |
| 11635 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11636 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11637 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11638 | Py_ssize_t i, length; |
| 11639 | int kind; |
| 11640 | void *data; |
| 11641 | |
| 11642 | if (PyUnicode_READY(self) == -1) |
| 11643 | return NULL; |
| 11644 | length = PyUnicode_GET_LENGTH(self); |
| 11645 | kind = PyUnicode_KIND(self); |
| 11646 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11647 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11648 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11649 | if (length == 1) |
| 11650 | return PyBool_FromLong( |
| 11651 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11652 | |
| 11653 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11654 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11655 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11656 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11657 | for (i = 0; i < length; i++) { |
| 11658 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11659 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11660 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11661 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11662 | } |
| 11663 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11664 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11665 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11666 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11667 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11668 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11669 | |
| 11670 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11671 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11672 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11673 | int kind; |
| 11674 | void *data; |
| 11675 | Py_ssize_t len, i; |
| 11676 | |
| 11677 | if (PyUnicode_READY(self) == -1) |
| 11678 | return NULL; |
| 11679 | |
| 11680 | kind = PyUnicode_KIND(self); |
| 11681 | data = PyUnicode_DATA(self); |
| 11682 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11683 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11684 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11685 | if (len == 1) { |
| 11686 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11687 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11688 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11689 | |
| 11690 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11691 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11692 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11693 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11694 | for (i = 0; i < len; i++) { |
| 11695 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11696 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11697 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11698 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11699 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11700 | } |
| 11701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11702 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11703 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11704 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11705 | 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] | 11706 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11707 | |
| 11708 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11709 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11710 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11711 | Py_ssize_t i, length; |
| 11712 | int kind; |
| 11713 | void *data; |
| 11714 | |
| 11715 | if (PyUnicode_READY(self) == -1) |
| 11716 | return NULL; |
| 11717 | length = PyUnicode_GET_LENGTH(self); |
| 11718 | kind = PyUnicode_KIND(self); |
| 11719 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11721 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11722 | if (length == 1) |
| 11723 | return PyBool_FromLong( |
| 11724 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11725 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11726 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11727 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11728 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11729 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11730 | for (i = 0; i < length; i++) { |
| 11731 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11732 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11733 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11734 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11735 | } |
| 11736 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11737 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11738 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11739 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11740 | Return True if all characters in S are digits\n\ |
| 11741 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11742 | |
| 11743 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11744 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11745 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11746 | Py_ssize_t i, length; |
| 11747 | int kind; |
| 11748 | void *data; |
| 11749 | |
| 11750 | if (PyUnicode_READY(self) == -1) |
| 11751 | return NULL; |
| 11752 | length = PyUnicode_GET_LENGTH(self); |
| 11753 | kind = PyUnicode_KIND(self); |
| 11754 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11756 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11757 | if (length == 1) { |
| 11758 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11759 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11760 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11761 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11762 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11763 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11764 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11765 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11766 | for (i = 0; i < length; i++) { |
| 11767 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11768 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11769 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11770 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | } |
| 11772 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11773 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11774 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11775 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11776 | 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] | 11777 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11778 | |
| 11779 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11780 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11781 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11782 | Py_ssize_t i, length; |
| 11783 | int kind; |
| 11784 | void *data; |
| 11785 | |
| 11786 | if (PyUnicode_READY(self) == -1) |
| 11787 | return NULL; |
| 11788 | length = PyUnicode_GET_LENGTH(self); |
| 11789 | kind = PyUnicode_KIND(self); |
| 11790 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11792 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11793 | if (length == 1) |
| 11794 | return PyBool_FromLong( |
| 11795 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11796 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11797 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11798 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11799 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11801 | for (i = 0; i < length; i++) { |
| 11802 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11803 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11804 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11805 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 | } |
| 11807 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11808 | int |
| 11809 | PyUnicode_IsIdentifier(PyObject *self) |
| 11810 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11811 | int kind; |
| 11812 | void *data; |
| 11813 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11814 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11815 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11816 | if (PyUnicode_READY(self) == -1) { |
| 11817 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11818 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11819 | } |
| 11820 | |
| 11821 | /* Special case for empty strings */ |
| 11822 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11823 | return 0; |
| 11824 | kind = PyUnicode_KIND(self); |
| 11825 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11826 | |
| 11827 | /* PEP 3131 says that the first character must be in |
| 11828 | XID_Start and subsequent characters in XID_Continue, |
| 11829 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11830 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11831 | letters, digits, underscore). However, given the current |
| 11832 | definition of XID_Start and XID_Continue, it is sufficient |
| 11833 | to check just for these, except that _ must be allowed |
| 11834 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11835 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11836 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11837 | return 0; |
| 11838 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11839 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11840 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11841 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11842 | return 1; |
| 11843 | } |
| 11844 | |
| 11845 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11846 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11847 | \n\ |
| 11848 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11849 | to the language definition.\n\ |
| 11850 | \n\ |
| 11851 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11852 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11853 | |
| 11854 | static PyObject* |
| 11855 | unicode_isidentifier(PyObject *self) |
| 11856 | { |
| 11857 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11858 | } |
| 11859 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11860 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11861 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11862 | \n\ |
| 11863 | Return True if all characters in S are considered\n\ |
| 11864 | printable in repr() or S is empty, False otherwise."); |
| 11865 | |
| 11866 | static PyObject* |
| 11867 | unicode_isprintable(PyObject *self) |
| 11868 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11869 | Py_ssize_t i, length; |
| 11870 | int kind; |
| 11871 | void *data; |
| 11872 | |
| 11873 | if (PyUnicode_READY(self) == -1) |
| 11874 | return NULL; |
| 11875 | length = PyUnicode_GET_LENGTH(self); |
| 11876 | kind = PyUnicode_KIND(self); |
| 11877 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11878 | |
| 11879 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11880 | if (length == 1) |
| 11881 | return PyBool_FromLong( |
| 11882 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11883 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11884 | for (i = 0; i < length; i++) { |
| 11885 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11886 | Py_RETURN_FALSE; |
| 11887 | } |
| 11888 | } |
| 11889 | Py_RETURN_TRUE; |
| 11890 | } |
| 11891 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11892 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11893 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11894 | \n\ |
| 11895 | 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] | 11896 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | |
| 11898 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11899 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11900 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11901 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11902 | } |
| 11903 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11904 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11905 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11906 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11907 | if (PyUnicode_READY(self) == -1) |
| 11908 | return -1; |
| 11909 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11910 | } |
| 11911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11912 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11913 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11914 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11915 | 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] | 11916 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11917 | |
| 11918 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11919 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11920 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11921 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11922 | Py_UCS4 fillchar = ' '; |
| 11923 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11924 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11925 | return NULL; |
| 11926 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11927 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11928 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11929 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11930 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11931 | return unicode_result_unchanged(self); |
| 11932 | |
| 11933 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11934 | } |
| 11935 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11936 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11937 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11938 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11939 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11940 | |
| 11941 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11942 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11944 | if (PyUnicode_READY(self) == -1) |
| 11945 | return NULL; |
| 11946 | if (PyUnicode_IS_ASCII(self)) |
| 11947 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11948 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11949 | } |
| 11950 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11951 | #define LEFTSTRIP 0 |
| 11952 | #define RIGHTSTRIP 1 |
| 11953 | #define BOTHSTRIP 2 |
| 11954 | |
| 11955 | /* Arrays indexed by above */ |
| 11956 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11957 | |
| 11958 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11959 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11960 | /* externally visible for str.strip(unicode) */ |
| 11961 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11962 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11963 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11964 | void *data; |
| 11965 | int kind; |
| 11966 | Py_ssize_t i, j, len; |
| 11967 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11968 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11969 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11970 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11971 | return NULL; |
| 11972 | |
| 11973 | kind = PyUnicode_KIND(self); |
| 11974 | data = PyUnicode_DATA(self); |
| 11975 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11976 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11977 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11978 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11979 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11980 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11981 | i = 0; |
| 11982 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11983 | while (i < len) { |
| 11984 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11985 | if (!BLOOM(sepmask, ch)) |
| 11986 | break; |
| 11987 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11988 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11989 | i++; |
| 11990 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11991 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11992 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11993 | j = len; |
| 11994 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11995 | j--; |
| 11996 | while (j >= i) { |
| 11997 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 11998 | if (!BLOOM(sepmask, ch)) |
| 11999 | break; |
| 12000 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12001 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12002 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12003 | } |
| 12004 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12005 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12006 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12007 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12008 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12009 | } |
| 12010 | |
| 12011 | PyObject* |
| 12012 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12013 | { |
| 12014 | unsigned char *data; |
| 12015 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12016 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12017 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12018 | if (PyUnicode_READY(self) == -1) |
| 12019 | return NULL; |
| 12020 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12021 | length = PyUnicode_GET_LENGTH(self); |
| 12022 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12023 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12024 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12025 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12026 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12027 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12028 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12029 | return NULL; |
| 12030 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12031 | if (start >= length || end < start) |
| 12032 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12033 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12034 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12035 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12036 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12037 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12038 | } |
| 12039 | else { |
| 12040 | kind = PyUnicode_KIND(self); |
| 12041 | data = PyUnicode_1BYTE_DATA(self); |
| 12042 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12043 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12044 | length); |
| 12045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12046 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12047 | |
| 12048 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12049 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12050 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12051 | Py_ssize_t len, i, j; |
| 12052 | |
| 12053 | if (PyUnicode_READY(self) == -1) |
| 12054 | return NULL; |
| 12055 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12056 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12057 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12058 | if (PyUnicode_IS_ASCII(self)) { |
| 12059 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12060 | |
| 12061 | i = 0; |
| 12062 | if (striptype != RIGHTSTRIP) { |
| 12063 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12064 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12065 | if (!_Py_ascii_whitespace[ch]) |
| 12066 | break; |
| 12067 | i++; |
| 12068 | } |
| 12069 | } |
| 12070 | |
| 12071 | j = len; |
| 12072 | if (striptype != LEFTSTRIP) { |
| 12073 | j--; |
| 12074 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12075 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12076 | if (!_Py_ascii_whitespace[ch]) |
| 12077 | break; |
| 12078 | j--; |
| 12079 | } |
| 12080 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12081 | } |
| 12082 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12083 | else { |
| 12084 | int kind = PyUnicode_KIND(self); |
| 12085 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12086 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12087 | i = 0; |
| 12088 | if (striptype != RIGHTSTRIP) { |
| 12089 | while (i < len) { |
| 12090 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12091 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12092 | break; |
| 12093 | i++; |
| 12094 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12095 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12096 | |
| 12097 | j = len; |
| 12098 | if (striptype != LEFTSTRIP) { |
| 12099 | j--; |
| 12100 | while (j >= i) { |
| 12101 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12102 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12103 | break; |
| 12104 | j--; |
| 12105 | } |
| 12106 | j++; |
| 12107 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12108 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12109 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12110 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12111 | } |
| 12112 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12113 | |
| 12114 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12115 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12116 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12117 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12118 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12119 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12120 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12121 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12122 | if (sep != NULL && sep != Py_None) { |
| 12123 | if (PyUnicode_Check(sep)) |
| 12124 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12125 | else { |
| 12126 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12127 | "%s arg must be None or str", |
| 12128 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12129 | return NULL; |
| 12130 | } |
| 12131 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12132 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12133 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12134 | } |
| 12135 | |
| 12136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12137 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12138 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12139 | \n\ |
| 12140 | Return a copy of the string S with leading and trailing\n\ |
| 12141 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12142 | 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] | 12143 | |
| 12144 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12145 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12146 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12147 | if (PyTuple_GET_SIZE(args) == 0) |
| 12148 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12149 | else |
| 12150 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12151 | } |
| 12152 | |
| 12153 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12154 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12155 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12156 | \n\ |
| 12157 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12158 | 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] | 12159 | |
| 12160 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12161 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12162 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12163 | if (PyTuple_GET_SIZE(args) == 0) |
| 12164 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12165 | else |
| 12166 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12167 | } |
| 12168 | |
| 12169 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12170 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12171 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12172 | \n\ |
| 12173 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12174 | 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] | 12175 | |
| 12176 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12177 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12178 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12179 | if (PyTuple_GET_SIZE(args) == 0) |
| 12180 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12181 | else |
| 12182 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12183 | } |
| 12184 | |
| 12185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12186 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12187 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12188 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12189 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12190 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12191 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12192 | if (len < 1) |
| 12193 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12194 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12195 | /* no repeat, return original string */ |
| 12196 | if (len == 1) |
| 12197 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12198 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12199 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12200 | return NULL; |
| 12201 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12202 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12203 | PyErr_SetString(PyExc_OverflowError, |
| 12204 | "repeated string is too long"); |
| 12205 | return NULL; |
| 12206 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12207 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12208 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12209 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12210 | if (!u) |
| 12211 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12212 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12213 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12214 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12215 | const int kind = PyUnicode_KIND(str); |
| 12216 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12217 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12218 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12219 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12220 | } |
| 12221 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12222 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12223 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12224 | ucs2[n] = fill_char; |
| 12225 | } else { |
| 12226 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12227 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12228 | for (n = 0; n < len; ++n) |
| 12229 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12230 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12231 | } |
| 12232 | else { |
| 12233 | /* number of characters copied this far */ |
| 12234 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12235 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12236 | char *to = (char *) PyUnicode_DATA(u); |
| 12237 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12238 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12239 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12240 | n = (done <= nchars-done) ? done : nchars-done; |
| 12241 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12242 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12243 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12244 | } |
| 12245 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12246 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12247 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12248 | } |
| 12249 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12250 | PyObject * |
| 12251 | PyUnicode_Replace(PyObject *obj, |
| 12252 | PyObject *subobj, |
| 12253 | PyObject *replobj, |
| 12254 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12255 | { |
| 12256 | PyObject *self; |
| 12257 | PyObject *str1; |
| 12258 | PyObject *str2; |
| 12259 | PyObject *result; |
| 12260 | |
| 12261 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12262 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12263 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12264 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12265 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12266 | Py_DECREF(self); |
| 12267 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12268 | } |
| 12269 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12270 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12271 | Py_DECREF(self); |
| 12272 | Py_DECREF(str1); |
| 12273 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12274 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12275 | if (PyUnicode_READY(self) == -1 || |
| 12276 | PyUnicode_READY(str1) == -1 || |
| 12277 | PyUnicode_READY(str2) == -1) |
| 12278 | result = NULL; |
| 12279 | else |
| 12280 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12281 | Py_DECREF(self); |
| 12282 | Py_DECREF(str1); |
| 12283 | Py_DECREF(str2); |
| 12284 | return result; |
| 12285 | } |
| 12286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12287 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12288 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12289 | \n\ |
| 12290 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12291 | old replaced by new. If the optional argument count is\n\ |
| 12292 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12293 | |
| 12294 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12295 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12296 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12297 | PyObject *str1; |
| 12298 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12299 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12300 | PyObject *result; |
| 12301 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12302 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12303 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12304 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12305 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12306 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12307 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12308 | return NULL; |
| 12309 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12310 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12311 | Py_DECREF(str1); |
| 12312 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12313 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12314 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12315 | result = NULL; |
| 12316 | else |
| 12317 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12318 | |
| 12319 | Py_DECREF(str1); |
| 12320 | Py_DECREF(str2); |
| 12321 | return result; |
| 12322 | } |
| 12323 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12324 | static PyObject * |
| 12325 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12326 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12327 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12328 | Py_ssize_t isize; |
| 12329 | Py_ssize_t osize, squote, dquote, i, o; |
| 12330 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12331 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12332 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12333 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12335 | return NULL; |
| 12336 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12337 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12338 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12339 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12340 | /* Compute length of output, quote characters, and |
| 12341 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12342 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12343 | max = 127; |
| 12344 | squote = dquote = 0; |
| 12345 | ikind = PyUnicode_KIND(unicode); |
| 12346 | for (i = 0; i < isize; i++) { |
| 12347 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12348 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12349 | switch (ch) { |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12350 | case '\'': squote++; break; |
| 12351 | case '"': dquote++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12352 | case '\\': case '\t': case '\r': case '\n': |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12353 | incr = 2; |
| 12354 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12355 | default: |
| 12356 | /* Fast-path ASCII */ |
| 12357 | if (ch < ' ' || ch == 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12358 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12359 | else if (ch < 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12360 | ; |
| 12361 | else if (Py_UNICODE_ISPRINTABLE(ch)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12362 | max = ch > max ? ch : max; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12363 | else if (ch < 0x100) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12364 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12365 | else if (ch < 0x10000) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12366 | incr = 6; /* \uHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12367 | else |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12368 | incr = 10; /* \uHHHHHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12369 | } |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12370 | if (osize > PY_SSIZE_T_MAX - incr) { |
| 12371 | PyErr_SetString(PyExc_OverflowError, |
| 12372 | "string is too long to generate repr"); |
| 12373 | return NULL; |
| 12374 | } |
| 12375 | osize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12376 | } |
| 12377 | |
| 12378 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12379 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12380 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12381 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12382 | if (dquote) |
| 12383 | /* Both squote and dquote present. Use squote, |
| 12384 | and escape them */ |
| 12385 | osize += squote; |
| 12386 | else |
| 12387 | quote = '"'; |
| 12388 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12389 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12390 | |
| 12391 | repr = PyUnicode_New(osize, max); |
| 12392 | if (repr == NULL) |
| 12393 | return NULL; |
| 12394 | okind = PyUnicode_KIND(repr); |
| 12395 | odata = PyUnicode_DATA(repr); |
| 12396 | |
| 12397 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12398 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12399 | if (unchanged) { |
| 12400 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12401 | unicode, 0, |
| 12402 | isize); |
| 12403 | } |
| 12404 | else { |
| 12405 | for (i = 0, o = 1; i < isize; i++) { |
| 12406 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12407 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12408 | /* Escape quotes and backslashes */ |
| 12409 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12410 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12411 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12412 | continue; |
| 12413 | } |
| 12414 | |
| 12415 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12416 | if (ch == '\t') { |
| 12417 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12418 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12419 | } |
| 12420 | else if (ch == '\n') { |
| 12421 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12422 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12423 | } |
| 12424 | else if (ch == '\r') { |
| 12425 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12426 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12427 | } |
| 12428 | |
| 12429 | /* Map non-printable US ASCII to '\xhh' */ |
| 12430 | else if (ch < ' ' || ch == 0x7F) { |
| 12431 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12432 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12433 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12434 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12435 | } |
| 12436 | |
| 12437 | /* Copy ASCII characters as-is */ |
| 12438 | else if (ch < 0x7F) { |
| 12439 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12440 | } |
| 12441 | |
| 12442 | /* Non-ASCII characters */ |
| 12443 | else { |
| 12444 | /* Map Unicode whitespace and control characters |
| 12445 | (categories Z* and C* except ASCII space) |
| 12446 | */ |
| 12447 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12448 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12449 | /* Map 8-bit characters to '\xhh' */ |
| 12450 | if (ch <= 0xff) { |
| 12451 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12452 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12453 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12454 | } |
| 12455 | /* Map 16-bit characters to '\uxxxx' */ |
| 12456 | else if (ch <= 0xffff) { |
| 12457 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12458 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12459 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12460 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12461 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12462 | } |
| 12463 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12464 | else { |
| 12465 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12466 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12467 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12468 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12469 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12470 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12471 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12472 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12473 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12474 | } |
| 12475 | } |
| 12476 | /* Copy characters as-is */ |
| 12477 | else { |
| 12478 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12479 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12480 | } |
| 12481 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12482 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12483 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12484 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12485 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12486 | } |
| 12487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12488 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12489 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12490 | \n\ |
| 12491 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12492 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12493 | arguments start and end are interpreted as in slice notation.\n\ |
| 12494 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12495 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12496 | |
| 12497 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12498 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12499 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12500 | /* initialize variables to prevent gcc warning */ |
| 12501 | PyObject *substring = NULL; |
| 12502 | Py_ssize_t start = 0; |
| 12503 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12504 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12505 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12506 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12507 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12508 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12510 | if (PyUnicode_READY(self) == -1) { |
| 12511 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12512 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12513 | } |
| 12514 | if (PyUnicode_READY(substring) == -1) { |
| 12515 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12516 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12517 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12518 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12519 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12520 | |
| 12521 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12522 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12523 | if (result == -2) |
| 12524 | return NULL; |
| 12525 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12526 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12527 | } |
| 12528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12529 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12530 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12531 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12532 | 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] | 12533 | |
| 12534 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12535 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12536 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12537 | /* initialize variables to prevent gcc warning */ |
| 12538 | PyObject *substring = NULL; |
| 12539 | Py_ssize_t start = 0; |
| 12540 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12541 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12542 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12543 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12544 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12545 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12546 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12547 | if (PyUnicode_READY(self) == -1) { |
| 12548 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12549 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12550 | } |
| 12551 | if (PyUnicode_READY(substring) == -1) { |
| 12552 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12553 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12554 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12555 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12556 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12557 | |
| 12558 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12559 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12560 | if (result == -2) |
| 12561 | return NULL; |
| 12562 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12563 | if (result < 0) { |
| 12564 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12565 | return NULL; |
| 12566 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12567 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12568 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12569 | } |
| 12570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12571 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12572 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12574 | 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] | 12575 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12576 | |
| 12577 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12578 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12579 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12580 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12581 | Py_UCS4 fillchar = ' '; |
| 12582 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12583 | 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] | 12584 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12585 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12586 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12587 | return NULL; |
| 12588 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12589 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12590 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12591 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12592 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12593 | } |
| 12594 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12595 | PyObject * |
| 12596 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12597 | { |
| 12598 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12600 | s = PyUnicode_FromObject(s); |
| 12601 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12602 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12603 | if (sep != NULL) { |
| 12604 | sep = PyUnicode_FromObject(sep); |
| 12605 | if (sep == NULL) { |
| 12606 | Py_DECREF(s); |
| 12607 | return NULL; |
| 12608 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12609 | } |
| 12610 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12611 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12612 | |
| 12613 | Py_DECREF(s); |
| 12614 | Py_XDECREF(sep); |
| 12615 | return result; |
| 12616 | } |
| 12617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12618 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12619 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12620 | \n\ |
| 12621 | Return a list of the words in S, using sep as the\n\ |
| 12622 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12623 | 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] | 12624 | whitespace string is a separator and empty strings are\n\ |
| 12625 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12626 | |
| 12627 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12628 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12629 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12630 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12631 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12632 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12633 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12634 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12635 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12636 | return NULL; |
| 12637 | |
| 12638 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12639 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12640 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12641 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12642 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12643 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12644 | } |
| 12645 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12646 | PyObject * |
| 12647 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12648 | { |
| 12649 | PyObject* str_obj; |
| 12650 | PyObject* sep_obj; |
| 12651 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12652 | int kind1, kind2; |
| 12653 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12654 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12655 | |
| 12656 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12657 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12658 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12659 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12660 | if (!sep_obj) { |
| 12661 | Py_DECREF(str_obj); |
| 12662 | return NULL; |
| 12663 | } |
| 12664 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12665 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12666 | Py_DECREF(str_obj); |
| 12667 | return NULL; |
| 12668 | } |
| 12669 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12670 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12671 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12672 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12673 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12674 | if (kind1 < kind2 || len1 < len2) { |
| 12675 | _Py_INCREF_UNICODE_EMPTY(); |
| 12676 | if (!unicode_empty) |
| 12677 | out = NULL; |
| 12678 | else { |
| 12679 | out = PyTuple_Pack(3, str_obj, unicode_empty, unicode_empty); |
| 12680 | Py_DECREF(unicode_empty); |
| 12681 | } |
| 12682 | Py_DECREF(sep_obj); |
| 12683 | Py_DECREF(str_obj); |
| 12684 | return out; |
| 12685 | } |
| 12686 | buf1 = PyUnicode_DATA(str_obj); |
| 12687 | buf2 = PyUnicode_DATA(sep_obj); |
| 12688 | if (kind2 != kind1) { |
| 12689 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12690 | if (!buf2) |
| 12691 | goto onError; |
| 12692 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12693 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12694 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12695 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12696 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12697 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12698 | else |
| 12699 | 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] | 12700 | break; |
| 12701 | case PyUnicode_2BYTE_KIND: |
| 12702 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12703 | break; |
| 12704 | case PyUnicode_4BYTE_KIND: |
| 12705 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12706 | break; |
| 12707 | default: |
| 12708 | assert(0); |
| 12709 | out = 0; |
| 12710 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12711 | |
| 12712 | Py_DECREF(sep_obj); |
| 12713 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12714 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12715 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12716 | |
| 12717 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12718 | onError: |
| 12719 | Py_DECREF(sep_obj); |
| 12720 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12721 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12722 | PyMem_Free(buf2); |
| 12723 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12724 | } |
| 12725 | |
| 12726 | |
| 12727 | PyObject * |
| 12728 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12729 | { |
| 12730 | PyObject* str_obj; |
| 12731 | PyObject* sep_obj; |
| 12732 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12733 | int kind1, kind2; |
| 12734 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12735 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12736 | |
| 12737 | str_obj = PyUnicode_FromObject(str_in); |
| 12738 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12739 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12740 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12741 | if (!sep_obj) { |
| 12742 | Py_DECREF(str_obj); |
| 12743 | return NULL; |
| 12744 | } |
| 12745 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12746 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12747 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12748 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12749 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12750 | if (kind1 < kind2 || len1 < len2) { |
| 12751 | _Py_INCREF_UNICODE_EMPTY(); |
| 12752 | if (!unicode_empty) |
| 12753 | out = NULL; |
| 12754 | else { |
| 12755 | out = PyTuple_Pack(3, unicode_empty, unicode_empty, str_obj); |
| 12756 | Py_DECREF(unicode_empty); |
| 12757 | } |
| 12758 | Py_DECREF(sep_obj); |
| 12759 | Py_DECREF(str_obj); |
| 12760 | return out; |
| 12761 | } |
| 12762 | buf1 = PyUnicode_DATA(str_obj); |
| 12763 | buf2 = PyUnicode_DATA(sep_obj); |
| 12764 | if (kind2 != kind1) { |
| 12765 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12766 | if (!buf2) |
| 12767 | goto onError; |
| 12768 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12769 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12770 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12771 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12772 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12773 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12774 | else |
| 12775 | 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] | 12776 | break; |
| 12777 | case PyUnicode_2BYTE_KIND: |
| 12778 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12779 | break; |
| 12780 | case PyUnicode_4BYTE_KIND: |
| 12781 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12782 | break; |
| 12783 | default: |
| 12784 | assert(0); |
| 12785 | out = 0; |
| 12786 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12787 | |
| 12788 | Py_DECREF(sep_obj); |
| 12789 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12790 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12791 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12792 | |
| 12793 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12794 | onError: |
| 12795 | Py_DECREF(sep_obj); |
| 12796 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12797 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12798 | PyMem_Free(buf2); |
| 12799 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12800 | } |
| 12801 | |
| 12802 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12803 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12804 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12805 | 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] | 12806 | 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] | 12807 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12808 | |
| 12809 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12810 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12811 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12812 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12813 | } |
| 12814 | |
| 12815 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12816 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12817 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12818 | 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] | 12819 | 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] | 12820 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12821 | |
| 12822 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12823 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12824 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12825 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12826 | } |
| 12827 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12828 | PyObject * |
| 12829 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12830 | { |
| 12831 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12832 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12833 | s = PyUnicode_FromObject(s); |
| 12834 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12835 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12836 | if (sep != NULL) { |
| 12837 | sep = PyUnicode_FromObject(sep); |
| 12838 | if (sep == NULL) { |
| 12839 | Py_DECREF(s); |
| 12840 | return NULL; |
| 12841 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12842 | } |
| 12843 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12844 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12845 | |
| 12846 | Py_DECREF(s); |
| 12847 | Py_XDECREF(sep); |
| 12848 | return result; |
| 12849 | } |
| 12850 | |
| 12851 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12852 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12853 | \n\ |
| 12854 | Return a list of the words in S, using sep as the\n\ |
| 12855 | delimiter string, starting at the end of the string and\n\ |
| 12856 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12857 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12858 | is a separator."); |
| 12859 | |
| 12860 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12861 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12862 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12863 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12864 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12865 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12866 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12867 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12868 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12869 | return NULL; |
| 12870 | |
| 12871 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12872 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12873 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12874 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12875 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12876 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12877 | } |
| 12878 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12879 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12880 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12881 | \n\ |
| 12882 | 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] | 12883 | 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] | 12884 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12885 | |
| 12886 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12887 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12888 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12889 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12890 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12891 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12892 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12893 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12894 | return NULL; |
| 12895 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12896 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12897 | } |
| 12898 | |
| 12899 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12900 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12901 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12902 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12903 | } |
| 12904 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12905 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12906 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12907 | \n\ |
| 12908 | 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] | 12909 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12910 | |
| 12911 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12912 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12913 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12914 | if (PyUnicode_READY(self) == -1) |
| 12915 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12916 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12917 | } |
| 12918 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 12919 | /*[clinic input] |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12920 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12921 | @staticmethod |
| 12922 | str.maketrans as unicode_maketrans |
| 12923 | |
| 12924 | x: object |
| 12925 | |
| 12926 | y: unicode=NULL |
| 12927 | |
| 12928 | z: unicode=NULL |
| 12929 | |
| 12930 | / |
| 12931 | |
| 12932 | Return a translation table usable for str.translate(). |
| 12933 | |
| 12934 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12935 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12936 | Character keys will be then converted to ordinals. |
| 12937 | If there are two arguments, they must be strings of equal length, and |
| 12938 | in the resulting dictionary, each character in x will be mapped to the |
| 12939 | character at the same position in y. If there is a third argument, it |
| 12940 | 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] | 12941 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12942 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12943 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12944 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12945 | /*[clinic end generated code: output=a925c89452bd5881 input=7bfbf529a293c6c5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12946 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12947 | PyObject *new = NULL, *key, *value; |
| 12948 | Py_ssize_t i = 0; |
| 12949 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12950 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12951 | new = PyDict_New(); |
| 12952 | if (!new) |
| 12953 | return NULL; |
| 12954 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12955 | int x_kind, y_kind, z_kind; |
| 12956 | void *x_data, *y_data, *z_data; |
| 12957 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12958 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12959 | if (!PyUnicode_Check(x)) { |
| 12960 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12961 | "be a string if there is a second argument"); |
| 12962 | goto err; |
| 12963 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12964 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12965 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12966 | "arguments must have equal length"); |
| 12967 | goto err; |
| 12968 | } |
| 12969 | /* 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] | 12970 | x_kind = PyUnicode_KIND(x); |
| 12971 | y_kind = PyUnicode_KIND(y); |
| 12972 | x_data = PyUnicode_DATA(x); |
| 12973 | y_data = PyUnicode_DATA(y); |
| 12974 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12975 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12976 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12977 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12978 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12979 | if (!value) { |
| 12980 | Py_DECREF(key); |
| 12981 | goto err; |
| 12982 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12983 | res = PyDict_SetItem(new, key, value); |
| 12984 | Py_DECREF(key); |
| 12985 | Py_DECREF(value); |
| 12986 | if (res < 0) |
| 12987 | goto err; |
| 12988 | } |
| 12989 | /* create entries for deleting chars in z */ |
| 12990 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12991 | z_kind = PyUnicode_KIND(z); |
| 12992 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12993 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12994 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12995 | if (!key) |
| 12996 | goto err; |
| 12997 | res = PyDict_SetItem(new, key, Py_None); |
| 12998 | Py_DECREF(key); |
| 12999 | if (res < 0) |
| 13000 | goto err; |
| 13001 | } |
| 13002 | } |
| 13003 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13004 | int kind; |
| 13005 | void *data; |
| 13006 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13007 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 13008 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13009 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 13010 | "to maketrans it must be a dict"); |
| 13011 | goto err; |
| 13012 | } |
| 13013 | /* copy entries into the new dict, converting string keys to int keys */ |
| 13014 | while (PyDict_Next(x, &i, &key, &value)) { |
| 13015 | if (PyUnicode_Check(key)) { |
| 13016 | /* convert string keys to integer keys */ |
| 13017 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13018 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13019 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13020 | "table must be of length 1"); |
| 13021 | goto err; |
| 13022 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13023 | kind = PyUnicode_KIND(key); |
| 13024 | data = PyUnicode_DATA(key); |
| 13025 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13026 | if (!newkey) |
| 13027 | goto err; |
| 13028 | res = PyDict_SetItem(new, newkey, value); |
| 13029 | Py_DECREF(newkey); |
| 13030 | if (res < 0) |
| 13031 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13032 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13033 | /* just keep integer keys */ |
| 13034 | if (PyDict_SetItem(new, key, value) < 0) |
| 13035 | goto err; |
| 13036 | } else { |
| 13037 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13038 | "be strings or integers"); |
| 13039 | goto err; |
| 13040 | } |
| 13041 | } |
| 13042 | } |
| 13043 | return new; |
| 13044 | err: |
| 13045 | Py_DECREF(new); |
| 13046 | return NULL; |
| 13047 | } |
| 13048 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13049 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13050 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13051 | \n\ |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 13052 | Return a copy of the string S in which each character has been mapped\n\ |
| 13053 | through the given translation table. The table must implement\n\ |
| 13054 | lookup/indexing via __getitem__, for instance a dictionary or list,\n\ |
| 13055 | mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n\ |
| 13056 | this operation raises LookupError, the character is left untouched.\n\ |
| 13057 | Characters mapped to None are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13058 | |
| 13059 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13060 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13061 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13062 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13063 | } |
| 13064 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13065 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13066 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13067 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13068 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13069 | |
| 13070 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13071 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13072 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13073 | if (PyUnicode_READY(self) == -1) |
| 13074 | return NULL; |
| 13075 | if (PyUnicode_IS_ASCII(self)) |
| 13076 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13077 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13078 | } |
| 13079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13080 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13081 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13082 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13083 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13084 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13085 | |
| 13086 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13087 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13088 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13089 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13090 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13091 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13092 | int kind; |
| 13093 | void *data; |
| 13094 | Py_UCS4 chr; |
| 13095 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13096 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13097 | return NULL; |
| 13098 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13099 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13100 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13101 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13102 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13103 | return unicode_result_unchanged(self); |
| 13104 | |
| 13105 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13106 | |
| 13107 | u = pad(self, fill, 0, '0'); |
| 13108 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13109 | if (u == NULL) |
| 13110 | return NULL; |
| 13111 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13112 | kind = PyUnicode_KIND(u); |
| 13113 | data = PyUnicode_DATA(u); |
| 13114 | chr = PyUnicode_READ(kind, data, fill); |
| 13115 | |
| 13116 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13117 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13118 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13119 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13120 | } |
| 13121 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13122 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13123 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13124 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13125 | |
| 13126 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13127 | static PyObject * |
| 13128 | unicode__decimal2ascii(PyObject *self) |
| 13129 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13130 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13131 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13132 | #endif |
| 13133 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13134 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13135 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13136 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13137 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13138 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13139 | With optional end, stop comparing S at that position.\n\ |
| 13140 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13141 | |
| 13142 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13143 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13144 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13145 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13146 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13147 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13148 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13149 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13150 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13151 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13152 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13153 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13154 | if (PyTuple_Check(subobj)) { |
| 13155 | Py_ssize_t i; |
| 13156 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13157 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13158 | if (substring == NULL) |
| 13159 | return NULL; |
| 13160 | result = tailmatch(self, substring, start, end, -1); |
| 13161 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13162 | if (result == -1) |
| 13163 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13164 | if (result) { |
| 13165 | Py_RETURN_TRUE; |
| 13166 | } |
| 13167 | } |
| 13168 | /* nothing matched */ |
| 13169 | Py_RETURN_FALSE; |
| 13170 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13171 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13172 | if (substring == NULL) { |
| 13173 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13174 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13175 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13176 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13177 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13178 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13179 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13180 | if (result == -1) |
| 13181 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13182 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13183 | } |
| 13184 | |
| 13185 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13186 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13187 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13188 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13189 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13190 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13191 | With optional end, stop comparing S at that position.\n\ |
| 13192 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13193 | |
| 13194 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13195 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13196 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13197 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13198 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13199 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13200 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13201 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13202 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13203 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13204 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13205 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13206 | if (PyTuple_Check(subobj)) { |
| 13207 | Py_ssize_t i; |
| 13208 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13209 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13210 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13211 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13212 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13213 | result = tailmatch(self, substring, start, end, +1); |
| 13214 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13215 | if (result == -1) |
| 13216 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13217 | if (result) { |
| 13218 | Py_RETURN_TRUE; |
| 13219 | } |
| 13220 | } |
| 13221 | Py_RETURN_FALSE; |
| 13222 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13223 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13224 | if (substring == NULL) { |
| 13225 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13226 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13227 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13228 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13229 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13230 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13231 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13232 | if (result == -1) |
| 13233 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13234 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13235 | } |
| 13236 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13237 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13238 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13239 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13240 | if (!writer->readonly) |
| 13241 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 13242 | else { |
| 13243 | /* Copy-on-write mode: set buffer size to 0 so |
| 13244 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13245 | * next write. */ |
| 13246 | writer->size = 0; |
| 13247 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13248 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13249 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13250 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13251 | } |
| 13252 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13253 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13254 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13255 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13256 | memset(writer, 0, sizeof(*writer)); |
| 13257 | #ifdef Py_DEBUG |
| 13258 | writer->kind = 5; /* invalid kind */ |
| 13259 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13260 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13261 | } |
| 13262 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13263 | int |
| 13264 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13265 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13266 | { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13267 | #ifdef MS_WINDOWS |
| 13268 | /* On Windows, overallocate by 50% is the best factor */ |
| 13269 | # define OVERALLOCATE_FACTOR 2 |
| 13270 | #else |
| 13271 | /* On Linux, overallocate by 25% is the best factor */ |
| 13272 | # define OVERALLOCATE_FACTOR 4 |
| 13273 | #endif |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13274 | Py_ssize_t newlen; |
| 13275 | PyObject *newbuffer; |
| 13276 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13277 | assert(length > 0); |
| 13278 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13279 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13280 | PyErr_NoMemory(); |
| 13281 | return -1; |
| 13282 | } |
| 13283 | newlen = writer->pos + length; |
| 13284 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13285 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13286 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13287 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13288 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13289 | if (writer->overallocate |
| 13290 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13291 | /* overallocate to limit the number of realloc() */ |
| 13292 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13293 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13294 | if (newlen < writer->min_length) |
| 13295 | newlen = writer->min_length; |
| 13296 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13297 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13298 | if (writer->buffer == NULL) |
| 13299 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13300 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13301 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13302 | if (writer->overallocate |
| 13303 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13304 | /* overallocate to limit the number of realloc() */ |
| 13305 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13306 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13307 | if (newlen < writer->min_length) |
| 13308 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13309 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13310 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13311 | /* resize + widen */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 13312 | maxchar = Py_MAX(maxchar, writer->maxchar); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13313 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13314 | if (newbuffer == NULL) |
| 13315 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13316 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13317 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13318 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13319 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13320 | } |
| 13321 | else { |
| 13322 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13323 | if (newbuffer == NULL) |
| 13324 | return -1; |
| 13325 | } |
| 13326 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13327 | } |
| 13328 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13329 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13330 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13331 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13332 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13333 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13334 | writer->buffer, 0, writer->pos); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 13335 | Py_SETREF(writer->buffer, newbuffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13336 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13337 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13338 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13339 | |
| 13340 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13341 | } |
| 13342 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13343 | Py_LOCAL_INLINE(int) |
| 13344 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13345 | { |
| 13346 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13347 | return -1; |
| 13348 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13349 | writer->pos++; |
| 13350 | return 0; |
| 13351 | } |
| 13352 | |
| 13353 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13354 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13355 | { |
| 13356 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13357 | } |
| 13358 | |
| 13359 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13360 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13361 | { |
| 13362 | Py_UCS4 maxchar; |
| 13363 | Py_ssize_t len; |
| 13364 | |
| 13365 | if (PyUnicode_READY(str) == -1) |
| 13366 | return -1; |
| 13367 | len = PyUnicode_GET_LENGTH(str); |
| 13368 | if (len == 0) |
| 13369 | return 0; |
| 13370 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13371 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13372 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 1912b39 | 2015-03-26 09:37:23 +0100 | [diff] [blame] | 13373 | assert(_PyUnicode_CheckConsistency(str, 1)); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13374 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13375 | Py_INCREF(str); |
| 13376 | writer->buffer = str; |
| 13377 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13378 | writer->pos += len; |
| 13379 | return 0; |
| 13380 | } |
| 13381 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13382 | return -1; |
| 13383 | } |
| 13384 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13385 | str, 0, len); |
| 13386 | writer->pos += len; |
| 13387 | return 0; |
| 13388 | } |
| 13389 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13390 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13391 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13392 | Py_ssize_t start, Py_ssize_t end) |
| 13393 | { |
| 13394 | Py_UCS4 maxchar; |
| 13395 | Py_ssize_t len; |
| 13396 | |
| 13397 | if (PyUnicode_READY(str) == -1) |
| 13398 | return -1; |
| 13399 | |
| 13400 | assert(0 <= start); |
| 13401 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13402 | assert(start <= end); |
| 13403 | |
| 13404 | if (end == 0) |
| 13405 | return 0; |
| 13406 | |
| 13407 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13408 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13409 | |
| 13410 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13411 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13412 | else |
| 13413 | maxchar = writer->maxchar; |
| 13414 | len = end - start; |
| 13415 | |
| 13416 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13417 | return -1; |
| 13418 | |
| 13419 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13420 | str, start, len); |
| 13421 | writer->pos += len; |
| 13422 | return 0; |
| 13423 | } |
| 13424 | |
| 13425 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13426 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13427 | const char *ascii, Py_ssize_t len) |
| 13428 | { |
| 13429 | if (len == -1) |
| 13430 | len = strlen(ascii); |
| 13431 | |
| 13432 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13433 | |
| 13434 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13435 | PyObject *str; |
| 13436 | |
| 13437 | str = _PyUnicode_FromASCII(ascii, len); |
| 13438 | if (str == NULL) |
| 13439 | return -1; |
| 13440 | |
| 13441 | writer->readonly = 1; |
| 13442 | writer->buffer = str; |
| 13443 | _PyUnicodeWriter_Update(writer); |
| 13444 | writer->pos += len; |
| 13445 | return 0; |
| 13446 | } |
| 13447 | |
| 13448 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13449 | return -1; |
| 13450 | |
| 13451 | switch (writer->kind) |
| 13452 | { |
| 13453 | case PyUnicode_1BYTE_KIND: |
| 13454 | { |
| 13455 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13456 | Py_UCS1 *data = writer->data; |
| 13457 | |
| 13458 | Py_MEMCPY(data + writer->pos, str, len); |
| 13459 | break; |
| 13460 | } |
| 13461 | case PyUnicode_2BYTE_KIND: |
| 13462 | { |
| 13463 | _PyUnicode_CONVERT_BYTES( |
| 13464 | Py_UCS1, Py_UCS2, |
| 13465 | ascii, ascii + len, |
| 13466 | (Py_UCS2 *)writer->data + writer->pos); |
| 13467 | break; |
| 13468 | } |
| 13469 | case PyUnicode_4BYTE_KIND: |
| 13470 | { |
| 13471 | _PyUnicode_CONVERT_BYTES( |
| 13472 | Py_UCS1, Py_UCS4, |
| 13473 | ascii, ascii + len, |
| 13474 | (Py_UCS4 *)writer->data + writer->pos); |
| 13475 | break; |
| 13476 | } |
| 13477 | default: |
| 13478 | assert(0); |
| 13479 | } |
| 13480 | |
| 13481 | writer->pos += len; |
| 13482 | return 0; |
| 13483 | } |
| 13484 | |
| 13485 | int |
| 13486 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13487 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13488 | { |
| 13489 | Py_UCS4 maxchar; |
| 13490 | |
| 13491 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13492 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13493 | return -1; |
| 13494 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13495 | writer->pos += len; |
| 13496 | return 0; |
| 13497 | } |
| 13498 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13499 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13500 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13501 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13502 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13503 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13504 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13505 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13506 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13507 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13508 | str = writer->buffer; |
| 13509 | writer->buffer = NULL; |
| 13510 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13511 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13512 | } |
| 13513 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13514 | PyObject *newbuffer; |
| 13515 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13516 | if (newbuffer == NULL) { |
Serhiy Storchaka | dfe98a1 | 2014-02-09 13:46:20 +0200 | [diff] [blame] | 13517 | Py_CLEAR(writer->buffer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13518 | return NULL; |
| 13519 | } |
| 13520 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13521 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13522 | str = writer->buffer; |
| 13523 | writer->buffer = NULL; |
| 13524 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13525 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13526 | } |
| 13527 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13528 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13529 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13530 | { |
| 13531 | Py_CLEAR(writer->buffer); |
| 13532 | } |
| 13533 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13534 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13535 | |
| 13536 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13537 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13538 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13539 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13540 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13541 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13542 | PyDoc_STRVAR(format_map__doc__, |
| 13543 | "S.format_map(mapping) -> str\n\ |
| 13544 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13545 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13546 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13547 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13548 | static PyObject * |
| 13549 | unicode__format__(PyObject* self, PyObject* args) |
| 13550 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13551 | PyObject *format_spec; |
| 13552 | _PyUnicodeWriter writer; |
| 13553 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13554 | |
| 13555 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13556 | return NULL; |
| 13557 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13558 | if (PyUnicode_READY(self) == -1) |
| 13559 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13560 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13561 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13562 | self, format_spec, 0, |
| 13563 | PyUnicode_GET_LENGTH(format_spec)); |
| 13564 | if (ret == -1) { |
| 13565 | _PyUnicodeWriter_Dealloc(&writer); |
| 13566 | return NULL; |
| 13567 | } |
| 13568 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13569 | } |
| 13570 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13571 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13572 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13573 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13574 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13575 | |
| 13576 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13577 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13578 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13579 | Py_ssize_t size; |
| 13580 | |
| 13581 | /* If it's a compact object, account for base structure + |
| 13582 | character data. */ |
| 13583 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13584 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13585 | else if (PyUnicode_IS_COMPACT(v)) |
| 13586 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13587 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13588 | else { |
| 13589 | /* If it is a two-block object, account for base object, and |
| 13590 | for character block if present. */ |
| 13591 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13592 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13593 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13594 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13595 | } |
| 13596 | /* 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] | 13597 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13598 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13599 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13600 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13601 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13602 | |
| 13603 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13604 | } |
| 13605 | |
| 13606 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13607 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13608 | |
| 13609 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13610 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13611 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13612 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13613 | if (!copy) |
| 13614 | return NULL; |
| 13615 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13616 | } |
| 13617 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13618 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13619 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13620 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13621 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13622 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13623 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13624 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13625 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13626 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13627 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13628 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13629 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13630 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13631 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13632 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13633 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13634 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13635 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13636 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13637 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13638 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13639 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13640 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13641 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13642 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13643 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13644 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13645 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13646 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13647 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13648 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13649 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13650 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13651 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13652 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13653 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13654 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13655 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13656 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13657 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13658 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13659 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13660 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13661 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13662 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13663 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13664 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13665 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13666 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13667 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13668 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13669 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13670 | #endif |
| 13671 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13672 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13673 | {NULL, NULL} |
| 13674 | }; |
| 13675 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13676 | static PyObject * |
| 13677 | unicode_mod(PyObject *v, PyObject *w) |
| 13678 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13679 | if (!PyUnicode_Check(v)) |
| 13680 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13681 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13682 | } |
| 13683 | |
| 13684 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13685 | 0, /*nb_add*/ |
| 13686 | 0, /*nb_subtract*/ |
| 13687 | 0, /*nb_multiply*/ |
| 13688 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13689 | }; |
| 13690 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13691 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13692 | (lenfunc) unicode_length, /* sq_length */ |
| 13693 | PyUnicode_Concat, /* sq_concat */ |
| 13694 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13695 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13696 | 0, /* sq_slice */ |
| 13697 | 0, /* sq_ass_item */ |
| 13698 | 0, /* sq_ass_slice */ |
| 13699 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13700 | }; |
| 13701 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13702 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13703 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13704 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13705 | if (PyUnicode_READY(self) == -1) |
| 13706 | return NULL; |
| 13707 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13708 | if (PyIndex_Check(item)) { |
| 13709 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13710 | if (i == -1 && PyErr_Occurred()) |
| 13711 | return NULL; |
| 13712 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13713 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13714 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13715 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13716 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13717 | PyObject *result; |
| 13718 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13719 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13720 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13722 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13723 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13724 | return NULL; |
| 13725 | } |
| 13726 | |
| 13727 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13728 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13729 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13730 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13731 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13732 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13733 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13734 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13735 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13736 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13737 | src_kind = PyUnicode_KIND(self); |
| 13738 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13739 | if (!PyUnicode_IS_ASCII(self)) { |
| 13740 | kind_limit = kind_maxchar_limit(src_kind); |
| 13741 | max_char = 0; |
| 13742 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13743 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13744 | if (ch > max_char) { |
| 13745 | max_char = ch; |
| 13746 | if (max_char >= kind_limit) |
| 13747 | break; |
| 13748 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13749 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13750 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13751 | else |
| 13752 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13753 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13754 | if (result == NULL) |
| 13755 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13756 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13757 | dest_data = PyUnicode_DATA(result); |
| 13758 | |
| 13759 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13760 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13761 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13762 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13763 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13764 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13765 | } else { |
| 13766 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13767 | return NULL; |
| 13768 | } |
| 13769 | } |
| 13770 | |
| 13771 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13772 | (lenfunc)unicode_length, /* mp_length */ |
| 13773 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13774 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13775 | }; |
| 13776 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13777 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13778 | /* Helpers for PyUnicode_Format() */ |
| 13779 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13780 | struct unicode_formatter_t { |
| 13781 | PyObject *args; |
| 13782 | int args_owned; |
| 13783 | Py_ssize_t arglen, argidx; |
| 13784 | PyObject *dict; |
| 13785 | |
| 13786 | enum PyUnicode_Kind fmtkind; |
| 13787 | Py_ssize_t fmtcnt, fmtpos; |
| 13788 | void *fmtdata; |
| 13789 | PyObject *fmtstr; |
| 13790 | |
| 13791 | _PyUnicodeWriter writer; |
| 13792 | }; |
| 13793 | |
| 13794 | struct unicode_format_arg_t { |
| 13795 | Py_UCS4 ch; |
| 13796 | int flags; |
| 13797 | Py_ssize_t width; |
| 13798 | int prec; |
| 13799 | int sign; |
| 13800 | }; |
| 13801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13802 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13803 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13804 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13805 | Py_ssize_t argidx = ctx->argidx; |
| 13806 | |
| 13807 | if (argidx < ctx->arglen) { |
| 13808 | ctx->argidx++; |
| 13809 | if (ctx->arglen < 0) |
| 13810 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13811 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13812 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13813 | } |
| 13814 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13815 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13816 | return NULL; |
| 13817 | } |
| 13818 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13819 | /* 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] | 13820 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13821 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13822 | otherwise. |
| 13823 | |
| 13824 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13825 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13826 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13827 | PyObject **p_output, |
| 13828 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13829 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13830 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13831 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13832 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13833 | int prec; |
| 13834 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13835 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13836 | x = PyFloat_AsDouble(v); |
| 13837 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13838 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13839 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13840 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13841 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13842 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13843 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13844 | if (arg->flags & F_ALT) |
| 13845 | dtoa_flags = Py_DTSF_ALT; |
| 13846 | else |
| 13847 | dtoa_flags = 0; |
| 13848 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13849 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13850 | return -1; |
| 13851 | len = strlen(p); |
| 13852 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13853 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13854 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13855 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13856 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13857 | } |
| 13858 | else |
| 13859 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13860 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13861 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13862 | } |
| 13863 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13864 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13865 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13866 | * Python's regular ints. |
| 13867 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13868 | * The output string is of the form |
| 13869 | * "-"? ("0x" | "0X")? digit+ |
| 13870 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13871 | * set in flags. The case of hex digits will be correct, |
| 13872 | * There will be at least prec digits, zero-filled on the left if |
| 13873 | * necessary to get that many. |
| 13874 | * val object to be converted |
| 13875 | * flags bitmask of format flags; only F_ALT is looked at |
| 13876 | * prec minimum number of digits; 0-fill on left if needed |
| 13877 | * type a character in [duoxX]; u acts the same as d |
| 13878 | * |
| 13879 | * CAUTION: o, x and X conversions on regular ints can never |
| 13880 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13881 | */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13882 | PyObject * |
| 13883 | _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13884 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13885 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13886 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13887 | Py_ssize_t i; |
| 13888 | int sign; /* 1 if '-', else 0 */ |
| 13889 | int len; /* number of characters */ |
| 13890 | Py_ssize_t llen; |
| 13891 | int numdigits; /* len == numnondigits + numdigits */ |
| 13892 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13893 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13894 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13895 | if (prec > INT_MAX-3) { |
| 13896 | PyErr_SetString(PyExc_OverflowError, |
| 13897 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13898 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13899 | } |
| 13900 | |
| 13901 | assert(PyLong_Check(val)); |
| 13902 | |
| 13903 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13904 | default: |
| 13905 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13906 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13907 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13908 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13909 | /* int and int subclasses should print numerically when a numeric */ |
| 13910 | /* format code is used (see issue18780) */ |
| 13911 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13912 | break; |
| 13913 | case 'o': |
| 13914 | numnondigits = 2; |
| 13915 | result = PyNumber_ToBase(val, 8); |
| 13916 | break; |
| 13917 | case 'x': |
| 13918 | case 'X': |
| 13919 | numnondigits = 2; |
| 13920 | result = PyNumber_ToBase(val, 16); |
| 13921 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13922 | } |
| 13923 | if (!result) |
| 13924 | return NULL; |
| 13925 | |
| 13926 | assert(unicode_modifiable(result)); |
| 13927 | assert(PyUnicode_IS_READY(result)); |
| 13928 | assert(PyUnicode_IS_ASCII(result)); |
| 13929 | |
| 13930 | /* To modify the string in-place, there can only be one reference. */ |
| 13931 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13932 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13933 | PyErr_BadInternalCall(); |
| 13934 | return NULL; |
| 13935 | } |
| 13936 | buf = PyUnicode_DATA(result); |
| 13937 | llen = PyUnicode_GET_LENGTH(result); |
| 13938 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13939 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13940 | PyErr_SetString(PyExc_ValueError, |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13941 | "string too large in _PyUnicode_FormatLong"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13942 | return NULL; |
| 13943 | } |
| 13944 | len = (int)llen; |
| 13945 | sign = buf[0] == '-'; |
| 13946 | numnondigits += sign; |
| 13947 | numdigits = len - numnondigits; |
| 13948 | assert(numdigits > 0); |
| 13949 | |
| 13950 | /* Get rid of base marker unless F_ALT */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13951 | if (((alt) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13952 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13953 | assert(buf[sign] == '0'); |
| 13954 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13955 | buf[sign+1] == 'o'); |
| 13956 | numnondigits -= 2; |
| 13957 | buf += 2; |
| 13958 | len -= 2; |
| 13959 | if (sign) |
| 13960 | buf[0] = '-'; |
| 13961 | assert(len == numnondigits + numdigits); |
| 13962 | assert(numdigits > 0); |
| 13963 | } |
| 13964 | |
| 13965 | /* Fill with leading zeroes to meet minimum width. */ |
| 13966 | if (prec > numdigits) { |
| 13967 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13968 | numnondigits + prec); |
| 13969 | char *b1; |
| 13970 | if (!r1) { |
| 13971 | Py_DECREF(result); |
| 13972 | return NULL; |
| 13973 | } |
| 13974 | b1 = PyBytes_AS_STRING(r1); |
| 13975 | for (i = 0; i < numnondigits; ++i) |
| 13976 | *b1++ = *buf++; |
| 13977 | for (i = 0; i < prec - numdigits; i++) |
| 13978 | *b1++ = '0'; |
| 13979 | for (i = 0; i < numdigits; i++) |
| 13980 | *b1++ = *buf++; |
| 13981 | *b1 = '\0'; |
| 13982 | Py_DECREF(result); |
| 13983 | result = r1; |
| 13984 | buf = PyBytes_AS_STRING(result); |
| 13985 | len = numnondigits + prec; |
| 13986 | } |
| 13987 | |
| 13988 | /* Fix up case for hex conversions. */ |
| 13989 | if (type == 'X') { |
| 13990 | /* Need to convert all lower case letters to upper case. |
| 13991 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13992 | for (i = 0; i < len; i++) |
| 13993 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13994 | buf[i] -= 'a'-'A'; |
| 13995 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13996 | if (!PyUnicode_Check(result) |
| 13997 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13998 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13999 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14000 | Py_DECREF(result); |
| 14001 | result = unicode; |
| 14002 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14003 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 14004 | if (PyUnicode_Resize(&result, len) < 0) |
| 14005 | Py_CLEAR(result); |
| 14006 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14007 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14008 | } |
| 14009 | |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14010 | /* Format an integer or a float as an integer. |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14011 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14012 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14013 | * -1 and raise an exception on error */ |
| 14014 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14015 | mainformatlong(PyObject *v, |
| 14016 | struct unicode_format_arg_t *arg, |
| 14017 | PyObject **p_output, |
| 14018 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14019 | { |
| 14020 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14021 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14022 | |
| 14023 | if (!PyNumber_Check(v)) |
| 14024 | goto wrongtype; |
| 14025 | |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14026 | /* 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] | 14027 | if (!PyLong_Check(v)) { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14028 | if (type == 'o' || type == 'x' || type == 'X') { |
| 14029 | iobj = PyNumber_Index(v); |
| 14030 | if (iobj == NULL) { |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14031 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14032 | goto wrongtype; |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14033 | return -1; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14034 | } |
| 14035 | } |
| 14036 | else { |
| 14037 | iobj = PyNumber_Long(v); |
| 14038 | if (iobj == NULL ) { |
| 14039 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14040 | goto wrongtype; |
| 14041 | return -1; |
| 14042 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14043 | } |
| 14044 | assert(PyLong_Check(iobj)); |
| 14045 | } |
| 14046 | else { |
| 14047 | iobj = v; |
| 14048 | Py_INCREF(iobj); |
| 14049 | } |
| 14050 | |
| 14051 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14052 | && arg->width == -1 && arg->prec == -1 |
| 14053 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14054 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14055 | { |
| 14056 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14057 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14058 | int base; |
| 14059 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14060 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14061 | { |
| 14062 | default: |
| 14063 | assert(0 && "'type' not in [diuoxX]"); |
| 14064 | case 'd': |
| 14065 | case 'i': |
| 14066 | case 'u': |
| 14067 | base = 10; |
| 14068 | break; |
| 14069 | case 'o': |
| 14070 | base = 8; |
| 14071 | break; |
| 14072 | case 'x': |
| 14073 | case 'X': |
| 14074 | base = 16; |
| 14075 | break; |
| 14076 | } |
| 14077 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14078 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14079 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14080 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14081 | } |
| 14082 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14083 | return 1; |
| 14084 | } |
| 14085 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14086 | res = _PyUnicode_FormatLong(iobj, arg->flags & F_ALT, arg->prec, type); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14087 | Py_DECREF(iobj); |
| 14088 | if (res == NULL) |
| 14089 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14090 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14091 | return 0; |
| 14092 | |
| 14093 | wrongtype: |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14094 | switch(type) |
| 14095 | { |
| 14096 | case 'o': |
| 14097 | case 'x': |
| 14098 | case 'X': |
| 14099 | PyErr_Format(PyExc_TypeError, |
| 14100 | "%%%c format: an integer is required, " |
| 14101 | "not %.200s", |
| 14102 | type, Py_TYPE(v)->tp_name); |
| 14103 | break; |
| 14104 | default: |
| 14105 | PyErr_Format(PyExc_TypeError, |
| 14106 | "%%%c format: a number is required, " |
| 14107 | "not %.200s", |
| 14108 | type, Py_TYPE(v)->tp_name); |
| 14109 | break; |
| 14110 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14111 | return -1; |
| 14112 | } |
| 14113 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14114 | static Py_UCS4 |
| 14115 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14116 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14117 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14118 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14119 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14120 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14121 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14122 | goto onError; |
| 14123 | } |
| 14124 | else { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14125 | PyObject *iobj; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14126 | long x; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14127 | /* make sure number is a type of integer */ |
| 14128 | if (!PyLong_Check(v)) { |
| 14129 | iobj = PyNumber_Index(v); |
| 14130 | if (iobj == NULL) { |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14131 | goto onError; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14132 | } |
| 14133 | v = iobj; |
| 14134 | Py_DECREF(iobj); |
| 14135 | } |
| 14136 | /* Integer input truncated to a character */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14137 | x = PyLong_AsLong(v); |
| 14138 | if (x == -1 && PyErr_Occurred()) |
| 14139 | goto onError; |
| 14140 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14141 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14142 | PyErr_SetString(PyExc_OverflowError, |
| 14143 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14144 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14145 | } |
| 14146 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14147 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14148 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14149 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14150 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14151 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14152 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14153 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14154 | } |
| 14155 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14156 | /* Parse options of an argument: flags, width, precision. |
| 14157 | Handle also "%(name)" syntax. |
| 14158 | |
| 14159 | Return 0 if the argument has been formatted into arg->str. |
| 14160 | Return 1 if the argument has been written into ctx->writer, |
| 14161 | Raise an exception and return -1 on error. */ |
| 14162 | static int |
| 14163 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14164 | struct unicode_format_arg_t *arg) |
| 14165 | { |
| 14166 | #define FORMAT_READ(ctx) \ |
| 14167 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14168 | |
| 14169 | PyObject *v; |
| 14170 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14171 | if (arg->ch == '(') { |
| 14172 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14173 | Py_ssize_t keystart; |
| 14174 | Py_ssize_t keylen; |
| 14175 | PyObject *key; |
| 14176 | int pcount = 1; |
| 14177 | |
| 14178 | if (ctx->dict == NULL) { |
| 14179 | PyErr_SetString(PyExc_TypeError, |
| 14180 | "format requires a mapping"); |
| 14181 | return -1; |
| 14182 | } |
| 14183 | ++ctx->fmtpos; |
| 14184 | --ctx->fmtcnt; |
| 14185 | keystart = ctx->fmtpos; |
| 14186 | /* Skip over balanced parentheses */ |
| 14187 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14188 | arg->ch = FORMAT_READ(ctx); |
| 14189 | if (arg->ch == ')') |
| 14190 | --pcount; |
| 14191 | else if (arg->ch == '(') |
| 14192 | ++pcount; |
| 14193 | ctx->fmtpos++; |
| 14194 | } |
| 14195 | keylen = ctx->fmtpos - keystart - 1; |
| 14196 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14197 | PyErr_SetString(PyExc_ValueError, |
| 14198 | "incomplete format key"); |
| 14199 | return -1; |
| 14200 | } |
| 14201 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14202 | keystart, keystart + keylen); |
| 14203 | if (key == NULL) |
| 14204 | return -1; |
| 14205 | if (ctx->args_owned) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14206 | ctx->args_owned = 0; |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 14207 | Py_DECREF(ctx->args); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14208 | } |
| 14209 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14210 | Py_DECREF(key); |
| 14211 | if (ctx->args == NULL) |
| 14212 | return -1; |
| 14213 | ctx->args_owned = 1; |
| 14214 | ctx->arglen = -1; |
| 14215 | ctx->argidx = -2; |
| 14216 | } |
| 14217 | |
| 14218 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14219 | while (--ctx->fmtcnt >= 0) { |
| 14220 | arg->ch = FORMAT_READ(ctx); |
| 14221 | ctx->fmtpos++; |
| 14222 | switch (arg->ch) { |
| 14223 | case '-': arg->flags |= F_LJUST; continue; |
| 14224 | case '+': arg->flags |= F_SIGN; continue; |
| 14225 | case ' ': arg->flags |= F_BLANK; continue; |
| 14226 | case '#': arg->flags |= F_ALT; continue; |
| 14227 | case '0': arg->flags |= F_ZERO; continue; |
| 14228 | } |
| 14229 | break; |
| 14230 | } |
| 14231 | |
| 14232 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14233 | if (arg->ch == '*') { |
| 14234 | v = unicode_format_getnextarg(ctx); |
| 14235 | if (v == NULL) |
| 14236 | return -1; |
| 14237 | if (!PyLong_Check(v)) { |
| 14238 | PyErr_SetString(PyExc_TypeError, |
| 14239 | "* wants int"); |
| 14240 | return -1; |
| 14241 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14242 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14243 | if (arg->width == -1 && PyErr_Occurred()) |
| 14244 | return -1; |
| 14245 | if (arg->width < 0) { |
| 14246 | arg->flags |= F_LJUST; |
| 14247 | arg->width = -arg->width; |
| 14248 | } |
| 14249 | if (--ctx->fmtcnt >= 0) { |
| 14250 | arg->ch = FORMAT_READ(ctx); |
| 14251 | ctx->fmtpos++; |
| 14252 | } |
| 14253 | } |
| 14254 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14255 | arg->width = arg->ch - '0'; |
| 14256 | while (--ctx->fmtcnt >= 0) { |
| 14257 | arg->ch = FORMAT_READ(ctx); |
| 14258 | ctx->fmtpos++; |
| 14259 | if (arg->ch < '0' || arg->ch > '9') |
| 14260 | break; |
| 14261 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14262 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14263 | '0' and '9', casting to int is safe. */ |
| 14264 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14265 | PyErr_SetString(PyExc_ValueError, |
| 14266 | "width too big"); |
| 14267 | return -1; |
| 14268 | } |
| 14269 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14270 | } |
| 14271 | } |
| 14272 | |
| 14273 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14274 | if (arg->ch == '.') { |
| 14275 | arg->prec = 0; |
| 14276 | if (--ctx->fmtcnt >= 0) { |
| 14277 | arg->ch = FORMAT_READ(ctx); |
| 14278 | ctx->fmtpos++; |
| 14279 | } |
| 14280 | if (arg->ch == '*') { |
| 14281 | v = unicode_format_getnextarg(ctx); |
| 14282 | if (v == NULL) |
| 14283 | return -1; |
| 14284 | if (!PyLong_Check(v)) { |
| 14285 | PyErr_SetString(PyExc_TypeError, |
| 14286 | "* wants int"); |
| 14287 | return -1; |
| 14288 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14289 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14290 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14291 | return -1; |
| 14292 | if (arg->prec < 0) |
| 14293 | arg->prec = 0; |
| 14294 | if (--ctx->fmtcnt >= 0) { |
| 14295 | arg->ch = FORMAT_READ(ctx); |
| 14296 | ctx->fmtpos++; |
| 14297 | } |
| 14298 | } |
| 14299 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14300 | arg->prec = arg->ch - '0'; |
| 14301 | while (--ctx->fmtcnt >= 0) { |
| 14302 | arg->ch = FORMAT_READ(ctx); |
| 14303 | ctx->fmtpos++; |
| 14304 | if (arg->ch < '0' || arg->ch > '9') |
| 14305 | break; |
| 14306 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14307 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14308 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14309 | return -1; |
| 14310 | } |
| 14311 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14312 | } |
| 14313 | } |
| 14314 | } |
| 14315 | |
| 14316 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14317 | if (ctx->fmtcnt >= 0) { |
| 14318 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14319 | if (--ctx->fmtcnt >= 0) { |
| 14320 | arg->ch = FORMAT_READ(ctx); |
| 14321 | ctx->fmtpos++; |
| 14322 | } |
| 14323 | } |
| 14324 | } |
| 14325 | if (ctx->fmtcnt < 0) { |
| 14326 | PyErr_SetString(PyExc_ValueError, |
| 14327 | "incomplete format"); |
| 14328 | return -1; |
| 14329 | } |
| 14330 | return 0; |
| 14331 | |
| 14332 | #undef FORMAT_READ |
| 14333 | } |
| 14334 | |
| 14335 | /* Format one argument. Supported conversion specifiers: |
| 14336 | |
| 14337 | - "s", "r", "a": any type |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14338 | - "i", "d", "u": int or float |
| 14339 | - "o", "x", "X": int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14340 | - "e", "E", "f", "F", "g", "G": float |
| 14341 | - "c": int or str (1 character) |
| 14342 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14343 | When possible, the output is written directly into the Unicode writer |
| 14344 | (ctx->writer). A string is created when padding is required. |
| 14345 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14346 | Return 0 if the argument has been formatted into *p_str, |
| 14347 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14348 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14349 | static int |
| 14350 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14351 | struct unicode_format_arg_t *arg, |
| 14352 | PyObject **p_str) |
| 14353 | { |
| 14354 | PyObject *v; |
| 14355 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14356 | |
| 14357 | if (ctx->fmtcnt == 0) |
| 14358 | ctx->writer.overallocate = 0; |
| 14359 | |
| 14360 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14361 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14362 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14363 | return 1; |
| 14364 | } |
| 14365 | |
| 14366 | v = unicode_format_getnextarg(ctx); |
| 14367 | if (v == NULL) |
| 14368 | return -1; |
| 14369 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14370 | |
| 14371 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14372 | case 's': |
| 14373 | case 'r': |
| 14374 | case 'a': |
| 14375 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14376 | /* Fast path */ |
| 14377 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14378 | return -1; |
| 14379 | return 1; |
| 14380 | } |
| 14381 | |
| 14382 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14383 | *p_str = v; |
| 14384 | Py_INCREF(*p_str); |
| 14385 | } |
| 14386 | else { |
| 14387 | if (arg->ch == 's') |
| 14388 | *p_str = PyObject_Str(v); |
| 14389 | else if (arg->ch == 'r') |
| 14390 | *p_str = PyObject_Repr(v); |
| 14391 | else |
| 14392 | *p_str = PyObject_ASCII(v); |
| 14393 | } |
| 14394 | break; |
| 14395 | |
| 14396 | case 'i': |
| 14397 | case 'd': |
| 14398 | case 'u': |
| 14399 | case 'o': |
| 14400 | case 'x': |
| 14401 | case 'X': |
| 14402 | { |
| 14403 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14404 | if (ret != 0) |
| 14405 | return ret; |
| 14406 | arg->sign = 1; |
| 14407 | break; |
| 14408 | } |
| 14409 | |
| 14410 | case 'e': |
| 14411 | case 'E': |
| 14412 | case 'f': |
| 14413 | case 'F': |
| 14414 | case 'g': |
| 14415 | case 'G': |
| 14416 | if (arg->width == -1 && arg->prec == -1 |
| 14417 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14418 | { |
| 14419 | /* Fast path */ |
| 14420 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14421 | return -1; |
| 14422 | return 1; |
| 14423 | } |
| 14424 | |
| 14425 | arg->sign = 1; |
| 14426 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14427 | return -1; |
| 14428 | break; |
| 14429 | |
| 14430 | case 'c': |
| 14431 | { |
| 14432 | Py_UCS4 ch = formatchar(v); |
| 14433 | if (ch == (Py_UCS4) -1) |
| 14434 | return -1; |
| 14435 | if (arg->width == -1 && arg->prec == -1) { |
| 14436 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14437 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14438 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14439 | return 1; |
| 14440 | } |
| 14441 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14442 | break; |
| 14443 | } |
| 14444 | |
| 14445 | default: |
| 14446 | PyErr_Format(PyExc_ValueError, |
| 14447 | "unsupported format character '%c' (0x%x) " |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 14448 | "at index %zd", |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14449 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14450 | (int)arg->ch, |
| 14451 | ctx->fmtpos - 1); |
| 14452 | return -1; |
| 14453 | } |
| 14454 | if (*p_str == NULL) |
| 14455 | return -1; |
| 14456 | assert (PyUnicode_Check(*p_str)); |
| 14457 | return 0; |
| 14458 | } |
| 14459 | |
| 14460 | static int |
| 14461 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14462 | struct unicode_format_arg_t *arg, |
| 14463 | PyObject *str) |
| 14464 | { |
| 14465 | Py_ssize_t len; |
| 14466 | enum PyUnicode_Kind kind; |
| 14467 | void *pbuf; |
| 14468 | Py_ssize_t pindex; |
| 14469 | Py_UCS4 signchar; |
| 14470 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14471 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14472 | Py_ssize_t sublen; |
| 14473 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14474 | Py_UCS4 fill; |
| 14475 | |
| 14476 | fill = ' '; |
| 14477 | if (arg->sign && arg->flags & F_ZERO) |
| 14478 | fill = '0'; |
| 14479 | |
| 14480 | if (PyUnicode_READY(str) == -1) |
| 14481 | return -1; |
| 14482 | |
| 14483 | len = PyUnicode_GET_LENGTH(str); |
| 14484 | if ((arg->width == -1 || arg->width <= len) |
| 14485 | && (arg->prec == -1 || arg->prec >= len) |
| 14486 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14487 | { |
| 14488 | /* Fast path */ |
| 14489 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14490 | return -1; |
| 14491 | return 0; |
| 14492 | } |
| 14493 | |
| 14494 | /* Truncate the string for "s", "r" and "a" formats |
| 14495 | if the precision is set */ |
| 14496 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14497 | if (arg->prec >= 0 && len > arg->prec) |
| 14498 | len = arg->prec; |
| 14499 | } |
| 14500 | |
| 14501 | /* Adjust sign and width */ |
| 14502 | kind = PyUnicode_KIND(str); |
| 14503 | pbuf = PyUnicode_DATA(str); |
| 14504 | pindex = 0; |
| 14505 | signchar = '\0'; |
| 14506 | if (arg->sign) { |
| 14507 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14508 | if (ch == '-' || ch == '+') { |
| 14509 | signchar = ch; |
| 14510 | len--; |
| 14511 | pindex++; |
| 14512 | } |
| 14513 | else if (arg->flags & F_SIGN) |
| 14514 | signchar = '+'; |
| 14515 | else if (arg->flags & F_BLANK) |
| 14516 | signchar = ' '; |
| 14517 | else |
| 14518 | arg->sign = 0; |
| 14519 | } |
| 14520 | if (arg->width < len) |
| 14521 | arg->width = len; |
| 14522 | |
| 14523 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14524 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14525 | if (!(arg->flags & F_LJUST)) { |
| 14526 | if (arg->sign) { |
| 14527 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14528 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14529 | } |
| 14530 | else { |
| 14531 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14532 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14533 | } |
| 14534 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14535 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14536 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14537 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14538 | } |
| 14539 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14540 | buflen = arg->width; |
| 14541 | if (arg->sign && len == arg->width) |
| 14542 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14543 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14544 | return -1; |
| 14545 | |
| 14546 | /* Write the sign if needed */ |
| 14547 | if (arg->sign) { |
| 14548 | if (fill != ' ') { |
| 14549 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14550 | writer->pos += 1; |
| 14551 | } |
| 14552 | if (arg->width > len) |
| 14553 | arg->width--; |
| 14554 | } |
| 14555 | |
| 14556 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14557 | if the alternate form is used. |
| 14558 | For example, write "0x" for the "%#x" format. */ |
| 14559 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14560 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14561 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14562 | if (fill != ' ') { |
| 14563 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14564 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14565 | writer->pos += 2; |
| 14566 | pindex += 2; |
| 14567 | } |
| 14568 | arg->width -= 2; |
| 14569 | if (arg->width < 0) |
| 14570 | arg->width = 0; |
| 14571 | len -= 2; |
| 14572 | } |
| 14573 | |
| 14574 | /* Pad left with the fill character if needed */ |
| 14575 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14576 | sublen = arg->width - len; |
| 14577 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14578 | writer->pos += sublen; |
| 14579 | arg->width = len; |
| 14580 | } |
| 14581 | |
| 14582 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14583 | the alternate form is used */ |
| 14584 | if (fill == ' ') { |
| 14585 | if (arg->sign) { |
| 14586 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14587 | writer->pos += 1; |
| 14588 | } |
| 14589 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14590 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14591 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14592 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14593 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14594 | writer->pos += 2; |
| 14595 | pindex += 2; |
| 14596 | } |
| 14597 | } |
| 14598 | |
| 14599 | /* Write characters */ |
| 14600 | if (len) { |
| 14601 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14602 | str, pindex, len); |
| 14603 | writer->pos += len; |
| 14604 | } |
| 14605 | |
| 14606 | /* Pad right with the fill character if needed */ |
| 14607 | if (arg->width > len) { |
| 14608 | sublen = arg->width - len; |
| 14609 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14610 | writer->pos += sublen; |
| 14611 | } |
| 14612 | return 0; |
| 14613 | } |
| 14614 | |
| 14615 | /* Helper of PyUnicode_Format(): format one arg. |
| 14616 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14617 | static int |
| 14618 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14619 | { |
| 14620 | struct unicode_format_arg_t arg; |
| 14621 | PyObject *str; |
| 14622 | int ret; |
| 14623 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14624 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14625 | arg.flags = 0; |
| 14626 | arg.width = -1; |
| 14627 | arg.prec = -1; |
| 14628 | arg.sign = 0; |
| 14629 | str = NULL; |
| 14630 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14631 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14632 | if (ret == -1) |
| 14633 | return -1; |
| 14634 | |
| 14635 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14636 | if (ret == -1) |
| 14637 | return -1; |
| 14638 | |
| 14639 | if (ret != 1) { |
| 14640 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14641 | Py_DECREF(str); |
| 14642 | if (ret == -1) |
| 14643 | return -1; |
| 14644 | } |
| 14645 | |
| 14646 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14647 | PyErr_SetString(PyExc_TypeError, |
| 14648 | "not all arguments converted during string formatting"); |
| 14649 | return -1; |
| 14650 | } |
| 14651 | return 0; |
| 14652 | } |
| 14653 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14654 | PyObject * |
| 14655 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14656 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14657 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14659 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14660 | PyErr_BadInternalCall(); |
| 14661 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14662 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14663 | |
| 14664 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14665 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14666 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14667 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14668 | Py_DECREF(ctx.fmtstr); |
| 14669 | return NULL; |
| 14670 | } |
| 14671 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14672 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14673 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14674 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14675 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14676 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14677 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14678 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14679 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14680 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14681 | ctx.arglen = PyTuple_Size(args); |
| 14682 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14683 | } |
| 14684 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14685 | ctx.arglen = -1; |
| 14686 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14687 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14688 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14689 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14690 | ctx.dict = args; |
| 14691 | else |
| 14692 | ctx.dict = NULL; |
| 14693 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14694 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14695 | while (--ctx.fmtcnt >= 0) { |
| 14696 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14697 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14698 | |
| 14699 | nonfmtpos = ctx.fmtpos++; |
| 14700 | while (ctx.fmtcnt >= 0 && |
| 14701 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14702 | ctx.fmtpos++; |
| 14703 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14704 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14705 | if (ctx.fmtcnt < 0) { |
| 14706 | ctx.fmtpos--; |
| 14707 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14708 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14709 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14710 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14711 | nonfmtpos, ctx.fmtpos) < 0) |
| 14712 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14713 | } |
| 14714 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14715 | ctx.fmtpos++; |
| 14716 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14717 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14718 | } |
| 14719 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14720 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14721 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14722 | PyErr_SetString(PyExc_TypeError, |
| 14723 | "not all arguments converted during string formatting"); |
| 14724 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14725 | } |
| 14726 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14727 | if (ctx.args_owned) { |
| 14728 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14729 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14730 | Py_DECREF(ctx.fmtstr); |
| 14731 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14732 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14733 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14734 | Py_DECREF(ctx.fmtstr); |
| 14735 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14736 | if (ctx.args_owned) { |
| 14737 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14738 | } |
| 14739 | return NULL; |
| 14740 | } |
| 14741 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14742 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14743 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14744 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14745 | static PyObject * |
| 14746 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14747 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14748 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14749 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14750 | char *encoding = NULL; |
| 14751 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14752 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14753 | if (type != &PyUnicode_Type) |
| 14754 | return unicode_subtype_new(type, args, kwds); |
| 14755 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14756 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14757 | return NULL; |
| 14758 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14759 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14760 | if (encoding == NULL && errors == NULL) |
| 14761 | return PyObject_Str(x); |
| 14762 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14763 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14764 | } |
| 14765 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14766 | static PyObject * |
| 14767 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14768 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14769 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14770 | Py_ssize_t length, char_size; |
| 14771 | int share_wstr, share_utf8; |
| 14772 | unsigned int kind; |
| 14773 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14774 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14775 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14776 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14777 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14778 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14779 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14780 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14781 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14782 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14783 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14784 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14785 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14786 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14787 | if (self == NULL) { |
| 14788 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14789 | return NULL; |
| 14790 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14791 | kind = PyUnicode_KIND(unicode); |
| 14792 | length = PyUnicode_GET_LENGTH(unicode); |
| 14793 | |
| 14794 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14795 | #ifdef Py_DEBUG |
| 14796 | _PyUnicode_HASH(self) = -1; |
| 14797 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14798 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14799 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14800 | _PyUnicode_STATE(self).interned = 0; |
| 14801 | _PyUnicode_STATE(self).kind = kind; |
| 14802 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14803 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14804 | _PyUnicode_STATE(self).ready = 1; |
| 14805 | _PyUnicode_WSTR(self) = NULL; |
| 14806 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14807 | _PyUnicode_UTF8(self) = NULL; |
| 14808 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14809 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14810 | |
| 14811 | share_utf8 = 0; |
| 14812 | share_wstr = 0; |
| 14813 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14814 | char_size = 1; |
| 14815 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14816 | share_utf8 = 1; |
| 14817 | } |
| 14818 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14819 | char_size = 2; |
| 14820 | if (sizeof(wchar_t) == 2) |
| 14821 | share_wstr = 1; |
| 14822 | } |
| 14823 | else { |
| 14824 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14825 | char_size = 4; |
| 14826 | if (sizeof(wchar_t) == 4) |
| 14827 | share_wstr = 1; |
| 14828 | } |
| 14829 | |
| 14830 | /* Ensure we won't overflow the length. */ |
| 14831 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14832 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14833 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14834 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14835 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14836 | if (data == NULL) { |
| 14837 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14838 | goto onError; |
| 14839 | } |
| 14840 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14841 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14842 | if (share_utf8) { |
| 14843 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14844 | _PyUnicode_UTF8(self) = data; |
| 14845 | } |
| 14846 | if (share_wstr) { |
| 14847 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14848 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14849 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14850 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14851 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14852 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14853 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14854 | #ifdef Py_DEBUG |
| 14855 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14856 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14857 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14858 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14859 | |
| 14860 | onError: |
| 14861 | Py_DECREF(unicode); |
| 14862 | Py_DECREF(self); |
| 14863 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14864 | } |
| 14865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14866 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14867 | "str(object='') -> str\n\ |
| 14868 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14869 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14870 | Create a new string object from the given object. If encoding or\n\ |
| 14871 | errors is specified, then the object must expose a data buffer\n\ |
| 14872 | that will be decoded using the given encoding and error handler.\n\ |
| 14873 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14874 | or repr(object).\n\ |
| 14875 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14876 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14877 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14878 | static PyObject *unicode_iter(PyObject *seq); |
| 14879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14880 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14881 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14882 | "str", /* tp_name */ |
| 14883 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14884 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14885 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14886 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14887 | 0, /* tp_print */ |
| 14888 | 0, /* tp_getattr */ |
| 14889 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14890 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14891 | unicode_repr, /* tp_repr */ |
| 14892 | &unicode_as_number, /* tp_as_number */ |
| 14893 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14894 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14895 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14896 | 0, /* tp_call*/ |
| 14897 | (reprfunc) unicode_str, /* tp_str */ |
| 14898 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14899 | 0, /* tp_setattro */ |
| 14900 | 0, /* tp_as_buffer */ |
| 14901 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14902 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14903 | unicode_doc, /* tp_doc */ |
| 14904 | 0, /* tp_traverse */ |
| 14905 | 0, /* tp_clear */ |
| 14906 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14907 | 0, /* tp_weaklistoffset */ |
| 14908 | unicode_iter, /* tp_iter */ |
| 14909 | 0, /* tp_iternext */ |
| 14910 | unicode_methods, /* tp_methods */ |
| 14911 | 0, /* tp_members */ |
| 14912 | 0, /* tp_getset */ |
| 14913 | &PyBaseObject_Type, /* tp_base */ |
| 14914 | 0, /* tp_dict */ |
| 14915 | 0, /* tp_descr_get */ |
| 14916 | 0, /* tp_descr_set */ |
| 14917 | 0, /* tp_dictoffset */ |
| 14918 | 0, /* tp_init */ |
| 14919 | 0, /* tp_alloc */ |
| 14920 | unicode_new, /* tp_new */ |
| 14921 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14922 | }; |
| 14923 | |
| 14924 | /* Initialize the Unicode implementation */ |
| 14925 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14926 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14927 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14928 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14929 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14930 | 0x000A, /* LINE FEED */ |
| 14931 | 0x000D, /* CARRIAGE RETURN */ |
| 14932 | 0x001C, /* FILE SEPARATOR */ |
| 14933 | 0x001D, /* GROUP SEPARATOR */ |
| 14934 | 0x001E, /* RECORD SEPARATOR */ |
| 14935 | 0x0085, /* NEXT LINE */ |
| 14936 | 0x2028, /* LINE SEPARATOR */ |
| 14937 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14938 | }; |
| 14939 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14940 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14941 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14942 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14943 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14944 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14945 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14946 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14947 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14948 | |
| 14949 | /* initialize the linebreak bloom filter */ |
| 14950 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14951 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14952 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14953 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14954 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14955 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14956 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14957 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14958 | Py_FatalError("Can't initialize field name iterator type"); |
| 14959 | |
| 14960 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14961 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14962 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14963 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14964 | } |
| 14965 | |
| 14966 | /* Finalize the Unicode implementation */ |
| 14967 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14968 | int |
| 14969 | PyUnicode_ClearFreeList(void) |
| 14970 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14971 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14972 | } |
| 14973 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14974 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14975 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14976 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14977 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14978 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14979 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14980 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 14981 | for (i = 0; i < 256; i++) |
| 14982 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14983 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14984 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14985 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14986 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14987 | void |
| 14988 | PyUnicode_InternInPlace(PyObject **p) |
| 14989 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 14990 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14991 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14992 | #ifdef Py_DEBUG |
| 14993 | assert(s != NULL); |
| 14994 | assert(_PyUnicode_CHECK(s)); |
| 14995 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14996 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14997 | return; |
| 14998 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14999 | /* If it's a subclass, we don't really know what putting |
| 15000 | it in the interned dict might do. */ |
| 15001 | if (!PyUnicode_CheckExact(s)) |
| 15002 | return; |
| 15003 | if (PyUnicode_CHECK_INTERNED(s)) |
| 15004 | return; |
| 15005 | if (interned == NULL) { |
| 15006 | interned = PyDict_New(); |
| 15007 | if (interned == NULL) { |
| 15008 | PyErr_Clear(); /* Don't leave an exception */ |
| 15009 | return; |
| 15010 | } |
| 15011 | } |
| 15012 | /* It might be that the GetItem call fails even |
| 15013 | though the key is present in the dictionary, |
| 15014 | namely when this happens during a stack overflow. */ |
| 15015 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15016 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15017 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15018 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15019 | if (t) { |
| 15020 | Py_INCREF(t); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 15021 | Py_SETREF(*p, t); |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15022 | return; |
| 15023 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15024 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15025 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15026 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15027 | PyErr_Clear(); |
| 15028 | PyThreadState_GET()->recursion_critical = 0; |
| 15029 | return; |
| 15030 | } |
| 15031 | PyThreadState_GET()->recursion_critical = 0; |
| 15032 | /* The two references in interned are not counted by refcnt. |
| 15033 | The deallocator will take care of this */ |
| 15034 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15035 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15036 | } |
| 15037 | |
| 15038 | void |
| 15039 | PyUnicode_InternImmortal(PyObject **p) |
| 15040 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15041 | PyUnicode_InternInPlace(p); |
| 15042 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 15043 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15044 | Py_INCREF(*p); |
| 15045 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15046 | } |
| 15047 | |
| 15048 | PyObject * |
| 15049 | PyUnicode_InternFromString(const char *cp) |
| 15050 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15051 | PyObject *s = PyUnicode_FromString(cp); |
| 15052 | if (s == NULL) |
| 15053 | return NULL; |
| 15054 | PyUnicode_InternInPlace(&s); |
| 15055 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15056 | } |
| 15057 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15058 | void |
| 15059 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15060 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15061 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15062 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15063 | Py_ssize_t i, n; |
| 15064 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15065 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15066 | if (interned == NULL || !PyDict_Check(interned)) |
| 15067 | return; |
| 15068 | keys = PyDict_Keys(interned); |
| 15069 | if (keys == NULL || !PyList_Check(keys)) { |
| 15070 | PyErr_Clear(); |
| 15071 | return; |
| 15072 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15073 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15074 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15075 | detector, interned unicode strings are not forcibly deallocated; |
| 15076 | rather, we give them their stolen references back, and then clear |
| 15077 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15078 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15079 | n = PyList_GET_SIZE(keys); |
| 15080 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15081 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15082 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15083 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15084 | if (PyUnicode_READY(s) == -1) { |
| 15085 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15086 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15087 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15088 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15089 | case SSTATE_NOT_INTERNED: |
| 15090 | /* XXX Shouldn't happen */ |
| 15091 | break; |
| 15092 | case SSTATE_INTERNED_IMMORTAL: |
| 15093 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15094 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15095 | break; |
| 15096 | case SSTATE_INTERNED_MORTAL: |
| 15097 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15098 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15099 | break; |
| 15100 | default: |
| 15101 | Py_FatalError("Inconsistent interned string state."); |
| 15102 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15103 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15104 | } |
| 15105 | fprintf(stderr, "total size of all interned strings: " |
| 15106 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15107 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15108 | Py_DECREF(keys); |
| 15109 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15110 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15111 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15112 | |
| 15113 | |
| 15114 | /********************* Unicode Iterator **************************/ |
| 15115 | |
| 15116 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15117 | PyObject_HEAD |
| 15118 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15119 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15120 | } unicodeiterobject; |
| 15121 | |
| 15122 | static void |
| 15123 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15124 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15125 | _PyObject_GC_UNTRACK(it); |
| 15126 | Py_XDECREF(it->it_seq); |
| 15127 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15128 | } |
| 15129 | |
| 15130 | static int |
| 15131 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15132 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15133 | Py_VISIT(it->it_seq); |
| 15134 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15135 | } |
| 15136 | |
| 15137 | static PyObject * |
| 15138 | unicodeiter_next(unicodeiterobject *it) |
| 15139 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15140 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15141 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15142 | assert(it != NULL); |
| 15143 | seq = it->it_seq; |
| 15144 | if (seq == NULL) |
| 15145 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15146 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15147 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15148 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15149 | int kind = PyUnicode_KIND(seq); |
| 15150 | void *data = PyUnicode_DATA(seq); |
| 15151 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15152 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15153 | if (item != NULL) |
| 15154 | ++it->it_index; |
| 15155 | return item; |
| 15156 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15157 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15158 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 15159 | Py_DECREF(seq); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15160 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15161 | } |
| 15162 | |
| 15163 | static PyObject * |
| 15164 | unicodeiter_len(unicodeiterobject *it) |
| 15165 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15166 | Py_ssize_t len = 0; |
| 15167 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15168 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15169 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15170 | } |
| 15171 | |
| 15172 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15173 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15174 | static PyObject * |
| 15175 | unicodeiter_reduce(unicodeiterobject *it) |
| 15176 | { |
| 15177 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15178 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15179 | it->it_seq, it->it_index); |
| 15180 | } else { |
| 15181 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15182 | if (u == NULL) |
| 15183 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15184 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15185 | } |
| 15186 | } |
| 15187 | |
| 15188 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15189 | |
| 15190 | static PyObject * |
| 15191 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15192 | { |
| 15193 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15194 | if (index == -1 && PyErr_Occurred()) |
| 15195 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 15196 | if (it->it_seq != NULL) { |
| 15197 | if (index < 0) |
| 15198 | index = 0; |
| 15199 | else if (index > PyUnicode_GET_LENGTH(it->it_seq)) |
| 15200 | index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */ |
| 15201 | it->it_index = index; |
| 15202 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15203 | Py_RETURN_NONE; |
| 15204 | } |
| 15205 | |
| 15206 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15207 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15208 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15209 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15210 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15211 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15212 | reduce_doc}, |
| 15213 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15214 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15215 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15216 | }; |
| 15217 | |
| 15218 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15219 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15220 | "str_iterator", /* tp_name */ |
| 15221 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15222 | 0, /* tp_itemsize */ |
| 15223 | /* methods */ |
| 15224 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15225 | 0, /* tp_print */ |
| 15226 | 0, /* tp_getattr */ |
| 15227 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15228 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15229 | 0, /* tp_repr */ |
| 15230 | 0, /* tp_as_number */ |
| 15231 | 0, /* tp_as_sequence */ |
| 15232 | 0, /* tp_as_mapping */ |
| 15233 | 0, /* tp_hash */ |
| 15234 | 0, /* tp_call */ |
| 15235 | 0, /* tp_str */ |
| 15236 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15237 | 0, /* tp_setattro */ |
| 15238 | 0, /* tp_as_buffer */ |
| 15239 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15240 | 0, /* tp_doc */ |
| 15241 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15242 | 0, /* tp_clear */ |
| 15243 | 0, /* tp_richcompare */ |
| 15244 | 0, /* tp_weaklistoffset */ |
| 15245 | PyObject_SelfIter, /* tp_iter */ |
| 15246 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15247 | unicodeiter_methods, /* tp_methods */ |
| 15248 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15249 | }; |
| 15250 | |
| 15251 | static PyObject * |
| 15252 | unicode_iter(PyObject *seq) |
| 15253 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15254 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15255 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15256 | if (!PyUnicode_Check(seq)) { |
| 15257 | PyErr_BadInternalCall(); |
| 15258 | return NULL; |
| 15259 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15260 | if (PyUnicode_READY(seq) == -1) |
| 15261 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15262 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15263 | if (it == NULL) |
| 15264 | return NULL; |
| 15265 | it->it_index = 0; |
| 15266 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15267 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15268 | _PyObject_GC_TRACK(it); |
| 15269 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15270 | } |
| 15271 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15272 | |
| 15273 | size_t |
| 15274 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15275 | { |
| 15276 | int res = 0; |
| 15277 | while(*u++) |
| 15278 | res++; |
| 15279 | return res; |
| 15280 | } |
| 15281 | |
| 15282 | Py_UNICODE* |
| 15283 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15284 | { |
| 15285 | Py_UNICODE *u = s1; |
| 15286 | while ((*u++ = *s2++)); |
| 15287 | return s1; |
| 15288 | } |
| 15289 | |
| 15290 | Py_UNICODE* |
| 15291 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15292 | { |
| 15293 | Py_UNICODE *u = s1; |
| 15294 | while ((*u++ = *s2++)) |
| 15295 | if (n-- == 0) |
| 15296 | break; |
| 15297 | return s1; |
| 15298 | } |
| 15299 | |
| 15300 | Py_UNICODE* |
| 15301 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15302 | { |
| 15303 | Py_UNICODE *u1 = s1; |
| 15304 | u1 += Py_UNICODE_strlen(u1); |
| 15305 | Py_UNICODE_strcpy(u1, s2); |
| 15306 | return s1; |
| 15307 | } |
| 15308 | |
| 15309 | int |
| 15310 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15311 | { |
| 15312 | while (*s1 && *s2 && *s1 == *s2) |
| 15313 | s1++, s2++; |
| 15314 | if (*s1 && *s2) |
| 15315 | return (*s1 < *s2) ? -1 : +1; |
| 15316 | if (*s1) |
| 15317 | return 1; |
| 15318 | if (*s2) |
| 15319 | return -1; |
| 15320 | return 0; |
| 15321 | } |
| 15322 | |
| 15323 | int |
| 15324 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15325 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15326 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15327 | for (; n != 0; n--) { |
| 15328 | u1 = *s1; |
| 15329 | u2 = *s2; |
| 15330 | if (u1 != u2) |
| 15331 | return (u1 < u2) ? -1 : +1; |
| 15332 | if (u1 == '\0') |
| 15333 | return 0; |
| 15334 | s1++; |
| 15335 | s2++; |
| 15336 | } |
| 15337 | return 0; |
| 15338 | } |
| 15339 | |
| 15340 | Py_UNICODE* |
| 15341 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15342 | { |
| 15343 | const Py_UNICODE *p; |
| 15344 | for (p = s; *p; p++) |
| 15345 | if (*p == c) |
| 15346 | return (Py_UNICODE*)p; |
| 15347 | return NULL; |
| 15348 | } |
| 15349 | |
| 15350 | Py_UNICODE* |
| 15351 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15352 | { |
| 15353 | const Py_UNICODE *p; |
| 15354 | p = s + Py_UNICODE_strlen(s); |
| 15355 | while (p != s) { |
| 15356 | p--; |
| 15357 | if (*p == c) |
| 15358 | return (Py_UNICODE*)p; |
| 15359 | } |
| 15360 | return NULL; |
| 15361 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15362 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15363 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15364 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15365 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15366 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15367 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15368 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15369 | if (!PyUnicode_Check(unicode)) { |
| 15370 | PyErr_BadArgument(); |
| 15371 | return NULL; |
| 15372 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15373 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15374 | if (u == NULL) |
| 15375 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15376 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 15377 | 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] | 15378 | PyErr_NoMemory(); |
| 15379 | return NULL; |
| 15380 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15381 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15382 | size *= sizeof(Py_UNICODE); |
| 15383 | copy = PyMem_Malloc(size); |
| 15384 | if (copy == NULL) { |
| 15385 | PyErr_NoMemory(); |
| 15386 | return NULL; |
| 15387 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15388 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15389 | return copy; |
| 15390 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15391 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15392 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15393 | to the string.Formatter class implemented in Python. */ |
| 15394 | |
| 15395 | static PyMethodDef _string_methods[] = { |
| 15396 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15397 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15398 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15399 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15400 | {NULL, NULL} |
| 15401 | }; |
| 15402 | |
| 15403 | static struct PyModuleDef _string_module = { |
| 15404 | PyModuleDef_HEAD_INIT, |
| 15405 | "_string", |
| 15406 | PyDoc_STR("string helper module"), |
| 15407 | 0, |
| 15408 | _string_methods, |
| 15409 | NULL, |
| 15410 | NULL, |
| 15411 | NULL, |
| 15412 | NULL |
| 15413 | }; |
| 15414 | |
| 15415 | PyMODINIT_FUNC |
| 15416 | PyInit__string(void) |
| 15417 | { |
| 15418 | return PyModule_Create(&_string_module); |
| 15419 | } |
| 15420 | |
| 15421 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15422 | #ifdef __cplusplus |
| 15423 | } |
| 15424 | #endif |