Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Benjamin Peterson | 31616ea | 2011-10-01 00:11:09 -0400 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com>. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | Major speed upgrades to the method implementations at the Reykjavik |
| 7 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 8 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 11 | -------------------------------------------------------------------- |
| 12 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14 | Copyright (c) 1999 by Secret Labs AB |
| 15 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 16 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 17 | By obtaining, using, and/or copying this software and/or its |
| 18 | associated documentation, you agree that you have read, understood, |
| 19 | and will comply with the following terms and conditions: |
| 20 | |
| 21 | Permission to use, copy, modify, and distribute this software and its |
| 22 | associated documentation for any purpose and without fee is hereby |
| 23 | granted, provided that the above copyright notice appears in all |
| 24 | copies, and that both that copyright notice and this permission notice |
| 25 | appear in supporting documentation, and that the name of Secret Labs |
| 26 | AB or the author not be used in advertising or publicity pertaining to |
| 27 | distribution of the software without specific, written prior |
| 28 | permission. |
| 29 | |
| 30 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 31 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 32 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 33 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 34 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 35 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 36 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 37 | -------------------------------------------------------------------- |
| 38 | |
| 39 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "Python.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Raymond Hettinger | ac2ef65 | 2015-07-04 16:04:44 -0700 | [diff] [blame] | 45 | #include "stringlib/eq.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 47 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 48 | #include <windows.h> |
| 49 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 50 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 51 | /*[clinic input] |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 52 | class str "PyUnicodeObject *" "&PyUnicode_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 53 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 54 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=604e916854800fa8]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 55 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 56 | /* --- Globals ------------------------------------------------------------ |
| 57 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 58 | NOTE: In the interpreter's initialization phase, some globals are currently |
| 59 | initialized dynamically as needed. In the process Unicode objects may |
| 60 | be created before the Unicode type is ready. |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 61 | |
| 62 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 64 | |
| 65 | #ifdef __cplusplus |
| 66 | extern "C" { |
| 67 | #endif |
| 68 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 69 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 70 | #define MAX_UNICODE 0x10ffff |
| 71 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 72 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 73 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 74 | #else |
| 75 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 76 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 77 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 78 | #define _PyUnicode_UTF8(op) \ |
| 79 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 80 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 81 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 82 | assert(PyUnicode_IS_READY(op)), \ |
| 83 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 84 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 85 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 86 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 87 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 88 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 89 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 90 | assert(PyUnicode_IS_READY(op)), \ |
| 91 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 92 | ((PyASCIIObject*)(op))->length : \ |
| 93 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 94 | #define _PyUnicode_WSTR(op) \ |
| 95 | (((PyASCIIObject*)(op))->wstr) |
| 96 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 97 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 98 | #define _PyUnicode_LENGTH(op) \ |
| 99 | (((PyASCIIObject *)(op))->length) |
| 100 | #define _PyUnicode_STATE(op) \ |
| 101 | (((PyASCIIObject *)(op))->state) |
| 102 | #define _PyUnicode_HASH(op) \ |
| 103 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 104 | #define _PyUnicode_KIND(op) \ |
| 105 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 106 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 107 | #define _PyUnicode_GET_LENGTH(op) \ |
| 108 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 109 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 110 | #define _PyUnicode_DATA_ANY(op) \ |
| 111 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 112 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 113 | #undef PyUnicode_READY |
| 114 | #define PyUnicode_READY(op) \ |
| 115 | (assert(_PyUnicode_CHECK(op)), \ |
| 116 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 117 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 118 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 119 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 120 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 121 | (assert(_PyUnicode_CHECK(op)), \ |
| 122 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 123 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 124 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 125 | (assert(_PyUnicode_CHECK(op)), \ |
| 126 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 127 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 128 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 129 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 130 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 131 | ((!PyUnicode_IS_COMPACT_ASCII(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 132 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 133 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 134 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 135 | /* true if the Unicode object has an allocated wstr memory block |
| 136 | (not shared with other data) */ |
| 137 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 138 | ((_PyUnicode_WSTR(op) && \ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 139 | (!PyUnicode_IS_READY(op) || \ |
| 140 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 141 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 142 | /* Generic helper macro to convert characters of different types. |
| 143 | from_type and to_type have to be valid type names, begin and end |
| 144 | are pointers to the source characters which should be of type |
| 145 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 146 | buffer where the result characters are written to. */ |
| 147 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 148 | do { \ |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 149 | to_type *_to = (to_type *)(to); \ |
| 150 | const from_type *_iter = (from_type *)(begin); \ |
| 151 | const from_type *_end = (from_type *)(end); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 152 | Py_ssize_t n = (_end) - (_iter); \ |
| 153 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 154 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 155 | while (_iter < (_unrolled_end)) { \ |
| 156 | _to[0] = (to_type) _iter[0]; \ |
| 157 | _to[1] = (to_type) _iter[1]; \ |
| 158 | _to[2] = (to_type) _iter[2]; \ |
| 159 | _to[3] = (to_type) _iter[3]; \ |
| 160 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 161 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 162 | while (_iter < (_end)) \ |
| 163 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 164 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 165 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 166 | /* This dictionary holds all interned unicode strings. Note that references |
| 167 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 168 | When the interned string reaches a refcnt of 0 the string deallocation |
| 169 | function will delete the reference from this dictionary. |
| 170 | |
| 171 | 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] | 172 | 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] | 173 | */ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 174 | static PyObject *interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 175 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 176 | /* The empty Unicode object is shared to improve performance. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 177 | static PyObject *unicode_empty = NULL; |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 178 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 179 | #define _Py_INCREF_UNICODE_EMPTY() \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 180 | do { \ |
| 181 | if (unicode_empty != NULL) \ |
| 182 | Py_INCREF(unicode_empty); \ |
| 183 | else { \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 184 | unicode_empty = PyUnicode_New(0, 0); \ |
| 185 | if (unicode_empty != NULL) { \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 186 | Py_INCREF(unicode_empty); \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 187 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); \ |
| 188 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 189 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 190 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 191 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 192 | #define _Py_RETURN_UNICODE_EMPTY() \ |
| 193 | do { \ |
| 194 | _Py_INCREF_UNICODE_EMPTY(); \ |
| 195 | return unicode_empty; \ |
| 196 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 197 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 198 | /* Forward declaration */ |
| 199 | Py_LOCAL_INLINE(int) |
| 200 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch); |
| 201 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 202 | /* List of static strings. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 203 | static _Py_Identifier *static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 204 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 205 | /* Single character Unicode strings in the Latin-1 range are being |
| 206 | shared as well. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 207 | static PyObject *unicode_latin1[256] = {NULL}; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 208 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 209 | /* Fast detection of the most frequent whitespace characters */ |
| 210 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 211 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 212 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 213 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 214 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 215 | /* case 0x000C: * FORM FEED */ |
| 216 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 217 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 218 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 219 | /* case 0x001C: * FILE SEPARATOR */ |
| 220 | /* case 0x001D: * GROUP SEPARATOR */ |
| 221 | /* case 0x001E: * RECORD SEPARATOR */ |
| 222 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 223 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 224 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 225 | 1, 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, |
| 228 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 229 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 237 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 240 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 241 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 242 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 243 | static int unicode_modifiable(PyObject *unicode); |
| 244 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 245 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 246 | static PyObject * |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 247 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 248 | static PyObject * |
| 249 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 250 | static PyObject * |
| 251 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 252 | |
| 253 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 254 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 255 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 256 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 257 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 258 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 259 | static void |
| 260 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 261 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 262 | PyObject *unicode, |
| 263 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 264 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 265 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 266 | /* Same for linebreaks */ |
| 267 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 268 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 269 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 270 | /* 0x000B, * LINE TABULATION */ |
| 271 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 272 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 273 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 274 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 275 | /* 0x001C, * FILE SEPARATOR */ |
| 276 | /* 0x001D, * GROUP SEPARATOR */ |
| 277 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 278 | 0, 0, 0, 0, 1, 1, 1, 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, |
| 282 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 283 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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, |
| 291 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 294 | #include "clinic/unicodeobject.c.h" |
| 295 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 296 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 297 | 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] | 298 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 299 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 300 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 301 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 302 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 303 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 304 | /* This is actually an illegal character, so it should |
| 305 | not be passed to unichr. */ |
| 306 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 307 | #endif |
| 308 | } |
| 309 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 310 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 311 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 312 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 313 | { |
| 314 | PyASCIIObject *ascii; |
| 315 | unsigned int kind; |
| 316 | |
| 317 | assert(PyUnicode_Check(op)); |
| 318 | |
| 319 | ascii = (PyASCIIObject *)op; |
| 320 | kind = ascii->state.kind; |
| 321 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 322 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 323 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 324 | assert(ascii->state.ready == 1); |
| 325 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 326 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 327 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 328 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 329 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 330 | if (ascii->state.compact == 1) { |
| 331 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 332 | assert(kind == PyUnicode_1BYTE_KIND |
| 333 | || kind == PyUnicode_2BYTE_KIND |
| 334 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 335 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 336 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 337 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 338 | } |
| 339 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 340 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 341 | |
| 342 | data = unicode->data.any; |
| 343 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 344 | assert(ascii->length == 0); |
| 345 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 346 | assert(ascii->state.compact == 0); |
| 347 | assert(ascii->state.ascii == 0); |
| 348 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 349 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 350 | assert(ascii->wstr != NULL); |
| 351 | assert(data == NULL); |
| 352 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 353 | } |
| 354 | else { |
| 355 | assert(kind == PyUnicode_1BYTE_KIND |
| 356 | || kind == PyUnicode_2BYTE_KIND |
| 357 | || kind == PyUnicode_4BYTE_KIND); |
| 358 | assert(ascii->state.compact == 0); |
| 359 | assert(ascii->state.ready == 1); |
| 360 | assert(data != NULL); |
| 361 | if (ascii->state.ascii) { |
| 362 | assert (compact->utf8 == data); |
| 363 | assert (compact->utf8_length == ascii->length); |
| 364 | } |
| 365 | else |
| 366 | assert (compact->utf8 != data); |
| 367 | } |
| 368 | } |
| 369 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 370 | if ( |
| 371 | #if SIZEOF_WCHAR_T == 2 |
| 372 | kind == PyUnicode_2BYTE_KIND |
| 373 | #else |
| 374 | kind == PyUnicode_4BYTE_KIND |
| 375 | #endif |
| 376 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 377 | { |
| 378 | assert(ascii->wstr == data); |
| 379 | assert(compact->wstr_length == ascii->length); |
| 380 | } else |
| 381 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 382 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 383 | |
| 384 | if (compact->utf8 == NULL) |
| 385 | assert(compact->utf8_length == 0); |
| 386 | if (ascii->wstr == NULL) |
| 387 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 388 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 389 | /* check that the best kind is used */ |
| 390 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 391 | { |
| 392 | Py_ssize_t i; |
| 393 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 394 | void *data; |
| 395 | Py_UCS4 ch; |
| 396 | |
| 397 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 398 | for (i=0; i < ascii->length; i++) |
| 399 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 400 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 401 | if (ch > maxchar) |
| 402 | maxchar = ch; |
| 403 | } |
| 404 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 405 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 406 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 407 | assert(maxchar <= 255); |
| 408 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 409 | else |
| 410 | assert(maxchar < 128); |
| 411 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 412 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 413 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 414 | assert(maxchar <= 0xFFFF); |
| 415 | } |
| 416 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 417 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 418 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 419 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 420 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 421 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 422 | return 1; |
| 423 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 424 | #endif |
| 425 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 426 | static PyObject* |
| 427 | unicode_result_wchar(PyObject *unicode) |
| 428 | { |
| 429 | #ifndef Py_DEBUG |
| 430 | Py_ssize_t len; |
| 431 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 432 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 433 | if (len == 0) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 434 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 435 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if (len == 1) { |
| 439 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 440 | if ((Py_UCS4)ch < 256) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 441 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 442 | Py_DECREF(unicode); |
| 443 | return latin1_char; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (_PyUnicode_Ready(unicode) < 0) { |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 448 | Py_DECREF(unicode); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 449 | return NULL; |
| 450 | } |
| 451 | #else |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 452 | assert(Py_REFCNT(unicode) == 1); |
| 453 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 454 | /* don't make the result ready in debug mode to ensure that the caller |
| 455 | makes the string ready before using it */ |
| 456 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 457 | #endif |
| 458 | return unicode; |
| 459 | } |
| 460 | |
| 461 | static PyObject* |
| 462 | unicode_result_ready(PyObject *unicode) |
| 463 | { |
| 464 | Py_ssize_t length; |
| 465 | |
| 466 | length = PyUnicode_GET_LENGTH(unicode); |
| 467 | if (length == 0) { |
| 468 | if (unicode != unicode_empty) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 469 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 470 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 471 | } |
| 472 | return unicode_empty; |
| 473 | } |
| 474 | |
| 475 | if (length == 1) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 476 | void *data = PyUnicode_DATA(unicode); |
| 477 | int kind = PyUnicode_KIND(unicode); |
| 478 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 479 | if (ch < 256) { |
| 480 | PyObject *latin1_char = unicode_latin1[ch]; |
| 481 | if (latin1_char != NULL) { |
| 482 | if (unicode != latin1_char) { |
| 483 | Py_INCREF(latin1_char); |
| 484 | Py_DECREF(unicode); |
| 485 | } |
| 486 | return latin1_char; |
| 487 | } |
| 488 | else { |
| 489 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 490 | Py_INCREF(unicode); |
| 491 | unicode_latin1[ch] = unicode; |
| 492 | return unicode; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 498 | return unicode; |
| 499 | } |
| 500 | |
| 501 | static PyObject* |
| 502 | unicode_result(PyObject *unicode) |
| 503 | { |
| 504 | assert(_PyUnicode_CHECK(unicode)); |
| 505 | if (PyUnicode_IS_READY(unicode)) |
| 506 | return unicode_result_ready(unicode); |
| 507 | else |
| 508 | return unicode_result_wchar(unicode); |
| 509 | } |
| 510 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 511 | static PyObject* |
| 512 | unicode_result_unchanged(PyObject *unicode) |
| 513 | { |
| 514 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 515 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 516 | return NULL; |
| 517 | Py_INCREF(unicode); |
| 518 | return unicode; |
| 519 | } |
| 520 | else |
| 521 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 522 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 525 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 526 | |
| 527 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 528 | to keep things simple, we use a single bitmask, using the least 5 |
| 529 | bits from each unicode characters as the bit index. */ |
| 530 | |
| 531 | /* the linebreak mask is set up by Unicode_Init below */ |
| 532 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 533 | #if LONG_BIT >= 128 |
| 534 | #define BLOOM_WIDTH 128 |
| 535 | #elif LONG_BIT >= 64 |
| 536 | #define BLOOM_WIDTH 64 |
| 537 | #elif LONG_BIT >= 32 |
| 538 | #define BLOOM_WIDTH 32 |
| 539 | #else |
| 540 | #error "LONG_BIT is smaller than 32" |
| 541 | #endif |
| 542 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 543 | #define BLOOM_MASK unsigned long |
| 544 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 545 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 546 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 547 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 548 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 549 | #define BLOOM_LINEBREAK(ch) \ |
| 550 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 551 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 552 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 553 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 554 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 555 | { |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 556 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN) \ |
| 557 | do { \ |
| 558 | TYPE *data = (TYPE *)PTR; \ |
| 559 | TYPE *end = data + LEN; \ |
| 560 | Py_UCS4 ch; \ |
| 561 | for (; data != end; data++) { \ |
| 562 | ch = *data; \ |
| 563 | MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ |
| 564 | } \ |
| 565 | break; \ |
| 566 | } while (0) |
| 567 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 568 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 569 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 570 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 571 | |
| 572 | mask = 0; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 573 | switch (kind) { |
| 574 | case PyUnicode_1BYTE_KIND: |
| 575 | BLOOM_UPDATE(Py_UCS1, mask, ptr, len); |
| 576 | break; |
| 577 | case PyUnicode_2BYTE_KIND: |
| 578 | BLOOM_UPDATE(Py_UCS2, mask, ptr, len); |
| 579 | break; |
| 580 | case PyUnicode_4BYTE_KIND: |
| 581 | BLOOM_UPDATE(Py_UCS4, mask, ptr, len); |
| 582 | break; |
| 583 | default: |
| 584 | assert(0); |
| 585 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 586 | return mask; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 587 | |
| 588 | #undef BLOOM_UPDATE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 591 | /* Compilation of templated routines */ |
| 592 | |
| 593 | #include "stringlib/asciilib.h" |
| 594 | #include "stringlib/fastsearch.h" |
| 595 | #include "stringlib/partition.h" |
| 596 | #include "stringlib/split.h" |
| 597 | #include "stringlib/count.h" |
| 598 | #include "stringlib/find.h" |
| 599 | #include "stringlib/find_max_char.h" |
| 600 | #include "stringlib/localeutil.h" |
| 601 | #include "stringlib/undef.h" |
| 602 | |
| 603 | #include "stringlib/ucs1lib.h" |
| 604 | #include "stringlib/fastsearch.h" |
| 605 | #include "stringlib/partition.h" |
| 606 | #include "stringlib/split.h" |
| 607 | #include "stringlib/count.h" |
| 608 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 609 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 610 | #include "stringlib/find_max_char.h" |
| 611 | #include "stringlib/localeutil.h" |
| 612 | #include "stringlib/undef.h" |
| 613 | |
| 614 | #include "stringlib/ucs2lib.h" |
| 615 | #include "stringlib/fastsearch.h" |
| 616 | #include "stringlib/partition.h" |
| 617 | #include "stringlib/split.h" |
| 618 | #include "stringlib/count.h" |
| 619 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 620 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 621 | #include "stringlib/find_max_char.h" |
| 622 | #include "stringlib/localeutil.h" |
| 623 | #include "stringlib/undef.h" |
| 624 | |
| 625 | #include "stringlib/ucs4lib.h" |
| 626 | #include "stringlib/fastsearch.h" |
| 627 | #include "stringlib/partition.h" |
| 628 | #include "stringlib/split.h" |
| 629 | #include "stringlib/count.h" |
| 630 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 631 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 632 | #include "stringlib/find_max_char.h" |
| 633 | #include "stringlib/localeutil.h" |
| 634 | #include "stringlib/undef.h" |
| 635 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 636 | #include "stringlib/unicodedefs.h" |
| 637 | #include "stringlib/fastsearch.h" |
| 638 | #include "stringlib/count.h" |
| 639 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 640 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 641 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 642 | /* --- Unicode Object ----------------------------------------------------- */ |
| 643 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 644 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 645 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 646 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 647 | Py_LOCAL_INLINE(Py_ssize_t) findchar(const void *s, int kind, |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 648 | Py_ssize_t size, Py_UCS4 ch, |
| 649 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 650 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 651 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 652 | |
| 653 | switch (kind) { |
| 654 | case PyUnicode_1BYTE_KIND: |
| 655 | { |
| 656 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 657 | if (ch1 == ch) |
| 658 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 659 | else |
| 660 | return -1; |
| 661 | } |
| 662 | case PyUnicode_2BYTE_KIND: |
| 663 | { |
| 664 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 665 | if (ch2 == ch) |
| 666 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 667 | else |
| 668 | return -1; |
| 669 | } |
| 670 | case PyUnicode_4BYTE_KIND: |
| 671 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 672 | default: |
| 673 | assert(0); |
| 674 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 675 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 676 | } |
| 677 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 678 | #ifdef Py_DEBUG |
| 679 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 680 | earlier. |
| 681 | |
| 682 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 683 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 684 | invalid character in Unicode 6.0. */ |
| 685 | static void |
| 686 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 687 | { |
| 688 | int kind = PyUnicode_KIND(unicode); |
| 689 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 690 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 691 | if (length <= old_length) |
| 692 | return; |
| 693 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 694 | } |
| 695 | #endif |
| 696 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 697 | static PyObject* |
| 698 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 699 | { |
| 700 | Py_ssize_t char_size; |
| 701 | Py_ssize_t struct_size; |
| 702 | Py_ssize_t new_size; |
| 703 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 704 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 705 | #ifdef Py_DEBUG |
| 706 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 707 | #endif |
| 708 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 709 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 710 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 711 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 712 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 713 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 714 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 715 | struct_size = sizeof(PyASCIIObject); |
| 716 | else |
| 717 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 718 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 719 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 720 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 721 | PyErr_NoMemory(); |
| 722 | return NULL; |
| 723 | } |
| 724 | new_size = (struct_size + (length + 1) * char_size); |
| 725 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 726 | _Py_DEC_REFTOTAL; |
| 727 | _Py_ForgetReference(unicode); |
| 728 | |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 729 | new_unicode = (PyObject *)PyObject_REALLOC(unicode, new_size); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 730 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 731 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 732 | PyErr_NoMemory(); |
| 733 | return NULL; |
| 734 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 735 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 736 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 737 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 738 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 739 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 740 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 741 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 742 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 743 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 744 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 745 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 746 | _PyUnicode_WSTR(unicode) = NULL; |
| 747 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 748 | #ifdef Py_DEBUG |
| 749 | unicode_fill_invalid(unicode, old_length); |
| 750 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 751 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 752 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 753 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 754 | return unicode; |
| 755 | } |
| 756 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 757 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 758 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 759 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 760 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 761 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 762 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 763 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 764 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 765 | if (PyUnicode_IS_READY(unicode)) { |
| 766 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 767 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 768 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 769 | #ifdef Py_DEBUG |
| 770 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 771 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 772 | |
| 773 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 774 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 775 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 776 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 777 | |
| 778 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 779 | PyErr_NoMemory(); |
| 780 | return -1; |
| 781 | } |
| 782 | new_size = (length + 1) * char_size; |
| 783 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 784 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 785 | { |
| 786 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 787 | _PyUnicode_UTF8(unicode) = NULL; |
| 788 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 789 | } |
| 790 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 791 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 792 | if (data == NULL) { |
| 793 | PyErr_NoMemory(); |
| 794 | return -1; |
| 795 | } |
| 796 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 797 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 798 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 799 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 800 | } |
| 801 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 802 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 803 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 804 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 805 | _PyUnicode_LENGTH(unicode) = length; |
| 806 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 807 | #ifdef Py_DEBUG |
| 808 | unicode_fill_invalid(unicode, old_length); |
| 809 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 810 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 811 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 812 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 813 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 814 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 815 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 816 | |
| 817 | /* check for integer overflow */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 818 | 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] | 819 | PyErr_NoMemory(); |
| 820 | return -1; |
| 821 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 822 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 823 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 824 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 825 | if (!wstr) { |
| 826 | PyErr_NoMemory(); |
| 827 | return -1; |
| 828 | } |
| 829 | _PyUnicode_WSTR(unicode) = wstr; |
| 830 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 831 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 832 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 833 | return 0; |
| 834 | } |
| 835 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 836 | static PyObject* |
| 837 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 838 | { |
| 839 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 840 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 841 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 842 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 843 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 844 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 845 | |
| 846 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 847 | if (copy == NULL) |
| 848 | return NULL; |
| 849 | |
| 850 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 851 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 852 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 853 | } |
| 854 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 855 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 856 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 857 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 858 | if (w == NULL) |
| 859 | return NULL; |
| 860 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 861 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 862 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 863 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 864 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 868 | /* 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] | 869 | Ux0000 terminated; some code (e.g. new_identifier) |
| 870 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 871 | |
| 872 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 873 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 874 | |
| 875 | */ |
| 876 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 877 | static PyUnicodeObject * |
| 878 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 879 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 880 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 881 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 882 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 883 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 884 | if (length == 0 && unicode_empty != NULL) { |
| 885 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 886 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 889 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 890 | 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] | 891 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 892 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 893 | if (length < 0) { |
| 894 | PyErr_SetString(PyExc_SystemError, |
| 895 | "Negative size passed to _PyUnicode_New"); |
| 896 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 899 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 900 | if (unicode == NULL) |
| 901 | return NULL; |
| 902 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 903 | |
| 904 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 905 | _PyUnicode_HASH(unicode) = -1; |
| 906 | _PyUnicode_STATE(unicode).interned = 0; |
| 907 | _PyUnicode_STATE(unicode).kind = 0; |
| 908 | _PyUnicode_STATE(unicode).compact = 0; |
| 909 | _PyUnicode_STATE(unicode).ready = 0; |
| 910 | _PyUnicode_STATE(unicode).ascii = 0; |
| 911 | _PyUnicode_DATA_ANY(unicode) = NULL; |
| 912 | _PyUnicode_LENGTH(unicode) = 0; |
| 913 | _PyUnicode_UTF8(unicode) = NULL; |
| 914 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 915 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 916 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 917 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 918 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 919 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 920 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 921 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 922 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 923 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 924 | * the caller fails before initializing str -- unicode_resize() |
| 925 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 926 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 927 | * We don't want unicode_resize to read uninitialized memory in |
| 928 | * that case. |
| 929 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 930 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 931 | _PyUnicode_WSTR(unicode)[length] = 0; |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 932 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 933 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 934 | return unicode; |
| 935 | } |
| 936 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 937 | static const char* |
| 938 | unicode_kind_name(PyObject *unicode) |
| 939 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 940 | /* don't check consistency: unicode_kind_name() is called from |
| 941 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 942 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 943 | { |
| 944 | if (!PyUnicode_IS_READY(unicode)) |
| 945 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 946 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 947 | { |
| 948 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 949 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 950 | return "legacy ascii"; |
| 951 | else |
| 952 | return "legacy latin1"; |
| 953 | case PyUnicode_2BYTE_KIND: |
| 954 | return "legacy UCS2"; |
| 955 | case PyUnicode_4BYTE_KIND: |
| 956 | return "legacy UCS4"; |
| 957 | default: |
| 958 | return "<legacy invalid kind>"; |
| 959 | } |
| 960 | } |
| 961 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 962 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 963 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 964 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 965 | return "ascii"; |
| 966 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 967 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 968 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 969 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 970 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 971 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 972 | default: |
| 973 | return "<invalid compact kind>"; |
| 974 | } |
| 975 | } |
| 976 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 977 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 978 | /* Functions wrapping macros for use in debugger */ |
| 979 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 980 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | void *_PyUnicode_compact_data(void *unicode) { |
| 984 | return _PyUnicode_COMPACT_DATA(unicode); |
| 985 | } |
| 986 | void *_PyUnicode_data(void *unicode){ |
| 987 | printf("obj %p\n", unicode); |
| 988 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 989 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 990 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 991 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 992 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 993 | return PyUnicode_DATA(unicode); |
| 994 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 995 | |
| 996 | void |
| 997 | _PyUnicode_Dump(PyObject *op) |
| 998 | { |
| 999 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1000 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 1001 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 1002 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1003 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1004 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1005 | { |
| 1006 | if (ascii->state.ascii) |
| 1007 | data = (ascii + 1); |
| 1008 | else |
| 1009 | data = (compact + 1); |
| 1010 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1011 | else |
| 1012 | data = unicode->data.any; |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1013 | printf("%s: len=%" PY_FORMAT_SIZE_T "u, ", |
| 1014 | unicode_kind_name(op), ascii->length); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1015 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1016 | if (ascii->wstr == data) |
| 1017 | printf("shared "); |
| 1018 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1019 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1020 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1021 | printf(" (%" PY_FORMAT_SIZE_T "u), ", compact->wstr_length); |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1022 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1023 | printf("shared "); |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1024 | printf("utf8=%p (%" PY_FORMAT_SIZE_T "u)", |
| 1025 | compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1026 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1027 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1028 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1029 | #endif |
| 1030 | |
| 1031 | PyObject * |
| 1032 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1033 | { |
| 1034 | PyObject *obj; |
| 1035 | PyCompactUnicodeObject *unicode; |
| 1036 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1037 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1038 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1039 | Py_ssize_t char_size; |
| 1040 | Py_ssize_t struct_size; |
| 1041 | |
| 1042 | /* Optimization for empty strings */ |
| 1043 | if (size == 0 && unicode_empty != NULL) { |
| 1044 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1045 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1048 | is_ascii = 0; |
| 1049 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1050 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1051 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1052 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | char_size = 1; |
| 1054 | is_ascii = 1; |
| 1055 | struct_size = sizeof(PyASCIIObject); |
| 1056 | } |
| 1057 | else if (maxchar < 256) { |
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 | } |
| 1061 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1062 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1063 | char_size = 2; |
| 1064 | if (sizeof(wchar_t) == 2) |
| 1065 | is_sharing = 1; |
| 1066 | } |
| 1067 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1068 | if (maxchar > MAX_UNICODE) { |
| 1069 | PyErr_SetString(PyExc_SystemError, |
| 1070 | "invalid maximum character passed to PyUnicode_New"); |
| 1071 | return NULL; |
| 1072 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1073 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1074 | char_size = 4; |
| 1075 | if (sizeof(wchar_t) == 4) |
| 1076 | is_sharing = 1; |
| 1077 | } |
| 1078 | |
| 1079 | /* Ensure we won't overflow the size. */ |
| 1080 | if (size < 0) { |
| 1081 | PyErr_SetString(PyExc_SystemError, |
| 1082 | "Negative size passed to PyUnicode_New"); |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1086 | return PyErr_NoMemory(); |
| 1087 | |
| 1088 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1089 | * PyObject_New() so we are able to allocate space for the object and |
| 1090 | * it's data buffer. |
| 1091 | */ |
| 1092 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1093 | if (obj == NULL) |
| 1094 | return PyErr_NoMemory(); |
| 1095 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1096 | if (obj == NULL) |
| 1097 | return NULL; |
| 1098 | |
| 1099 | unicode = (PyCompactUnicodeObject *)obj; |
| 1100 | if (is_ascii) |
| 1101 | data = ((PyASCIIObject*)obj) + 1; |
| 1102 | else |
| 1103 | data = unicode + 1; |
| 1104 | _PyUnicode_LENGTH(unicode) = size; |
| 1105 | _PyUnicode_HASH(unicode) = -1; |
| 1106 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1107 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1108 | _PyUnicode_STATE(unicode).compact = 1; |
| 1109 | _PyUnicode_STATE(unicode).ready = 1; |
| 1110 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1111 | if (is_ascii) { |
| 1112 | ((char*)data)[size] = 0; |
| 1113 | _PyUnicode_WSTR(unicode) = NULL; |
| 1114 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1115 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1116 | ((char*)data)[size] = 0; |
| 1117 | _PyUnicode_WSTR(unicode) = NULL; |
| 1118 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1119 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1120 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1121 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1122 | else { |
| 1123 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1124 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1125 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1126 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1127 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1128 | ((Py_UCS4*)data)[size] = 0; |
| 1129 | if (is_sharing) { |
| 1130 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1131 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1132 | } |
| 1133 | else { |
| 1134 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1135 | _PyUnicode_WSTR(unicode) = NULL; |
| 1136 | } |
| 1137 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1138 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1139 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1140 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1141 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1142 | return obj; |
| 1143 | } |
| 1144 | |
| 1145 | #if SIZEOF_WCHAR_T == 2 |
| 1146 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1147 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1148 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1149 | |
| 1150 | This function assumes that unicode can hold one more code point than wstr |
| 1151 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1152 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1153 | 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] | 1154 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1155 | { |
| 1156 | const wchar_t *iter; |
| 1157 | Py_UCS4 *ucs4_out; |
| 1158 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1159 | assert(unicode != NULL); |
| 1160 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1161 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1162 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1163 | |
| 1164 | for (iter = begin; iter < end; ) { |
| 1165 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1166 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1167 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1168 | && (iter+1) < end |
| 1169 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1170 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1171 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1172 | iter += 2; |
| 1173 | } |
| 1174 | else { |
| 1175 | *ucs4_out++ = *iter; |
| 1176 | iter++; |
| 1177 | } |
| 1178 | } |
| 1179 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1180 | _PyUnicode_GET_LENGTH(unicode))); |
| 1181 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1182 | } |
| 1183 | #endif |
| 1184 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1185 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1186 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1187 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1188 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1189 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1190 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1191 | return -1; |
| 1192 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1193 | return 0; |
| 1194 | } |
| 1195 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1196 | static int |
| 1197 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1198 | PyObject *from, Py_ssize_t from_start, |
| 1199 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1200 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1201 | unsigned int from_kind, to_kind; |
| 1202 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1203 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1204 | assert(0 <= how_many); |
| 1205 | assert(0 <= from_start); |
| 1206 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1207 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1208 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1209 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1210 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1211 | assert(PyUnicode_Check(to)); |
| 1212 | assert(PyUnicode_IS_READY(to)); |
| 1213 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1214 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1215 | if (how_many == 0) |
| 1216 | return 0; |
| 1217 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1218 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1219 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1220 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1221 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1222 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1223 | #ifdef Py_DEBUG |
| 1224 | if (!check_maxchar |
| 1225 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1226 | { |
| 1227 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1228 | Py_UCS4 ch; |
| 1229 | Py_ssize_t i; |
| 1230 | for (i=0; i < how_many; i++) { |
| 1231 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1232 | assert(ch <= to_maxchar); |
| 1233 | } |
| 1234 | } |
| 1235 | #endif |
| 1236 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1237 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1238 | if (check_maxchar |
| 1239 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1240 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1241 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1242 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1243 | Py_UCS4 max_char; |
| 1244 | max_char = ucs1lib_find_max_char(from_data, |
| 1245 | (Py_UCS1*)from_data + how_many); |
| 1246 | if (max_char >= 128) |
| 1247 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1248 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1249 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1250 | (char*)from_data + from_kind * from_start, |
| 1251 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1252 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1253 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1254 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1255 | { |
| 1256 | _PyUnicode_CONVERT_BYTES( |
| 1257 | Py_UCS1, Py_UCS2, |
| 1258 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1259 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1260 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1261 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1262 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1263 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1264 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1265 | { |
| 1266 | _PyUnicode_CONVERT_BYTES( |
| 1267 | Py_UCS1, Py_UCS4, |
| 1268 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1269 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1270 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1271 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1272 | } |
| 1273 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1274 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1275 | { |
| 1276 | _PyUnicode_CONVERT_BYTES( |
| 1277 | Py_UCS2, Py_UCS4, |
| 1278 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1279 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1280 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1281 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1282 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1283 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1284 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1285 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1286 | if (!check_maxchar) { |
| 1287 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1288 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1289 | { |
| 1290 | _PyUnicode_CONVERT_BYTES( |
| 1291 | Py_UCS2, Py_UCS1, |
| 1292 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1293 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1294 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1295 | ); |
| 1296 | } |
| 1297 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1298 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1299 | { |
| 1300 | _PyUnicode_CONVERT_BYTES( |
| 1301 | Py_UCS4, Py_UCS1, |
| 1302 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1303 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1304 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1305 | ); |
| 1306 | } |
| 1307 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1308 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1309 | { |
| 1310 | _PyUnicode_CONVERT_BYTES( |
| 1311 | Py_UCS4, Py_UCS2, |
| 1312 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1313 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1314 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1315 | ); |
| 1316 | } |
| 1317 | else { |
| 1318 | assert(0); |
| 1319 | return -1; |
| 1320 | } |
| 1321 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1322 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1323 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1324 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1325 | Py_ssize_t i; |
| 1326 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1327 | for (i=0; i < how_many; i++) { |
| 1328 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1329 | if (ch > to_maxchar) |
| 1330 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1331 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1332 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1333 | } |
| 1334 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1335 | return 0; |
| 1336 | } |
| 1337 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1338 | void |
| 1339 | _PyUnicode_FastCopyCharacters( |
| 1340 | PyObject *to, Py_ssize_t to_start, |
| 1341 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1342 | { |
| 1343 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1344 | } |
| 1345 | |
| 1346 | Py_ssize_t |
| 1347 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1348 | PyObject *from, Py_ssize_t from_start, |
| 1349 | Py_ssize_t how_many) |
| 1350 | { |
| 1351 | int err; |
| 1352 | |
| 1353 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1354 | PyErr_BadInternalCall(); |
| 1355 | return -1; |
| 1356 | } |
| 1357 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1358 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1359 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1360 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1361 | return -1; |
| 1362 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1363 | if (from_start < 0) { |
| 1364 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1365 | return -1; |
| 1366 | } |
| 1367 | if (to_start < 0) { |
| 1368 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1369 | return -1; |
| 1370 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1371 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1372 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1373 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 1374 | "Cannot write %zi characters at %zi " |
| 1375 | "in a string of %zi characters", |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1376 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1377 | return -1; |
| 1378 | } |
| 1379 | |
| 1380 | if (how_many == 0) |
| 1381 | return 0; |
| 1382 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1383 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1384 | return -1; |
| 1385 | |
| 1386 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1387 | if (err) { |
| 1388 | PyErr_Format(PyExc_SystemError, |
| 1389 | "Cannot copy %s characters " |
| 1390 | "into a string of %s characters", |
| 1391 | unicode_kind_name(from), |
| 1392 | unicode_kind_name(to)); |
| 1393 | return -1; |
| 1394 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1395 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1398 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1399 | correct string length can be computed before converting a string to UCS4. |
| 1400 | This function counts single surrogates as a character and not as a pair. |
| 1401 | |
| 1402 | Return 0 on success, or -1 on error. */ |
| 1403 | static int |
| 1404 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1405 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1406 | { |
| 1407 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1408 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1409 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1410 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1411 | *num_surrogates = 0; |
| 1412 | *maxchar = 0; |
| 1413 | |
| 1414 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1415 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1416 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1417 | && (iter+1) < end |
| 1418 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1419 | { |
| 1420 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1421 | ++(*num_surrogates); |
| 1422 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1423 | } |
| 1424 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1425 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1426 | { |
| 1427 | ch = *iter; |
| 1428 | iter++; |
| 1429 | } |
| 1430 | if (ch > *maxchar) { |
| 1431 | *maxchar = ch; |
| 1432 | if (*maxchar > MAX_UNICODE) { |
| 1433 | PyErr_Format(PyExc_ValueError, |
| 1434 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1435 | ch); |
| 1436 | return -1; |
| 1437 | } |
| 1438 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1439 | } |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1443 | int |
| 1444 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1445 | { |
| 1446 | wchar_t *end; |
| 1447 | Py_UCS4 maxchar = 0; |
| 1448 | Py_ssize_t num_surrogates; |
| 1449 | #if SIZEOF_WCHAR_T == 2 |
| 1450 | Py_ssize_t length_wo_surrogates; |
| 1451 | #endif |
| 1452 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1453 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1454 | strings were created using _PyObject_New() and where no canonical |
| 1455 | representation (the str field) has been set yet aka strings |
| 1456 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1457 | assert(_PyUnicode_CHECK(unicode)); |
| 1458 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1460 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1461 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1462 | /* Actually, it should neither be interned nor be anything else: */ |
| 1463 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1464 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1466 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1467 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1468 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1469 | |
| 1470 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1471 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1472 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1473 | PyErr_NoMemory(); |
| 1474 | return -1; |
| 1475 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1476 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1477 | _PyUnicode_WSTR(unicode), end, |
| 1478 | PyUnicode_1BYTE_DATA(unicode)); |
| 1479 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1480 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1481 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1482 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1483 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1484 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1485 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1486 | } |
| 1487 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1488 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1489 | _PyUnicode_UTF8(unicode) = NULL; |
| 1490 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1491 | } |
| 1492 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1493 | _PyUnicode_WSTR(unicode) = NULL; |
| 1494 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1495 | } |
| 1496 | /* In this case we might have to convert down from 4-byte native |
| 1497 | wchar_t to 2-byte unicode. */ |
| 1498 | else if (maxchar < 65536) { |
| 1499 | assert(num_surrogates == 0 && |
| 1500 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1501 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1502 | #if SIZEOF_WCHAR_T == 2 |
| 1503 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1504 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1505 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1506 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1507 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1508 | _PyUnicode_UTF8(unicode) = NULL; |
| 1509 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1510 | #else |
| 1511 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1512 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1513 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1514 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1515 | PyErr_NoMemory(); |
| 1516 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1517 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1518 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1519 | _PyUnicode_WSTR(unicode), end, |
| 1520 | PyUnicode_2BYTE_DATA(unicode)); |
| 1521 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1522 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1523 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1524 | _PyUnicode_UTF8(unicode) = NULL; |
| 1525 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1526 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1527 | _PyUnicode_WSTR(unicode) = NULL; |
| 1528 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1529 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1530 | } |
| 1531 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1532 | else { |
| 1533 | #if SIZEOF_WCHAR_T == 2 |
| 1534 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1535 | new normalized 4-byte version. */ |
| 1536 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 1537 | if (length_wo_surrogates > PY_SSIZE_T_MAX / 4 - 1) { |
| 1538 | PyErr_NoMemory(); |
| 1539 | return -1; |
| 1540 | } |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1541 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1542 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1543 | PyErr_NoMemory(); |
| 1544 | return -1; |
| 1545 | } |
| 1546 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1547 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1548 | _PyUnicode_UTF8(unicode) = NULL; |
| 1549 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1550 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1551 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1552 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1553 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1554 | _PyUnicode_WSTR(unicode) = NULL; |
| 1555 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1556 | #else |
| 1557 | assert(num_surrogates == 0); |
| 1558 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1559 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1560 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1561 | _PyUnicode_UTF8(unicode) = NULL; |
| 1562 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1563 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1564 | #endif |
| 1565 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1566 | } |
| 1567 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1568 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1569 | return 0; |
| 1570 | } |
| 1571 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1572 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1573 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1574 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1575 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1576 | case SSTATE_NOT_INTERNED: |
| 1577 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1578 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1579 | case SSTATE_INTERNED_MORTAL: |
| 1580 | /* revive dead object temporarily for DelItem */ |
| 1581 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1582 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1583 | Py_FatalError( |
| 1584 | "deletion of interned string failed"); |
| 1585 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1586 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1587 | case SSTATE_INTERNED_IMMORTAL: |
| 1588 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1589 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1590 | default: |
| 1591 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1594 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1595 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1596 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1597 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1598 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1599 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1600 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1601 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1604 | #ifdef Py_DEBUG |
| 1605 | static int |
| 1606 | unicode_is_singleton(PyObject *unicode) |
| 1607 | { |
| 1608 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1609 | if (unicode == unicode_empty) |
| 1610 | return 1; |
| 1611 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1612 | { |
| 1613 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1614 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1615 | return 1; |
| 1616 | } |
| 1617 | return 0; |
| 1618 | } |
| 1619 | #endif |
| 1620 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1621 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1622 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1623 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1624 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1625 | if (Py_REFCNT(unicode) != 1) |
| 1626 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1627 | if (_PyUnicode_HASH(unicode) != -1) |
| 1628 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1629 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1630 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1631 | if (!PyUnicode_CheckExact(unicode)) |
| 1632 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1633 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1634 | /* singleton refcount is greater than 1 */ |
| 1635 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1636 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1637 | return 1; |
| 1638 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1639 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1640 | static int |
| 1641 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1642 | { |
| 1643 | PyObject *unicode; |
| 1644 | Py_ssize_t old_length; |
| 1645 | |
| 1646 | assert(p_unicode != NULL); |
| 1647 | unicode = *p_unicode; |
| 1648 | |
| 1649 | assert(unicode != NULL); |
| 1650 | assert(PyUnicode_Check(unicode)); |
| 1651 | assert(0 <= length); |
| 1652 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1653 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1654 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1655 | else |
| 1656 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1657 | if (old_length == length) |
| 1658 | return 0; |
| 1659 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1660 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1661 | _Py_INCREF_UNICODE_EMPTY(); |
| 1662 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1663 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1664 | Py_DECREF(*p_unicode); |
| 1665 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1666 | return 0; |
| 1667 | } |
| 1668 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1669 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1670 | PyObject *copy = resize_copy(unicode, length); |
| 1671 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1672 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1673 | Py_DECREF(*p_unicode); |
| 1674 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1675 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1678 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1679 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1680 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1681 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1682 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1683 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1684 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1685 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1688 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1689 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1690 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1691 | PyObject *unicode; |
| 1692 | if (p_unicode == NULL) { |
| 1693 | PyErr_BadInternalCall(); |
| 1694 | return -1; |
| 1695 | } |
| 1696 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1697 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1698 | { |
| 1699 | PyErr_BadInternalCall(); |
| 1700 | return -1; |
| 1701 | } |
| 1702 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1703 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1704 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1705 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1706 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1707 | WARNING: The function doesn't copy the terminating null character and |
| 1708 | doesn't check the maximum character (may write a latin1 character in an |
| 1709 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1710 | static void |
| 1711 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1712 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1713 | { |
| 1714 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1715 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1716 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1717 | |
| 1718 | switch (kind) { |
| 1719 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1720 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1721 | #ifdef Py_DEBUG |
| 1722 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1723 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1724 | (const Py_UCS1*)str, |
| 1725 | (const Py_UCS1*)str + len); |
| 1726 | assert(maxchar < 128); |
| 1727 | } |
| 1728 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1729 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1730 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1731 | } |
| 1732 | case PyUnicode_2BYTE_KIND: { |
| 1733 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1734 | Py_UCS2 *ucs2 = start; |
| 1735 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1736 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1737 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1738 | *ucs2 = (Py_UCS2)*str; |
| 1739 | |
| 1740 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1741 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1742 | } |
| 1743 | default: { |
| 1744 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1745 | Py_UCS4 *ucs4 = start; |
| 1746 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1747 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1748 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1749 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1750 | *ucs4 = (Py_UCS4)*str; |
| 1751 | |
| 1752 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1757 | static PyObject* |
| 1758 | get_latin1_char(unsigned char ch) |
| 1759 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1760 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1761 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1762 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1763 | if (!unicode) |
| 1764 | return NULL; |
| 1765 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1766 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1767 | unicode_latin1[ch] = unicode; |
| 1768 | } |
| 1769 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1770 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1771 | } |
| 1772 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1773 | static PyObject* |
| 1774 | unicode_char(Py_UCS4 ch) |
| 1775 | { |
| 1776 | PyObject *unicode; |
| 1777 | |
| 1778 | assert(ch <= MAX_UNICODE); |
| 1779 | |
Victor Stinner | f3b46b4 | 2014-01-03 13:16:00 +0100 | [diff] [blame] | 1780 | if (ch < 256) |
| 1781 | return get_latin1_char(ch); |
| 1782 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1783 | unicode = PyUnicode_New(1, ch); |
| 1784 | if (unicode == NULL) |
| 1785 | return NULL; |
| 1786 | switch (PyUnicode_KIND(unicode)) { |
| 1787 | case PyUnicode_1BYTE_KIND: |
| 1788 | PyUnicode_1BYTE_DATA(unicode)[0] = (Py_UCS1)ch; |
| 1789 | break; |
| 1790 | case PyUnicode_2BYTE_KIND: |
| 1791 | PyUnicode_2BYTE_DATA(unicode)[0] = (Py_UCS2)ch; |
| 1792 | break; |
| 1793 | default: |
| 1794 | assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1795 | PyUnicode_4BYTE_DATA(unicode)[0] = ch; |
| 1796 | } |
| 1797 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1798 | return unicode; |
| 1799 | } |
| 1800 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1801 | PyObject * |
| 1802 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1803 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1804 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1805 | Py_UCS4 maxchar = 0; |
| 1806 | Py_ssize_t num_surrogates; |
| 1807 | |
| 1808 | if (u == NULL) |
| 1809 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1810 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1811 | /* If the Unicode data is known at construction time, we can apply |
| 1812 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1813 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1814 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1815 | if (size == 0) |
| 1816 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1818 | /* Single character Unicode objects in the Latin-1 range are |
| 1819 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1820 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1821 | return get_latin1_char((unsigned char)*u); |
| 1822 | |
| 1823 | /* If not empty and not single character, copy the Unicode data |
| 1824 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1825 | if (find_maxchar_surrogates(u, u + size, |
| 1826 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1827 | return NULL; |
| 1828 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1829 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1830 | if (!unicode) |
| 1831 | return NULL; |
| 1832 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1833 | switch (PyUnicode_KIND(unicode)) { |
| 1834 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1835 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1836 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1837 | break; |
| 1838 | case PyUnicode_2BYTE_KIND: |
| 1839 | #if Py_UNICODE_SIZE == 2 |
| 1840 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1841 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1842 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1843 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1844 | #endif |
| 1845 | break; |
| 1846 | case PyUnicode_4BYTE_KIND: |
| 1847 | #if SIZEOF_WCHAR_T == 2 |
| 1848 | /* This is the only case which has to process surrogates, thus |
| 1849 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1850 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1851 | #else |
| 1852 | assert(num_surrogates == 0); |
| 1853 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1854 | #endif |
| 1855 | break; |
| 1856 | default: |
| 1857 | assert(0 && "Impossible state"); |
| 1858 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1859 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1860 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1863 | PyObject * |
| 1864 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1865 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1866 | if (size < 0) { |
| 1867 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1868 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1869 | return NULL; |
| 1870 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1871 | if (u != NULL) |
| 1872 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1873 | else |
| 1874 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1877 | PyObject * |
| 1878 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1879 | { |
| 1880 | size_t size = strlen(u); |
| 1881 | if (size > PY_SSIZE_T_MAX) { |
| 1882 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1883 | return NULL; |
| 1884 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1885 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1888 | PyObject * |
| 1889 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1890 | { |
| 1891 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1892 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1893 | strlen(id->string), |
| 1894 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1895 | if (!id->object) |
| 1896 | return NULL; |
| 1897 | PyUnicode_InternInPlace(&id->object); |
| 1898 | assert(!id->next); |
| 1899 | id->next = static_strings; |
| 1900 | static_strings = id; |
| 1901 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1902 | return id->object; |
| 1903 | } |
| 1904 | |
| 1905 | void |
| 1906 | _PyUnicode_ClearStaticStrings() |
| 1907 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1908 | _Py_Identifier *tmp, *s = static_strings; |
| 1909 | while (s) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 1910 | Py_CLEAR(s->object); |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1911 | tmp = s->next; |
| 1912 | s->next = NULL; |
| 1913 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1914 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1915 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1916 | } |
| 1917 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1918 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1919 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1920 | PyObject* |
| 1921 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1922 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1923 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1924 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1925 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1926 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1927 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1928 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1929 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1930 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1931 | unicode = PyUnicode_New(size, 127); |
| 1932 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1933 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1934 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1935 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1936 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1937 | } |
| 1938 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1939 | static Py_UCS4 |
| 1940 | kind_maxchar_limit(unsigned int kind) |
| 1941 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1942 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1943 | case PyUnicode_1BYTE_KIND: |
| 1944 | return 0x80; |
| 1945 | case PyUnicode_2BYTE_KIND: |
| 1946 | return 0x100; |
| 1947 | case PyUnicode_4BYTE_KIND: |
| 1948 | return 0x10000; |
| 1949 | default: |
| 1950 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1951 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1955 | Py_LOCAL_INLINE(Py_UCS4) |
| 1956 | align_maxchar(Py_UCS4 maxchar) |
| 1957 | { |
| 1958 | if (maxchar <= 127) |
| 1959 | return 127; |
| 1960 | else if (maxchar <= 255) |
| 1961 | return 255; |
| 1962 | else if (maxchar <= 65535) |
| 1963 | return 65535; |
| 1964 | else |
| 1965 | return MAX_UNICODE; |
| 1966 | } |
| 1967 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1968 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1969 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1970 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1971 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1972 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1973 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1974 | if (size == 0) |
| 1975 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1976 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1977 | if (size == 1) |
| 1978 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1979 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1980 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1981 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1982 | if (!res) |
| 1983 | return NULL; |
| 1984 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1985 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1986 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1989 | static PyObject* |
| 1990 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1991 | { |
| 1992 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1993 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1994 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1995 | if (size == 0) |
| 1996 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1997 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1998 | if (size == 1) |
| 1999 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2000 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2001 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2002 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2003 | if (!res) |
| 2004 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2005 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2006 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2007 | else { |
| 2008 | _PyUnicode_CONVERT_BYTES( |
| 2009 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 2010 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2011 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2012 | return res; |
| 2013 | } |
| 2014 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2015 | static PyObject* |
| 2016 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2017 | { |
| 2018 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2019 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2020 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2021 | if (size == 0) |
| 2022 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2023 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2024 | if (size == 1) |
| 2025 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2026 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2027 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2028 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2029 | if (!res) |
| 2030 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2031 | if (max_char < 256) |
| 2032 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2033 | PyUnicode_1BYTE_DATA(res)); |
| 2034 | else if (max_char < 0x10000) |
| 2035 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2036 | PyUnicode_2BYTE_DATA(res)); |
| 2037 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2038 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2039 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2040 | return res; |
| 2041 | } |
| 2042 | |
| 2043 | PyObject* |
| 2044 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2045 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2046 | if (size < 0) { |
| 2047 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2048 | return NULL; |
| 2049 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2050 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2051 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2052 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2053 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2054 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2055 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2056 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2057 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2058 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2059 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2060 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2061 | } |
| 2062 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2063 | Py_UCS4 |
| 2064 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2065 | { |
| 2066 | enum PyUnicode_Kind kind; |
| 2067 | void *startptr, *endptr; |
| 2068 | |
| 2069 | assert(PyUnicode_IS_READY(unicode)); |
| 2070 | assert(0 <= start); |
| 2071 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2072 | assert(start <= end); |
| 2073 | |
| 2074 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2075 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2076 | |
| 2077 | if (start == end) |
| 2078 | return 127; |
| 2079 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2080 | if (PyUnicode_IS_ASCII(unicode)) |
| 2081 | return 127; |
| 2082 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2083 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2084 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2085 | endptr = (char *)startptr + end * kind; |
| 2086 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2087 | switch(kind) { |
| 2088 | case PyUnicode_1BYTE_KIND: |
| 2089 | return ucs1lib_find_max_char(startptr, endptr); |
| 2090 | case PyUnicode_2BYTE_KIND: |
| 2091 | return ucs2lib_find_max_char(startptr, endptr); |
| 2092 | case PyUnicode_4BYTE_KIND: |
| 2093 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2094 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2095 | assert(0); |
| 2096 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2100 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2101 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2102 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2103 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2104 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2105 | { |
| 2106 | PyObject *unicode, *copy; |
| 2107 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2108 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2109 | unsigned int kind; |
| 2110 | |
| 2111 | assert(p_unicode != NULL); |
| 2112 | unicode = *p_unicode; |
| 2113 | assert(PyUnicode_IS_READY(unicode)); |
| 2114 | if (PyUnicode_IS_ASCII(unicode)) |
| 2115 | return; |
| 2116 | |
| 2117 | len = PyUnicode_GET_LENGTH(unicode); |
| 2118 | kind = PyUnicode_KIND(unicode); |
| 2119 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2120 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2121 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2122 | if (max_char >= 128) |
| 2123 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2124 | } |
| 2125 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2126 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2127 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2128 | if (max_char >= 256) |
| 2129 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2130 | } |
| 2131 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2132 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2133 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2134 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2135 | if (max_char >= 0x10000) |
| 2136 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2137 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2138 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2139 | if (copy != NULL) |
| 2140 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2141 | Py_DECREF(unicode); |
| 2142 | *p_unicode = copy; |
| 2143 | } |
| 2144 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2145 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2146 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2147 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2148 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2149 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2150 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2151 | if (!PyUnicode_Check(unicode)) { |
| 2152 | PyErr_BadInternalCall(); |
| 2153 | return NULL; |
| 2154 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2155 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2156 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2157 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2158 | length = PyUnicode_GET_LENGTH(unicode); |
| 2159 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2160 | if (!copy) |
| 2161 | return NULL; |
| 2162 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2163 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2164 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2165 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2166 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2167 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2168 | } |
| 2169 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2170 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2171 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2172 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2173 | |
| 2174 | void* |
| 2175 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2176 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2177 | Py_ssize_t len; |
| 2178 | void *result; |
| 2179 | unsigned int skind; |
| 2180 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2181 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2182 | return NULL; |
| 2183 | |
| 2184 | len = PyUnicode_GET_LENGTH(s); |
| 2185 | skind = PyUnicode_KIND(s); |
| 2186 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2187 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2188 | return NULL; |
| 2189 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2190 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2191 | case PyUnicode_2BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2192 | result = PyMem_New(Py_UCS2, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2193 | if (!result) |
| 2194 | return PyErr_NoMemory(); |
| 2195 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2196 | _PyUnicode_CONVERT_BYTES( |
| 2197 | Py_UCS1, Py_UCS2, |
| 2198 | PyUnicode_1BYTE_DATA(s), |
| 2199 | PyUnicode_1BYTE_DATA(s) + len, |
| 2200 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2201 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2202 | case PyUnicode_4BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2203 | result = PyMem_New(Py_UCS4, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2204 | if (!result) |
| 2205 | return PyErr_NoMemory(); |
| 2206 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2207 | _PyUnicode_CONVERT_BYTES( |
| 2208 | Py_UCS2, Py_UCS4, |
| 2209 | PyUnicode_2BYTE_DATA(s), |
| 2210 | PyUnicode_2BYTE_DATA(s) + len, |
| 2211 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2212 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2213 | else { |
| 2214 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2215 | _PyUnicode_CONVERT_BYTES( |
| 2216 | Py_UCS1, Py_UCS4, |
| 2217 | PyUnicode_1BYTE_DATA(s), |
| 2218 | PyUnicode_1BYTE_DATA(s) + len, |
| 2219 | result); |
| 2220 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2221 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2222 | default: |
| 2223 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2224 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2225 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2226 | return NULL; |
| 2227 | } |
| 2228 | |
| 2229 | static Py_UCS4* |
| 2230 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2231 | int copy_null) |
| 2232 | { |
| 2233 | int kind; |
| 2234 | void *data; |
| 2235 | Py_ssize_t len, targetlen; |
| 2236 | if (PyUnicode_READY(string) == -1) |
| 2237 | return NULL; |
| 2238 | kind = PyUnicode_KIND(string); |
| 2239 | data = PyUnicode_DATA(string); |
| 2240 | len = PyUnicode_GET_LENGTH(string); |
| 2241 | targetlen = len; |
| 2242 | if (copy_null) |
| 2243 | targetlen++; |
| 2244 | if (!target) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2245 | target = PyMem_New(Py_UCS4, targetlen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2246 | if (!target) { |
| 2247 | PyErr_NoMemory(); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | } |
| 2251 | else { |
| 2252 | if (targetsize < targetlen) { |
| 2253 | PyErr_Format(PyExc_SystemError, |
| 2254 | "string is longer than the buffer"); |
| 2255 | if (copy_null && 0 < targetsize) |
| 2256 | target[0] = 0; |
| 2257 | return NULL; |
| 2258 | } |
| 2259 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2260 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2261 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2262 | _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] | 2263 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2264 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2265 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2266 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2267 | } |
| 2268 | else { |
| 2269 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2270 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2271 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2272 | if (copy_null) |
| 2273 | target[len] = 0; |
| 2274 | return target; |
| 2275 | } |
| 2276 | |
| 2277 | Py_UCS4* |
| 2278 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2279 | int copy_null) |
| 2280 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2281 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2282 | PyErr_BadInternalCall(); |
| 2283 | return NULL; |
| 2284 | } |
| 2285 | return as_ucs4(string, target, targetsize, copy_null); |
| 2286 | } |
| 2287 | |
| 2288 | Py_UCS4* |
| 2289 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2290 | { |
| 2291 | return as_ucs4(string, NULL, 0, 1); |
| 2292 | } |
| 2293 | |
| 2294 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2295 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2296 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2297 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2298 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2299 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2301 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2302 | PyErr_BadInternalCall(); |
| 2303 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2306 | if (size == -1) { |
| 2307 | size = wcslen(w); |
| 2308 | } |
| 2309 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2310 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2313 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2314 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2315 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2316 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2317 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2318 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2319 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2320 | static int |
| 2321 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2322 | Py_ssize_t width, Py_ssize_t precision) |
| 2323 | { |
| 2324 | Py_ssize_t length, fill, arglen; |
| 2325 | Py_UCS4 maxchar; |
| 2326 | |
| 2327 | if (PyUnicode_READY(str) == -1) |
| 2328 | return -1; |
| 2329 | |
| 2330 | length = PyUnicode_GET_LENGTH(str); |
| 2331 | if ((precision == -1 || precision >= length) |
| 2332 | && width <= length) |
| 2333 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2334 | |
| 2335 | if (precision != -1) |
| 2336 | length = Py_MIN(precision, length); |
| 2337 | |
| 2338 | arglen = Py_MAX(length, width); |
| 2339 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2340 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2341 | else |
| 2342 | maxchar = writer->maxchar; |
| 2343 | |
| 2344 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2345 | return -1; |
| 2346 | |
| 2347 | if (width > length) { |
| 2348 | fill = width - length; |
| 2349 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2350 | return -1; |
| 2351 | writer->pos += fill; |
| 2352 | } |
| 2353 | |
| 2354 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2355 | str, 0, length); |
| 2356 | writer->pos += length; |
| 2357 | return 0; |
| 2358 | } |
| 2359 | |
| 2360 | static int |
| 2361 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2362 | Py_ssize_t width, Py_ssize_t precision) |
| 2363 | { |
| 2364 | /* UTF-8 */ |
| 2365 | Py_ssize_t length; |
| 2366 | PyObject *unicode; |
| 2367 | int res; |
| 2368 | |
| 2369 | length = strlen(str); |
| 2370 | if (precision != -1) |
| 2371 | length = Py_MIN(length, precision); |
| 2372 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2373 | if (unicode == NULL) |
| 2374 | return -1; |
| 2375 | |
| 2376 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2377 | Py_DECREF(unicode); |
| 2378 | return res; |
| 2379 | } |
| 2380 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2381 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2382 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2383 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2384 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2385 | const char *p; |
| 2386 | Py_ssize_t len; |
| 2387 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2388 | Py_ssize_t width; |
| 2389 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2390 | int longflag; |
| 2391 | int longlongflag; |
| 2392 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2393 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2394 | |
| 2395 | p = f; |
| 2396 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2397 | zeropad = 0; |
| 2398 | if (*f == '0') { |
| 2399 | zeropad = 1; |
| 2400 | f++; |
| 2401 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2402 | |
| 2403 | /* 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] | 2404 | width = -1; |
| 2405 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2406 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2407 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2408 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2409 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2410 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2411 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2412 | return NULL; |
| 2413 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2414 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2415 | f++; |
| 2416 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2417 | } |
| 2418 | precision = -1; |
| 2419 | if (*f == '.') { |
| 2420 | f++; |
| 2421 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2422 | precision = (*f - '0'); |
| 2423 | f++; |
| 2424 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2425 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2426 | PyErr_SetString(PyExc_ValueError, |
| 2427 | "precision too big"); |
| 2428 | return NULL; |
| 2429 | } |
| 2430 | precision = (precision * 10) + (*f - '0'); |
| 2431 | f++; |
| 2432 | } |
| 2433 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2434 | if (*f == '%') { |
| 2435 | /* "%.3%s" => f points to "3" */ |
| 2436 | f--; |
| 2437 | } |
| 2438 | } |
| 2439 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2440 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2441 | f--; |
| 2442 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2443 | |
| 2444 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2445 | longflag = 0; |
| 2446 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2447 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2448 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2449 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2450 | longflag = 1; |
| 2451 | ++f; |
| 2452 | } |
| 2453 | #ifdef HAVE_LONG_LONG |
| 2454 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2455 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2456 | longlongflag = 1; |
| 2457 | f += 2; |
| 2458 | } |
| 2459 | #endif |
| 2460 | } |
| 2461 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2462 | 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] | 2463 | size_tflag = 1; |
| 2464 | ++f; |
| 2465 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2466 | |
| 2467 | if (f[1] == '\0') |
| 2468 | writer->overallocate = 0; |
| 2469 | |
| 2470 | switch (*f) { |
| 2471 | case 'c': |
| 2472 | { |
| 2473 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2474 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2475 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2476 | "character argument not in range(0x110000)"); |
| 2477 | return NULL; |
| 2478 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2479 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2480 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2481 | break; |
| 2482 | } |
| 2483 | |
| 2484 | case 'i': |
| 2485 | case 'd': |
| 2486 | case 'u': |
| 2487 | case 'x': |
| 2488 | { |
| 2489 | /* used by sprintf */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2490 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2491 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2492 | |
| 2493 | if (*f == 'u') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2494 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2495 | len = sprintf(buffer, "%lu", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2496 | va_arg(*vargs, unsigned long)); |
| 2497 | #ifdef HAVE_LONG_LONG |
| 2498 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2499 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2500 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2501 | #endif |
| 2502 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2503 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2504 | va_arg(*vargs, size_t)); |
| 2505 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2506 | len = sprintf(buffer, "%u", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2507 | va_arg(*vargs, unsigned int)); |
| 2508 | } |
| 2509 | else if (*f == 'x') { |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2510 | len = sprintf(buffer, "%x", va_arg(*vargs, int)); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2511 | } |
| 2512 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2513 | if (longflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2514 | len = sprintf(buffer, "%li", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2515 | va_arg(*vargs, long)); |
| 2516 | #ifdef HAVE_LONG_LONG |
| 2517 | else if (longlongflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2518 | len = sprintf(buffer, "%" PY_FORMAT_LONG_LONG "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2519 | va_arg(*vargs, PY_LONG_LONG)); |
| 2520 | #endif |
| 2521 | else if (size_tflag) |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2522 | len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2523 | va_arg(*vargs, Py_ssize_t)); |
| 2524 | else |
Victor Stinner | 3aa979e | 2014-11-18 21:40:51 +0100 | [diff] [blame] | 2525 | len = sprintf(buffer, "%i", |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2526 | va_arg(*vargs, int)); |
| 2527 | } |
| 2528 | assert(len >= 0); |
| 2529 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2530 | if (precision < len) |
| 2531 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2532 | |
| 2533 | arglen = Py_MAX(precision, width); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2534 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2535 | return NULL; |
| 2536 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2537 | if (width > precision) { |
| 2538 | Py_UCS4 fillchar; |
| 2539 | fill = width - precision; |
| 2540 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2541 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2542 | return NULL; |
| 2543 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2544 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2545 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2546 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2547 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2548 | return NULL; |
| 2549 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2550 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2551 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2552 | if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) |
| 2553 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2554 | break; |
| 2555 | } |
| 2556 | |
| 2557 | case 'p': |
| 2558 | { |
| 2559 | char number[MAX_LONG_LONG_CHARS]; |
| 2560 | |
| 2561 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2562 | assert(len >= 0); |
| 2563 | |
| 2564 | /* %p is ill-defined: ensure leading 0x. */ |
| 2565 | if (number[1] == 'X') |
| 2566 | number[1] = 'x'; |
| 2567 | else if (number[1] != 'x') { |
| 2568 | memmove(number + 2, number, |
| 2569 | strlen(number) + 1); |
| 2570 | number[0] = '0'; |
| 2571 | number[1] = 'x'; |
| 2572 | len += 2; |
| 2573 | } |
| 2574 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2575 | if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2576 | return NULL; |
| 2577 | break; |
| 2578 | } |
| 2579 | |
| 2580 | case 's': |
| 2581 | { |
| 2582 | /* UTF-8 */ |
| 2583 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2584 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2585 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2586 | break; |
| 2587 | } |
| 2588 | |
| 2589 | case 'U': |
| 2590 | { |
| 2591 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2592 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2593 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2594 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2595 | return NULL; |
| 2596 | break; |
| 2597 | } |
| 2598 | |
| 2599 | case 'V': |
| 2600 | { |
| 2601 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2602 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2603 | if (obj) { |
| 2604 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2605 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2606 | return NULL; |
| 2607 | } |
| 2608 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2609 | assert(str != NULL); |
| 2610 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2611 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2612 | } |
| 2613 | break; |
| 2614 | } |
| 2615 | |
| 2616 | case 'S': |
| 2617 | { |
| 2618 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2619 | PyObject *str; |
| 2620 | assert(obj); |
| 2621 | str = PyObject_Str(obj); |
| 2622 | if (!str) |
| 2623 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2624 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2625 | Py_DECREF(str); |
| 2626 | return NULL; |
| 2627 | } |
| 2628 | Py_DECREF(str); |
| 2629 | break; |
| 2630 | } |
| 2631 | |
| 2632 | case 'R': |
| 2633 | { |
| 2634 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2635 | PyObject *repr; |
| 2636 | assert(obj); |
| 2637 | repr = PyObject_Repr(obj); |
| 2638 | if (!repr) |
| 2639 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2640 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2641 | Py_DECREF(repr); |
| 2642 | return NULL; |
| 2643 | } |
| 2644 | Py_DECREF(repr); |
| 2645 | break; |
| 2646 | } |
| 2647 | |
| 2648 | case 'A': |
| 2649 | { |
| 2650 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2651 | PyObject *ascii; |
| 2652 | assert(obj); |
| 2653 | ascii = PyObject_ASCII(obj); |
| 2654 | if (!ascii) |
| 2655 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2656 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2657 | Py_DECREF(ascii); |
| 2658 | return NULL; |
| 2659 | } |
| 2660 | Py_DECREF(ascii); |
| 2661 | break; |
| 2662 | } |
| 2663 | |
| 2664 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2665 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2666 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2667 | break; |
| 2668 | |
| 2669 | default: |
| 2670 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2671 | of the format string to the output string. (we cannot just |
| 2672 | skip the code, since there's no way to know what's in the |
| 2673 | argument list) */ |
| 2674 | len = strlen(p); |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2675 | if (_PyUnicodeWriter_WriteLatin1String(writer, p, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2676 | return NULL; |
| 2677 | f = p+len; |
| 2678 | return f; |
| 2679 | } |
| 2680 | |
| 2681 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2682 | return f; |
| 2683 | } |
| 2684 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2685 | PyObject * |
| 2686 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2687 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2688 | va_list vargs2; |
| 2689 | const char *f; |
| 2690 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2691 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2692 | _PyUnicodeWriter_Init(&writer); |
| 2693 | writer.min_length = strlen(format) + 100; |
| 2694 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2695 | |
| 2696 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2697 | Copy it to be able to pass a reference to a subfunction. */ |
| 2698 | Py_VA_COPY(vargs2, vargs); |
| 2699 | |
| 2700 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2701 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2702 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2703 | if (f == NULL) |
| 2704 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2705 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2706 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2707 | const char *p; |
| 2708 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2709 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2710 | p = f; |
| 2711 | do |
| 2712 | { |
| 2713 | if ((unsigned char)*p > 127) { |
| 2714 | PyErr_Format(PyExc_ValueError, |
| 2715 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2716 | "string, got a non-ASCII byte: 0x%02x", |
| 2717 | (unsigned char)*p); |
| 2718 | return NULL; |
| 2719 | } |
| 2720 | p++; |
| 2721 | } |
| 2722 | while (*p != '\0' && *p != '%'); |
| 2723 | len = p - f; |
| 2724 | |
| 2725 | if (*p == '\0') |
| 2726 | writer.overallocate = 0; |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2727 | |
| 2728 | if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2729 | goto fail; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2730 | |
| 2731 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2732 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2733 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2734 | return _PyUnicodeWriter_Finish(&writer); |
| 2735 | |
| 2736 | fail: |
| 2737 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2738 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2741 | PyObject * |
| 2742 | PyUnicode_FromFormat(const char *format, ...) |
| 2743 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2744 | PyObject* ret; |
| 2745 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2746 | |
| 2747 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2748 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2749 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2750 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2751 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2752 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2753 | va_end(vargs); |
| 2754 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2757 | #ifdef HAVE_WCHAR_H |
| 2758 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2759 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2760 | convert a Unicode object to a wide character string. |
| 2761 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2762 | - 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] | 2763 | character) required to convert the unicode object. Ignore size argument. |
| 2764 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2765 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2766 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2767 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2768 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2769 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2770 | wchar_t *w, |
| 2771 | Py_ssize_t size) |
| 2772 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2773 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2774 | const wchar_t *wstr; |
| 2775 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2776 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2777 | if (wstr == NULL) |
| 2778 | return -1; |
| 2779 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2780 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2781 | if (size > res) |
| 2782 | size = res + 1; |
| 2783 | else |
| 2784 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2785 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2786 | return res; |
| 2787 | } |
| 2788 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2789 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2793 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2794 | wchar_t *w, |
| 2795 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2796 | { |
| 2797 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2798 | PyErr_BadInternalCall(); |
| 2799 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2800 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2801 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2804 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2805 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2806 | Py_ssize_t *size) |
| 2807 | { |
| 2808 | wchar_t* buffer; |
| 2809 | Py_ssize_t buflen; |
| 2810 | |
| 2811 | if (unicode == NULL) { |
| 2812 | PyErr_BadInternalCall(); |
| 2813 | return NULL; |
| 2814 | } |
| 2815 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2816 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2817 | if (buflen == -1) |
| 2818 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2819 | buffer = PyMem_NEW(wchar_t, buflen); |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2820 | if (buffer == NULL) { |
| 2821 | PyErr_NoMemory(); |
| 2822 | return NULL; |
| 2823 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2824 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2825 | if (buflen == -1) { |
| 2826 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2827 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2828 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2829 | if (size != NULL) |
| 2830 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2831 | return buffer; |
| 2832 | } |
| 2833 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2834 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2835 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2836 | PyObject * |
| 2837 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2838 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2839 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2840 | PyErr_SetString(PyExc_ValueError, |
| 2841 | "chr() arg not in range(0x110000)"); |
| 2842 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2843 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2844 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2845 | return unicode_char((Py_UCS4)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2848 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2849 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2850 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2851 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2852 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2853 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2854 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2855 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2856 | Py_INCREF(obj); |
| 2857 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2858 | } |
| 2859 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2860 | /* For a Unicode subtype that's not a Unicode object, |
| 2861 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2862 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2863 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2864 | PyErr_Format(PyExc_TypeError, |
| 2865 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2866 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2867 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2870 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2871 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2872 | const char *encoding, |
| 2873 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2874 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2875 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2876 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2877 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2878 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2879 | PyErr_BadInternalCall(); |
| 2880 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2881 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2882 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2883 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2884 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2885 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2886 | _Py_RETURN_UNICODE_EMPTY(); |
| 2887 | v = PyUnicode_Decode( |
| 2888 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2889 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2890 | return v; |
| 2891 | } |
| 2892 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2893 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2894 | PyErr_SetString(PyExc_TypeError, |
| 2895 | "decoding str is not supported"); |
| 2896 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2897 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2898 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2899 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2900 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2901 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 2902 | "coercing to str: need a bytes-like object, %.80s found", |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2903 | Py_TYPE(obj)->tp_name); |
| 2904 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2905 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2906 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2907 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2908 | PyBuffer_Release(&buffer); |
| 2909 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2910 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2911 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2912 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2913 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2914 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2917 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2918 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2919 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2920 | int |
| 2921 | _Py_normalize_encoding(const char *encoding, |
| 2922 | char *lower, |
| 2923 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2924 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2925 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2926 | char *l; |
| 2927 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2928 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2929 | if (encoding == NULL) { |
Victor Stinner | 66b3270 | 2013-11-07 23:12:23 +0100 | [diff] [blame] | 2930 | /* 6 == strlen("utf-8") + 1 */ |
Victor Stinner | df23e30 | 2013-11-07 13:33:36 +0100 | [diff] [blame] | 2931 | if (lower_len < 6) |
| 2932 | return 0; |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2933 | strcpy(lower, "utf-8"); |
| 2934 | return 1; |
| 2935 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2936 | e = encoding; |
| 2937 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2938 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2939 | while (*e) { |
| 2940 | if (l == l_end) |
| 2941 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2942 | if (Py_ISUPPER(*e)) { |
| 2943 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2944 | } |
| 2945 | else if (*e == '_') { |
| 2946 | *l++ = '-'; |
| 2947 | e++; |
| 2948 | } |
| 2949 | else { |
| 2950 | *l++ = *e++; |
| 2951 | } |
| 2952 | } |
| 2953 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2954 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2957 | PyObject * |
| 2958 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2959 | Py_ssize_t size, |
| 2960 | const char *encoding, |
| 2961 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2962 | { |
| 2963 | PyObject *buffer = NULL, *unicode; |
| 2964 | Py_buffer info; |
| 2965 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2966 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2967 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2968 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2969 | if ((strcmp(lower, "utf-8") == 0) || |
| 2970 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2971 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2972 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2973 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 2974 | (strcmp(lower, "iso-8859-1") == 0) || |
| 2975 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2976 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2977 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2978 | else if (strcmp(lower, "mbcs") == 0) |
| 2979 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2980 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2981 | else if (strcmp(lower, "ascii") == 0) |
| 2982 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2983 | else if (strcmp(lower, "utf-16") == 0) |
| 2984 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2985 | else if (strcmp(lower, "utf-32") == 0) |
| 2986 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2987 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2988 | |
| 2989 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2990 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2991 | 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] | 2992 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2993 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2994 | if (buffer == NULL) |
| 2995 | goto onError; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2996 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2997 | if (unicode == NULL) |
| 2998 | goto onError; |
| 2999 | if (!PyUnicode_Check(unicode)) { |
| 3000 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3001 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3002 | "use codecs.decode() to decode to arbitrary types", |
| 3003 | encoding, |
| 3004 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3005 | Py_DECREF(unicode); |
| 3006 | goto onError; |
| 3007 | } |
| 3008 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3009 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3010 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3011 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3012 | Py_XDECREF(buffer); |
| 3013 | return NULL; |
| 3014 | } |
| 3015 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3016 | PyObject * |
| 3017 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3018 | const char *encoding, |
| 3019 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3020 | { |
| 3021 | PyObject *v; |
| 3022 | |
| 3023 | if (!PyUnicode_Check(unicode)) { |
| 3024 | PyErr_BadArgument(); |
| 3025 | goto onError; |
| 3026 | } |
| 3027 | |
| 3028 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3029 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3030 | |
| 3031 | /* Decode via the codec registry */ |
| 3032 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3033 | if (v == NULL) |
| 3034 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3035 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3036 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3037 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3038 | return NULL; |
| 3039 | } |
| 3040 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3041 | PyObject * |
| 3042 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3043 | const char *encoding, |
| 3044 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3045 | { |
| 3046 | PyObject *v; |
| 3047 | |
| 3048 | if (!PyUnicode_Check(unicode)) { |
| 3049 | PyErr_BadArgument(); |
| 3050 | goto onError; |
| 3051 | } |
| 3052 | |
| 3053 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3054 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3055 | |
| 3056 | /* Decode via the codec registry */ |
| 3057 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3058 | if (v == NULL) |
| 3059 | goto onError; |
| 3060 | if (!PyUnicode_Check(v)) { |
| 3061 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3062 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3063 | "use codecs.decode() to decode to arbitrary types", |
| 3064 | encoding, |
| 3065 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3066 | Py_DECREF(v); |
| 3067 | goto onError; |
| 3068 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3069 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3070 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3071 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3072 | return NULL; |
| 3073 | } |
| 3074 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3075 | PyObject * |
| 3076 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3077 | Py_ssize_t size, |
| 3078 | const char *encoding, |
| 3079 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3080 | { |
| 3081 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3082 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3083 | unicode = PyUnicode_FromUnicode(s, size); |
| 3084 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3085 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3086 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3087 | Py_DECREF(unicode); |
| 3088 | return v; |
| 3089 | } |
| 3090 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3091 | PyObject * |
| 3092 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3093 | const char *encoding, |
| 3094 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3095 | { |
| 3096 | PyObject *v; |
| 3097 | |
| 3098 | if (!PyUnicode_Check(unicode)) { |
| 3099 | PyErr_BadArgument(); |
| 3100 | goto onError; |
| 3101 | } |
| 3102 | |
| 3103 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3104 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3105 | |
| 3106 | /* Encode via the codec registry */ |
| 3107 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3108 | if (v == NULL) |
| 3109 | goto onError; |
| 3110 | return v; |
| 3111 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3112 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3113 | return NULL; |
| 3114 | } |
| 3115 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3116 | static size_t |
| 3117 | wcstombs_errorpos(const wchar_t *wstr) |
| 3118 | { |
| 3119 | size_t len; |
| 3120 | #if SIZEOF_WCHAR_T == 2 |
| 3121 | wchar_t buf[3]; |
| 3122 | #else |
| 3123 | wchar_t buf[2]; |
| 3124 | #endif |
| 3125 | char outbuf[MB_LEN_MAX]; |
| 3126 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3127 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3128 | #if SIZEOF_WCHAR_T == 2 |
| 3129 | buf[2] = 0; |
| 3130 | #else |
| 3131 | buf[1] = 0; |
| 3132 | #endif |
| 3133 | start = wstr; |
| 3134 | while (*wstr != L'\0') |
| 3135 | { |
| 3136 | previous = wstr; |
| 3137 | #if SIZEOF_WCHAR_T == 2 |
| 3138 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3139 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3140 | { |
| 3141 | buf[0] = wstr[0]; |
| 3142 | buf[1] = wstr[1]; |
| 3143 | wstr += 2; |
| 3144 | } |
| 3145 | else { |
| 3146 | buf[0] = *wstr; |
| 3147 | buf[1] = 0; |
| 3148 | wstr++; |
| 3149 | } |
| 3150 | #else |
| 3151 | buf[0] = *wstr; |
| 3152 | wstr++; |
| 3153 | #endif |
| 3154 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3155 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3156 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3160 | return 0; |
| 3161 | } |
| 3162 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3163 | static int |
| 3164 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3165 | { |
| 3166 | if (errors == NULL) { |
| 3167 | *surrogateescape = 0; |
| 3168 | return 0; |
| 3169 | } |
| 3170 | |
| 3171 | if (strcmp(errors, "strict") == 0) { |
| 3172 | *surrogateescape = 0; |
| 3173 | return 0; |
| 3174 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3175 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3176 | *surrogateescape = 1; |
| 3177 | return 0; |
| 3178 | } |
| 3179 | PyErr_Format(PyExc_ValueError, |
| 3180 | "only 'strict' and 'surrogateescape' error handlers " |
| 3181 | "are supported, not '%s'", |
| 3182 | errors); |
| 3183 | return -1; |
| 3184 | } |
| 3185 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3186 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3187 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3188 | { |
| 3189 | Py_ssize_t wlen, wlen2; |
| 3190 | wchar_t *wstr; |
| 3191 | PyObject *bytes = NULL; |
| 3192 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3193 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3194 | PyObject *exc; |
| 3195 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3196 | int surrogateescape; |
| 3197 | |
| 3198 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3199 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3200 | |
| 3201 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3202 | if (wstr == NULL) |
| 3203 | return NULL; |
| 3204 | |
| 3205 | wlen2 = wcslen(wstr); |
| 3206 | if (wlen2 != wlen) { |
| 3207 | PyMem_Free(wstr); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3208 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3209 | return NULL; |
| 3210 | } |
| 3211 | |
| 3212 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3213 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3214 | char *str; |
| 3215 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3216 | str = Py_EncodeLocale(wstr, &error_pos); |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3217 | if (str == NULL) { |
| 3218 | if (error_pos == (size_t)-1) { |
| 3219 | PyErr_NoMemory(); |
| 3220 | PyMem_Free(wstr); |
| 3221 | return NULL; |
| 3222 | } |
| 3223 | else { |
| 3224 | goto encode_error; |
| 3225 | } |
| 3226 | } |
| 3227 | PyMem_Free(wstr); |
| 3228 | |
| 3229 | bytes = PyBytes_FromString(str); |
| 3230 | PyMem_Free(str); |
| 3231 | } |
| 3232 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3233 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3234 | size_t len, len2; |
| 3235 | |
| 3236 | len = wcstombs(NULL, wstr, 0); |
| 3237 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3238 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3239 | goto encode_error; |
| 3240 | } |
| 3241 | |
| 3242 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3243 | if (bytes == NULL) { |
| 3244 | PyMem_Free(wstr); |
| 3245 | return NULL; |
| 3246 | } |
| 3247 | |
| 3248 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3249 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3250 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3251 | goto encode_error; |
| 3252 | } |
| 3253 | PyMem_Free(wstr); |
| 3254 | } |
| 3255 | return bytes; |
| 3256 | |
| 3257 | encode_error: |
| 3258 | errmsg = strerror(errno); |
| 3259 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3260 | |
| 3261 | if (error_pos == (size_t)-1) |
| 3262 | error_pos = wcstombs_errorpos(wstr); |
| 3263 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3264 | PyMem_Free(wstr); |
| 3265 | Py_XDECREF(bytes); |
| 3266 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3267 | if (errmsg != NULL) { |
| 3268 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3269 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3270 | if (wstr != NULL) { |
| 3271 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3272 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3273 | } else |
| 3274 | errmsg = NULL; |
| 3275 | } |
| 3276 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3277 | reason = PyUnicode_FromString( |
| 3278 | "wcstombs() encountered an unencodable " |
| 3279 | "wide character"); |
| 3280 | if (reason == NULL) |
| 3281 | return NULL; |
| 3282 | |
| 3283 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3284 | "locale", unicode, |
| 3285 | (Py_ssize_t)error_pos, |
| 3286 | (Py_ssize_t)(error_pos+1), |
| 3287 | reason); |
| 3288 | Py_DECREF(reason); |
| 3289 | if (exc != NULL) { |
| 3290 | PyCodec_StrictErrors(exc); |
| 3291 | Py_XDECREF(exc); |
| 3292 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3293 | return NULL; |
| 3294 | } |
| 3295 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3296 | PyObject * |
| 3297 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3298 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3299 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3300 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3301 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3302 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3303 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3304 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3305 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3306 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3307 | the Python codec requires to encode at least its own filename. Use the C |
| 3308 | version of the locale codec until the codec registry is initialized and |
| 3309 | the Python codec is loaded. |
| 3310 | |
| 3311 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3312 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3313 | subinterpreters. */ |
| 3314 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3315 | return PyUnicode_AsEncodedString(unicode, |
| 3316 | Py_FileSystemDefaultEncoding, |
| 3317 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3318 | } |
| 3319 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3320 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3321 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3322 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3325 | PyObject * |
| 3326 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3327 | const char *encoding, |
| 3328 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3329 | { |
| 3330 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3331 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3332 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3333 | if (!PyUnicode_Check(unicode)) { |
| 3334 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3335 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3336 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3337 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3338 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3339 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3340 | if ((strcmp(lower, "utf-8") == 0) || |
| 3341 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3342 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3343 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3344 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3345 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3346 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3347 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3348 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3349 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3350 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3351 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3352 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3353 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3354 | else if (strcmp(lower, "mbcs") == 0) |
| 3355 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3356 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3357 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3358 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3359 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3360 | |
| 3361 | /* Encode via the codec registry */ |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3362 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3363 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3364 | return NULL; |
| 3365 | |
| 3366 | /* The normal path */ |
| 3367 | if (PyBytes_Check(v)) |
| 3368 | return v; |
| 3369 | |
| 3370 | /* 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] | 3371 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3372 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3373 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3374 | |
| 3375 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3376 | "encoder %s returned bytearray instead of bytes; " |
| 3377 | "use codecs.encode() to encode to arbitrary types", |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3378 | encoding); |
| 3379 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3380 | Py_DECREF(v); |
| 3381 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3382 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3383 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3384 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3385 | Py_DECREF(v); |
| 3386 | return b; |
| 3387 | } |
| 3388 | |
| 3389 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3390 | "'%.400s' encoder returned '%.400s' instead of 'bytes'; " |
| 3391 | "use codecs.encode() to encode to arbitrary types", |
| 3392 | encoding, |
| 3393 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3394 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3395 | return NULL; |
| 3396 | } |
| 3397 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3398 | PyObject * |
| 3399 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3400 | const char *encoding, |
| 3401 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3402 | { |
| 3403 | PyObject *v; |
| 3404 | |
| 3405 | if (!PyUnicode_Check(unicode)) { |
| 3406 | PyErr_BadArgument(); |
| 3407 | goto onError; |
| 3408 | } |
| 3409 | |
| 3410 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3411 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3412 | |
| 3413 | /* Encode via the codec registry */ |
| 3414 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3415 | if (v == NULL) |
| 3416 | goto onError; |
| 3417 | if (!PyUnicode_Check(v)) { |
| 3418 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3419 | "'%.400s' encoder returned '%.400s' instead of 'str'; " |
| 3420 | "use codecs.encode() to encode to arbitrary types", |
| 3421 | encoding, |
| 3422 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3423 | Py_DECREF(v); |
| 3424 | goto onError; |
| 3425 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3426 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3427 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3428 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3429 | return NULL; |
| 3430 | } |
| 3431 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3432 | static size_t |
| 3433 | mbstowcs_errorpos(const char *str, size_t len) |
| 3434 | { |
| 3435 | #ifdef HAVE_MBRTOWC |
| 3436 | const char *start = str; |
| 3437 | mbstate_t mbs; |
| 3438 | size_t converted; |
| 3439 | wchar_t ch; |
| 3440 | |
| 3441 | memset(&mbs, 0, sizeof mbs); |
| 3442 | while (len) |
| 3443 | { |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3444 | converted = mbrtowc(&ch, str, len, &mbs); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3445 | if (converted == 0) |
| 3446 | /* Reached end of string */ |
| 3447 | break; |
| 3448 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3449 | /* Conversion error or incomplete character */ |
| 3450 | return str - start; |
| 3451 | } |
| 3452 | else { |
| 3453 | str += converted; |
| 3454 | len -= converted; |
| 3455 | } |
| 3456 | } |
| 3457 | /* failed to find the undecodable byte sequence */ |
| 3458 | return 0; |
| 3459 | #endif |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3463 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3464 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3465 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3466 | { |
| 3467 | wchar_t smallbuf[256]; |
| 3468 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3469 | wchar_t *wstr; |
| 3470 | size_t wlen, wlen2; |
| 3471 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3472 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3473 | size_t error_pos; |
| 3474 | char *errmsg; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 3475 | PyObject *reason = NULL; /* initialize to prevent gcc warning */ |
| 3476 | PyObject *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3477 | |
| 3478 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3479 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3480 | |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3481 | if (str[len] != '\0' || (size_t)len != strlen(str)) { |
| 3482 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3483 | return NULL; |
| 3484 | } |
| 3485 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3486 | if (surrogateescape) { |
| 3487 | /* "surrogateescape" error handler */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3488 | wstr = Py_DecodeLocale(str, &wlen); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3489 | if (wstr == NULL) { |
| 3490 | if (wlen == (size_t)-1) |
| 3491 | PyErr_NoMemory(); |
| 3492 | else |
| 3493 | PyErr_SetFromErrno(PyExc_OSError); |
| 3494 | return NULL; |
| 3495 | } |
| 3496 | |
| 3497 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3498 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3499 | } |
| 3500 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3501 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3502 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3503 | wlen = mbstowcs(NULL, str, 0); |
| 3504 | #else |
| 3505 | wlen = len; |
| 3506 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3507 | if (wlen == (size_t)-1) |
| 3508 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3509 | if (wlen+1 <= smallbuf_len) { |
| 3510 | wstr = smallbuf; |
| 3511 | } |
| 3512 | else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3513 | wstr = PyMem_New(wchar_t, wlen+1); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3514 | if (!wstr) |
| 3515 | return PyErr_NoMemory(); |
| 3516 | } |
| 3517 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3518 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3519 | if (wlen2 == (size_t)-1) { |
| 3520 | if (wstr != smallbuf) |
| 3521 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3522 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3523 | } |
| 3524 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3525 | assert(wlen2 == wlen); |
| 3526 | #endif |
| 3527 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3528 | if (wstr != smallbuf) |
| 3529 | PyMem_Free(wstr); |
| 3530 | } |
| 3531 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3532 | |
| 3533 | decode_error: |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3534 | reason = NULL; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3535 | errmsg = strerror(errno); |
| 3536 | assert(errmsg != NULL); |
| 3537 | |
| 3538 | error_pos = mbstowcs_errorpos(str, len); |
| 3539 | if (errmsg != NULL) { |
| 3540 | size_t errlen; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 3541 | wstr = Py_DecodeLocale(errmsg, &errlen); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3542 | if (wstr != NULL) { |
| 3543 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3544 | PyMem_RawFree(wstr); |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3545 | } |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3546 | } |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3547 | if (reason == NULL) |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3548 | reason = PyUnicode_FromString( |
| 3549 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3550 | if (reason == NULL) |
| 3551 | return NULL; |
| 3552 | |
| 3553 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3554 | "locale", str, len, |
| 3555 | (Py_ssize_t)error_pos, |
| 3556 | (Py_ssize_t)(error_pos+1), |
| 3557 | reason); |
| 3558 | Py_DECREF(reason); |
| 3559 | if (exc != NULL) { |
| 3560 | PyCodec_StrictErrors(exc); |
| 3561 | Py_XDECREF(exc); |
| 3562 | } |
| 3563 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3567 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3568 | { |
| 3569 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3570 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | |
| 3574 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3575 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3576 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3577 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3578 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3579 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3580 | PyObject* |
| 3581 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3582 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3583 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3584 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3585 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3586 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3587 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3588 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3589 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3590 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3591 | the Python codec requires to encode at least its own filename. Use the C |
| 3592 | version of the locale codec until the codec registry is initialized and |
| 3593 | the Python codec is loaded. |
| 3594 | |
| 3595 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3596 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3597 | subinterpreters. */ |
| 3598 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3599 | return PyUnicode_Decode(s, size, |
| 3600 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3601 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3602 | } |
| 3603 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3604 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3605 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3606 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3609 | |
| 3610 | int |
| 3611 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3612 | { |
| 3613 | PyObject *output = NULL; |
| 3614 | Py_ssize_t size; |
| 3615 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3616 | if (arg == NULL) { |
| 3617 | Py_DECREF(*(PyObject**)addr); |
| 3618 | return 1; |
| 3619 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3620 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3621 | output = arg; |
| 3622 | Py_INCREF(output); |
| 3623 | } |
| 3624 | else { |
| 3625 | arg = PyUnicode_FromObject(arg); |
| 3626 | if (!arg) |
| 3627 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3628 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3629 | Py_DECREF(arg); |
| 3630 | if (!output) |
| 3631 | return 0; |
| 3632 | if (!PyBytes_Check(output)) { |
| 3633 | Py_DECREF(output); |
| 3634 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3635 | return 0; |
| 3636 | } |
| 3637 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3638 | size = PyBytes_GET_SIZE(output); |
| 3639 | data = PyBytes_AS_STRING(output); |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 3640 | if ((size_t)size != strlen(data)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3641 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3642 | Py_DECREF(output); |
| 3643 | return 0; |
| 3644 | } |
| 3645 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3646 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3650 | int |
| 3651 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3652 | { |
| 3653 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3654 | if (arg == NULL) { |
| 3655 | Py_DECREF(*(PyObject**)addr); |
| 3656 | return 1; |
| 3657 | } |
| 3658 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3659 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3660 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3661 | output = arg; |
| 3662 | Py_INCREF(output); |
| 3663 | } |
| 3664 | else { |
| 3665 | arg = PyBytes_FromObject(arg); |
| 3666 | if (!arg) |
| 3667 | return 0; |
| 3668 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3669 | PyBytes_GET_SIZE(arg)); |
| 3670 | Py_DECREF(arg); |
| 3671 | if (!output) |
| 3672 | return 0; |
| 3673 | if (!PyUnicode_Check(output)) { |
| 3674 | Py_DECREF(output); |
| 3675 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3676 | return 0; |
| 3677 | } |
| 3678 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3679 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3680 | Py_DECREF(output); |
| 3681 | return 0; |
| 3682 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3683 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3684 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 3685 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3686 | Py_DECREF(output); |
| 3687 | return 0; |
| 3688 | } |
| 3689 | *(PyObject**)addr = output; |
| 3690 | return Py_CLEANUP_SUPPORTED; |
| 3691 | } |
| 3692 | |
| 3693 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3694 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3695 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3696 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3697 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3698 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3699 | if (!PyUnicode_Check(unicode)) { |
| 3700 | PyErr_BadArgument(); |
| 3701 | return NULL; |
| 3702 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3703 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3704 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3705 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3706 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3707 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3708 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3709 | if (bytes == NULL) |
| 3710 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3711 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3712 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3713 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3714 | Py_DECREF(bytes); |
| 3715 | return NULL; |
| 3716 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3717 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3718 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3719 | PyBytes_AS_STRING(bytes), |
| 3720 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3721 | Py_DECREF(bytes); |
| 3722 | } |
| 3723 | |
| 3724 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3725 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3726 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3730 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3731 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3732 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3733 | } |
| 3734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3735 | Py_UNICODE * |
| 3736 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3737 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3738 | const unsigned char *one_byte; |
| 3739 | #if SIZEOF_WCHAR_T == 4 |
| 3740 | const Py_UCS2 *two_bytes; |
| 3741 | #else |
| 3742 | const Py_UCS4 *four_bytes; |
| 3743 | const Py_UCS4 *ucs4_end; |
| 3744 | Py_ssize_t num_surrogates; |
| 3745 | #endif |
| 3746 | wchar_t *w; |
| 3747 | wchar_t *wchar_end; |
| 3748 | |
| 3749 | if (!PyUnicode_Check(unicode)) { |
| 3750 | PyErr_BadArgument(); |
| 3751 | return NULL; |
| 3752 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3753 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3754 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3755 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3756 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3757 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3758 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3759 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3760 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3761 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3762 | num_surrogates = 0; |
| 3763 | |
| 3764 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3765 | if (*four_bytes > 0xFFFF) |
| 3766 | ++num_surrogates; |
| 3767 | } |
| 3768 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3769 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3770 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3771 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3772 | PyErr_NoMemory(); |
| 3773 | return NULL; |
| 3774 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3775 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3776 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3777 | w = _PyUnicode_WSTR(unicode); |
| 3778 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3779 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3780 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3781 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3782 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3783 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3784 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3785 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3786 | } |
| 3787 | else |
| 3788 | *w = *four_bytes; |
| 3789 | |
| 3790 | if (w > wchar_end) { |
| 3791 | assert(0 && "Miscalculated string end"); |
| 3792 | } |
| 3793 | } |
| 3794 | *w = 0; |
| 3795 | #else |
| 3796 | /* sizeof(wchar_t) == 4 */ |
| 3797 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3798 | "should share memory already."); |
| 3799 | return NULL; |
| 3800 | #endif |
| 3801 | } |
| 3802 | else { |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 3803 | if ((size_t)_PyUnicode_LENGTH(unicode) > |
| 3804 | PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 3805 | PyErr_NoMemory(); |
| 3806 | return NULL; |
| 3807 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3808 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3809 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3810 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3811 | PyErr_NoMemory(); |
| 3812 | return NULL; |
| 3813 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3814 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3815 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3816 | w = _PyUnicode_WSTR(unicode); |
| 3817 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3818 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3819 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3820 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3821 | for (; w < wchar_end; ++one_byte, ++w) |
| 3822 | *w = *one_byte; |
| 3823 | /* null-terminate the wstr */ |
| 3824 | *w = 0; |
| 3825 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3826 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3827 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3828 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3829 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3830 | *w = *two_bytes; |
| 3831 | /* null-terminate the wstr */ |
| 3832 | *w = 0; |
| 3833 | #else |
| 3834 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3835 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3836 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3837 | Py_FatalError("Impossible unicode object state, wstr " |
| 3838 | "and str should share memory already."); |
| 3839 | return NULL; |
| 3840 | #endif |
| 3841 | } |
| 3842 | else { |
| 3843 | assert(0 && "This should never happen."); |
| 3844 | } |
| 3845 | } |
| 3846 | } |
| 3847 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3848 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3849 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3852 | Py_UNICODE * |
| 3853 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3854 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3855 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3858 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3859 | Py_ssize_t |
| 3860 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3861 | { |
| 3862 | if (!PyUnicode_Check(unicode)) { |
| 3863 | PyErr_BadArgument(); |
| 3864 | goto onError; |
| 3865 | } |
| 3866 | return PyUnicode_GET_SIZE(unicode); |
| 3867 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3868 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3869 | return -1; |
| 3870 | } |
| 3871 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3872 | Py_ssize_t |
| 3873 | PyUnicode_GetLength(PyObject *unicode) |
| 3874 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3875 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3876 | PyErr_BadArgument(); |
| 3877 | return -1; |
| 3878 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3879 | if (PyUnicode_READY(unicode) == -1) |
| 3880 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3881 | return PyUnicode_GET_LENGTH(unicode); |
| 3882 | } |
| 3883 | |
| 3884 | Py_UCS4 |
| 3885 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3886 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3887 | void *data; |
| 3888 | int kind; |
| 3889 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3890 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3891 | PyErr_BadArgument(); |
| 3892 | return (Py_UCS4)-1; |
| 3893 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3894 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3895 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3896 | return (Py_UCS4)-1; |
| 3897 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3898 | data = PyUnicode_DATA(unicode); |
| 3899 | kind = PyUnicode_KIND(unicode); |
| 3900 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | int |
| 3904 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3905 | { |
| 3906 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3907 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3908 | return -1; |
| 3909 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3910 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3911 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3912 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3913 | return -1; |
| 3914 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3915 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3916 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3917 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3918 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3919 | return -1; |
| 3920 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3921 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3922 | index, ch); |
| 3923 | return 0; |
| 3924 | } |
| 3925 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3926 | const char * |
| 3927 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3928 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3929 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3932 | /* create or adjust a UnicodeDecodeError */ |
| 3933 | static void |
| 3934 | make_decode_exception(PyObject **exceptionObject, |
| 3935 | const char *encoding, |
| 3936 | const char *input, Py_ssize_t length, |
| 3937 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3938 | const char *reason) |
| 3939 | { |
| 3940 | if (*exceptionObject == NULL) { |
| 3941 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3942 | encoding, input, length, startpos, endpos, reason); |
| 3943 | } |
| 3944 | else { |
| 3945 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3946 | goto onError; |
| 3947 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3948 | goto onError; |
| 3949 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3950 | goto onError; |
| 3951 | } |
| 3952 | return; |
| 3953 | |
| 3954 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3955 | Py_CLEAR(*exceptionObject); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3958 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3959 | /* error handling callback helper: |
| 3960 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3961 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3962 | and adjust various state variables. |
| 3963 | return 0 on success, -1 on error |
| 3964 | */ |
| 3965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3966 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3967 | unicode_decode_call_errorhandler_wchar( |
| 3968 | const char *errors, PyObject **errorHandler, |
| 3969 | const char *encoding, const char *reason, |
| 3970 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3971 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3972 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3973 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3974 | 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] | 3975 | |
| 3976 | PyObject *restuple = NULL; |
| 3977 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3978 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3979 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3980 | Py_ssize_t requiredsize; |
| 3981 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3982 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3983 | wchar_t *repwstr; |
| 3984 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3985 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3986 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 3987 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3988 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3989 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3990 | *errorHandler = PyCodec_LookupError(errors); |
| 3991 | if (*errorHandler == NULL) |
| 3992 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3995 | make_decode_exception(exceptionObject, |
| 3996 | encoding, |
| 3997 | *input, *inend - *input, |
| 3998 | *startinpos, *endinpos, |
| 3999 | reason); |
| 4000 | if (*exceptionObject == NULL) |
| 4001 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4002 | |
| 4003 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4004 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4005 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4006 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4007 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4008 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4009 | } |
| 4010 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4011 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4012 | |
| 4013 | /* Copy back the bytes variables, which might have been modified by the |
| 4014 | callback */ |
| 4015 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4016 | if (!inputobj) |
| 4017 | goto onError; |
| 4018 | if (!PyBytes_Check(inputobj)) { |
| 4019 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4020 | } |
| 4021 | *input = PyBytes_AS_STRING(inputobj); |
| 4022 | insize = PyBytes_GET_SIZE(inputobj); |
| 4023 | *inend = *input + insize; |
| 4024 | /* we can DECREF safely, as the exception has another reference, |
| 4025 | so the object won't go away. */ |
| 4026 | Py_DECREF(inputobj); |
| 4027 | |
| 4028 | if (newpos<0) |
| 4029 | newpos = insize+newpos; |
| 4030 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4031 | 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] | 4032 | goto onError; |
| 4033 | } |
| 4034 | |
| 4035 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4036 | if (repwstr == NULL) |
| 4037 | goto onError; |
| 4038 | /* need more space? (at least enough for what we |
| 4039 | have+the replacement+the rest of the string (starting |
| 4040 | at the new input position), so we won't have to check space |
| 4041 | when there are no errors in the rest of the string) */ |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4042 | requiredsize = *outpos; |
| 4043 | if (requiredsize > PY_SSIZE_T_MAX - repwlen) |
| 4044 | goto overflow; |
| 4045 | requiredsize += repwlen; |
| 4046 | if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) |
| 4047 | goto overflow; |
| 4048 | requiredsize += insize - newpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4049 | if (requiredsize > outsize) { |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4050 | if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4051 | requiredsize = 2*outsize; |
| 4052 | if (unicode_resize(output, requiredsize) < 0) |
| 4053 | goto onError; |
| 4054 | } |
| 4055 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4056 | *outpos += repwlen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4057 | *endinpos = newpos; |
| 4058 | *inptr = *input + newpos; |
| 4059 | |
| 4060 | /* we made it! */ |
| 4061 | Py_XDECREF(restuple); |
| 4062 | return 0; |
| 4063 | |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4064 | overflow: |
| 4065 | PyErr_SetString(PyExc_OverflowError, |
| 4066 | "decoded result is too long for a Python string"); |
| 4067 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4068 | onError: |
| 4069 | Py_XDECREF(restuple); |
| 4070 | return -1; |
| 4071 | } |
| 4072 | #endif /* HAVE_MBCS */ |
| 4073 | |
| 4074 | static int |
| 4075 | unicode_decode_call_errorhandler_writer( |
| 4076 | const char *errors, PyObject **errorHandler, |
| 4077 | const char *encoding, const char *reason, |
| 4078 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4079 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4080 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4081 | { |
| 4082 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4083 | |
| 4084 | PyObject *restuple = NULL; |
| 4085 | PyObject *repunicode = NULL; |
| 4086 | Py_ssize_t insize; |
| 4087 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4088 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4089 | PyObject *inputobj = NULL; |
| 4090 | |
| 4091 | if (*errorHandler == NULL) { |
| 4092 | *errorHandler = PyCodec_LookupError(errors); |
| 4093 | if (*errorHandler == NULL) |
| 4094 | goto onError; |
| 4095 | } |
| 4096 | |
| 4097 | make_decode_exception(exceptionObject, |
| 4098 | encoding, |
| 4099 | *input, *inend - *input, |
| 4100 | *startinpos, *endinpos, |
| 4101 | reason); |
| 4102 | if (*exceptionObject == NULL) |
| 4103 | goto onError; |
| 4104 | |
| 4105 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4106 | if (restuple == NULL) |
| 4107 | goto onError; |
| 4108 | if (!PyTuple_Check(restuple)) { |
| 4109 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4110 | goto onError; |
| 4111 | } |
| 4112 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4113 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4114 | |
| 4115 | /* Copy back the bytes variables, which might have been modified by the |
| 4116 | callback */ |
| 4117 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4118 | if (!inputobj) |
| 4119 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4120 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4121 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4122 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4123 | *input = PyBytes_AS_STRING(inputobj); |
| 4124 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4125 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4126 | /* we can DECREF safely, as the exception has another reference, |
| 4127 | so the object won't go away. */ |
| 4128 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4129 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4130 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4131 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4132 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4133 | 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] | 4134 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4135 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4136 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4137 | if (PyUnicode_READY(repunicode) < 0) |
| 4138 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4139 | replen = PyUnicode_GET_LENGTH(repunicode); |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4140 | if (replen > 1) { |
| 4141 | writer->min_length += replen - 1; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4142 | writer->overallocate = 1; |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4143 | if (_PyUnicodeWriter_Prepare(writer, writer->min_length, |
| 4144 | PyUnicode_MAX_CHAR_VALUE(repunicode)) == -1) |
| 4145 | goto onError; |
| 4146 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4147 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4148 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4149 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4150 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4151 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4152 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4153 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4154 | Py_XDECREF(restuple); |
| 4155 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4156 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4157 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4158 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4159 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4162 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4163 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4164 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4165 | |
| 4166 | /* Three simple macros defining base-64. */ |
| 4167 | |
| 4168 | /* Is c a base-64 character? */ |
| 4169 | |
| 4170 | #define IS_BASE64(c) \ |
| 4171 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4172 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4173 | ((c) >= '0' && (c) <= '9') || \ |
| 4174 | (c) == '+' || (c) == '/') |
| 4175 | |
| 4176 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4177 | |
| 4178 | #define FROM_BASE64(c) \ |
| 4179 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4180 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4181 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4182 | (c) == '+' ? 62 : 63) |
| 4183 | |
| 4184 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4185 | |
| 4186 | #define TO_BASE64(n) \ |
| 4187 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4188 | |
| 4189 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4190 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4191 | * byte not decoding to itself is the + which begins a base64 |
| 4192 | * string. */ |
| 4193 | |
| 4194 | #define DECODE_DIRECT(c) \ |
| 4195 | ((c) <= 127 && (c) != '+') |
| 4196 | |
| 4197 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4198 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4199 | * the above). See RFC2152. This array identifies these different |
| 4200 | * sets: |
| 4201 | * 0 : "Set D" |
| 4202 | * alphanumeric and '(),-./:? |
| 4203 | * 1 : "Set O" |
| 4204 | * !"#$%&*;<=>@[]^_`{|} |
| 4205 | * 2 : "whitespace" |
| 4206 | * ht nl cr sp |
| 4207 | * 3 : special (must be base64 encoded) |
| 4208 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4209 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4210 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4211 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4212 | char utf7_category[128] = { |
| 4213 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4214 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4215 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4216 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4217 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4218 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4219 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4220 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4221 | /* @ A B C D E F G H I J K L M N O */ |
| 4222 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4223 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4224 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4225 | /* ` a b c d e f g h i j k l m n o */ |
| 4226 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4227 | /* p q r s t u v w x y z { | } ~ del */ |
| 4228 | 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] | 4229 | }; |
| 4230 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4231 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4232 | * answer depends on whether we are encoding set O as itself, and also |
| 4233 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4234 | * clear that the answers to these questions vary between |
| 4235 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4236 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4237 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4238 | ((c) < 128 && (c) > 0 && \ |
| 4239 | ((utf7_category[(c)] == 0) || \ |
| 4240 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4241 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4242 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4243 | PyObject * |
| 4244 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4245 | Py_ssize_t size, |
| 4246 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4247 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4248 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4249 | } |
| 4250 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4251 | /* The decoder. The only state we preserve is our read position, |
| 4252 | * i.e. how many characters we have consumed. So if we end in the |
| 4253 | * middle of a shift sequence we have to back off the read position |
| 4254 | * and the output to the beginning of the sequence, otherwise we lose |
| 4255 | * all the shift state (seen bits, number of bits seen, high |
| 4256 | * surrogate). */ |
| 4257 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4258 | PyObject * |
| 4259 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4260 | Py_ssize_t size, |
| 4261 | const char *errors, |
| 4262 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4263 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4264 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4265 | Py_ssize_t startinpos; |
| 4266 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4267 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4268 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4269 | const char *errmsg = ""; |
| 4270 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4271 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4272 | unsigned int base64bits = 0; |
| 4273 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4274 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4275 | PyObject *errorHandler = NULL; |
| 4276 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4277 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4278 | if (size == 0) { |
| 4279 | if (consumed) |
| 4280 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4281 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4282 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4283 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4284 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4285 | _PyUnicodeWriter_Init(&writer); |
| 4286 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4287 | |
| 4288 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4289 | e = s + size; |
| 4290 | |
| 4291 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4292 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4293 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4294 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4295 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4296 | if (inShift) { /* in a base-64 section */ |
| 4297 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4298 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4299 | base64bits += 6; |
| 4300 | s++; |
| 4301 | if (base64bits >= 16) { |
| 4302 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4303 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4304 | base64bits -= 16; |
| 4305 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4306 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4307 | if (surrogate) { |
| 4308 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4309 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4310 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4311 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4312 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4313 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4314 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4315 | } |
| 4316 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4317 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4318 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4319 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4320 | } |
| 4321 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4322 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4323 | /* first surrogate */ |
| 4324 | surrogate = outCh; |
| 4325 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4326 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4327 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4328 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4329 | } |
| 4330 | } |
| 4331 | } |
| 4332 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4333 | inShift = 0; |
| 4334 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4335 | if (surrogate) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4336 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4337 | goto onError; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4338 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4339 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4340 | if (base64bits > 0) { /* left-over bits */ |
| 4341 | if (base64bits >= 6) { |
| 4342 | /* We've seen at least one base-64 character */ |
| 4343 | errmsg = "partial character in shift sequence"; |
| 4344 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4345 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4346 | else { |
| 4347 | /* Some bits remain; they should be zero */ |
| 4348 | if (base64buffer != 0) { |
| 4349 | errmsg = "non-zero padding bits in shift sequence"; |
| 4350 | goto utf7Error; |
| 4351 | } |
| 4352 | } |
| 4353 | } |
| 4354 | if (ch != '-') { |
| 4355 | /* '-' is absorbed; other terminating |
| 4356 | characters are preserved */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4357 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4358 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4359 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
| 4362 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4364 | s++; /* consume '+' */ |
| 4365 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4366 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4367 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4368 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | } |
| 4370 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4371 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4372 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4373 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4374 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4375 | } |
| 4376 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4377 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4378 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4379 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4380 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4381 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4382 | else { |
| 4383 | startinpos = s-starts; |
| 4384 | s++; |
| 4385 | errmsg = "unexpected special character"; |
| 4386 | goto utf7Error; |
| 4387 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4388 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4389 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4390 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4391 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4392 | errors, &errorHandler, |
| 4393 | "utf7", errmsg, |
| 4394 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4395 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4396 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4399 | /* end of string */ |
| 4400 | |
| 4401 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4402 | /* if we're in an inconsistent state, that's an error */ |
| 4403 | if (surrogate || |
| 4404 | (base64bits >= 6) || |
| 4405 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4406 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4407 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4408 | errors, &errorHandler, |
| 4409 | "utf7", "unterminated shift sequence", |
| 4410 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4411 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4412 | goto onError; |
| 4413 | if (s < e) |
| 4414 | goto restart; |
| 4415 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4417 | |
| 4418 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4419 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4420 | if (inShift) { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4421 | *consumed = startinpos; |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4422 | if (writer.pos != shiftOutStart && writer.maxchar > 127) { |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4423 | PyObject *result = PyUnicode_FromKindAndData( |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4424 | writer.kind, writer.data, shiftOutStart); |
| 4425 | Py_XDECREF(errorHandler); |
| 4426 | Py_XDECREF(exc); |
| 4427 | _PyUnicodeWriter_Dealloc(&writer); |
| 4428 | return result; |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4429 | } |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4430 | writer.pos = shiftOutStart; /* back off output */ |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4431 | } |
| 4432 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4433 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4434 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4435 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4436 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4437 | Py_XDECREF(errorHandler); |
| 4438 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4439 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4440 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4441 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4442 | Py_XDECREF(errorHandler); |
| 4443 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4444 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4445 | return NULL; |
| 4446 | } |
| 4447 | |
| 4448 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4449 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4450 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4451 | int base64SetO, |
| 4452 | int base64WhiteSpace, |
| 4453 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4454 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4455 | int kind; |
| 4456 | void *data; |
| 4457 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4458 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4459 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4460 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | unsigned int base64bits = 0; |
| 4462 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4463 | char * out; |
| 4464 | char * start; |
| 4465 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4466 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4467 | return NULL; |
| 4468 | kind = PyUnicode_KIND(str); |
| 4469 | data = PyUnicode_DATA(str); |
| 4470 | len = PyUnicode_GET_LENGTH(str); |
| 4471 | |
| 4472 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4473 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4474 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4475 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4476 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4477 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4478 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4479 | if (v == NULL) |
| 4480 | return NULL; |
| 4481 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4482 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4483 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4484 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4485 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4486 | if (inShift) { |
| 4487 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4488 | /* shifting out */ |
| 4489 | if (base64bits) { /* output remaining bits */ |
| 4490 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4491 | base64buffer = 0; |
| 4492 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4493 | } |
| 4494 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4495 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4496 | so no '-' is required, except if the character is itself a '-' */ |
| 4497 | if (IS_BASE64(ch) || ch == '-') { |
| 4498 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4499 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4500 | *out++ = (char) ch; |
| 4501 | } |
| 4502 | else { |
| 4503 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4504 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4505 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4506 | else { /* not in a shift sequence */ |
| 4507 | if (ch == '+') { |
| 4508 | *out++ = '+'; |
| 4509 | *out++ = '-'; |
| 4510 | } |
| 4511 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4512 | *out++ = (char) ch; |
| 4513 | } |
| 4514 | else { |
| 4515 | *out++ = '+'; |
| 4516 | inShift = 1; |
| 4517 | goto encode_char; |
| 4518 | } |
| 4519 | } |
| 4520 | continue; |
| 4521 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4522 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4523 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4524 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4525 | /* code first surrogate */ |
| 4526 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4527 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4528 | while (base64bits >= 6) { |
| 4529 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4530 | base64bits -= 6; |
| 4531 | } |
| 4532 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4533 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4534 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4535 | base64bits += 16; |
| 4536 | base64buffer = (base64buffer << 16) | ch; |
| 4537 | while (base64bits >= 6) { |
| 4538 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4539 | base64bits -= 6; |
| 4540 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4541 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4542 | if (base64bits) |
| 4543 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4544 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4545 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4546 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4547 | return NULL; |
| 4548 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4549 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4550 | PyObject * |
| 4551 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4552 | Py_ssize_t size, |
| 4553 | int base64SetO, |
| 4554 | int base64WhiteSpace, |
| 4555 | const char *errors) |
| 4556 | { |
| 4557 | PyObject *result; |
| 4558 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4559 | if (tmp == NULL) |
| 4560 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4561 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4562 | base64WhiteSpace, errors); |
| 4563 | Py_DECREF(tmp); |
| 4564 | return result; |
| 4565 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4566 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4567 | #undef IS_BASE64 |
| 4568 | #undef FROM_BASE64 |
| 4569 | #undef TO_BASE64 |
| 4570 | #undef DECODE_DIRECT |
| 4571 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4572 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4573 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4574 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4575 | PyObject * |
| 4576 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4577 | Py_ssize_t size, |
| 4578 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4579 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4580 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4581 | } |
| 4582 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4583 | #include "stringlib/asciilib.h" |
| 4584 | #include "stringlib/codecs.h" |
| 4585 | #include "stringlib/undef.h" |
| 4586 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4587 | #include "stringlib/ucs1lib.h" |
| 4588 | #include "stringlib/codecs.h" |
| 4589 | #include "stringlib/undef.h" |
| 4590 | |
| 4591 | #include "stringlib/ucs2lib.h" |
| 4592 | #include "stringlib/codecs.h" |
| 4593 | #include "stringlib/undef.h" |
| 4594 | |
| 4595 | #include "stringlib/ucs4lib.h" |
| 4596 | #include "stringlib/codecs.h" |
| 4597 | #include "stringlib/undef.h" |
| 4598 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4599 | /* Mask to quickly check whether a C 'long' contains a |
| 4600 | non-ASCII, UTF8-encoded char. */ |
| 4601 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4602 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4603 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4604 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4605 | #else |
| 4606 | # error C 'long' size should be either 4 or 8! |
| 4607 | #endif |
| 4608 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4609 | static Py_ssize_t |
| 4610 | 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] | 4611 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4612 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4613 | 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] | 4614 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4615 | /* |
| 4616 | * Issue #17237: m68k is a bit different from most architectures in |
| 4617 | * that objects do not use "natural alignment" - for example, int and |
| 4618 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4619 | * won't work; also, tests have shown that skipping the "optimised |
| 4620 | * version" will even speed up m68k. |
| 4621 | */ |
| 4622 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4623 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4624 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4625 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4626 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4627 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4628 | /* Help allocation */ |
| 4629 | const char *_p = p; |
| 4630 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4631 | while (_p < aligned_end) { |
| 4632 | unsigned long value = *(const unsigned long *) _p; |
| 4633 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4634 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4635 | *((unsigned long *)q) = value; |
| 4636 | _p += SIZEOF_LONG; |
| 4637 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4638 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4639 | p = _p; |
| 4640 | while (p < end) { |
| 4641 | if ((unsigned char)*p & 0x80) |
| 4642 | break; |
| 4643 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4644 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4645 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4646 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4647 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4648 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4649 | while (p < end) { |
| 4650 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4651 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4652 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4653 | /* Help allocation */ |
| 4654 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4655 | while (_p < aligned_end) { |
| 4656 | unsigned long value = *(unsigned long *) _p; |
| 4657 | if (value & ASCII_CHAR_MASK) |
| 4658 | break; |
| 4659 | _p += SIZEOF_LONG; |
| 4660 | } |
| 4661 | p = _p; |
| 4662 | if (_p == end) |
| 4663 | break; |
| 4664 | } |
| 4665 | if ((unsigned char)*p & 0x80) |
| 4666 | break; |
| 4667 | ++p; |
| 4668 | } |
| 4669 | memcpy(dest, start, p - start); |
| 4670 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4671 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4672 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4673 | PyObject * |
| 4674 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4675 | Py_ssize_t size, |
| 4676 | const char *errors, |
| 4677 | Py_ssize_t *consumed) |
| 4678 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4679 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4680 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4681 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4682 | |
| 4683 | Py_ssize_t startinpos; |
| 4684 | Py_ssize_t endinpos; |
| 4685 | const char *errmsg = ""; |
| 4686 | PyObject *errorHandler = NULL; |
| 4687 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4688 | |
| 4689 | if (size == 0) { |
| 4690 | if (consumed) |
| 4691 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4692 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4693 | } |
| 4694 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4695 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4696 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4697 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4698 | *consumed = 1; |
| 4699 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4700 | } |
| 4701 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4702 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4703 | writer.min_length = size; |
| 4704 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4705 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4706 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4707 | writer.pos = ascii_decode(s, end, writer.data); |
| 4708 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4709 | while (s < end) { |
| 4710 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4711 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4712 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4713 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4714 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4715 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4716 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4717 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4718 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4719 | } else { |
| 4720 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4721 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | switch (ch) { |
| 4725 | case 0: |
| 4726 | if (s == end || consumed) |
| 4727 | goto End; |
| 4728 | errmsg = "unexpected end of data"; |
| 4729 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4730 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4731 | break; |
| 4732 | case 1: |
| 4733 | errmsg = "invalid start byte"; |
| 4734 | startinpos = s - starts; |
| 4735 | endinpos = startinpos + 1; |
| 4736 | break; |
| 4737 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4738 | case 3: |
| 4739 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4740 | errmsg = "invalid continuation byte"; |
| 4741 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4742 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4743 | break; |
| 4744 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4745 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4746 | goto onError; |
| 4747 | continue; |
| 4748 | } |
| 4749 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4750 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4751 | errors, &errorHandler, |
| 4752 | "utf-8", errmsg, |
| 4753 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4754 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4755 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4756 | } |
| 4757 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4758 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4759 | if (consumed) |
| 4760 | *consumed = s - starts; |
| 4761 | |
| 4762 | Py_XDECREF(errorHandler); |
| 4763 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4764 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4765 | |
| 4766 | onError: |
| 4767 | Py_XDECREF(errorHandler); |
| 4768 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4769 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4770 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4771 | } |
| 4772 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4773 | #ifdef __APPLE__ |
| 4774 | |
| 4775 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4776 | used to decode the command line arguments on Mac OS X. |
| 4777 | |
| 4778 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4779 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4780 | |
| 4781 | wchar_t* |
| 4782 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4783 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4784 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4785 | wchar_t *unicode; |
| 4786 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4787 | |
| 4788 | /* Note: size will always be longer than the resulting Unicode |
| 4789 | character count */ |
Victor Stinner | f50e187 | 2015-03-20 11:32:24 +0100 | [diff] [blame] | 4790 | 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] | 4791 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4792 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4793 | if (!unicode) |
| 4794 | return NULL; |
| 4795 | |
| 4796 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4797 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4798 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4799 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4800 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4801 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4802 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4803 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4804 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4805 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4806 | if (ch > 0xFF) { |
| 4807 | #if SIZEOF_WCHAR_T == 4 |
| 4808 | assert(0); |
| 4809 | #else |
| 4810 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4811 | /* compute and append the two surrogates: */ |
| 4812 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4813 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4814 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4815 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4816 | else { |
| 4817 | if (!ch && s == e) |
| 4818 | break; |
| 4819 | /* surrogateescape */ |
| 4820 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4821 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4822 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4823 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4824 | return unicode; |
| 4825 | } |
| 4826 | |
| 4827 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4828 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4829 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4830 | |
| 4831 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4832 | and allocate exactly as much space needed at the end. Else allocate the |
| 4833 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4834 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4835 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4836 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4837 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4838 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4839 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4840 | void *data; |
| 4841 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4842 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4843 | if (!PyUnicode_Check(unicode)) { |
| 4844 | PyErr_BadArgument(); |
| 4845 | return NULL; |
| 4846 | } |
| 4847 | |
| 4848 | if (PyUnicode_READY(unicode) == -1) |
| 4849 | return NULL; |
| 4850 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4851 | if (PyUnicode_UTF8(unicode)) |
| 4852 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4853 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4854 | |
| 4855 | kind = PyUnicode_KIND(unicode); |
| 4856 | data = PyUnicode_DATA(unicode); |
| 4857 | size = PyUnicode_GET_LENGTH(unicode); |
| 4858 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4859 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4860 | default: |
| 4861 | assert(0); |
| 4862 | case PyUnicode_1BYTE_KIND: |
| 4863 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4864 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4865 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4866 | case PyUnicode_2BYTE_KIND: |
| 4867 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4868 | case PyUnicode_4BYTE_KIND: |
| 4869 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4870 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4873 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4874 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4875 | Py_ssize_t size, |
| 4876 | const char *errors) |
| 4877 | { |
| 4878 | PyObject *v, *unicode; |
| 4879 | |
| 4880 | unicode = PyUnicode_FromUnicode(s, size); |
| 4881 | if (unicode == NULL) |
| 4882 | return NULL; |
| 4883 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4884 | Py_DECREF(unicode); |
| 4885 | return v; |
| 4886 | } |
| 4887 | |
| 4888 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4889 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4890 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4891 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4894 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4895 | |
| 4896 | PyObject * |
| 4897 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4898 | Py_ssize_t size, |
| 4899 | const char *errors, |
| 4900 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4901 | { |
| 4902 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4903 | } |
| 4904 | |
| 4905 | PyObject * |
| 4906 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4907 | Py_ssize_t size, |
| 4908 | const char *errors, |
| 4909 | int *byteorder, |
| 4910 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4911 | { |
| 4912 | const char *starts = s; |
| 4913 | Py_ssize_t startinpos; |
| 4914 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4915 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4916 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4917 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4918 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4919 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4920 | PyObject *errorHandler = NULL; |
| 4921 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4922 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4923 | q = (unsigned char *)s; |
| 4924 | e = q + size; |
| 4925 | |
| 4926 | if (byteorder) |
| 4927 | bo = *byteorder; |
| 4928 | |
| 4929 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4930 | byte order setting accordingly. In native mode, the leading BOM |
| 4931 | mark is skipped, in all other modes, it is copied to the output |
| 4932 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4933 | if (bo == 0 && size >= 4) { |
| 4934 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4935 | if (bom == 0x0000FEFF) { |
| 4936 | bo = -1; |
| 4937 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4938 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4939 | else if (bom == 0xFFFE0000) { |
| 4940 | bo = 1; |
| 4941 | q += 4; |
| 4942 | } |
| 4943 | if (byteorder) |
| 4944 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4947 | if (q == e) { |
| 4948 | if (consumed) |
| 4949 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4950 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4953 | #ifdef WORDS_BIGENDIAN |
| 4954 | le = bo < 0; |
| 4955 | #else |
| 4956 | le = bo <= 0; |
| 4957 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4958 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4959 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4960 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4961 | writer.min_length = (e - q + 3) / 4; |
| 4962 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4963 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4964 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4965 | while (1) { |
| 4966 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4967 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4968 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4969 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4970 | enum PyUnicode_Kind kind = writer.kind; |
| 4971 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4972 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4973 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4974 | if (le) { |
| 4975 | do { |
| 4976 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4977 | if (ch > maxch) |
| 4978 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4979 | if (kind != PyUnicode_1BYTE_KIND && |
| 4980 | Py_UNICODE_IS_SURROGATE(ch)) |
| 4981 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4982 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4983 | q += 4; |
| 4984 | } while (q <= last); |
| 4985 | } |
| 4986 | else { |
| 4987 | do { |
| 4988 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 4989 | if (ch > maxch) |
| 4990 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4991 | if (kind != PyUnicode_1BYTE_KIND && |
| 4992 | Py_UNICODE_IS_SURROGATE(ch)) |
| 4993 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4994 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4995 | q += 4; |
| 4996 | } while (q <= last); |
| 4997 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4998 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4999 | } |
| 5000 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5001 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5002 | errmsg = "code point in surrogate code point range(0xd800, 0xe000)"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5003 | startinpos = ((const char *)q) - starts; |
| 5004 | endinpos = startinpos + 4; |
| 5005 | } |
| 5006 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5007 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5008 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5009 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5010 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5011 | startinpos = ((const char *)q) - starts; |
| 5012 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5013 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5014 | else { |
| 5015 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5016 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5017 | goto onError; |
| 5018 | q += 4; |
| 5019 | continue; |
| 5020 | } |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5021 | errmsg = "code point not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5022 | startinpos = ((const char *)q) - starts; |
| 5023 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5024 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5025 | |
| 5026 | /* The remaining input chars are ignored if the callback |
| 5027 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5028 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5029 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5030 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5031 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5032 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5033 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5034 | } |
| 5035 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5036 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5037 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5038 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5039 | Py_XDECREF(errorHandler); |
| 5040 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5041 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5042 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5043 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5044 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5045 | Py_XDECREF(errorHandler); |
| 5046 | Py_XDECREF(exc); |
| 5047 | return NULL; |
| 5048 | } |
| 5049 | |
| 5050 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5051 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5052 | const char *errors, |
| 5053 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5054 | { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5055 | enum PyUnicode_Kind kind; |
| 5056 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5057 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5058 | PyObject *v; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5059 | PY_UINT32_T *out; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5060 | #if PY_LITTLE_ENDIAN |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5061 | int native_ordering = byteorder <= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5062 | #else |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5063 | int native_ordering = byteorder >= 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5064 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5065 | const char *encoding; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5066 | Py_ssize_t nsize, pos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5067 | PyObject *errorHandler = NULL; |
| 5068 | PyObject *exc = NULL; |
| 5069 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5070 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5071 | if (!PyUnicode_Check(str)) { |
| 5072 | PyErr_BadArgument(); |
| 5073 | return NULL; |
| 5074 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5075 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5076 | return NULL; |
| 5077 | kind = PyUnicode_KIND(str); |
| 5078 | data = PyUnicode_DATA(str); |
| 5079 | len = PyUnicode_GET_LENGTH(str); |
| 5080 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5081 | if (len > PY_SSIZE_T_MAX / 4 - (byteorder == 0)) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5082 | return PyErr_NoMemory(); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5083 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5084 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5085 | if (v == NULL) |
| 5086 | return NULL; |
| 5087 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5088 | /* output buffer is 4-bytes aligned */ |
| 5089 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 4)); |
| 5090 | out = (PY_UINT32_T *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | if (byteorder == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5092 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5093 | if (len == 0) |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5094 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5095 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5096 | if (byteorder == -1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5097 | encoding = "utf-32-le"; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5098 | else if (byteorder == 1) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5099 | encoding = "utf-32-be"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5100 | else |
| 5101 | encoding = "utf-32"; |
| 5102 | |
| 5103 | if (kind == PyUnicode_1BYTE_KIND) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5104 | ucs1lib_utf32_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5105 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5108 | pos = 0; |
| 5109 | while (pos < len) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5110 | Py_ssize_t repsize, moreunits; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5111 | |
| 5112 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5113 | pos += ucs2lib_utf32_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5114 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5115 | } |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5116 | else { |
| 5117 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5118 | pos += ucs4lib_utf32_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5119 | &out, native_ordering); |
| 5120 | } |
| 5121 | if (pos == len) |
| 5122 | break; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5123 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5124 | rep = unicode_encode_call_errorhandler( |
| 5125 | errors, &errorHandler, |
| 5126 | encoding, "surrogates not allowed", |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5127 | str, &exc, pos, pos + 1, &pos); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5128 | if (!rep) |
| 5129 | goto error; |
| 5130 | |
| 5131 | if (PyBytes_Check(rep)) { |
| 5132 | repsize = PyBytes_GET_SIZE(rep); |
| 5133 | if (repsize & 3) { |
| 5134 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5135 | str, pos - 1, pos, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5136 | "surrogates not allowed"); |
| 5137 | goto error; |
| 5138 | } |
| 5139 | moreunits = repsize / 4; |
| 5140 | } |
| 5141 | else { |
| 5142 | assert(PyUnicode_Check(rep)); |
| 5143 | if (PyUnicode_READY(rep) < 0) |
| 5144 | goto error; |
| 5145 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5146 | if (!PyUnicode_IS_ASCII(rep)) { |
| 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 | } |
| 5153 | |
| 5154 | /* four bytes are reserved for each surrogate */ |
| 5155 | if (moreunits > 1) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5156 | Py_ssize_t outpos = out - (PY_UINT32_T*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5157 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5158 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5159 | /* integer overflow */ |
| 5160 | PyErr_NoMemory(); |
| 5161 | goto error; |
| 5162 | } |
| 5163 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5164 | goto error; |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5165 | out = (PY_UINT32_T*) PyBytes_AS_STRING(v) + outpos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | if (PyBytes_Check(rep)) { |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5169 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5170 | out += moreunits; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5171 | } else /* rep is unicode */ { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5172 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5173 | ucs1lib_utf32_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5174 | &out, native_ordering); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | Py_CLEAR(rep); |
| 5178 | } |
| 5179 | |
| 5180 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5181 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5182 | handler is used. */ |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5183 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5184 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5185 | _PyBytes_Resize(&v, nsize); |
| 5186 | Py_XDECREF(errorHandler); |
| 5187 | Py_XDECREF(exc); |
Serhiy Storchaka | 0d4df75 | 2015-05-12 23:12:45 +0300 | [diff] [blame] | 5188 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5189 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5190 | error: |
| 5191 | Py_XDECREF(rep); |
| 5192 | Py_XDECREF(errorHandler); |
| 5193 | Py_XDECREF(exc); |
| 5194 | Py_XDECREF(v); |
| 5195 | return NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5198 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5199 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5200 | Py_ssize_t size, |
| 5201 | const char *errors, |
| 5202 | int byteorder) |
| 5203 | { |
| 5204 | PyObject *result; |
| 5205 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5206 | if (tmp == NULL) |
| 5207 | return NULL; |
| 5208 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5209 | Py_DECREF(tmp); |
| 5210 | return result; |
| 5211 | } |
| 5212 | |
| 5213 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5214 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5215 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5216 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5220 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5221 | PyObject * |
| 5222 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5223 | Py_ssize_t size, |
| 5224 | const char *errors, |
| 5225 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5227 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5228 | } |
| 5229 | |
| 5230 | PyObject * |
| 5231 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5232 | Py_ssize_t size, |
| 5233 | const char *errors, |
| 5234 | int *byteorder, |
| 5235 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5236 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5237 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5238 | Py_ssize_t startinpos; |
| 5239 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5240 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5241 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5242 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5243 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5244 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5245 | PyObject *errorHandler = NULL; |
| 5246 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5247 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5248 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5249 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5250 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5251 | |
| 5252 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5253 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5255 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5256 | byte order setting accordingly. In native mode, the leading BOM |
| 5257 | mark is skipped, in all other modes, it is copied to the output |
| 5258 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5259 | if (bo == 0 && size >= 2) { |
| 5260 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5261 | if (bom == 0xFEFF) { |
| 5262 | q += 2; |
| 5263 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5264 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5265 | else if (bom == 0xFFFE) { |
| 5266 | q += 2; |
| 5267 | bo = 1; |
| 5268 | } |
| 5269 | if (byteorder) |
| 5270 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5271 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5272 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5273 | if (q == e) { |
| 5274 | if (consumed) |
| 5275 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5276 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5277 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5278 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5279 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5280 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5281 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5282 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5283 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5284 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5285 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5286 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5287 | /* Note: size will always be longer than the resulting Unicode |
| 5288 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5289 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5290 | writer.min_length = (e - q + 1) / 2; |
| 5291 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5292 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5293 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5294 | while (1) { |
| 5295 | Py_UCS4 ch = 0; |
| 5296 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5297 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5298 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5299 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5300 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5301 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5302 | native_ordering); |
| 5303 | else |
| 5304 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5305 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5306 | native_ordering); |
| 5307 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5308 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5309 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5310 | native_ordering); |
| 5311 | } else { |
| 5312 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5313 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5314 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5315 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5316 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5317 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5318 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5319 | switch (ch) |
| 5320 | { |
| 5321 | case 0: |
| 5322 | /* remaining byte at the end? (size should be even) */ |
| 5323 | if (q == e || consumed) |
| 5324 | goto End; |
| 5325 | errmsg = "truncated data"; |
| 5326 | startinpos = ((const char *)q) - starts; |
| 5327 | endinpos = ((const char *)e) - starts; |
| 5328 | break; |
| 5329 | /* The remaining input chars are ignored if the callback |
| 5330 | chooses to skip the input */ |
| 5331 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5332 | q -= 2; |
| 5333 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5334 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5335 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5336 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5337 | endinpos = ((const char *)e) - starts; |
| 5338 | break; |
| 5339 | case 2: |
| 5340 | errmsg = "illegal encoding"; |
| 5341 | startinpos = ((const char *)q) - 2 - starts; |
| 5342 | endinpos = startinpos + 2; |
| 5343 | break; |
| 5344 | case 3: |
| 5345 | errmsg = "illegal UTF-16 surrogate"; |
| 5346 | startinpos = ((const char *)q) - 4 - starts; |
| 5347 | endinpos = startinpos + 2; |
| 5348 | break; |
| 5349 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5350 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5351 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5352 | continue; |
| 5353 | } |
| 5354 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5355 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5356 | errors, |
| 5357 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5358 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5359 | &starts, |
| 5360 | (const char **)&e, |
| 5361 | &startinpos, |
| 5362 | &endinpos, |
| 5363 | &exc, |
| 5364 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5365 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5366 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5369 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5370 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5371 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5372 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5373 | Py_XDECREF(errorHandler); |
| 5374 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5375 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5376 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5377 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5378 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5379 | Py_XDECREF(errorHandler); |
| 5380 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | return NULL; |
| 5382 | } |
| 5383 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5384 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5385 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5386 | const char *errors, |
| 5387 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5388 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5389 | enum PyUnicode_Kind kind; |
| 5390 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5391 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5392 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5393 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5394 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5395 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5396 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5397 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5398 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5399 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5400 | const char *encoding; |
| 5401 | Py_ssize_t nsize, pos; |
| 5402 | PyObject *errorHandler = NULL; |
| 5403 | PyObject *exc = NULL; |
| 5404 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5405 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5406 | if (!PyUnicode_Check(str)) { |
| 5407 | PyErr_BadArgument(); |
| 5408 | return NULL; |
| 5409 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5410 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5411 | return NULL; |
| 5412 | kind = PyUnicode_KIND(str); |
| 5413 | data = PyUnicode_DATA(str); |
| 5414 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5415 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5416 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5417 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5418 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5419 | const Py_UCS4 *end = in + len; |
| 5420 | while (in < end) |
| 5421 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5422 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5423 | } |
| 5424 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5425 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5426 | nsize = len + pairs + (byteorder == 0); |
| 5427 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | if (v == NULL) |
| 5429 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5431 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5432 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5433 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5435 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5436 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5437 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5438 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5439 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5440 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5441 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5442 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5443 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5444 | if (byteorder < 0) |
| 5445 | encoding = "utf-16-le"; |
| 5446 | else if (byteorder > 0) |
| 5447 | encoding = "utf-16-be"; |
| 5448 | else |
| 5449 | encoding = "utf-16"; |
| 5450 | |
| 5451 | pos = 0; |
| 5452 | while (pos < len) { |
| 5453 | Py_ssize_t repsize, moreunits; |
| 5454 | |
| 5455 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5456 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5457 | &out, native_ordering); |
| 5458 | } |
| 5459 | else { |
| 5460 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5461 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5462 | &out, native_ordering); |
| 5463 | } |
| 5464 | if (pos == len) |
| 5465 | break; |
| 5466 | |
| 5467 | rep = unicode_encode_call_errorhandler( |
| 5468 | errors, &errorHandler, |
| 5469 | encoding, "surrogates not allowed", |
| 5470 | str, &exc, pos, pos + 1, &pos); |
| 5471 | if (!rep) |
| 5472 | goto error; |
| 5473 | |
| 5474 | if (PyBytes_Check(rep)) { |
| 5475 | repsize = PyBytes_GET_SIZE(rep); |
| 5476 | if (repsize & 1) { |
| 5477 | raise_encode_exception(&exc, encoding, |
| 5478 | str, pos - 1, pos, |
| 5479 | "surrogates not allowed"); |
| 5480 | goto error; |
| 5481 | } |
| 5482 | moreunits = repsize / 2; |
| 5483 | } |
| 5484 | else { |
| 5485 | assert(PyUnicode_Check(rep)); |
| 5486 | if (PyUnicode_READY(rep) < 0) |
| 5487 | goto error; |
| 5488 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5489 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5490 | raise_encode_exception(&exc, encoding, |
| 5491 | str, pos - 1, pos, |
| 5492 | "surrogates not allowed"); |
| 5493 | goto error; |
| 5494 | } |
| 5495 | } |
| 5496 | |
| 5497 | /* two bytes are reserved for each surrogate */ |
| 5498 | if (moreunits > 1) { |
| 5499 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5500 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5501 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5502 | /* integer overflow */ |
| 5503 | PyErr_NoMemory(); |
| 5504 | goto error; |
| 5505 | } |
| 5506 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5507 | goto error; |
| 5508 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5509 | } |
| 5510 | |
| 5511 | if (PyBytes_Check(rep)) { |
| 5512 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5513 | out += moreunits; |
| 5514 | } else /* rep is unicode */ { |
| 5515 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5516 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5517 | &out, native_ordering); |
| 5518 | } |
| 5519 | |
| 5520 | Py_CLEAR(rep); |
| 5521 | } |
| 5522 | |
| 5523 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5524 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5525 | is used. */ |
| 5526 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5527 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5528 | _PyBytes_Resize(&v, nsize); |
| 5529 | Py_XDECREF(errorHandler); |
| 5530 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5531 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5532 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5533 | error: |
| 5534 | Py_XDECREF(rep); |
| 5535 | Py_XDECREF(errorHandler); |
| 5536 | Py_XDECREF(exc); |
| 5537 | Py_XDECREF(v); |
| 5538 | return NULL; |
| 5539 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5542 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5543 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5544 | Py_ssize_t size, |
| 5545 | const char *errors, |
| 5546 | int byteorder) |
| 5547 | { |
| 5548 | PyObject *result; |
| 5549 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5550 | if (tmp == NULL) |
| 5551 | return NULL; |
| 5552 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5553 | Py_DECREF(tmp); |
| 5554 | return result; |
| 5555 | } |
| 5556 | |
| 5557 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5558 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5560 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
| 5563 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5564 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5565 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5566 | if all the escapes in the string make it still a valid ASCII string. |
| 5567 | Returns -1 if any escapes were found which cause the string to |
| 5568 | pop out of ASCII range. Otherwise returns the length of the |
| 5569 | required buffer to hold the string. |
| 5570 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5571 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5572 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5573 | { |
| 5574 | const unsigned char *p = (const unsigned char *)s; |
| 5575 | const unsigned char *end = p + size; |
| 5576 | Py_ssize_t length = 0; |
| 5577 | |
| 5578 | if (size < 0) |
| 5579 | return -1; |
| 5580 | |
| 5581 | for (; p < end; ++p) { |
| 5582 | if (*p > 127) { |
| 5583 | /* Non-ASCII */ |
| 5584 | return -1; |
| 5585 | } |
| 5586 | else if (*p != '\\') { |
| 5587 | /* Normal character */ |
| 5588 | ++length; |
| 5589 | } |
| 5590 | else { |
| 5591 | /* Backslash-escape, check next char */ |
| 5592 | ++p; |
| 5593 | /* Escape sequence reaches till end of string or |
| 5594 | non-ASCII follow-up. */ |
| 5595 | if (p >= end || *p > 127) |
| 5596 | return -1; |
| 5597 | switch (*p) { |
| 5598 | case '\n': |
| 5599 | /* backslash + \n result in zero characters */ |
| 5600 | break; |
| 5601 | case '\\': case '\'': case '\"': |
| 5602 | case 'b': case 'f': case 't': |
| 5603 | case 'n': case 'r': case 'v': case 'a': |
| 5604 | ++length; |
| 5605 | break; |
| 5606 | case '0': case '1': case '2': case '3': |
| 5607 | case '4': case '5': case '6': case '7': |
| 5608 | case 'x': case 'u': case 'U': case 'N': |
| 5609 | /* these do not guarantee ASCII characters */ |
| 5610 | return -1; |
| 5611 | default: |
| 5612 | /* count the backslash + the other character */ |
| 5613 | length += 2; |
| 5614 | } |
| 5615 | } |
| 5616 | } |
| 5617 | return length; |
| 5618 | } |
| 5619 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5620 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5621 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5622 | PyObject * |
| 5623 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5624 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5625 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5626 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5627 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5628 | Py_ssize_t startinpos; |
| 5629 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5630 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5632 | char* message; |
| 5633 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5634 | PyObject *errorHandler = NULL; |
| 5635 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5636 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5637 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5638 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5639 | if (len == 0) |
| 5640 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5641 | |
| 5642 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5643 | either the string is pure ASCII with named escapes like \n, etc. |
| 5644 | and we determined it's exact size (common case) |
| 5645 | or it contains \x, \u, ... escape sequences. then we create a |
| 5646 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5647 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5648 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5649 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5650 | } |
| 5651 | else { |
| 5652 | /* Escaped strings will always be longer than the resulting |
| 5653 | Unicode string, so we start with size here and then reduce the |
| 5654 | length after conversion to the true value. |
| 5655 | (but if the error callback returns a long replacement string |
| 5656 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5657 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5658 | } |
| 5659 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5660 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5661 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5664 | while (s < end) { |
| 5665 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5666 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5667 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | |
| 5669 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5670 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5671 | x = (unsigned char)*s; |
| 5672 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5673 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5674 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | continue; |
| 5676 | } |
| 5677 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5678 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5679 | /* \ - Escapes */ |
| 5680 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5681 | c = *s++; |
| 5682 | if (s > end) |
| 5683 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5684 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5685 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5687 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5688 | #define WRITECHAR(ch) \ |
| 5689 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5690 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5691 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5692 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5695 | case '\\': WRITECHAR('\\'); break; |
| 5696 | case '\'': WRITECHAR('\''); break; |
| 5697 | case '\"': WRITECHAR('\"'); break; |
| 5698 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5699 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5700 | case 'f': WRITECHAR('\014'); break; |
| 5701 | case 't': WRITECHAR('\t'); break; |
| 5702 | case 'n': WRITECHAR('\n'); break; |
| 5703 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5704 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5705 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5706 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5707 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5708 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5709 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | case '0': case '1': case '2': case '3': |
| 5711 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5712 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5713 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5714 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5715 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5716 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5718 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | break; |
| 5720 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5721 | /* hex escapes */ |
| 5722 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5724 | digits = 2; |
| 5725 | message = "truncated \\xXX escape"; |
| 5726 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5727 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5728 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5730 | digits = 4; |
| 5731 | message = "truncated \\uXXXX escape"; |
| 5732 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5734 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5735 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5736 | digits = 8; |
| 5737 | message = "truncated \\UXXXXXXXX escape"; |
| 5738 | hexescape: |
| 5739 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5740 | if (end - s < digits) { |
| 5741 | /* count only hex digits */ |
| 5742 | for (; s < end; ++s) { |
| 5743 | c = (unsigned char)*s; |
| 5744 | if (!Py_ISXDIGIT(c)) |
| 5745 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5746 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5747 | goto error; |
| 5748 | } |
| 5749 | for (; digits--; ++s) { |
| 5750 | c = (unsigned char)*s; |
| 5751 | if (!Py_ISXDIGIT(c)) |
| 5752 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5753 | chr = (chr<<4) & ~0xF; |
| 5754 | if (c >= '0' && c <= '9') |
| 5755 | chr += c - '0'; |
| 5756 | else if (c >= 'a' && c <= 'f') |
| 5757 | chr += 10 + c - 'a'; |
| 5758 | else |
| 5759 | chr += 10 + c - 'A'; |
| 5760 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5761 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5762 | /* _decoding_error will have already written into the |
| 5763 | target buffer. */ |
| 5764 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5765 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5766 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5767 | message = "illegal Unicode character"; |
| 5768 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5769 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5770 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5771 | break; |
| 5772 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5773 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5774 | case 'N': |
| 5775 | message = "malformed \\N character escape"; |
| 5776 | if (ucnhash_CAPI == NULL) { |
| 5777 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5778 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5779 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5780 | if (ucnhash_CAPI == NULL) |
| 5781 | goto ucnhashError; |
| 5782 | } |
| 5783 | if (*s == '{') { |
| 5784 | const char *start = s+1; |
| 5785 | /* look for the closing brace */ |
| 5786 | while (*s != '}' && s < end) |
| 5787 | s++; |
| 5788 | if (s > start && s < end && *s == '}') { |
| 5789 | /* found a name. look it up in the unicode database */ |
| 5790 | message = "unknown Unicode character name"; |
| 5791 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5792 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5793 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5794 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5795 | goto store; |
| 5796 | } |
| 5797 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5798 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5799 | |
| 5800 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5801 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5802 | message = "\\ at end of string"; |
| 5803 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5804 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5805 | } |
| 5806 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5807 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5808 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5809 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5810 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5812 | continue; |
| 5813 | |
| 5814 | error: |
| 5815 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5816 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5817 | errors, &errorHandler, |
| 5818 | "unicodeescape", message, |
| 5819 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5820 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5821 | goto onError; |
| 5822 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5823 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5824 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5825 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5826 | Py_XDECREF(errorHandler); |
| 5827 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5828 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5829 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5831 | PyErr_SetString( |
| 5832 | PyExc_UnicodeError, |
| 5833 | "\\N escapes not supported (can't load unicodedata module)" |
| 5834 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5835 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5836 | Py_XDECREF(errorHandler); |
| 5837 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5838 | return NULL; |
| 5839 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5840 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5841 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5842 | Py_XDECREF(errorHandler); |
| 5843 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | return NULL; |
| 5845 | } |
| 5846 | |
| 5847 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5848 | |
| 5849 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5850 | appropriate. |
| 5851 | |
| 5852 | */ |
| 5853 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5854 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5855 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5857 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5858 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5859 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5860 | int kind; |
| 5861 | void *data; |
| 5862 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5863 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5864 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5865 | escape. |
| 5866 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5867 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5868 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5869 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5870 | */ |
| 5871 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5872 | if (!PyUnicode_Check(unicode)) { |
| 5873 | PyErr_BadArgument(); |
| 5874 | return NULL; |
| 5875 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5876 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5877 | return NULL; |
| 5878 | len = PyUnicode_GET_LENGTH(unicode); |
| 5879 | kind = PyUnicode_KIND(unicode); |
| 5880 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5881 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5882 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5883 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5884 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5885 | } |
| 5886 | |
| 5887 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5888 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5889 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5890 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5891 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5892 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5893 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5894 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5895 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5896 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | if (repr == NULL) |
| 5898 | return NULL; |
| 5899 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5900 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5901 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5902 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5903 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5904 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5905 | /* Escape backslashes */ |
| 5906 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | *p++ = '\\'; |
| 5908 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5909 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5910 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5911 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5912 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5913 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5914 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5915 | *p++ = '\\'; |
| 5916 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5917 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5918 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5919 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5920 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5921 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5922 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5923 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5924 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5925 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5926 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5929 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | *p++ = '\\'; |
| 5931 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5932 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5933 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5934 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5935 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5936 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5937 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5938 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5939 | else if (ch == '\t') { |
| 5940 | *p++ = '\\'; |
| 5941 | *p++ = 't'; |
| 5942 | } |
| 5943 | else if (ch == '\n') { |
| 5944 | *p++ = '\\'; |
| 5945 | *p++ = 'n'; |
| 5946 | } |
| 5947 | else if (ch == '\r') { |
| 5948 | *p++ = '\\'; |
| 5949 | *p++ = 'r'; |
| 5950 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5951 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5952 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5953 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5955 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5956 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5957 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5958 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5959 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | /* Copy everything else as-is */ |
| 5961 | else |
| 5962 | *p++ = (char) ch; |
| 5963 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5964 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5965 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5966 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5967 | return NULL; |
| 5968 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5971 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5972 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5973 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5974 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5975 | PyObject *result; |
| 5976 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5977 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5979 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5980 | Py_DECREF(tmp); |
| 5981 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
| 5984 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5985 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5986 | PyObject * |
| 5987 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5988 | Py_ssize_t size, |
| 5989 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5990 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5991 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5992 | Py_ssize_t startinpos; |
| 5993 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5994 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5995 | const char *end; |
| 5996 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5997 | PyObject *errorHandler = NULL; |
| 5998 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5999 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6000 | if (size == 0) |
| 6001 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | /* Escaped strings will always be longer than the resulting |
| 6004 | 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] | 6005 | length after conversion to the true value. (But decoding error |
| 6006 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6007 | _PyUnicodeWriter_Init(&writer); |
| 6008 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6009 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6010 | end = s + size; |
| 6011 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6012 | unsigned char c; |
| 6013 | Py_UCS4 x; |
| 6014 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6015 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6017 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6018 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6019 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6020 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6021 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6022 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6023 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6024 | startinpos = s-starts; |
| 6025 | |
| 6026 | /* \u-escapes are only interpreted iff the number of leading |
| 6027 | backslashes if odd */ |
| 6028 | bs = s; |
| 6029 | for (;s < end;) { |
| 6030 | if (*s != '\\') |
| 6031 | break; |
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 | } |
| 6036 | if (((s - bs) & 1) == 0 || |
| 6037 | s >= end || |
| 6038 | (*s != 'u' && *s != 'U')) { |
| 6039 | continue; |
| 6040 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6041 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6042 | count = *s=='u' ? 4 : 8; |
| 6043 | s++; |
| 6044 | |
| 6045 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6046 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6047 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6048 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6049 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6050 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6051 | errors, &errorHandler, |
| 6052 | "rawunicodeescape", "truncated \\uXXXX", |
| 6053 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6054 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6055 | goto onError; |
| 6056 | goto nextByte; |
| 6057 | } |
| 6058 | x = (x<<4) & ~0xF; |
| 6059 | if (c >= '0' && c <= '9') |
| 6060 | x += c - '0'; |
| 6061 | else if (c >= 'a' && c <= 'f') |
| 6062 | x += 10 + c - 'a'; |
| 6063 | else |
| 6064 | x += 10 + c - 'A'; |
| 6065 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6066 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6067 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6068 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6069 | } |
| 6070 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6071 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6072 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6073 | errors, &errorHandler, |
| 6074 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6075 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6076 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6077 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6078 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6079 | nextByte: |
| 6080 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6082 | Py_XDECREF(errorHandler); |
| 6083 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6084 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6085 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6086 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6087 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6088 | Py_XDECREF(errorHandler); |
| 6089 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6090 | return NULL; |
| 6091 | } |
| 6092 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6093 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6094 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6095 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6097 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6098 | char *p; |
| 6099 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6100 | Py_ssize_t expandsize, pos; |
| 6101 | int kind; |
| 6102 | void *data; |
| 6103 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6104 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6105 | if (!PyUnicode_Check(unicode)) { |
| 6106 | PyErr_BadArgument(); |
| 6107 | return NULL; |
| 6108 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6109 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6110 | return NULL; |
| 6111 | kind = PyUnicode_KIND(unicode); |
| 6112 | data = PyUnicode_DATA(unicode); |
| 6113 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6114 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6115 | bytes, and 1 byte characters 4. */ |
| 6116 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6117 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6118 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6119 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6120 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6121 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6122 | if (repr == NULL) |
| 6123 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6124 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6125 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6127 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6128 | for (pos = 0; pos < len; pos++) { |
| 6129 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6130 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6131 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6132 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6133 | *p++ = '\\'; |
| 6134 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6135 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6136 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6137 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6138 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6139 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6140 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6141 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6142 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6143 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6144 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6145 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +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 >> 12) & 0xf]; |
| 6149 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6150 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6151 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6153 | /* Copy everything else as-is */ |
| 6154 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | *p++ = (char) ch; |
| 6156 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6157 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6158 | assert(p > q); |
| 6159 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6160 | return NULL; |
| 6161 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6164 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6165 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6166 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6168 | PyObject *result; |
| 6169 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6170 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6171 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6172 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6173 | Py_DECREF(tmp); |
| 6174 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6177 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6178 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6179 | PyObject * |
| 6180 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6181 | Py_ssize_t size, |
| 6182 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6183 | { |
| 6184 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6185 | Py_ssize_t startinpos; |
| 6186 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6187 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6188 | const char *end; |
| 6189 | const char *reason; |
| 6190 | PyObject *errorHandler = NULL; |
| 6191 | PyObject *exc = NULL; |
| 6192 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6193 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6194 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6195 | 1)) |
| 6196 | return NULL; |
| 6197 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6198 | if (size == 0) |
| 6199 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6200 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6201 | _PyUnicodeWriter_Init(&writer); |
| 6202 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6203 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6204 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6205 | } |
| 6206 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6207 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6208 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6209 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6210 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6211 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6212 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6213 | endinpos = end-starts; |
| 6214 | reason = "truncated input"; |
| 6215 | goto error; |
| 6216 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6217 | /* We copy the raw representation one byte at a time because the |
| 6218 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6219 | ((char *) &uch)[0] = s[0]; |
| 6220 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6221 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6222 | ((char *) &uch)[2] = s[2]; |
| 6223 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6224 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6225 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6226 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6227 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6228 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6229 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6230 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6231 | reason = "illegal code point (> 0x10FFFF)"; |
| 6232 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6233 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6234 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6235 | s += Py_UNICODE_SIZE; |
| 6236 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6237 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6238 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6239 | Py_UNICODE uch2; |
| 6240 | ((char *) &uch2)[0] = s[0]; |
| 6241 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6242 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6243 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6244 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6245 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6246 | } |
| 6247 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6248 | #endif |
| 6249 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6250 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6251 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6252 | continue; |
| 6253 | |
| 6254 | error: |
| 6255 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6256 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6257 | errors, &errorHandler, |
| 6258 | "unicode_internal", reason, |
| 6259 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6260 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6261 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6262 | } |
| 6263 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6264 | Py_XDECREF(errorHandler); |
| 6265 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6266 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6267 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6268 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6269 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6270 | Py_XDECREF(errorHandler); |
| 6271 | Py_XDECREF(exc); |
| 6272 | return NULL; |
| 6273 | } |
| 6274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6275 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6276 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6277 | PyObject * |
| 6278 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6279 | Py_ssize_t size, |
| 6280 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6281 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6282 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6283 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6284 | } |
| 6285 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6286 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6287 | static void |
| 6288 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6289 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6290 | PyObject *unicode, |
| 6291 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6292 | const char *reason) |
| 6293 | { |
| 6294 | if (*exceptionObject == NULL) { |
| 6295 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6296 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6297 | encoding, unicode, startpos, endpos, reason); |
| 6298 | } |
| 6299 | else { |
| 6300 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6301 | goto onError; |
| 6302 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6303 | goto onError; |
| 6304 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6305 | goto onError; |
| 6306 | return; |
| 6307 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 6308 | Py_CLEAR(*exceptionObject); |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6309 | } |
| 6310 | } |
| 6311 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6312 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6313 | static void |
| 6314 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6315 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6316 | PyObject *unicode, |
| 6317 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6318 | const char *reason) |
| 6319 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6320 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6321 | encoding, unicode, startpos, endpos, reason); |
| 6322 | if (*exceptionObject != NULL) |
| 6323 | PyCodec_StrictErrors(*exceptionObject); |
| 6324 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6325 | |
| 6326 | /* error handling callback helper: |
| 6327 | build arguments, call the callback and check the arguments, |
| 6328 | put the result into newpos and return the replacement string, which |
| 6329 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6330 | static PyObject * |
| 6331 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6332 | PyObject **errorHandler, |
| 6333 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6334 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6335 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6336 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6337 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6338 | 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] | 6339 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6340 | PyObject *restuple; |
| 6341 | PyObject *resunicode; |
| 6342 | |
| 6343 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6344 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6345 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6346 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6347 | } |
| 6348 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6349 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6350 | return NULL; |
| 6351 | len = PyUnicode_GET_LENGTH(unicode); |
| 6352 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6353 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6354 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6355 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6356 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6357 | |
| 6358 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6359 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6360 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6361 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6362 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6363 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6364 | Py_DECREF(restuple); |
| 6365 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6366 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6367 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6368 | &resunicode, newpos)) { |
| 6369 | Py_DECREF(restuple); |
| 6370 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6371 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6372 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6373 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6374 | Py_DECREF(restuple); |
| 6375 | return NULL; |
| 6376 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6377 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6378 | *newpos = len + *newpos; |
| 6379 | if (*newpos<0 || *newpos>len) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 6380 | 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] | 6381 | Py_DECREF(restuple); |
| 6382 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6383 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6384 | Py_INCREF(resunicode); |
| 6385 | Py_DECREF(restuple); |
| 6386 | return resunicode; |
| 6387 | } |
| 6388 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6389 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6390 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6391 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6392 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6393 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6394 | /* input state */ |
| 6395 | Py_ssize_t pos=0, size; |
| 6396 | int kind; |
| 6397 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6398 | /* output object */ |
| 6399 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6400 | /* pointer into the output */ |
| 6401 | char *str; |
| 6402 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6403 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6404 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6405 | 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] | 6406 | PyObject *errorHandler = NULL; |
| 6407 | PyObject *exc = NULL; |
| 6408 | /* the following variable is used for caching string comparisons |
| 6409 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6410 | int known_errorHandler = -1; |
| 6411 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6412 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6413 | return NULL; |
| 6414 | size = PyUnicode_GET_LENGTH(unicode); |
| 6415 | kind = PyUnicode_KIND(unicode); |
| 6416 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6417 | /* allocate enough for a simple encoding without |
| 6418 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6419 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6420 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6421 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6422 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6423 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6424 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6425 | ressize = size; |
| 6426 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6427 | while (pos < size) { |
| 6428 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6429 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6430 | /* can we encode this? */ |
| 6431 | if (c<limit) { |
| 6432 | /* no overflow check, because we know that the space is enough */ |
| 6433 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6434 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6435 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6436 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6437 | Py_ssize_t requiredsize; |
| 6438 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6439 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6440 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6441 | Py_ssize_t collstart = pos; |
| 6442 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 | /* find all unecodable characters */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6444 | while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6445 | ++collend; |
| 6446 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6447 | if (known_errorHandler==-1) { |
| 6448 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6449 | known_errorHandler = 1; |
| 6450 | else if (!strcmp(errors, "replace")) |
| 6451 | known_errorHandler = 2; |
| 6452 | else if (!strcmp(errors, "ignore")) |
| 6453 | known_errorHandler = 3; |
| 6454 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6455 | known_errorHandler = 4; |
| 6456 | else |
| 6457 | known_errorHandler = 0; |
| 6458 | } |
| 6459 | switch (known_errorHandler) { |
| 6460 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6461 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6462 | goto onError; |
| 6463 | case 2: /* replace */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6464 | while (collstart++ < collend) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6465 | *str++ = '?'; /* fall through */ |
| 6466 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6467 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6468 | break; |
| 6469 | case 4: /* xmlcharrefreplace */ |
| 6470 | respos = str - PyBytes_AS_STRING(res); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6471 | requiredsize = respos; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6472 | /* determine replacement size */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6473 | for (i = collstart; i < collend; ++i) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6474 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6475 | Py_ssize_t incr; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6476 | if (ch < 10) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6477 | incr = 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6478 | else if (ch < 100) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6479 | incr = 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6480 | else if (ch < 1000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6481 | incr = 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6482 | else if (ch < 10000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6483 | incr = 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6484 | else if (ch < 100000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6485 | incr = 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6486 | else if (ch < 1000000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6487 | incr = 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6488 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6489 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6490 | incr = 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6491 | } |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6492 | if (requiredsize > PY_SSIZE_T_MAX - incr) |
| 6493 | goto overflow; |
| 6494 | requiredsize += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6495 | } |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6496 | if (requiredsize > PY_SSIZE_T_MAX - (size - collend)) |
| 6497 | goto overflow; |
| 6498 | requiredsize += size - collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6499 | if (requiredsize > ressize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6500 | if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6501 | requiredsize = 2*ressize; |
| 6502 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6503 | goto onError; |
| 6504 | str = PyBytes_AS_STRING(res) + respos; |
| 6505 | ressize = requiredsize; |
| 6506 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6507 | /* generate replacement */ |
| 6508 | for (i = collstart; i < collend; ++i) { |
| 6509 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6510 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6511 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6512 | break; |
| 6513 | default: |
| 6514 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6515 | encoding, reason, unicode, &exc, |
| 6516 | collstart, collend, &newpos); |
| 6517 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6518 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6519 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6520 | if (PyBytes_Check(repunicode)) { |
| 6521 | /* Directly copy bytes result to output. */ |
| 6522 | repsize = PyBytes_Size(repunicode); |
| 6523 | if (repsize > 1) { |
| 6524 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6525 | respos = str - PyBytes_AS_STRING(res); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6526 | if (ressize > PY_SSIZE_T_MAX - repsize - 1) { |
| 6527 | Py_DECREF(repunicode); |
| 6528 | goto overflow; |
| 6529 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6530 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6531 | Py_DECREF(repunicode); |
| 6532 | goto onError; |
| 6533 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6534 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6535 | ressize += repsize-1; |
| 6536 | } |
| 6537 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6538 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6539 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6540 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6541 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6542 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6543 | /* need more space? (at least enough for what we |
| 6544 | have+the replacement+the rest of the string, so |
| 6545 | we won't have to check space for encodable characters) */ |
| 6546 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6547 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6548 | requiredsize = respos; |
| 6549 | if (requiredsize > PY_SSIZE_T_MAX - repsize) |
| 6550 | goto overflow; |
| 6551 | requiredsize += repsize; |
| 6552 | if (requiredsize > PY_SSIZE_T_MAX - (size - collend)) |
| 6553 | goto overflow; |
| 6554 | requiredsize += size - collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6555 | if (requiredsize > ressize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6556 | if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6557 | requiredsize = 2*ressize; |
| 6558 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6559 | Py_DECREF(repunicode); |
| 6560 | goto onError; |
| 6561 | } |
| 6562 | str = PyBytes_AS_STRING(res) + respos; |
| 6563 | ressize = requiredsize; |
| 6564 | } |
| 6565 | /* check if there is anything unencodable in the replacement |
| 6566 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6567 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6568 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6569 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6570 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6571 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6572 | Py_DECREF(repunicode); |
| 6573 | goto onError; |
| 6574 | } |
| 6575 | *str = (char)c; |
| 6576 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6577 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6578 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6579 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6580 | } |
| 6581 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6582 | /* Resize if we allocated to much */ |
| 6583 | size = str - PyBytes_AS_STRING(res); |
| 6584 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6585 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6586 | if (_PyBytes_Resize(&res, size) < 0) |
| 6587 | goto onError; |
| 6588 | } |
| 6589 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6590 | Py_XDECREF(errorHandler); |
| 6591 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6592 | return res; |
| 6593 | |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6594 | overflow: |
| 6595 | PyErr_SetString(PyExc_OverflowError, |
| 6596 | "encoded result is too long for a Python string"); |
| 6597 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6598 | onError: |
| 6599 | Py_XDECREF(res); |
| 6600 | Py_XDECREF(errorHandler); |
| 6601 | Py_XDECREF(exc); |
| 6602 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6603 | } |
| 6604 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6605 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6606 | PyObject * |
| 6607 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6608 | Py_ssize_t size, |
| 6609 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6610 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6611 | PyObject *result; |
| 6612 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6613 | if (unicode == NULL) |
| 6614 | return NULL; |
| 6615 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6616 | Py_DECREF(unicode); |
| 6617 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6618 | } |
| 6619 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6620 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6621 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | { |
| 6623 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6624 | PyErr_BadArgument(); |
| 6625 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6626 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6627 | if (PyUnicode_READY(unicode) == -1) |
| 6628 | return NULL; |
| 6629 | /* Fast path: if it is a one-byte string, construct |
| 6630 | bytes object directly. */ |
| 6631 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6632 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6633 | PyUnicode_GET_LENGTH(unicode)); |
| 6634 | /* Non-Latin-1 characters present. Defer to above function to |
| 6635 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6636 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6637 | } |
| 6638 | |
| 6639 | PyObject* |
| 6640 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6641 | { |
| 6642 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
| 6645 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6646 | |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame^] | 6647 | typedef enum { |
| 6648 | _Py_ERROR_UNKNOWN=0, |
| 6649 | _Py_ERROR_SURROGATEESCAPE, |
| 6650 | _Py_ERROR_REPLACE, |
| 6651 | _Py_ERROR_IGNORE, |
| 6652 | _Py_ERROR_OTHER |
| 6653 | } _Py_error_handler; |
| 6654 | |
| 6655 | static _Py_error_handler |
| 6656 | get_error_handler(const char *errors) |
| 6657 | { |
| 6658 | if (errors == NULL) |
| 6659 | return _Py_ERROR_OTHER; |
| 6660 | if (strcmp(errors, "surrogateescape") == 0) |
| 6661 | return _Py_ERROR_SURROGATEESCAPE; |
| 6662 | if (strcmp(errors, "ignore") == 0) |
| 6663 | return _Py_ERROR_IGNORE; |
| 6664 | if (strcmp(errors, "replace") == 0) |
| 6665 | return _Py_ERROR_REPLACE; |
| 6666 | return _Py_ERROR_OTHER; |
| 6667 | } |
| 6668 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6669 | PyObject * |
| 6670 | PyUnicode_DecodeASCII(const char *s, |
| 6671 | Py_ssize_t size, |
| 6672 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6674 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6675 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6676 | int kind; |
| 6677 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6678 | Py_ssize_t startinpos; |
| 6679 | Py_ssize_t endinpos; |
| 6680 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6681 | const char *e; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame^] | 6682 | PyObject *error_handler_obj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6683 | PyObject *exc = NULL; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame^] | 6684 | _Py_error_handler error_handler = _Py_ERROR_UNKNOWN; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6687 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6689 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6690 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6691 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6692 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6693 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6694 | writer.min_length = size; |
| 6695 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6696 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6697 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6698 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6699 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6700 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6701 | writer.pos = outpos; |
| 6702 | if (writer.pos == size) |
| 6703 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6704 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6705 | s += writer.pos; |
| 6706 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6707 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6708 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6709 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6710 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6711 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6712 | ++s; |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame^] | 6713 | continue; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6714 | } |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame^] | 6715 | |
| 6716 | /* byte outsize range 0x00..0x7f: call the error handler */ |
| 6717 | |
| 6718 | if (error_handler == _Py_ERROR_UNKNOWN) |
| 6719 | error_handler = get_error_handler(errors); |
| 6720 | |
| 6721 | switch (error_handler) |
| 6722 | { |
| 6723 | case _Py_ERROR_REPLACE: |
| 6724 | case _Py_ERROR_SURROGATEESCAPE: |
| 6725 | /* Fast-path: the error handler only writes one character, |
| 6726 | but we must switch to UCS2 at the first write */ |
| 6727 | if (kind < PyUnicode_2BYTE_KIND) { |
| 6728 | if (_PyUnicodeWriter_Prepare(&writer, size - writer.pos, |
| 6729 | 0xffff) < 0) |
| 6730 | return NULL; |
| 6731 | kind = writer.kind; |
| 6732 | data = writer.data; |
| 6733 | } |
| 6734 | |
| 6735 | if (error_handler == _Py_ERROR_REPLACE) |
| 6736 | PyUnicode_WRITE(kind, data, writer.pos, 0xfffd); |
| 6737 | else |
| 6738 | PyUnicode_WRITE(kind, data, writer.pos, c + 0xdc00); |
| 6739 | writer.pos++; |
| 6740 | ++s; |
| 6741 | break; |
| 6742 | |
| 6743 | case _Py_ERROR_IGNORE: |
| 6744 | ++s; |
| 6745 | break; |
| 6746 | |
| 6747 | default: |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6748 | startinpos = s-starts; |
| 6749 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6750 | if (unicode_decode_call_errorhandler_writer( |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame^] | 6751 | errors, &error_handler_obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6752 | "ascii", "ordinal not in range(128)", |
| 6753 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6754 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6755 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6756 | kind = writer.kind; |
| 6757 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6758 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | } |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame^] | 6760 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6761 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6762 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6763 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6764 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6765 | _PyUnicodeWriter_Dealloc(&writer); |
Victor Stinner | f96418d | 2015-09-21 23:06:27 +0200 | [diff] [blame^] | 6766 | Py_XDECREF(error_handler_obj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6767 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6768 | return NULL; |
| 6769 | } |
| 6770 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6771 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6772 | PyObject * |
| 6773 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6774 | Py_ssize_t size, |
| 6775 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6777 | PyObject *result; |
| 6778 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6779 | if (unicode == NULL) |
| 6780 | return NULL; |
| 6781 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6782 | Py_DECREF(unicode); |
| 6783 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | } |
| 6785 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6786 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6787 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | { |
| 6789 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6790 | PyErr_BadArgument(); |
| 6791 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6793 | if (PyUnicode_READY(unicode) == -1) |
| 6794 | return NULL; |
| 6795 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6796 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6797 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6798 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6799 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6800 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | PyObject * |
| 6804 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6805 | { |
| 6806 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6807 | } |
| 6808 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6809 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6810 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6811 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6812 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6813 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6814 | #define NEED_RETRY |
| 6815 | #endif |
| 6816 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6817 | #ifndef WC_ERR_INVALID_CHARS |
| 6818 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6819 | #endif |
| 6820 | |
| 6821 | static char* |
| 6822 | code_page_name(UINT code_page, PyObject **obj) |
| 6823 | { |
| 6824 | *obj = NULL; |
| 6825 | if (code_page == CP_ACP) |
| 6826 | return "mbcs"; |
| 6827 | if (code_page == CP_UTF7) |
| 6828 | return "CP_UTF7"; |
| 6829 | if (code_page == CP_UTF8) |
| 6830 | return "CP_UTF8"; |
| 6831 | |
| 6832 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6833 | if (*obj == NULL) |
| 6834 | return NULL; |
| 6835 | return PyBytes_AS_STRING(*obj); |
| 6836 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6837 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6838 | static DWORD |
| 6839 | decode_code_page_flags(UINT code_page) |
| 6840 | { |
| 6841 | if (code_page == CP_UTF7) { |
| 6842 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6843 | return 0; |
| 6844 | } |
| 6845 | else |
| 6846 | return MB_ERR_INVALID_CHARS; |
| 6847 | } |
| 6848 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6849 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6850 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6851 | * mode. |
| 6852 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6853 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6854 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6855 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6856 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6857 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6858 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6859 | const char *in, |
| 6860 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6861 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6862 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6863 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6864 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6865 | |
| 6866 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6867 | assert(insize > 0); |
| 6868 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6869 | if (outsize <= 0) |
| 6870 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6871 | |
| 6872 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6873 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6874 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6875 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6876 | if (*v == NULL) |
| 6877 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6878 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6879 | } |
| 6880 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6881 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6882 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6883 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6884 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6885 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6886 | } |
| 6887 | |
| 6888 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6889 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6890 | if (outsize <= 0) |
| 6891 | goto error; |
| 6892 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6893 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6894 | error: |
| 6895 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6896 | return -2; |
| 6897 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6898 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6899 | } |
| 6900 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6901 | /* |
| 6902 | * Decode a byte string from a code page into unicode object with an error |
| 6903 | * handler. |
| 6904 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6905 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6906 | * UnicodeDecodeError exception and returns -1 on error. |
| 6907 | */ |
| 6908 | static int |
| 6909 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6910 | PyObject **v, |
| 6911 | const char *in, const int size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 6912 | const char *errors, int final) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6913 | { |
| 6914 | const char *startin = in; |
| 6915 | const char *endin = in + size; |
| 6916 | const DWORD flags = decode_code_page_flags(code_page); |
| 6917 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6918 | 2000 English version of the message. */ |
| 6919 | const char *reason = "No mapping for the Unicode character exists " |
| 6920 | "in the target code page."; |
| 6921 | /* each step cannot decode more than 1 character, but a character can be |
| 6922 | represented as a surrogate pair */ |
| 6923 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6924 | int insize; |
| 6925 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6926 | PyObject *errorHandler = NULL; |
| 6927 | PyObject *exc = NULL; |
| 6928 | PyObject *encoding_obj = NULL; |
| 6929 | char *encoding; |
| 6930 | DWORD err; |
| 6931 | int ret = -1; |
| 6932 | |
| 6933 | assert(size > 0); |
| 6934 | |
| 6935 | encoding = code_page_name(code_page, &encoding_obj); |
| 6936 | if (encoding == NULL) |
| 6937 | return -1; |
| 6938 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 6939 | if ((errors == NULL || strcmp(errors, "strict") == 0) && final) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6940 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6941 | UnicodeDecodeError. */ |
| 6942 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6943 | if (exc != NULL) { |
| 6944 | PyCodec_StrictErrors(exc); |
| 6945 | Py_CLEAR(exc); |
| 6946 | } |
| 6947 | goto error; |
| 6948 | } |
| 6949 | |
| 6950 | if (*v == NULL) { |
| 6951 | /* Create unicode object */ |
| 6952 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6953 | PyErr_NoMemory(); |
| 6954 | goto error; |
| 6955 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6956 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6957 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6958 | if (*v == NULL) |
| 6959 | goto error; |
| 6960 | startout = PyUnicode_AS_UNICODE(*v); |
| 6961 | } |
| 6962 | else { |
| 6963 | /* Extend unicode object */ |
| 6964 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6965 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6966 | PyErr_NoMemory(); |
| 6967 | goto error; |
| 6968 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6969 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6970 | goto error; |
| 6971 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6972 | } |
| 6973 | |
| 6974 | /* Decode the byte string character per character */ |
| 6975 | out = startout; |
| 6976 | while (in < endin) |
| 6977 | { |
| 6978 | /* Decode a character */ |
| 6979 | insize = 1; |
| 6980 | do |
| 6981 | { |
| 6982 | outsize = MultiByteToWideChar(code_page, flags, |
| 6983 | in, insize, |
| 6984 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6985 | if (outsize > 0) |
| 6986 | break; |
| 6987 | err = GetLastError(); |
| 6988 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6989 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6990 | { |
| 6991 | PyErr_SetFromWindowsErr(0); |
| 6992 | goto error; |
| 6993 | } |
| 6994 | insize++; |
| 6995 | } |
| 6996 | /* 4=maximum length of a UTF-8 sequence */ |
| 6997 | while (insize <= 4 && (in + insize) <= endin); |
| 6998 | |
| 6999 | if (outsize <= 0) { |
| 7000 | Py_ssize_t startinpos, endinpos, outpos; |
| 7001 | |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7002 | /* last character in partial decode? */ |
| 7003 | if (in + insize >= endin && !final) |
| 7004 | break; |
| 7005 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7006 | startinpos = in - startin; |
| 7007 | endinpos = startinpos + 1; |
| 7008 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7009 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7010 | errors, &errorHandler, |
| 7011 | encoding, reason, |
| 7012 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7013 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7014 | { |
| 7015 | goto error; |
| 7016 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7017 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7018 | } |
| 7019 | else { |
| 7020 | in += insize; |
| 7021 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7022 | out += outsize; |
| 7023 | } |
| 7024 | } |
| 7025 | |
| 7026 | /* write a NUL character at the end */ |
| 7027 | *out = 0; |
| 7028 | |
| 7029 | /* Extend unicode object */ |
| 7030 | outsize = out - startout; |
| 7031 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7032 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7033 | goto error; |
Victor Stinner | e1f17c6 | 2014-07-25 14:03:03 +0200 | [diff] [blame] | 7034 | /* (in - startin) <= size and size is an int */ |
| 7035 | ret = Py_SAFE_DOWNCAST(in - startin, Py_ssize_t, int); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7036 | |
| 7037 | error: |
| 7038 | Py_XDECREF(encoding_obj); |
| 7039 | Py_XDECREF(errorHandler); |
| 7040 | Py_XDECREF(exc); |
| 7041 | return ret; |
| 7042 | } |
| 7043 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7044 | static PyObject * |
| 7045 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7046 | const char *s, Py_ssize_t size, |
| 7047 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7048 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7049 | PyObject *v = NULL; |
| 7050 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7051 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7052 | if (code_page < 0) { |
| 7053 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7054 | return NULL; |
| 7055 | } |
| 7056 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7057 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7058 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7059 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7060 | do |
| 7061 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7062 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7063 | if (size > INT_MAX) { |
| 7064 | chunk_size = INT_MAX; |
| 7065 | final = 0; |
| 7066 | done = 0; |
| 7067 | } |
| 7068 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7069 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7070 | { |
| 7071 | chunk_size = (int)size; |
| 7072 | final = (consumed == NULL); |
| 7073 | done = 1; |
| 7074 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7075 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7076 | if (chunk_size == 0 && done) { |
| 7077 | if (v != NULL) |
| 7078 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7079 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7080 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7081 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7082 | converted = decode_code_page_strict(code_page, &v, |
| 7083 | s, chunk_size); |
| 7084 | if (converted == -2) |
| 7085 | converted = decode_code_page_errors(code_page, &v, |
| 7086 | s, chunk_size, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 7087 | errors, final); |
| 7088 | assert(converted != 0 || done); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7089 | |
| 7090 | if (converted < 0) { |
| 7091 | Py_XDECREF(v); |
| 7092 | return NULL; |
| 7093 | } |
| 7094 | |
| 7095 | if (consumed) |
| 7096 | *consumed += converted; |
| 7097 | |
| 7098 | s += converted; |
| 7099 | size -= converted; |
| 7100 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7101 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7102 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7105 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7106 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7107 | const char *s, |
| 7108 | Py_ssize_t size, |
| 7109 | const char *errors, |
| 7110 | Py_ssize_t *consumed) |
| 7111 | { |
| 7112 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7113 | } |
| 7114 | |
| 7115 | PyObject * |
| 7116 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7117 | Py_ssize_t size, |
| 7118 | const char *errors, |
| 7119 | Py_ssize_t *consumed) |
| 7120 | { |
| 7121 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7122 | } |
| 7123 | |
| 7124 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7125 | PyUnicode_DecodeMBCS(const char *s, |
| 7126 | Py_ssize_t size, |
| 7127 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7128 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7129 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7130 | } |
| 7131 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7132 | static DWORD |
| 7133 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7134 | { |
| 7135 | if (code_page == CP_UTF8) { |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 7136 | return WC_ERR_INVALID_CHARS; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7137 | } |
| 7138 | else if (code_page == CP_UTF7) { |
| 7139 | /* CP_UTF7 only supports flags=0 */ |
| 7140 | return 0; |
| 7141 | } |
| 7142 | else { |
| 7143 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7144 | return 0; |
| 7145 | else |
| 7146 | return WC_NO_BEST_FIT_CHARS; |
| 7147 | } |
| 7148 | } |
| 7149 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7150 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7151 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7152 | * mode. |
| 7153 | * |
| 7154 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7155 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7156 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7157 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7158 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7159 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7160 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7161 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7162 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7163 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7164 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7165 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7166 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7167 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7168 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7169 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7170 | /* Create a substring so that we can get the UTF-16 representation |
| 7171 | of just the slice under consideration. */ |
| 7172 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7173 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7174 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7175 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7176 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7177 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7178 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7179 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7180 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7181 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7182 | if (substring == NULL) |
| 7183 | return -1; |
| 7184 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7185 | if (p == NULL) { |
| 7186 | Py_DECREF(substring); |
| 7187 | return -1; |
| 7188 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7189 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7190 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7191 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7192 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7193 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7194 | NULL, 0, |
| 7195 | NULL, pusedDefaultChar); |
| 7196 | if (outsize <= 0) |
| 7197 | goto error; |
| 7198 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7199 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7200 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7201 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7202 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7203 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7204 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7205 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7206 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7207 | if (*outbytes == NULL) { |
| 7208 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7209 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7210 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7211 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7212 | } |
| 7213 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7214 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7215 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7216 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7217 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7218 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7219 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7220 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7221 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7222 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7223 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7224 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7225 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7226 | } |
| 7227 | |
| 7228 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7229 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7230 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7231 | out, outsize, |
| 7232 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7233 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7234 | if (outsize <= 0) |
| 7235 | goto error; |
| 7236 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7237 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7238 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7239 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7240 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7241 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7242 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7243 | return -2; |
| 7244 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7245 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7246 | } |
| 7247 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7248 | /* |
| 7249 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7250 | * error handler. |
| 7251 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7252 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7253 | * -1 on other error. |
| 7254 | */ |
| 7255 | static int |
| 7256 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7257 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7258 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7259 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7260 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7261 | Py_ssize_t pos = unicode_offset; |
| 7262 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7263 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7264 | 2000 English version of the message. */ |
| 7265 | const char *reason = "invalid character"; |
| 7266 | /* 4=maximum length of a UTF-8 sequence */ |
| 7267 | char buffer[4]; |
| 7268 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7269 | Py_ssize_t outsize; |
| 7270 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7271 | PyObject *errorHandler = NULL; |
| 7272 | PyObject *exc = NULL; |
| 7273 | PyObject *encoding_obj = NULL; |
| 7274 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7275 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7276 | PyObject *rep; |
| 7277 | int ret = -1; |
| 7278 | |
| 7279 | assert(insize > 0); |
| 7280 | |
| 7281 | encoding = code_page_name(code_page, &encoding_obj); |
| 7282 | if (encoding == NULL) |
| 7283 | return -1; |
| 7284 | |
| 7285 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7286 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7287 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7288 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7289 | if (exc != NULL) { |
| 7290 | PyCodec_StrictErrors(exc); |
| 7291 | Py_DECREF(exc); |
| 7292 | } |
| 7293 | Py_XDECREF(encoding_obj); |
| 7294 | return -1; |
| 7295 | } |
| 7296 | |
| 7297 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7298 | pusedDefaultChar = &usedDefaultChar; |
| 7299 | else |
| 7300 | pusedDefaultChar = NULL; |
| 7301 | |
| 7302 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7303 | PyErr_NoMemory(); |
| 7304 | goto error; |
| 7305 | } |
| 7306 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7307 | |
| 7308 | if (*outbytes == NULL) { |
| 7309 | /* Create string object */ |
| 7310 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7311 | if (*outbytes == NULL) |
| 7312 | goto error; |
| 7313 | out = PyBytes_AS_STRING(*outbytes); |
| 7314 | } |
| 7315 | else { |
| 7316 | /* Extend string object */ |
| 7317 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7318 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7319 | PyErr_NoMemory(); |
| 7320 | goto error; |
| 7321 | } |
| 7322 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7323 | goto error; |
| 7324 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7325 | } |
| 7326 | |
| 7327 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7328 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7329 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7330 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7331 | wchar_t chars[2]; |
| 7332 | int charsize; |
| 7333 | if (ch < 0x10000) { |
| 7334 | chars[0] = (wchar_t)ch; |
| 7335 | charsize = 1; |
| 7336 | } |
| 7337 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7338 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7339 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7340 | charsize = 2; |
| 7341 | } |
| 7342 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7343 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7344 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7345 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7346 | NULL, pusedDefaultChar); |
| 7347 | if (outsize > 0) { |
| 7348 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7349 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7350 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7351 | memcpy(out, buffer, outsize); |
| 7352 | out += outsize; |
| 7353 | continue; |
| 7354 | } |
| 7355 | } |
| 7356 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7357 | PyErr_SetFromWindowsErr(0); |
| 7358 | goto error; |
| 7359 | } |
| 7360 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7361 | rep = unicode_encode_call_errorhandler( |
| 7362 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7363 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7364 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7365 | if (rep == NULL) |
| 7366 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7367 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7368 | |
| 7369 | if (PyBytes_Check(rep)) { |
| 7370 | outsize = PyBytes_GET_SIZE(rep); |
| 7371 | if (outsize != 1) { |
| 7372 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7373 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7374 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7375 | Py_DECREF(rep); |
| 7376 | goto error; |
| 7377 | } |
| 7378 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7379 | } |
| 7380 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7381 | out += outsize; |
| 7382 | } |
| 7383 | else { |
| 7384 | Py_ssize_t i; |
| 7385 | enum PyUnicode_Kind kind; |
| 7386 | void *data; |
| 7387 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7388 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7389 | Py_DECREF(rep); |
| 7390 | goto error; |
| 7391 | } |
| 7392 | |
| 7393 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7394 | if (outsize != 1) { |
| 7395 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7396 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7397 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7398 | Py_DECREF(rep); |
| 7399 | goto error; |
| 7400 | } |
| 7401 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7402 | } |
| 7403 | kind = PyUnicode_KIND(rep); |
| 7404 | data = PyUnicode_DATA(rep); |
| 7405 | for (i=0; i < outsize; i++) { |
| 7406 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7407 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7408 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7409 | encoding, unicode, |
| 7410 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7411 | "unable to encode error handler result to ASCII"); |
| 7412 | Py_DECREF(rep); |
| 7413 | goto error; |
| 7414 | } |
| 7415 | *out = (unsigned char)ch; |
| 7416 | out++; |
| 7417 | } |
| 7418 | } |
| 7419 | Py_DECREF(rep); |
| 7420 | } |
| 7421 | /* write a NUL byte */ |
| 7422 | *out = 0; |
| 7423 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7424 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7425 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7426 | goto error; |
| 7427 | ret = 0; |
| 7428 | |
| 7429 | error: |
| 7430 | Py_XDECREF(encoding_obj); |
| 7431 | Py_XDECREF(errorHandler); |
| 7432 | Py_XDECREF(exc); |
| 7433 | return ret; |
| 7434 | } |
| 7435 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7436 | static PyObject * |
| 7437 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7438 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7439 | const char *errors) |
| 7440 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7441 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7442 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7443 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7444 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7445 | |
Victor Stinner | 29dacf2 | 2015-01-26 16:41:32 +0100 | [diff] [blame] | 7446 | if (!PyUnicode_Check(unicode)) { |
| 7447 | PyErr_BadArgument(); |
| 7448 | return NULL; |
| 7449 | } |
| 7450 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7451 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7452 | return NULL; |
| 7453 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7454 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7455 | if (code_page < 0) { |
| 7456 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7457 | return NULL; |
| 7458 | } |
| 7459 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7460 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7461 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7462 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7463 | offset = 0; |
| 7464 | do |
| 7465 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7466 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7467 | /* 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] | 7468 | chunks. */ |
| 7469 | if (len > INT_MAX/2) { |
| 7470 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7471 | done = 0; |
| 7472 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7473 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7474 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7475 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7476 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7477 | done = 1; |
| 7478 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7479 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7480 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7481 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7482 | errors); |
| 7483 | if (ret == -2) |
| 7484 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7485 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7486 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7487 | if (ret < 0) { |
| 7488 | Py_XDECREF(outbytes); |
| 7489 | return NULL; |
| 7490 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7491 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7492 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7493 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7494 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7495 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7496 | return outbytes; |
| 7497 | } |
| 7498 | |
| 7499 | PyObject * |
| 7500 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7501 | Py_ssize_t size, |
| 7502 | const char *errors) |
| 7503 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7504 | PyObject *unicode, *res; |
| 7505 | unicode = PyUnicode_FromUnicode(p, size); |
| 7506 | if (unicode == NULL) |
| 7507 | return NULL; |
| 7508 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7509 | Py_DECREF(unicode); |
| 7510 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7511 | } |
| 7512 | |
| 7513 | PyObject * |
| 7514 | PyUnicode_EncodeCodePage(int code_page, |
| 7515 | PyObject *unicode, |
| 7516 | const char *errors) |
| 7517 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7518 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7519 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7520 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7521 | PyObject * |
| 7522 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7523 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7524 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7525 | } |
| 7526 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7527 | #undef NEED_RETRY |
| 7528 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7529 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7530 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7531 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7532 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7533 | static int |
| 7534 | charmap_decode_string(const char *s, |
| 7535 | Py_ssize_t size, |
| 7536 | PyObject *mapping, |
| 7537 | const char *errors, |
| 7538 | _PyUnicodeWriter *writer) |
| 7539 | { |
| 7540 | const char *starts = s; |
| 7541 | const char *e; |
| 7542 | Py_ssize_t startinpos, endinpos; |
| 7543 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7544 | Py_ssize_t maplen; |
| 7545 | enum PyUnicode_Kind mapkind; |
| 7546 | void *mapdata; |
| 7547 | Py_UCS4 x; |
| 7548 | unsigned char ch; |
| 7549 | |
| 7550 | if (PyUnicode_READY(mapping) == -1) |
| 7551 | return -1; |
| 7552 | |
| 7553 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7554 | mapdata = PyUnicode_DATA(mapping); |
| 7555 | mapkind = PyUnicode_KIND(mapping); |
| 7556 | |
| 7557 | e = s + size; |
| 7558 | |
| 7559 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7560 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7561 | * is disabled in encoding aliases, latin1 is preferred because |
| 7562 | * its implementation is faster. */ |
| 7563 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7564 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7565 | Py_UCS4 maxchar = writer->maxchar; |
| 7566 | |
| 7567 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7568 | while (s < e) { |
| 7569 | ch = *s; |
| 7570 | x = mapdata_ucs1[ch]; |
| 7571 | if (x > maxchar) { |
| 7572 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7573 | goto onError; |
| 7574 | maxchar = writer->maxchar; |
| 7575 | outdata = (Py_UCS1 *)writer->data; |
| 7576 | } |
| 7577 | outdata[writer->pos] = x; |
| 7578 | writer->pos++; |
| 7579 | ++s; |
| 7580 | } |
| 7581 | return 0; |
| 7582 | } |
| 7583 | |
| 7584 | while (s < e) { |
| 7585 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7586 | enum PyUnicode_Kind outkind = writer->kind; |
| 7587 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7588 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7589 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7590 | Py_UCS4 maxchar = writer->maxchar; |
| 7591 | while (s < e) { |
| 7592 | ch = *s; |
| 7593 | x = mapdata_ucs2[ch]; |
| 7594 | if (x > maxchar) |
| 7595 | goto Error; |
| 7596 | outdata[writer->pos] = x; |
| 7597 | writer->pos++; |
| 7598 | ++s; |
| 7599 | } |
| 7600 | break; |
| 7601 | } |
| 7602 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7603 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7604 | while (s < e) { |
| 7605 | ch = *s; |
| 7606 | x = mapdata_ucs2[ch]; |
| 7607 | if (x == 0xFFFE) |
| 7608 | goto Error; |
| 7609 | outdata[writer->pos] = x; |
| 7610 | writer->pos++; |
| 7611 | ++s; |
| 7612 | } |
| 7613 | break; |
| 7614 | } |
| 7615 | } |
| 7616 | ch = *s; |
| 7617 | |
| 7618 | if (ch < maplen) |
| 7619 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7620 | else |
| 7621 | x = 0xfffe; /* invalid value */ |
| 7622 | Error: |
| 7623 | if (x == 0xfffe) |
| 7624 | { |
| 7625 | /* undefined mapping */ |
| 7626 | startinpos = s-starts; |
| 7627 | endinpos = startinpos+1; |
| 7628 | if (unicode_decode_call_errorhandler_writer( |
| 7629 | errors, &errorHandler, |
| 7630 | "charmap", "character maps to <undefined>", |
| 7631 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7632 | writer)) { |
| 7633 | goto onError; |
| 7634 | } |
| 7635 | continue; |
| 7636 | } |
| 7637 | |
| 7638 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7639 | goto onError; |
| 7640 | ++s; |
| 7641 | } |
| 7642 | Py_XDECREF(errorHandler); |
| 7643 | Py_XDECREF(exc); |
| 7644 | return 0; |
| 7645 | |
| 7646 | onError: |
| 7647 | Py_XDECREF(errorHandler); |
| 7648 | Py_XDECREF(exc); |
| 7649 | return -1; |
| 7650 | } |
| 7651 | |
| 7652 | static int |
| 7653 | charmap_decode_mapping(const char *s, |
| 7654 | Py_ssize_t size, |
| 7655 | PyObject *mapping, |
| 7656 | const char *errors, |
| 7657 | _PyUnicodeWriter *writer) |
| 7658 | { |
| 7659 | const char *starts = s; |
| 7660 | const char *e; |
| 7661 | Py_ssize_t startinpos, endinpos; |
| 7662 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7663 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7664 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7665 | |
| 7666 | e = s + size; |
| 7667 | |
| 7668 | while (s < e) { |
| 7669 | ch = *s; |
| 7670 | |
| 7671 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7672 | key = PyLong_FromLong((long)ch); |
| 7673 | if (key == NULL) |
| 7674 | goto onError; |
| 7675 | |
| 7676 | item = PyObject_GetItem(mapping, key); |
| 7677 | Py_DECREF(key); |
| 7678 | if (item == NULL) { |
| 7679 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7680 | /* No mapping found means: mapping is undefined. */ |
| 7681 | PyErr_Clear(); |
| 7682 | goto Undefined; |
| 7683 | } else |
| 7684 | goto onError; |
| 7685 | } |
| 7686 | |
| 7687 | /* Apply mapping */ |
| 7688 | if (item == Py_None) |
| 7689 | goto Undefined; |
| 7690 | if (PyLong_Check(item)) { |
| 7691 | long value = PyLong_AS_LONG(item); |
| 7692 | if (value == 0xFFFE) |
| 7693 | goto Undefined; |
| 7694 | if (value < 0 || value > MAX_UNICODE) { |
| 7695 | PyErr_Format(PyExc_TypeError, |
| 7696 | "character mapping must be in range(0x%lx)", |
| 7697 | (unsigned long)MAX_UNICODE + 1); |
| 7698 | goto onError; |
| 7699 | } |
| 7700 | |
| 7701 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7702 | goto onError; |
| 7703 | } |
| 7704 | else if (PyUnicode_Check(item)) { |
| 7705 | if (PyUnicode_READY(item) == -1) |
| 7706 | goto onError; |
| 7707 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7708 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7709 | if (value == 0xFFFE) |
| 7710 | goto Undefined; |
| 7711 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7712 | goto onError; |
| 7713 | } |
| 7714 | else { |
| 7715 | writer->overallocate = 1; |
| 7716 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7717 | goto onError; |
| 7718 | } |
| 7719 | } |
| 7720 | else { |
| 7721 | /* wrong return value */ |
| 7722 | PyErr_SetString(PyExc_TypeError, |
| 7723 | "character mapping must return integer, None or str"); |
| 7724 | goto onError; |
| 7725 | } |
| 7726 | Py_CLEAR(item); |
| 7727 | ++s; |
| 7728 | continue; |
| 7729 | |
| 7730 | Undefined: |
| 7731 | /* undefined mapping */ |
| 7732 | Py_CLEAR(item); |
| 7733 | startinpos = s-starts; |
| 7734 | endinpos = startinpos+1; |
| 7735 | if (unicode_decode_call_errorhandler_writer( |
| 7736 | errors, &errorHandler, |
| 7737 | "charmap", "character maps to <undefined>", |
| 7738 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7739 | writer)) { |
| 7740 | goto onError; |
| 7741 | } |
| 7742 | } |
| 7743 | Py_XDECREF(errorHandler); |
| 7744 | Py_XDECREF(exc); |
| 7745 | return 0; |
| 7746 | |
| 7747 | onError: |
| 7748 | Py_XDECREF(item); |
| 7749 | Py_XDECREF(errorHandler); |
| 7750 | Py_XDECREF(exc); |
| 7751 | return -1; |
| 7752 | } |
| 7753 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7754 | PyObject * |
| 7755 | PyUnicode_DecodeCharmap(const char *s, |
| 7756 | Py_ssize_t size, |
| 7757 | PyObject *mapping, |
| 7758 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7760 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7761 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7762 | /* Default to Latin-1 */ |
| 7763 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7764 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7767 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7768 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7769 | writer.min_length = size; |
| 7770 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7771 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7772 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7773 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7774 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7775 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7776 | } |
| 7777 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7778 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7779 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7780 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7781 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7782 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7783 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7784 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7785 | return NULL; |
| 7786 | } |
| 7787 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7788 | /* Charmap encoding: the lookup table */ |
| 7789 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7790 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7791 | PyObject_HEAD |
| 7792 | unsigned char level1[32]; |
| 7793 | int count2, count3; |
| 7794 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7795 | }; |
| 7796 | |
| 7797 | static PyObject* |
| 7798 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7799 | { |
| 7800 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7801 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7802 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7803 | } |
| 7804 | |
| 7805 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7806 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7807 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7808 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7809 | }; |
| 7810 | |
| 7811 | static void |
| 7812 | encoding_map_dealloc(PyObject* o) |
| 7813 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7814 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7815 | } |
| 7816 | |
| 7817 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7818 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7819 | "EncodingMap", /*tp_name*/ |
| 7820 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7821 | 0, /*tp_itemsize*/ |
| 7822 | /* methods */ |
| 7823 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7824 | 0, /*tp_print*/ |
| 7825 | 0, /*tp_getattr*/ |
| 7826 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7827 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7828 | 0, /*tp_repr*/ |
| 7829 | 0, /*tp_as_number*/ |
| 7830 | 0, /*tp_as_sequence*/ |
| 7831 | 0, /*tp_as_mapping*/ |
| 7832 | 0, /*tp_hash*/ |
| 7833 | 0, /*tp_call*/ |
| 7834 | 0, /*tp_str*/ |
| 7835 | 0, /*tp_getattro*/ |
| 7836 | 0, /*tp_setattro*/ |
| 7837 | 0, /*tp_as_buffer*/ |
| 7838 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7839 | 0, /*tp_doc*/ |
| 7840 | 0, /*tp_traverse*/ |
| 7841 | 0, /*tp_clear*/ |
| 7842 | 0, /*tp_richcompare*/ |
| 7843 | 0, /*tp_weaklistoffset*/ |
| 7844 | 0, /*tp_iter*/ |
| 7845 | 0, /*tp_iternext*/ |
| 7846 | encoding_map_methods, /*tp_methods*/ |
| 7847 | 0, /*tp_members*/ |
| 7848 | 0, /*tp_getset*/ |
| 7849 | 0, /*tp_base*/ |
| 7850 | 0, /*tp_dict*/ |
| 7851 | 0, /*tp_descr_get*/ |
| 7852 | 0, /*tp_descr_set*/ |
| 7853 | 0, /*tp_dictoffset*/ |
| 7854 | 0, /*tp_init*/ |
| 7855 | 0, /*tp_alloc*/ |
| 7856 | 0, /*tp_new*/ |
| 7857 | 0, /*tp_free*/ |
| 7858 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7859 | }; |
| 7860 | |
| 7861 | PyObject* |
| 7862 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7863 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7864 | PyObject *result; |
| 7865 | struct encoding_map *mresult; |
| 7866 | int i; |
| 7867 | int need_dict = 0; |
| 7868 | unsigned char level1[32]; |
| 7869 | unsigned char level2[512]; |
| 7870 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7871 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7872 | int kind; |
| 7873 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7874 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7875 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7876 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7877 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7878 | PyErr_BadArgument(); |
| 7879 | return NULL; |
| 7880 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7881 | kind = PyUnicode_KIND(string); |
| 7882 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7883 | length = PyUnicode_GET_LENGTH(string); |
| 7884 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7885 | memset(level1, 0xFF, sizeof level1); |
| 7886 | memset(level2, 0xFF, sizeof level2); |
| 7887 | |
| 7888 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7889 | or if there are non-BMP characters, we need to use |
| 7890 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7891 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7892 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7893 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7894 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7895 | ch = PyUnicode_READ(kind, data, i); |
| 7896 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7897 | need_dict = 1; |
| 7898 | break; |
| 7899 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7900 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7901 | /* unmapped character */ |
| 7902 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7903 | l1 = ch >> 11; |
| 7904 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7905 | if (level1[l1] == 0xFF) |
| 7906 | level1[l1] = count2++; |
| 7907 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7908 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7909 | } |
| 7910 | |
| 7911 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7912 | need_dict = 1; |
| 7913 | |
| 7914 | if (need_dict) { |
| 7915 | PyObject *result = PyDict_New(); |
| 7916 | PyObject *key, *value; |
| 7917 | if (!result) |
| 7918 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7919 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7920 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7921 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7922 | if (!key || !value) |
| 7923 | goto failed1; |
| 7924 | if (PyDict_SetItem(result, key, value) == -1) |
| 7925 | goto failed1; |
| 7926 | Py_DECREF(key); |
| 7927 | Py_DECREF(value); |
| 7928 | } |
| 7929 | return result; |
| 7930 | failed1: |
| 7931 | Py_XDECREF(key); |
| 7932 | Py_XDECREF(value); |
| 7933 | Py_DECREF(result); |
| 7934 | return NULL; |
| 7935 | } |
| 7936 | |
| 7937 | /* Create a three-level trie */ |
| 7938 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7939 | 16*count2 + 128*count3 - 1); |
| 7940 | if (!result) |
| 7941 | return PyErr_NoMemory(); |
| 7942 | PyObject_Init(result, &EncodingMapType); |
| 7943 | mresult = (struct encoding_map*)result; |
| 7944 | mresult->count2 = count2; |
| 7945 | mresult->count3 = count3; |
| 7946 | mlevel1 = mresult->level1; |
| 7947 | mlevel2 = mresult->level23; |
| 7948 | mlevel3 = mresult->level23 + 16*count2; |
| 7949 | memcpy(mlevel1, level1, 32); |
| 7950 | memset(mlevel2, 0xFF, 16*count2); |
| 7951 | memset(mlevel3, 0, 128*count3); |
| 7952 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7953 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7954 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7955 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7956 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7957 | /* unmapped character */ |
| 7958 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7959 | o1 = ch>>11; |
| 7960 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7961 | i2 = 16*mlevel1[o1] + o2; |
| 7962 | if (mlevel2[i2] == 0xFF) |
| 7963 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7964 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7965 | i3 = 128*mlevel2[i2] + o3; |
| 7966 | mlevel3[i3] = i; |
| 7967 | } |
| 7968 | return result; |
| 7969 | } |
| 7970 | |
| 7971 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7972 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7973 | { |
| 7974 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7975 | int l1 = c>>11; |
| 7976 | int l2 = (c>>7) & 0xF; |
| 7977 | int l3 = c & 0x7F; |
| 7978 | int i; |
| 7979 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7980 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7981 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7982 | if (c == 0) |
| 7983 | return 0; |
| 7984 | /* level 1*/ |
| 7985 | i = map->level1[l1]; |
| 7986 | if (i == 0xFF) { |
| 7987 | return -1; |
| 7988 | } |
| 7989 | /* level 2*/ |
| 7990 | i = map->level23[16*i+l2]; |
| 7991 | if (i == 0xFF) { |
| 7992 | return -1; |
| 7993 | } |
| 7994 | /* level 3 */ |
| 7995 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7996 | if (i == 0) { |
| 7997 | return -1; |
| 7998 | } |
| 7999 | return i; |
| 8000 | } |
| 8001 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8002 | /* Lookup the character ch in the mapping. If the character |
| 8003 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8004 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8005 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8006 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8007 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8008 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8009 | PyObject *x; |
| 8010 | |
| 8011 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8012 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8013 | x = PyObject_GetItem(mapping, w); |
| 8014 | Py_DECREF(w); |
| 8015 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8016 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8017 | /* No mapping found means: mapping is undefined. */ |
| 8018 | PyErr_Clear(); |
| 8019 | x = Py_None; |
| 8020 | Py_INCREF(x); |
| 8021 | return x; |
| 8022 | } else |
| 8023 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8024 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8025 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8026 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8027 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8028 | long value = PyLong_AS_LONG(x); |
| 8029 | if (value < 0 || value > 255) { |
| 8030 | PyErr_SetString(PyExc_TypeError, |
| 8031 | "character mapping must be in range(256)"); |
| 8032 | Py_DECREF(x); |
| 8033 | return NULL; |
| 8034 | } |
| 8035 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8036 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8037 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8039 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8040 | /* wrong return value */ |
| 8041 | PyErr_Format(PyExc_TypeError, |
| 8042 | "character mapping must return integer, bytes or None, not %.400s", |
| 8043 | x->ob_type->tp_name); |
| 8044 | Py_DECREF(x); |
| 8045 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8046 | } |
| 8047 | } |
| 8048 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8049 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8050 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8051 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8052 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8053 | /* exponentially overallocate to minimize reallocations */ |
| 8054 | if (requiredsize < 2*outsize) |
| 8055 | requiredsize = 2*outsize; |
| 8056 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8057 | return -1; |
| 8058 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8059 | } |
| 8060 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8061 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8062 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8063 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8064 | /* 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] | 8065 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8066 | space is available. Return a new reference to the object that |
| 8067 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8068 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8069 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8070 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8071 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8072 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8073 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8074 | PyObject *rep; |
| 8075 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8076 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8077 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8078 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8079 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8080 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8081 | if (res == -1) |
| 8082 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8083 | if (outsize<requiredsize) |
| 8084 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8085 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8086 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | outstart[(*outpos)++] = (char)res; |
| 8088 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8089 | } |
| 8090 | |
| 8091 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8092 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8093 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8094 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | Py_DECREF(rep); |
| 8096 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8097 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8098 | if (PyLong_Check(rep)) { |
| 8099 | Py_ssize_t requiredsize = *outpos+1; |
| 8100 | if (outsize<requiredsize) |
| 8101 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8102 | Py_DECREF(rep); |
| 8103 | return enc_EXCEPTION; |
| 8104 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8105 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8106 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8107 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8108 | else { |
| 8109 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8110 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8111 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8112 | if (outsize<requiredsize) |
| 8113 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8114 | Py_DECREF(rep); |
| 8115 | return enc_EXCEPTION; |
| 8116 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8117 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8118 | memcpy(outstart + *outpos, repchars, repsize); |
| 8119 | *outpos += repsize; |
| 8120 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8121 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8122 | Py_DECREF(rep); |
| 8123 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8124 | } |
| 8125 | |
| 8126 | /* handle an error in PyUnicode_EncodeCharmap |
| 8127 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8128 | static int |
| 8129 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8130 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8131 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8132 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8133 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8134 | { |
| 8135 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8136 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8137 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8138 | enum PyUnicode_Kind kind; |
| 8139 | void *data; |
| 8140 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8141 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8142 | Py_ssize_t collstartpos = *inpos; |
| 8143 | Py_ssize_t collendpos = *inpos+1; |
| 8144 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8145 | char *encoding = "charmap"; |
| 8146 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8147 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8148 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8149 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8150 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8151 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8152 | return -1; |
| 8153 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8154 | /* find all unencodable characters */ |
| 8155 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8156 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8157 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8158 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8159 | val = encoding_map_lookup(ch, mapping); |
| 8160 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8161 | break; |
| 8162 | ++collendpos; |
| 8163 | continue; |
| 8164 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8165 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8166 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8167 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | if (rep==NULL) |
| 8169 | return -1; |
| 8170 | else if (rep!=Py_None) { |
| 8171 | Py_DECREF(rep); |
| 8172 | break; |
| 8173 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8174 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8175 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8176 | } |
| 8177 | /* cache callback name lookup |
| 8178 | * (if not done yet, i.e. it's the first error) */ |
| 8179 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8181 | *known_errorHandler = 1; |
| 8182 | else if (!strcmp(errors, "replace")) |
| 8183 | *known_errorHandler = 2; |
| 8184 | else if (!strcmp(errors, "ignore")) |
| 8185 | *known_errorHandler = 3; |
| 8186 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8187 | *known_errorHandler = 4; |
| 8188 | else |
| 8189 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8190 | } |
| 8191 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8192 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8193 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8194 | return -1; |
| 8195 | case 2: /* replace */ |
| 8196 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8197 | x = charmapencode_output('?', mapping, res, respos); |
| 8198 | if (x==enc_EXCEPTION) { |
| 8199 | return -1; |
| 8200 | } |
| 8201 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8202 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8203 | return -1; |
| 8204 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8205 | } |
| 8206 | /* fall through */ |
| 8207 | case 3: /* ignore */ |
| 8208 | *inpos = collendpos; |
| 8209 | break; |
| 8210 | case 4: /* xmlcharrefreplace */ |
| 8211 | /* generate replacement (temporarily (mis)uses p) */ |
| 8212 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8213 | char buffer[2+29+1+1]; |
| 8214 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8215 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8216 | for (cp = buffer; *cp; ++cp) { |
| 8217 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8218 | if (x==enc_EXCEPTION) |
| 8219 | return -1; |
| 8220 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8221 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8222 | return -1; |
| 8223 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8224 | } |
| 8225 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8226 | *inpos = collendpos; |
| 8227 | break; |
| 8228 | default: |
| 8229 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8230 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8231 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8232 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8233 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8234 | if (PyBytes_Check(repunicode)) { |
| 8235 | /* Directly copy bytes result to output. */ |
| 8236 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8237 | Py_ssize_t requiredsize; |
| 8238 | repsize = PyBytes_Size(repunicode); |
| 8239 | requiredsize = *respos + repsize; |
| 8240 | if (requiredsize > outsize) |
| 8241 | /* Make room for all additional bytes. */ |
| 8242 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8243 | Py_DECREF(repunicode); |
| 8244 | return -1; |
| 8245 | } |
| 8246 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8247 | PyBytes_AsString(repunicode), repsize); |
| 8248 | *respos += repsize; |
| 8249 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8250 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8251 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8252 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8253 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8254 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8255 | Py_DECREF(repunicode); |
| 8256 | return -1; |
| 8257 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8258 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8259 | data = PyUnicode_DATA(repunicode); |
| 8260 | kind = PyUnicode_KIND(repunicode); |
| 8261 | for (index = 0; index < repsize; index++) { |
| 8262 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8263 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8264 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8265 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8266 | return -1; |
| 8267 | } |
| 8268 | else if (x==enc_FAILED) { |
| 8269 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8270 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | return -1; |
| 8272 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8273 | } |
| 8274 | *inpos = newpos; |
| 8275 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8276 | } |
| 8277 | return 0; |
| 8278 | } |
| 8279 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8280 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8281 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8282 | PyObject *mapping, |
| 8283 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8284 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8285 | /* output object */ |
| 8286 | PyObject *res = NULL; |
| 8287 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8288 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8289 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8290 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8291 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8292 | PyObject *errorHandler = NULL; |
| 8293 | PyObject *exc = NULL; |
| 8294 | /* the following variable is used for caching string comparisons |
| 8295 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8296 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8297 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8298 | void *data; |
| 8299 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8300 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8301 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8302 | return NULL; |
| 8303 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8304 | data = PyUnicode_DATA(unicode); |
| 8305 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8307 | /* Default to Latin-1 */ |
| 8308 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8309 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8310 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8311 | /* allocate enough for a simple encoding without |
| 8312 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8313 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8314 | if (res == NULL) |
| 8315 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8316 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8317 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8318 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8319 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8320 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8321 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8322 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8323 | if (x==enc_EXCEPTION) /* error */ |
| 8324 | goto onError; |
| 8325 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8326 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8327 | &exc, |
| 8328 | &known_errorHandler, &errorHandler, errors, |
| 8329 | &res, &respos)) { |
| 8330 | goto onError; |
| 8331 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8332 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | else |
| 8334 | /* done with this character => adjust input position */ |
| 8335 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8336 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8337 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8338 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8339 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8340 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8341 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8342 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8343 | Py_XDECREF(exc); |
| 8344 | Py_XDECREF(errorHandler); |
| 8345 | return res; |
| 8346 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8347 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8348 | Py_XDECREF(res); |
| 8349 | Py_XDECREF(exc); |
| 8350 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | return NULL; |
| 8352 | } |
| 8353 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8354 | /* Deprecated */ |
| 8355 | PyObject * |
| 8356 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8357 | Py_ssize_t size, |
| 8358 | PyObject *mapping, |
| 8359 | const char *errors) |
| 8360 | { |
| 8361 | PyObject *result; |
| 8362 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8363 | if (unicode == NULL) |
| 8364 | return NULL; |
| 8365 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8366 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8367 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8368 | } |
| 8369 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8370 | PyObject * |
| 8371 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8372 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8373 | { |
| 8374 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8375 | PyErr_BadArgument(); |
| 8376 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8377 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8378 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8379 | } |
| 8380 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8381 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8382 | static void |
| 8383 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8384 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8385 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8386 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8387 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8388 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8389 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8390 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8391 | } |
| 8392 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8393 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8394 | goto onError; |
| 8395 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8396 | goto onError; |
| 8397 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8398 | goto onError; |
| 8399 | return; |
| 8400 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 8401 | Py_CLEAR(*exceptionObject); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8402 | } |
| 8403 | } |
| 8404 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8405 | /* error handling callback helper: |
| 8406 | build arguments, call the callback and check the arguments, |
| 8407 | put the result into newpos and return the replacement string, which |
| 8408 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8409 | static PyObject * |
| 8410 | unicode_translate_call_errorhandler(const char *errors, |
| 8411 | PyObject **errorHandler, |
| 8412 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8413 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8414 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8415 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8416 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8417 | 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] | 8418 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8419 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8420 | PyObject *restuple; |
| 8421 | PyObject *resunicode; |
| 8422 | |
| 8423 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8424 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8425 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8426 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8427 | } |
| 8428 | |
| 8429 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8430 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8431 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8432 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8433 | |
| 8434 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8435 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8436 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8438 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8439 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8440 | Py_DECREF(restuple); |
| 8441 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8442 | } |
| 8443 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8444 | &resunicode, &i_newpos)) { |
| 8445 | Py_DECREF(restuple); |
| 8446 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8447 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8448 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8449 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8450 | else |
| 8451 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8452 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 8453 | 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] | 8454 | Py_DECREF(restuple); |
| 8455 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8456 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8457 | Py_INCREF(resunicode); |
| 8458 | Py_DECREF(restuple); |
| 8459 | return resunicode; |
| 8460 | } |
| 8461 | |
| 8462 | /* Lookup the character ch in the mapping and put the result in result, |
| 8463 | which must be decrefed by the caller. |
| 8464 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8465 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8466 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8467 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8468 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8469 | PyObject *x; |
| 8470 | |
| 8471 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8472 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8473 | x = PyObject_GetItem(mapping, w); |
| 8474 | Py_DECREF(w); |
| 8475 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8476 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8477 | /* No mapping found means: use 1:1 mapping. */ |
| 8478 | PyErr_Clear(); |
| 8479 | *result = NULL; |
| 8480 | return 0; |
| 8481 | } else |
| 8482 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8483 | } |
| 8484 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8485 | *result = x; |
| 8486 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8487 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8488 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8489 | long value = PyLong_AS_LONG(x); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8490 | if (value < 0 || value > MAX_UNICODE) { |
| 8491 | PyErr_Format(PyExc_ValueError, |
| 8492 | "character mapping must be in range(0x%x)", |
| 8493 | MAX_UNICODE+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8494 | Py_DECREF(x); |
| 8495 | return -1; |
| 8496 | } |
| 8497 | *result = x; |
| 8498 | return 0; |
| 8499 | } |
| 8500 | else if (PyUnicode_Check(x)) { |
| 8501 | *result = x; |
| 8502 | return 0; |
| 8503 | } |
| 8504 | else { |
| 8505 | /* wrong return value */ |
| 8506 | PyErr_SetString(PyExc_TypeError, |
| 8507 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8508 | Py_DECREF(x); |
| 8509 | return -1; |
| 8510 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8511 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8512 | |
| 8513 | /* lookup the character, write the result into the writer. |
| 8514 | Return 1 if the result was written into the writer, return 0 if the mapping |
| 8515 | was undefined, raise an exception return -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8516 | static int |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8517 | charmaptranslate_output(Py_UCS4 ch, PyObject *mapping, |
| 8518 | _PyUnicodeWriter *writer) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8519 | { |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8520 | PyObject *item; |
| 8521 | |
| 8522 | if (charmaptranslate_lookup(ch, mapping, &item)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8523 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8524 | |
| 8525 | if (item == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8526 | /* not found => default to 1:1 mapping */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8527 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8528 | return -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8530 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8531 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8532 | |
| 8533 | if (item == Py_None) { |
| 8534 | Py_DECREF(item); |
| 8535 | return 0; |
| 8536 | } |
| 8537 | |
| 8538 | if (PyLong_Check(item)) { |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8539 | long ch = (Py_UCS4)PyLong_AS_LONG(item); |
| 8540 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8541 | used it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8542 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) { |
| 8543 | Py_DECREF(item); |
| 8544 | return -1; |
| 8545 | } |
| 8546 | Py_DECREF(item); |
| 8547 | return 1; |
| 8548 | } |
| 8549 | |
| 8550 | if (!PyUnicode_Check(item)) { |
| 8551 | Py_DECREF(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8552 | return -1; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8553 | } |
| 8554 | |
| 8555 | if (_PyUnicodeWriter_WriteStr(writer, item) < 0) { |
| 8556 | Py_DECREF(item); |
| 8557 | return -1; |
| 8558 | } |
| 8559 | |
| 8560 | Py_DECREF(item); |
| 8561 | return 1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8562 | } |
| 8563 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8564 | static int |
| 8565 | unicode_fast_translate_lookup(PyObject *mapping, Py_UCS1 ch, |
| 8566 | Py_UCS1 *translate) |
| 8567 | { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8568 | PyObject *item = NULL; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8569 | int ret = 0; |
| 8570 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8571 | if (charmaptranslate_lookup(ch, mapping, &item)) { |
| 8572 | return -1; |
| 8573 | } |
| 8574 | |
| 8575 | if (item == Py_None) { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8576 | /* deletion */ |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8577 | translate[ch] = 0xfe; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8578 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8579 | else if (item == NULL) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8580 | /* not found => default to 1:1 mapping */ |
| 8581 | translate[ch] = ch; |
| 8582 | return 1; |
| 8583 | } |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8584 | else if (PyLong_Check(item)) { |
Victor Stinner | 4dd2525 | 2014-04-08 09:14:21 +0200 | [diff] [blame] | 8585 | long replace = PyLong_AS_LONG(item); |
Victor Stinner | 4ff33af | 2014-04-05 11:56:37 +0200 | [diff] [blame] | 8586 | /* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already |
| 8587 | used it */ |
| 8588 | if (127 < replace) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8589 | /* invalid character or character outside ASCII: |
| 8590 | skip the fast translate */ |
| 8591 | goto exit; |
| 8592 | } |
| 8593 | translate[ch] = (Py_UCS1)replace; |
| 8594 | } |
| 8595 | else if (PyUnicode_Check(item)) { |
| 8596 | Py_UCS4 replace; |
| 8597 | |
| 8598 | if (PyUnicode_READY(item) == -1) { |
| 8599 | Py_DECREF(item); |
| 8600 | return -1; |
| 8601 | } |
| 8602 | if (PyUnicode_GET_LENGTH(item) != 1) |
| 8603 | goto exit; |
| 8604 | |
| 8605 | replace = PyUnicode_READ_CHAR(item, 0); |
| 8606 | if (replace > 127) |
| 8607 | goto exit; |
| 8608 | translate[ch] = (Py_UCS1)replace; |
| 8609 | } |
| 8610 | else { |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8611 | /* not None, NULL, long or unicode */ |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8612 | goto exit; |
| 8613 | } |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8614 | ret = 1; |
| 8615 | |
Benjamin Peterson | 1365de7 | 2014-04-07 20:15:41 -0400 | [diff] [blame] | 8616 | exit: |
| 8617 | Py_DECREF(item); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8618 | return ret; |
| 8619 | } |
| 8620 | |
| 8621 | /* Fast path for ascii => ascii translation. Return 1 if the whole string |
| 8622 | was translated into writer, return 0 if the input string was partially |
| 8623 | translated into writer, raise an exception and return -1 on error. */ |
| 8624 | static int |
| 8625 | unicode_fast_translate(PyObject *input, PyObject *mapping, |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8626 | _PyUnicodeWriter *writer, int ignore) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8627 | { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8628 | Py_UCS1 ascii_table[128], ch, ch2; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8629 | Py_ssize_t len; |
| 8630 | Py_UCS1 *in, *end, *out; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8631 | int res = 0; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8632 | |
| 8633 | if (PyUnicode_READY(input) == -1) |
| 8634 | return -1; |
| 8635 | if (!PyUnicode_IS_ASCII(input)) |
| 8636 | return 0; |
| 8637 | len = PyUnicode_GET_LENGTH(input); |
| 8638 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8639 | memset(ascii_table, 0xff, 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8640 | |
| 8641 | in = PyUnicode_1BYTE_DATA(input); |
| 8642 | end = in + len; |
| 8643 | |
| 8644 | assert(PyUnicode_IS_ASCII(writer->buffer)); |
| 8645 | assert(PyUnicode_GET_LENGTH(writer->buffer) == len); |
| 8646 | out = PyUnicode_1BYTE_DATA(writer->buffer); |
| 8647 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8648 | for (; in < end; in++) { |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8649 | ch = *in; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8650 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8651 | if (ch2 == 0xff) { |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8652 | int translate = unicode_fast_translate_lookup(mapping, ch, |
| 8653 | ascii_table); |
| 8654 | if (translate < 0) |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8655 | return -1; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8656 | if (translate == 0) |
| 8657 | goto exit; |
| 8658 | ch2 = ascii_table[ch]; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8659 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8660 | if (ch2 == 0xfe) { |
| 8661 | if (ignore) |
| 8662 | continue; |
| 8663 | goto exit; |
| 8664 | } |
| 8665 | assert(ch2 < 128); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8666 | *out = ch2; |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8667 | out++; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8668 | } |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8669 | res = 1; |
| 8670 | |
| 8671 | exit: |
| 8672 | writer->pos = out - PyUnicode_1BYTE_DATA(writer->buffer); |
| 8673 | return res; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8674 | } |
| 8675 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8676 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8678 | PyObject *mapping, |
| 8679 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8680 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8681 | /* input object */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8682 | char *data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8683 | Py_ssize_t size, i; |
| 8684 | int kind; |
| 8685 | /* output buffer */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8686 | _PyUnicodeWriter writer; |
| 8687 | /* error handler */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8688 | char *reason = "character maps to <undefined>"; |
| 8689 | PyObject *errorHandler = NULL; |
| 8690 | PyObject *exc = NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8691 | int ignore; |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8692 | int res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8694 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8695 | PyErr_BadArgument(); |
| 8696 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8697 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8698 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8699 | if (PyUnicode_READY(input) == -1) |
| 8700 | return NULL; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8701 | data = (char*)PyUnicode_DATA(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8702 | kind = PyUnicode_KIND(input); |
| 8703 | size = PyUnicode_GET_LENGTH(input); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8704 | |
| 8705 | if (size == 0) { |
| 8706 | Py_INCREF(input); |
| 8707 | return input; |
| 8708 | } |
| 8709 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8710 | /* allocate enough for a simple 1:1 translation without |
| 8711 | replacements, if we need more, we'll resize */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8712 | _PyUnicodeWriter_Init(&writer); |
| 8713 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8714 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8715 | |
Victor Stinner | 872b291 | 2014-04-05 14:27:07 +0200 | [diff] [blame] | 8716 | ignore = (errors != NULL && strcmp(errors, "ignore") == 0); |
| 8717 | |
| 8718 | res = unicode_fast_translate(input, mapping, &writer, ignore); |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8719 | if (res < 0) { |
| 8720 | _PyUnicodeWriter_Dealloc(&writer); |
| 8721 | return NULL; |
| 8722 | } |
| 8723 | if (res == 1) |
| 8724 | return _PyUnicodeWriter_Finish(&writer); |
| 8725 | |
Victor Stinner | 89a76ab | 2014-04-05 11:44:04 +0200 | [diff] [blame] | 8726 | i = writer.pos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8727 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8728 | /* try to encode it */ |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8729 | int translate; |
| 8730 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8731 | Py_ssize_t newpos; |
| 8732 | /* startpos for collecting untranslatable chars */ |
| 8733 | Py_ssize_t collstart; |
| 8734 | Py_ssize_t collend; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8735 | Py_UCS4 ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8736 | |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8737 | ch = PyUnicode_READ(kind, data, i); |
| 8738 | translate = charmaptranslate_output(ch, mapping, &writer); |
| 8739 | if (translate < 0) |
| 8740 | goto onError; |
| 8741 | |
| 8742 | if (translate != 0) { |
| 8743 | /* it worked => adjust input pointer */ |
| 8744 | ++i; |
| 8745 | continue; |
| 8746 | } |
| 8747 | |
| 8748 | /* untranslatable character */ |
| 8749 | collstart = i; |
| 8750 | collend = i+1; |
| 8751 | |
| 8752 | /* find all untranslatable characters */ |
| 8753 | while (collend < size) { |
| 8754 | PyObject *x; |
| 8755 | ch = PyUnicode_READ(kind, data, collend); |
| 8756 | if (charmaptranslate_lookup(ch, mapping, &x)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8757 | goto onError; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8758 | Py_XDECREF(x); |
| 8759 | if (x != Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8760 | break; |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8761 | ++collend; |
| 8762 | } |
| 8763 | |
| 8764 | if (ignore) { |
| 8765 | i = collend; |
| 8766 | } |
| 8767 | else { |
| 8768 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 8769 | reason, input, &exc, |
| 8770 | collstart, collend, &newpos); |
| 8771 | if (repunicode == NULL) |
| 8772 | goto onError; |
| 8773 | if (_PyUnicodeWriter_WriteStr(&writer, repunicode) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8774 | Py_DECREF(repunicode); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8775 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8776 | } |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8777 | Py_DECREF(repunicode); |
| 8778 | i = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8779 | } |
| 8780 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8781 | Py_XDECREF(exc); |
| 8782 | Py_XDECREF(errorHandler); |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8783 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8784 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8785 | onError: |
Victor Stinner | 1194ea0 | 2014-04-04 19:37:40 +0200 | [diff] [blame] | 8786 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8787 | Py_XDECREF(exc); |
| 8788 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | return NULL; |
| 8790 | } |
| 8791 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8793 | PyObject * |
| 8794 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8795 | Py_ssize_t size, |
| 8796 | PyObject *mapping, |
| 8797 | const char *errors) |
| 8798 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8799 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8800 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8801 | if (!unicode) |
| 8802 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8803 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8804 | Py_DECREF(unicode); |
| 8805 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8806 | } |
| 8807 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8808 | PyObject * |
| 8809 | PyUnicode_Translate(PyObject *str, |
| 8810 | PyObject *mapping, |
| 8811 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8812 | { |
| 8813 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8814 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8815 | str = PyUnicode_FromObject(str); |
| 8816 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8817 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8818 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8819 | Py_DECREF(str); |
| 8820 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8821 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8822 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8823 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8824 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8825 | { |
| 8826 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8827 | called as a callback from fixup() which does it already. */ |
| 8828 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8829 | const int kind = PyUnicode_KIND(self); |
| 8830 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8831 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8832 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 | Py_ssize_t i; |
| 8834 | |
| 8835 | for (i = 0; i < len; ++i) { |
| 8836 | ch = PyUnicode_READ(kind, data, i); |
| 8837 | fixed = 0; |
| 8838 | if (ch > 127) { |
| 8839 | if (Py_UNICODE_ISSPACE(ch)) |
| 8840 | fixed = ' '; |
| 8841 | else { |
| 8842 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8843 | if (decimal >= 0) |
| 8844 | fixed = '0' + decimal; |
| 8845 | } |
| 8846 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8847 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8848 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8849 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8850 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8851 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8852 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8853 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8854 | } |
| 8855 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8856 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8857 | } |
| 8858 | |
| 8859 | PyObject * |
| 8860 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8861 | { |
| 8862 | if (!PyUnicode_Check(unicode)) { |
| 8863 | PyErr_BadInternalCall(); |
| 8864 | return NULL; |
| 8865 | } |
| 8866 | if (PyUnicode_READY(unicode) == -1) |
| 8867 | return NULL; |
| 8868 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8869 | /* If the string is already ASCII, just return the same string */ |
| 8870 | Py_INCREF(unicode); |
| 8871 | return unicode; |
| 8872 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8873 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8874 | } |
| 8875 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8876 | PyObject * |
| 8877 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8878 | Py_ssize_t length) |
| 8879 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8880 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8881 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8882 | Py_UCS4 maxchar; |
| 8883 | enum PyUnicode_Kind kind; |
| 8884 | void *data; |
| 8885 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8886 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8887 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 8888 | Py_UCS4 ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8889 | if (ch > 127) { |
| 8890 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8891 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8892 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8893 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8894 | } |
| 8895 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8896 | |
| 8897 | /* Copy to a new string */ |
| 8898 | decimal = PyUnicode_New(length, maxchar); |
| 8899 | if (decimal == NULL) |
| 8900 | return decimal; |
| 8901 | kind = PyUnicode_KIND(decimal); |
| 8902 | data = PyUnicode_DATA(decimal); |
| 8903 | /* Iterate over code points */ |
| 8904 | for (i = 0; i < length; i++) { |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 8905 | Py_UCS4 ch = s[i]; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8906 | if (ch > 127) { |
| 8907 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8908 | if (decimal >= 0) |
| 8909 | ch = '0' + decimal; |
| 8910 | } |
| 8911 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8912 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8913 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8914 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8915 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8916 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8917 | int |
| 8918 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8919 | Py_ssize_t length, |
| 8920 | char *output, |
| 8921 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8922 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8923 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8924 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8925 | enum PyUnicode_Kind kind; |
| 8926 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8927 | |
| 8928 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8929 | PyErr_BadArgument(); |
| 8930 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8931 | } |
| 8932 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8933 | unicode = PyUnicode_FromUnicode(s, length); |
| 8934 | if (unicode == NULL) |
| 8935 | return -1; |
| 8936 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8937 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8938 | Py_DECREF(unicode); |
| 8939 | return -1; |
| 8940 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8941 | kind = PyUnicode_KIND(unicode); |
| 8942 | data = PyUnicode_DATA(unicode); |
| 8943 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8944 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8945 | PyObject *exc; |
| 8946 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8947 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8948 | Py_ssize_t startpos; |
| 8949 | |
| 8950 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8951 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8952 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8953 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8954 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8955 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8956 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8957 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8958 | if (decimal >= 0) { |
| 8959 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8960 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8961 | continue; |
| 8962 | } |
| 8963 | if (0 < ch && ch < 256) { |
| 8964 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8965 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8966 | continue; |
| 8967 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8968 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8969 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8970 | exc = NULL; |
| 8971 | raise_encode_exception(&exc, "decimal", unicode, |
| 8972 | startpos, startpos+1, |
| 8973 | "invalid decimal Unicode string"); |
| 8974 | Py_XDECREF(exc); |
| 8975 | Py_DECREF(unicode); |
| 8976 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8977 | } |
| 8978 | /* 0-terminate the output string */ |
| 8979 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8980 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8981 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8982 | } |
| 8983 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | /* --- Helpers ------------------------------------------------------------ */ |
| 8985 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 8986 | /* helper macro to fixup start/end slice values */ |
| 8987 | #define ADJUST_INDICES(start, end, len) \ |
| 8988 | if (end > len) \ |
| 8989 | end = len; \ |
| 8990 | else if (end < 0) { \ |
| 8991 | end += len; \ |
| 8992 | if (end < 0) \ |
| 8993 | end = 0; \ |
| 8994 | } \ |
| 8995 | if (start < 0) { \ |
| 8996 | start += len; \ |
| 8997 | if (start < 0) \ |
| 8998 | start = 0; \ |
| 8999 | } |
| 9000 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9002 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9003 | Py_ssize_t start, |
| 9004 | Py_ssize_t end) |
| 9005 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9006 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9007 | void *buf1, *buf2; |
| 9008 | Py_ssize_t len1, len2, result; |
| 9009 | |
| 9010 | kind1 = PyUnicode_KIND(s1); |
| 9011 | kind2 = PyUnicode_KIND(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9012 | if (kind1 < kind2) |
| 9013 | return -1; |
| 9014 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9015 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9016 | len2 = PyUnicode_GET_LENGTH(s2); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9017 | ADJUST_INDICES(start, end, len1); |
| 9018 | if (end - start < len2) |
| 9019 | return -1; |
| 9020 | |
| 9021 | buf1 = PyUnicode_DATA(s1); |
| 9022 | buf2 = PyUnicode_DATA(s2); |
| 9023 | if (len2 == 1) { |
| 9024 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 9025 | result = findchar((const char *)buf1 + kind1*start, |
| 9026 | kind1, end - start, ch, direction); |
| 9027 | if (result == -1) |
| 9028 | return -1; |
| 9029 | else |
| 9030 | return start + result; |
| 9031 | } |
| 9032 | |
| 9033 | if (kind2 != kind1) { |
| 9034 | buf2 = _PyUnicode_AsKind(s2, kind1); |
| 9035 | if (!buf2) |
| 9036 | return -2; |
| 9037 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9038 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9039 | if (direction > 0) { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9040 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9041 | case PyUnicode_1BYTE_KIND: |
| 9042 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9043 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9044 | else |
| 9045 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9046 | break; |
| 9047 | case PyUnicode_2BYTE_KIND: |
| 9048 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9049 | break; |
| 9050 | case PyUnicode_4BYTE_KIND: |
| 9051 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9052 | break; |
| 9053 | default: |
| 9054 | assert(0); result = -2; |
| 9055 | } |
| 9056 | } |
| 9057 | else { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9058 | switch (kind1) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9059 | case PyUnicode_1BYTE_KIND: |
| 9060 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9061 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9062 | else |
| 9063 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9064 | break; |
| 9065 | case PyUnicode_2BYTE_KIND: |
| 9066 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9067 | break; |
| 9068 | case PyUnicode_4BYTE_KIND: |
| 9069 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9070 | break; |
| 9071 | default: |
| 9072 | assert(0); result = -2; |
| 9073 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9074 | } |
| 9075 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9076 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9077 | PyMem_Free(buf2); |
| 9078 | |
| 9079 | return result; |
| 9080 | } |
| 9081 | |
| 9082 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9083 | _PyUnicode_InsertThousandsGrouping( |
| 9084 | PyObject *unicode, Py_ssize_t index, |
| 9085 | Py_ssize_t n_buffer, |
| 9086 | void *digits, Py_ssize_t n_digits, |
| 9087 | Py_ssize_t min_width, |
| 9088 | const char *grouping, PyObject *thousands_sep, |
| 9089 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9090 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9091 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9092 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9093 | Py_ssize_t thousands_sep_len; |
| 9094 | Py_ssize_t len; |
| 9095 | |
| 9096 | if (unicode != NULL) { |
| 9097 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9098 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9099 | } |
| 9100 | else { |
| 9101 | kind = PyUnicode_1BYTE_KIND; |
| 9102 | data = NULL; |
| 9103 | } |
| 9104 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9105 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9106 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9107 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9108 | if (thousands_sep_kind < kind) { |
| 9109 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9110 | if (!thousands_sep_data) |
| 9111 | return -1; |
| 9112 | } |
| 9113 | else { |
| 9114 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9115 | if (!data) |
| 9116 | return -1; |
| 9117 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9118 | } |
| 9119 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9120 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9121 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9122 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9123 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9124 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9125 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9126 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9127 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9128 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9129 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9130 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9131 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9132 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9133 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9134 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9135 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9136 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9137 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9138 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9139 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9140 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9141 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9142 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9143 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9144 | break; |
| 9145 | default: |
| 9146 | assert(0); |
| 9147 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9148 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9149 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9150 | if (thousands_sep_kind < kind) |
| 9151 | PyMem_Free(thousands_sep_data); |
| 9152 | else |
| 9153 | PyMem_Free(data); |
| 9154 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9155 | if (unicode == NULL) { |
| 9156 | *maxchar = 127; |
| 9157 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9158 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9159 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9160 | } |
| 9161 | } |
| 9162 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9163 | } |
| 9164 | |
| 9165 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9166 | Py_ssize_t |
| 9167 | PyUnicode_Count(PyObject *str, |
| 9168 | PyObject *substr, |
| 9169 | Py_ssize_t start, |
| 9170 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9171 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9172 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9173 | PyObject* str_obj; |
| 9174 | PyObject* sub_obj; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9175 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9176 | void *buf1 = NULL, *buf2 = NULL; |
| 9177 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9178 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9179 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9180 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9181 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9182 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9183 | if (!sub_obj) { |
| 9184 | Py_DECREF(str_obj); |
| 9185 | return -1; |
| 9186 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9187 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9188 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9189 | Py_DECREF(str_obj); |
| 9190 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9191 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9192 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9193 | kind1 = PyUnicode_KIND(str_obj); |
| 9194 | kind2 = PyUnicode_KIND(sub_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9195 | if (kind1 < kind2) { |
| 9196 | Py_DECREF(sub_obj); |
| 9197 | Py_DECREF(str_obj); |
| 9198 | return 0; |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9199 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9200 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9201 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9202 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9203 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9204 | if (end - start < len2) { |
| 9205 | Py_DECREF(sub_obj); |
| 9206 | Py_DECREF(str_obj); |
| 9207 | return 0; |
| 9208 | } |
| 9209 | |
| 9210 | buf1 = PyUnicode_DATA(str_obj); |
| 9211 | buf2 = PyUnicode_DATA(sub_obj); |
| 9212 | if (kind2 != kind1) { |
| 9213 | buf2 = _PyUnicode_AsKind(sub_obj, kind1); |
| 9214 | if (!buf2) |
| 9215 | goto onError; |
| 9216 | } |
| 9217 | |
| 9218 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9219 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9220 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9221 | result = asciilib_count( |
| 9222 | ((Py_UCS1*)buf1) + start, end - start, |
| 9223 | buf2, len2, PY_SSIZE_T_MAX |
| 9224 | ); |
| 9225 | else |
| 9226 | result = ucs1lib_count( |
| 9227 | ((Py_UCS1*)buf1) + start, end - start, |
| 9228 | buf2, len2, PY_SSIZE_T_MAX |
| 9229 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9230 | break; |
| 9231 | case PyUnicode_2BYTE_KIND: |
| 9232 | result = ucs2lib_count( |
| 9233 | ((Py_UCS2*)buf1) + start, end - start, |
| 9234 | buf2, len2, PY_SSIZE_T_MAX |
| 9235 | ); |
| 9236 | break; |
| 9237 | case PyUnicode_4BYTE_KIND: |
| 9238 | result = ucs4lib_count( |
| 9239 | ((Py_UCS4*)buf1) + start, end - start, |
| 9240 | buf2, len2, PY_SSIZE_T_MAX |
| 9241 | ); |
| 9242 | break; |
| 9243 | default: |
| 9244 | assert(0); result = 0; |
| 9245 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9246 | |
| 9247 | Py_DECREF(sub_obj); |
| 9248 | Py_DECREF(str_obj); |
| 9249 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9250 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9251 | PyMem_Free(buf2); |
| 9252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9253 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9254 | onError: |
| 9255 | Py_DECREF(sub_obj); |
| 9256 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9257 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9258 | PyMem_Free(buf2); |
| 9259 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9260 | } |
| 9261 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9262 | Py_ssize_t |
| 9263 | PyUnicode_Find(PyObject *str, |
| 9264 | PyObject *sub, |
| 9265 | Py_ssize_t start, |
| 9266 | Py_ssize_t end, |
| 9267 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9268 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9269 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9271 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9272 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9273 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9274 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9275 | if (!sub) { |
| 9276 | Py_DECREF(str); |
| 9277 | return -2; |
| 9278 | } |
| 9279 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9280 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9281 | Py_DECREF(str); |
| 9282 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9283 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9284 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9285 | result = any_find_slice(direction, |
| 9286 | str, sub, start, end |
| 9287 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9288 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9289 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9290 | Py_DECREF(sub); |
| 9291 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9292 | return result; |
| 9293 | } |
| 9294 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | Py_ssize_t |
| 9296 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9297 | Py_ssize_t start, Py_ssize_t end, |
| 9298 | int direction) |
| 9299 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9300 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9301 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9302 | if (PyUnicode_READY(str) == -1) |
| 9303 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9304 | if (start < 0 || end < 0) { |
| 9305 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9306 | return -2; |
| 9307 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9308 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9309 | end = PyUnicode_GET_LENGTH(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 9310 | if (start >= end) |
| 9311 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9312 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9313 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9314 | kind, end-start, ch, direction); |
| 9315 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9316 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9317 | else |
| 9318 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9319 | } |
| 9320 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9321 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9322 | tailmatch(PyObject *self, |
| 9323 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9324 | Py_ssize_t start, |
| 9325 | Py_ssize_t end, |
| 9326 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9327 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | int kind_self; |
| 9329 | int kind_sub; |
| 9330 | void *data_self; |
| 9331 | void *data_sub; |
| 9332 | Py_ssize_t offset; |
| 9333 | Py_ssize_t i; |
| 9334 | Py_ssize_t end_sub; |
| 9335 | |
| 9336 | if (PyUnicode_READY(self) == -1 || |
| 9337 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9338 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9339 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9340 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9341 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9342 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9343 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9344 | |
Serhiy Storchaka | d4ea03c | 2015-05-31 09:15:51 +0300 | [diff] [blame] | 9345 | if (PyUnicode_GET_LENGTH(substring) == 0) |
| 9346 | return 1; |
| 9347 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9348 | kind_self = PyUnicode_KIND(self); |
| 9349 | data_self = PyUnicode_DATA(self); |
| 9350 | kind_sub = PyUnicode_KIND(substring); |
| 9351 | data_sub = PyUnicode_DATA(substring); |
| 9352 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9353 | |
| 9354 | if (direction > 0) |
| 9355 | offset = end; |
| 9356 | else |
| 9357 | offset = start; |
| 9358 | |
| 9359 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9360 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9361 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9362 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9363 | /* If both are of the same kind, memcmp is sufficient */ |
| 9364 | if (kind_self == kind_sub) { |
| 9365 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9366 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9367 | data_sub, |
| 9368 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9369 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9370 | } |
| 9371 | /* otherwise we have to compare each character by first accesing it */ |
| 9372 | else { |
| 9373 | /* We do not need to compare 0 and len(substring)-1 because |
| 9374 | the if statement above ensured already that they are equal |
| 9375 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9376 | for (i = 1; i < end_sub; ++i) { |
| 9377 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9378 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9379 | return 0; |
| 9380 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9381 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9382 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9383 | } |
| 9384 | |
| 9385 | return 0; |
| 9386 | } |
| 9387 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9388 | Py_ssize_t |
| 9389 | PyUnicode_Tailmatch(PyObject *str, |
| 9390 | PyObject *substr, |
| 9391 | Py_ssize_t start, |
| 9392 | Py_ssize_t end, |
| 9393 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9394 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9395 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9397 | str = PyUnicode_FromObject(str); |
| 9398 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9399 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9400 | substr = PyUnicode_FromObject(substr); |
| 9401 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9402 | Py_DECREF(str); |
| 9403 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9404 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9405 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9406 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9407 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9408 | Py_DECREF(str); |
| 9409 | Py_DECREF(substr); |
| 9410 | return result; |
| 9411 | } |
| 9412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9413 | /* Apply fixfct filter to the Unicode object self and return a |
| 9414 | reference to the modified object */ |
| 9415 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9416 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9417 | fixup(PyObject *self, |
| 9418 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9419 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9420 | PyObject *u; |
| 9421 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9422 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9423 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9424 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9425 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9426 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9427 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9428 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9429 | /* fix functions return the new maximum character in a string, |
| 9430 | if the kind of the resulting unicode object does not change, |
| 9431 | everything is fine. Otherwise we need to change the string kind |
| 9432 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9433 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9434 | |
| 9435 | if (maxchar_new == 0) { |
| 9436 | /* no changes */; |
| 9437 | if (PyUnicode_CheckExact(self)) { |
| 9438 | Py_DECREF(u); |
| 9439 | Py_INCREF(self); |
| 9440 | return self; |
| 9441 | } |
| 9442 | else |
| 9443 | return u; |
| 9444 | } |
| 9445 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9446 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9447 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9448 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9449 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9450 | |
| 9451 | /* In case the maximum character changed, we need to |
| 9452 | convert the string to the new category. */ |
| 9453 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9454 | if (v == NULL) { |
| 9455 | Py_DECREF(u); |
| 9456 | return NULL; |
| 9457 | } |
| 9458 | if (maxchar_new > maxchar_old) { |
| 9459 | /* If the maxchar increased so that the kind changed, not all |
| 9460 | characters are representable anymore and we need to fix the |
| 9461 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9462 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9463 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9464 | maxchar_old = fixfct(v); |
| 9465 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9466 | } |
| 9467 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9468 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9469 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9470 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9471 | Py_DECREF(u); |
| 9472 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9473 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9474 | } |
| 9475 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9476 | static PyObject * |
| 9477 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9478 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9479 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9480 | char *resdata, *data = PyUnicode_DATA(self); |
| 9481 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9482 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9483 | res = PyUnicode_New(len, 127); |
| 9484 | if (res == NULL) |
| 9485 | return NULL; |
| 9486 | resdata = PyUnicode_DATA(res); |
| 9487 | if (lower) |
| 9488 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9489 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9490 | _Py_bytes_upper(resdata, data, len); |
| 9491 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9492 | } |
| 9493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9494 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9495 | 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] | 9496 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9497 | Py_ssize_t j; |
| 9498 | int final_sigma; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 9499 | Py_UCS4 c = 0; /* initialize to prevent gcc warning */ |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9500 | /* 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] | 9501 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9502 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9503 | |
| 9504 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9505 | */ |
| 9506 | for (j = i - 1; j >= 0; j--) { |
| 9507 | c = PyUnicode_READ(kind, data, j); |
| 9508 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9509 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9510 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9511 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9512 | if (final_sigma) { |
| 9513 | for (j = i + 1; j < length; j++) { |
| 9514 | c = PyUnicode_READ(kind, data, j); |
| 9515 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9516 | break; |
| 9517 | } |
| 9518 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9519 | } |
| 9520 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9521 | } |
| 9522 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9523 | static int |
| 9524 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9525 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9526 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9527 | /* Obscure special case. */ |
| 9528 | if (c == 0x3A3) { |
| 9529 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9530 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9532 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9533 | } |
| 9534 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9535 | static Py_ssize_t |
| 9536 | 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] | 9537 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9538 | Py_ssize_t i, k = 0; |
| 9539 | int n_res, j; |
| 9540 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9541 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9542 | c = PyUnicode_READ(kind, data, 0); |
| 9543 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9544 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9545 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9546 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9547 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9548 | for (i = 1; i < length; i++) { |
| 9549 | c = PyUnicode_READ(kind, data, i); |
| 9550 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9551 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9552 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9553 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9554 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9555 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9556 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9557 | } |
| 9558 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9559 | static Py_ssize_t |
| 9560 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9561 | Py_ssize_t i, k = 0; |
| 9562 | |
| 9563 | for (i = 0; i < length; i++) { |
| 9564 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9565 | int n_res, j; |
| 9566 | if (Py_UNICODE_ISUPPER(c)) { |
| 9567 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9568 | } |
| 9569 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9570 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9571 | } |
| 9572 | else { |
| 9573 | n_res = 1; |
| 9574 | mapped[0] = c; |
| 9575 | } |
| 9576 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9577 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9578 | res[k++] = mapped[j]; |
| 9579 | } |
| 9580 | } |
| 9581 | return k; |
| 9582 | } |
| 9583 | |
| 9584 | static Py_ssize_t |
| 9585 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9586 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9587 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9588 | Py_ssize_t i, k = 0; |
| 9589 | |
| 9590 | for (i = 0; i < length; i++) { |
| 9591 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9592 | int n_res, j; |
| 9593 | if (lower) |
| 9594 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9595 | else |
| 9596 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9597 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9598 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9599 | res[k++] = mapped[j]; |
| 9600 | } |
| 9601 | } |
| 9602 | return k; |
| 9603 | } |
| 9604 | |
| 9605 | static Py_ssize_t |
| 9606 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9607 | { |
| 9608 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9609 | } |
| 9610 | |
| 9611 | static Py_ssize_t |
| 9612 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9613 | { |
| 9614 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9615 | } |
| 9616 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9617 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9618 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9619 | { |
| 9620 | Py_ssize_t i, k = 0; |
| 9621 | |
| 9622 | for (i = 0; i < length; i++) { |
| 9623 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9624 | Py_UCS4 mapped[3]; |
| 9625 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9626 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9627 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9628 | res[k++] = mapped[j]; |
| 9629 | } |
| 9630 | } |
| 9631 | return k; |
| 9632 | } |
| 9633 | |
| 9634 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9635 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9636 | { |
| 9637 | Py_ssize_t i, k = 0; |
| 9638 | int previous_is_cased; |
| 9639 | |
| 9640 | previous_is_cased = 0; |
| 9641 | for (i = 0; i < length; i++) { |
| 9642 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9643 | Py_UCS4 mapped[3]; |
| 9644 | int n_res, j; |
| 9645 | |
| 9646 | if (previous_is_cased) |
| 9647 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9648 | else |
| 9649 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9650 | |
| 9651 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9652 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9653 | res[k++] = mapped[j]; |
| 9654 | } |
| 9655 | |
| 9656 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9657 | } |
| 9658 | return k; |
| 9659 | } |
| 9660 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9661 | static PyObject * |
| 9662 | case_operation(PyObject *self, |
| 9663 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9664 | { |
| 9665 | PyObject *res = NULL; |
| 9666 | Py_ssize_t length, newlength = 0; |
| 9667 | int kind, outkind; |
| 9668 | void *data, *outdata; |
| 9669 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9670 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9671 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9672 | |
| 9673 | kind = PyUnicode_KIND(self); |
| 9674 | data = PyUnicode_DATA(self); |
| 9675 | length = PyUnicode_GET_LENGTH(self); |
Antoine Pitrou | 4e33424 | 2014-10-15 23:14:53 +0200 | [diff] [blame] | 9676 | if ((size_t) length > PY_SSIZE_T_MAX / (3 * sizeof(Py_UCS4))) { |
Benjamin Peterson | e1bd38c | 2014-10-15 11:47:36 -0400 | [diff] [blame] | 9677 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 9678 | return NULL; |
| 9679 | } |
Benjamin Peterson | 1e211ff | 2014-10-15 12:17:21 -0400 | [diff] [blame] | 9680 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9681 | if (tmp == NULL) |
| 9682 | return PyErr_NoMemory(); |
| 9683 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9684 | res = PyUnicode_New(newlength, maxchar); |
| 9685 | if (res == NULL) |
| 9686 | goto leave; |
| 9687 | tmpend = tmp + newlength; |
| 9688 | outdata = PyUnicode_DATA(res); |
| 9689 | outkind = PyUnicode_KIND(res); |
| 9690 | switch (outkind) { |
| 9691 | case PyUnicode_1BYTE_KIND: |
| 9692 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9693 | break; |
| 9694 | case PyUnicode_2BYTE_KIND: |
| 9695 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9696 | break; |
| 9697 | case PyUnicode_4BYTE_KIND: |
| 9698 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9699 | break; |
| 9700 | default: |
| 9701 | assert(0); |
| 9702 | break; |
| 9703 | } |
| 9704 | leave: |
| 9705 | PyMem_FREE(tmp); |
| 9706 | return res; |
| 9707 | } |
| 9708 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9709 | PyObject * |
| 9710 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9711 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9712 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9713 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9714 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9715 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9716 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9717 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9718 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9719 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9720 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9721 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9722 | int use_memcpy; |
| 9723 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9724 | PyObject *last_obj; |
| 9725 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9726 | |
Benjamin Peterson | 9743b2c | 2014-02-15 13:02:52 -0500 | [diff] [blame] | 9727 | fseq = PySequence_Fast(seq, "can only join an iterable"); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9728 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9729 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9730 | } |
| 9731 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9732 | /* NOTE: the following code can't call back into Python code, |
| 9733 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9734 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9735 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9736 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9737 | /* If empty sequence, return u"". */ |
| 9738 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9739 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9740 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9741 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9742 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9743 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9744 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9745 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9746 | if (seqlen == 1) { |
| 9747 | if (PyUnicode_CheckExact(items[0])) { |
| 9748 | res = items[0]; |
| 9749 | Py_INCREF(res); |
| 9750 | Py_DECREF(fseq); |
| 9751 | return res; |
| 9752 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9753 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9754 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9755 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9756 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9757 | /* Set up sep and seplen */ |
| 9758 | if (separator == NULL) { |
| 9759 | /* fall back to a blank space separator */ |
| 9760 | sep = PyUnicode_FromOrdinal(' '); |
| 9761 | if (!sep) |
| 9762 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9763 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9764 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9765 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9766 | else { |
| 9767 | if (!PyUnicode_Check(separator)) { |
| 9768 | PyErr_Format(PyExc_TypeError, |
| 9769 | "separator: expected str instance," |
| 9770 | " %.80s found", |
| 9771 | Py_TYPE(separator)->tp_name); |
| 9772 | goto onError; |
| 9773 | } |
| 9774 | if (PyUnicode_READY(separator)) |
| 9775 | goto onError; |
| 9776 | sep = separator; |
| 9777 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9778 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9779 | /* inc refcount to keep this code path symmetric with the |
| 9780 | above case of a blank separator */ |
| 9781 | Py_INCREF(sep); |
| 9782 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9783 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9784 | } |
| 9785 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9786 | /* There are at least two things to join, or else we have a subclass |
| 9787 | * of str in the sequence. |
| 9788 | * Do a pre-pass to figure out the total amount of space we'll |
| 9789 | * need (sz), and see whether all argument are strings. |
| 9790 | */ |
| 9791 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9792 | #ifdef Py_DEBUG |
| 9793 | use_memcpy = 0; |
| 9794 | #else |
| 9795 | use_memcpy = 1; |
| 9796 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9797 | for (i = 0; i < seqlen; i++) { |
| 9798 | const Py_ssize_t old_sz = sz; |
| 9799 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9800 | if (!PyUnicode_Check(item)) { |
| 9801 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 9802 | "sequence item %zd: expected str instance," |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9803 | " %.80s found", |
| 9804 | i, Py_TYPE(item)->tp_name); |
| 9805 | goto onError; |
| 9806 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9807 | if (PyUnicode_READY(item) == -1) |
| 9808 | goto onError; |
| 9809 | sz += PyUnicode_GET_LENGTH(item); |
| 9810 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9811 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9812 | if (i != 0) |
| 9813 | sz += seplen; |
| 9814 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9815 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9816 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9817 | goto onError; |
| 9818 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9819 | if (use_memcpy && last_obj != NULL) { |
| 9820 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9821 | use_memcpy = 0; |
| 9822 | } |
| 9823 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9824 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9825 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9826 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9827 | if (res == NULL) |
| 9828 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9829 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9830 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9831 | #ifdef Py_DEBUG |
| 9832 | use_memcpy = 0; |
| 9833 | #else |
| 9834 | if (use_memcpy) { |
| 9835 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9836 | kind = PyUnicode_KIND(res); |
| 9837 | if (seplen != 0) |
| 9838 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9839 | } |
| 9840 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9841 | if (use_memcpy) { |
| 9842 | for (i = 0; i < seqlen; ++i) { |
| 9843 | Py_ssize_t itemlen; |
| 9844 | item = items[i]; |
| 9845 | |
| 9846 | /* Copy item, and maybe the separator. */ |
| 9847 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9848 | Py_MEMCPY(res_data, |
| 9849 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9850 | kind * seplen); |
| 9851 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9852 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9853 | |
| 9854 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9855 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9856 | Py_MEMCPY(res_data, |
| 9857 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9858 | kind * itemlen); |
| 9859 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9860 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9861 | } |
| 9862 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9863 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9864 | } |
| 9865 | else { |
| 9866 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9867 | Py_ssize_t itemlen; |
| 9868 | item = items[i]; |
| 9869 | |
| 9870 | /* Copy item, and maybe the separator. */ |
| 9871 | if (i && seplen != 0) { |
| 9872 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9873 | res_offset += seplen; |
| 9874 | } |
| 9875 | |
| 9876 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9877 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9878 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9879 | res_offset += itemlen; |
| 9880 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9881 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9882 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9883 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9884 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9885 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9886 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9887 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9888 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9889 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9890 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9891 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9892 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9893 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9894 | return NULL; |
| 9895 | } |
| 9896 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9897 | #define FILL(kind, data, value, start, length) \ |
| 9898 | do { \ |
| 9899 | Py_ssize_t i_ = 0; \ |
| 9900 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9901 | switch ((kind)) { \ |
| 9902 | case PyUnicode_1BYTE_KIND: { \ |
| 9903 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9904 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9905 | break; \ |
| 9906 | } \ |
| 9907 | case PyUnicode_2BYTE_KIND: { \ |
| 9908 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9909 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9910 | break; \ |
| 9911 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9912 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9913 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9914 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9915 | break; \ |
| 9916 | } \ |
Serhiy Storchaka | 133b11b | 2014-12-01 18:56:28 +0200 | [diff] [blame] | 9917 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9918 | } \ |
| 9919 | } while (0) |
| 9920 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9921 | void |
| 9922 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9923 | Py_UCS4 fill_char) |
| 9924 | { |
| 9925 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9926 | const void *data = PyUnicode_DATA(unicode); |
| 9927 | assert(PyUnicode_IS_READY(unicode)); |
| 9928 | assert(unicode_modifiable(unicode)); |
| 9929 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9930 | assert(start >= 0); |
| 9931 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9932 | FILL(kind, data, fill_char, start, length); |
| 9933 | } |
| 9934 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9935 | Py_ssize_t |
| 9936 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9937 | Py_UCS4 fill_char) |
| 9938 | { |
| 9939 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9940 | |
| 9941 | if (!PyUnicode_Check(unicode)) { |
| 9942 | PyErr_BadInternalCall(); |
| 9943 | return -1; |
| 9944 | } |
| 9945 | if (PyUnicode_READY(unicode) == -1) |
| 9946 | return -1; |
| 9947 | if (unicode_check_modifiable(unicode)) |
| 9948 | return -1; |
| 9949 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9950 | if (start < 0) { |
| 9951 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9952 | return -1; |
| 9953 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9954 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9955 | PyErr_SetString(PyExc_ValueError, |
| 9956 | "fill character is bigger than " |
| 9957 | "the string maximum character"); |
| 9958 | return -1; |
| 9959 | } |
| 9960 | |
| 9961 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9962 | length = Py_MIN(maxlen, length); |
| 9963 | if (length <= 0) |
| 9964 | return 0; |
| 9965 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9966 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9967 | return length; |
| 9968 | } |
| 9969 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9970 | static PyObject * |
| 9971 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9972 | Py_ssize_t left, |
| 9973 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9974 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9975 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9976 | PyObject *u; |
| 9977 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9978 | int kind; |
| 9979 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9980 | |
| 9981 | if (left < 0) |
| 9982 | left = 0; |
| 9983 | if (right < 0) |
| 9984 | right = 0; |
| 9985 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9986 | if (left == 0 && right == 0) |
| 9987 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9988 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9989 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9990 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9991 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9992 | return NULL; |
| 9993 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9994 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9995 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9996 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9997 | if (!u) |
| 9998 | return NULL; |
| 9999 | |
| 10000 | kind = PyUnicode_KIND(u); |
| 10001 | data = PyUnicode_DATA(u); |
| 10002 | if (left) |
| 10003 | FILL(kind, data, fill, 0, left); |
| 10004 | if (right) |
| 10005 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10006 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10007 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 10008 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10009 | } |
| 10010 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10011 | PyObject * |
| 10012 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10013 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10014 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10015 | |
| 10016 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10017 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10018 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10019 | if (PyUnicode_READY(string) == -1) { |
| 10020 | Py_DECREF(string); |
| 10021 | return NULL; |
| 10022 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10023 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10024 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10025 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10026 | if (PyUnicode_IS_ASCII(string)) |
| 10027 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10028 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10029 | PyUnicode_GET_LENGTH(string), keepends); |
| 10030 | else |
| 10031 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10032 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10033 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10034 | break; |
| 10035 | case PyUnicode_2BYTE_KIND: |
| 10036 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10037 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10038 | PyUnicode_GET_LENGTH(string), keepends); |
| 10039 | break; |
| 10040 | case PyUnicode_4BYTE_KIND: |
| 10041 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10042 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10043 | PyUnicode_GET_LENGTH(string), keepends); |
| 10044 | break; |
| 10045 | default: |
| 10046 | assert(0); |
| 10047 | list = 0; |
| 10048 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10049 | Py_DECREF(string); |
| 10050 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10051 | } |
| 10052 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10053 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10054 | split(PyObject *self, |
| 10055 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10056 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10057 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10058 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10059 | void *buf1, *buf2; |
| 10060 | Py_ssize_t len1, len2; |
| 10061 | PyObject* out; |
| 10062 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10063 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10064 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10065 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10066 | if (PyUnicode_READY(self) == -1) |
| 10067 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10070 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10072 | if (PyUnicode_IS_ASCII(self)) |
| 10073 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10074 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10075 | PyUnicode_GET_LENGTH(self), maxcount |
| 10076 | ); |
| 10077 | else |
| 10078 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10079 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10080 | PyUnicode_GET_LENGTH(self), maxcount |
| 10081 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10082 | case PyUnicode_2BYTE_KIND: |
| 10083 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10084 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10085 | PyUnicode_GET_LENGTH(self), maxcount |
| 10086 | ); |
| 10087 | case PyUnicode_4BYTE_KIND: |
| 10088 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10089 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10090 | PyUnicode_GET_LENGTH(self), maxcount |
| 10091 | ); |
| 10092 | default: |
| 10093 | assert(0); |
| 10094 | return NULL; |
| 10095 | } |
| 10096 | |
| 10097 | if (PyUnicode_READY(substring) == -1) |
| 10098 | return NULL; |
| 10099 | |
| 10100 | kind1 = PyUnicode_KIND(self); |
| 10101 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10102 | len1 = PyUnicode_GET_LENGTH(self); |
| 10103 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10104 | if (kind1 < kind2 || len1 < len2) { |
| 10105 | out = PyList_New(1); |
| 10106 | if (out == NULL) |
| 10107 | return NULL; |
| 10108 | Py_INCREF(self); |
| 10109 | PyList_SET_ITEM(out, 0, self); |
| 10110 | return out; |
| 10111 | } |
| 10112 | buf1 = PyUnicode_DATA(self); |
| 10113 | buf2 = PyUnicode_DATA(substring); |
| 10114 | if (kind2 != kind1) { |
| 10115 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10116 | if (!buf2) |
| 10117 | return NULL; |
| 10118 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10119 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10120 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10121 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10122 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10123 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10124 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10125 | else |
| 10126 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10127 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10128 | break; |
| 10129 | case PyUnicode_2BYTE_KIND: |
| 10130 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10131 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10132 | break; |
| 10133 | case PyUnicode_4BYTE_KIND: |
| 10134 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10135 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10136 | break; |
| 10137 | default: |
| 10138 | out = NULL; |
| 10139 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10140 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | PyMem_Free(buf2); |
| 10142 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10143 | } |
| 10144 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10145 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10146 | rsplit(PyObject *self, |
| 10147 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10148 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10149 | { |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10150 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10151 | void *buf1, *buf2; |
| 10152 | Py_ssize_t len1, len2; |
| 10153 | PyObject* out; |
| 10154 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10155 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10156 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10157 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10158 | if (PyUnicode_READY(self) == -1) |
| 10159 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10160 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10161 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10162 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10163 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10164 | if (PyUnicode_IS_ASCII(self)) |
| 10165 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10166 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10167 | PyUnicode_GET_LENGTH(self), maxcount |
| 10168 | ); |
| 10169 | else |
| 10170 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10171 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10172 | PyUnicode_GET_LENGTH(self), maxcount |
| 10173 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10174 | case PyUnicode_2BYTE_KIND: |
| 10175 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10176 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10177 | PyUnicode_GET_LENGTH(self), maxcount |
| 10178 | ); |
| 10179 | case PyUnicode_4BYTE_KIND: |
| 10180 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10181 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10182 | PyUnicode_GET_LENGTH(self), maxcount |
| 10183 | ); |
| 10184 | default: |
| 10185 | assert(0); |
| 10186 | return NULL; |
| 10187 | } |
| 10188 | |
| 10189 | if (PyUnicode_READY(substring) == -1) |
| 10190 | return NULL; |
| 10191 | |
| 10192 | kind1 = PyUnicode_KIND(self); |
| 10193 | kind2 = PyUnicode_KIND(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10194 | len1 = PyUnicode_GET_LENGTH(self); |
| 10195 | len2 = PyUnicode_GET_LENGTH(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10196 | if (kind1 < kind2 || len1 < len2) { |
| 10197 | out = PyList_New(1); |
| 10198 | if (out == NULL) |
| 10199 | return NULL; |
| 10200 | Py_INCREF(self); |
| 10201 | PyList_SET_ITEM(out, 0, self); |
| 10202 | return out; |
| 10203 | } |
| 10204 | buf1 = PyUnicode_DATA(self); |
| 10205 | buf2 = PyUnicode_DATA(substring); |
| 10206 | if (kind2 != kind1) { |
| 10207 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 10208 | if (!buf2) |
| 10209 | return NULL; |
| 10210 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10211 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10212 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10214 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10215 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10216 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10217 | else |
| 10218 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10219 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10220 | break; |
| 10221 | case PyUnicode_2BYTE_KIND: |
| 10222 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10223 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10224 | break; |
| 10225 | case PyUnicode_4BYTE_KIND: |
| 10226 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10227 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10228 | break; |
| 10229 | default: |
| 10230 | out = NULL; |
| 10231 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10232 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10233 | PyMem_Free(buf2); |
| 10234 | return out; |
| 10235 | } |
| 10236 | |
| 10237 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10238 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10239 | 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] | 10240 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10241 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10242 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10243 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10244 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10245 | else |
| 10246 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10247 | case PyUnicode_2BYTE_KIND: |
| 10248 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10249 | case PyUnicode_4BYTE_KIND: |
| 10250 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10251 | } |
| 10252 | assert(0); |
| 10253 | return -1; |
| 10254 | } |
| 10255 | |
| 10256 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10257 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10258 | 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] | 10259 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10260 | switch (kind) { |
| 10261 | case PyUnicode_1BYTE_KIND: |
| 10262 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10263 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10264 | else |
| 10265 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10266 | case PyUnicode_2BYTE_KIND: |
| 10267 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10268 | case PyUnicode_4BYTE_KIND: |
| 10269 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10270 | } |
| 10271 | assert(0); |
| 10272 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10273 | } |
| 10274 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10275 | static void |
| 10276 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10277 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10278 | { |
| 10279 | int kind = PyUnicode_KIND(u); |
| 10280 | void *data = PyUnicode_DATA(u); |
| 10281 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10282 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10283 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10284 | (Py_UCS1 *)data + len, |
| 10285 | u1, u2, maxcount); |
| 10286 | } |
| 10287 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10288 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10289 | (Py_UCS2 *)data + len, |
| 10290 | u1, u2, maxcount); |
| 10291 | } |
| 10292 | else { |
| 10293 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10294 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10295 | (Py_UCS4 *)data + len, |
| 10296 | u1, u2, maxcount); |
| 10297 | } |
| 10298 | } |
| 10299 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10300 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | replace(PyObject *self, PyObject *str1, |
| 10302 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10303 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10304 | PyObject *u; |
| 10305 | char *sbuf = PyUnicode_DATA(self); |
| 10306 | char *buf1 = PyUnicode_DATA(str1); |
| 10307 | char *buf2 = PyUnicode_DATA(str2); |
| 10308 | int srelease = 0, release1 = 0, release2 = 0; |
| 10309 | int skind = PyUnicode_KIND(self); |
| 10310 | int kind1 = PyUnicode_KIND(str1); |
| 10311 | int kind2 = PyUnicode_KIND(str2); |
| 10312 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10313 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10314 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10315 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10316 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10317 | |
| 10318 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10319 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10320 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10321 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10322 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10323 | if (str1 == str2) |
| 10324 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10325 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10326 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10327 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10328 | if (maxchar < maxchar_str1) |
| 10329 | /* substring too wide to be present */ |
| 10330 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10331 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10332 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10333 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10334 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10335 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10336 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10337 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10338 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10340 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10341 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10342 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10343 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10344 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10345 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10346 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10347 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10348 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10349 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10350 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10351 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10352 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10353 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10354 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10355 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10356 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10357 | } |
| 10358 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10359 | int rkind = skind; |
| 10360 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10361 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10362 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10363 | if (kind1 < rkind) { |
| 10364 | /* widen substring */ |
| 10365 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10366 | if (!buf1) goto error; |
| 10367 | release1 = 1; |
| 10368 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10369 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10370 | if (i < 0) |
| 10371 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10372 | if (rkind > kind2) { |
| 10373 | /* widen replacement */ |
| 10374 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10375 | if (!buf2) goto error; |
| 10376 | release2 = 1; |
| 10377 | } |
| 10378 | else if (rkind < kind2) { |
| 10379 | /* widen self and buf1 */ |
| 10380 | rkind = kind2; |
| 10381 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10382 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10384 | if (!sbuf) goto error; |
| 10385 | srelease = 1; |
| 10386 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10387 | if (!buf1) goto error; |
| 10388 | release1 = 1; |
| 10389 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10390 | u = PyUnicode_New(slen, maxchar); |
| 10391 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10392 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10393 | assert(PyUnicode_KIND(u) == rkind); |
| 10394 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10395 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10396 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10397 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10398 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10399 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10400 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10401 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10402 | |
| 10403 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10404 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10405 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10406 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10407 | if (i == -1) |
| 10408 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10409 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10410 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10411 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10412 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10413 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10414 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10415 | } |
| 10416 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10417 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10418 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10419 | int rkind = skind; |
| 10420 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10421 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10422 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10423 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10424 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10425 | if (!buf1) goto error; |
| 10426 | release1 = 1; |
| 10427 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10428 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10429 | if (n == 0) |
| 10430 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10431 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10432 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10433 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10434 | if (!buf2) goto error; |
| 10435 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10436 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10437 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10438 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10439 | rkind = kind2; |
| 10440 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10441 | if (!sbuf) goto error; |
| 10442 | srelease = 1; |
| 10443 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10444 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10446 | if (!buf1) goto error; |
| 10447 | release1 = 1; |
| 10448 | } |
| 10449 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10450 | PyUnicode_GET_LENGTH(str1))); */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10451 | 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] | 10452 | PyErr_SetString(PyExc_OverflowError, |
| 10453 | "replace string is too long"); |
| 10454 | goto error; |
| 10455 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10456 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10457 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10458 | _Py_INCREF_UNICODE_EMPTY(); |
| 10459 | if (!unicode_empty) |
| 10460 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10461 | u = unicode_empty; |
| 10462 | goto done; |
| 10463 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10464 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10465 | PyErr_SetString(PyExc_OverflowError, |
| 10466 | "replace string is too long"); |
| 10467 | goto error; |
| 10468 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10469 | u = PyUnicode_New(new_size, maxchar); |
| 10470 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10472 | assert(PyUnicode_KIND(u) == rkind); |
| 10473 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10474 | ires = i = 0; |
| 10475 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10476 | while (n-- > 0) { |
| 10477 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10478 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10479 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10480 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10481 | if (j == -1) |
| 10482 | break; |
| 10483 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10484 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10485 | memcpy(res + rkind * ires, |
| 10486 | sbuf + rkind * i, |
| 10487 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10488 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10489 | } |
| 10490 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10492 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10493 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10494 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10495 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10496 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10497 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10498 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10499 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10500 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10501 | memcpy(res + rkind * ires, |
| 10502 | sbuf + rkind * i, |
| 10503 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10504 | } |
| 10505 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10506 | /* interleave */ |
| 10507 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10508 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10509 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10510 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10511 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10512 | if (--n <= 0) |
| 10513 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10514 | memcpy(res + rkind * ires, |
| 10515 | sbuf + rkind * i, |
| 10516 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10517 | ires++; |
| 10518 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10519 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10520 | memcpy(res + rkind * ires, |
| 10521 | sbuf + rkind * i, |
| 10522 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10523 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10524 | } |
| 10525 | |
| 10526 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10527 | unicode_adjust_maxchar(&u); |
| 10528 | if (u == NULL) |
| 10529 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10530 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10531 | |
| 10532 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10533 | if (srelease) |
| 10534 | PyMem_FREE(sbuf); |
| 10535 | if (release1) |
| 10536 | PyMem_FREE(buf1); |
| 10537 | if (release2) |
| 10538 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10539 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10540 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10541 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10542 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10543 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10544 | if (srelease) |
| 10545 | PyMem_FREE(sbuf); |
| 10546 | if (release1) |
| 10547 | PyMem_FREE(buf1); |
| 10548 | if (release2) |
| 10549 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10550 | return unicode_result_unchanged(self); |
| 10551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10552 | error: |
| 10553 | if (srelease && sbuf) |
| 10554 | PyMem_FREE(sbuf); |
| 10555 | if (release1 && buf1) |
| 10556 | PyMem_FREE(buf1); |
| 10557 | if (release2 && buf2) |
| 10558 | PyMem_FREE(buf2); |
| 10559 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10560 | } |
| 10561 | |
| 10562 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10563 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10564 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10565 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10566 | \n\ |
| 10567 | 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] | 10568 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10569 | |
| 10570 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10571 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10572 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10573 | if (PyUnicode_READY(self) == -1) |
| 10574 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10575 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10576 | } |
| 10577 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10578 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10579 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10580 | \n\ |
| 10581 | 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] | 10582 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10583 | |
| 10584 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10585 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10586 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10587 | if (PyUnicode_READY(self) == -1) |
| 10588 | return NULL; |
| 10589 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10590 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10591 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10592 | } |
| 10593 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10594 | PyDoc_STRVAR(casefold__doc__, |
| 10595 | "S.casefold() -> str\n\ |
| 10596 | \n\ |
| 10597 | Return a version of S suitable for caseless comparisons."); |
| 10598 | |
| 10599 | static PyObject * |
| 10600 | unicode_casefold(PyObject *self) |
| 10601 | { |
| 10602 | if (PyUnicode_READY(self) == -1) |
| 10603 | return NULL; |
| 10604 | if (PyUnicode_IS_ASCII(self)) |
| 10605 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10606 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10607 | } |
| 10608 | |
| 10609 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10610 | /* Argument converter. Coerces to a single unicode character */ |
| 10611 | |
| 10612 | static int |
| 10613 | convert_uc(PyObject *obj, void *addr) |
| 10614 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10615 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10616 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10617 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10618 | uniobj = PyUnicode_FromObject(obj); |
| 10619 | if (uniobj == NULL) { |
| 10620 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10621 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10622 | return 0; |
| 10623 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10624 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10625 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10626 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10627 | Py_DECREF(uniobj); |
| 10628 | return 0; |
| 10629 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10630 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10631 | Py_DECREF(uniobj); |
| 10632 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10633 | } |
| 10634 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10635 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10636 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10637 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10638 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10639 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10640 | |
| 10641 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10642 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10643 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10644 | Py_ssize_t marg, left; |
| 10645 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10646 | Py_UCS4 fillchar = ' '; |
| 10647 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10648 | 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] | 10649 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10650 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10651 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10652 | return NULL; |
| 10653 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10654 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10655 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10656 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10657 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10658 | left = marg / 2 + (marg & width & 1); |
| 10659 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10660 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10661 | } |
| 10662 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10663 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10664 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10665 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10666 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10667 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10668 | #define COMPARE(TYPE1, TYPE2) \ |
| 10669 | do { \ |
| 10670 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10671 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10672 | TYPE1* end = p1 + len; \ |
| 10673 | Py_UCS4 c1, c2; \ |
| 10674 | for (; p1 != end; p1++, p2++) { \ |
| 10675 | c1 = *p1; \ |
| 10676 | c2 = *p2; \ |
| 10677 | if (c1 != c2) \ |
| 10678 | return (c1 < c2) ? -1 : 1; \ |
| 10679 | } \ |
| 10680 | } \ |
| 10681 | while (0) |
| 10682 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10683 | int kind1, kind2; |
| 10684 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10685 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10686 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10687 | kind1 = PyUnicode_KIND(str1); |
| 10688 | kind2 = PyUnicode_KIND(str2); |
| 10689 | data1 = PyUnicode_DATA(str1); |
| 10690 | data2 = PyUnicode_DATA(str2); |
| 10691 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10692 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10693 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10694 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10695 | switch(kind1) { |
| 10696 | case PyUnicode_1BYTE_KIND: |
| 10697 | { |
| 10698 | switch(kind2) { |
| 10699 | case PyUnicode_1BYTE_KIND: |
| 10700 | { |
| 10701 | int cmp = memcmp(data1, data2, len); |
| 10702 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10703 | if (cmp < 0) |
| 10704 | return -1; |
| 10705 | if (cmp > 0) |
| 10706 | return 1; |
| 10707 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10708 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10709 | case PyUnicode_2BYTE_KIND: |
| 10710 | COMPARE(Py_UCS1, Py_UCS2); |
| 10711 | break; |
| 10712 | case PyUnicode_4BYTE_KIND: |
| 10713 | COMPARE(Py_UCS1, Py_UCS4); |
| 10714 | break; |
| 10715 | default: |
| 10716 | assert(0); |
| 10717 | } |
| 10718 | break; |
| 10719 | } |
| 10720 | case PyUnicode_2BYTE_KIND: |
| 10721 | { |
| 10722 | switch(kind2) { |
| 10723 | case PyUnicode_1BYTE_KIND: |
| 10724 | COMPARE(Py_UCS2, Py_UCS1); |
| 10725 | break; |
| 10726 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10727 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10728 | COMPARE(Py_UCS2, Py_UCS2); |
| 10729 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10730 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10731 | case PyUnicode_4BYTE_KIND: |
| 10732 | COMPARE(Py_UCS2, Py_UCS4); |
| 10733 | break; |
| 10734 | default: |
| 10735 | assert(0); |
| 10736 | } |
| 10737 | break; |
| 10738 | } |
| 10739 | case PyUnicode_4BYTE_KIND: |
| 10740 | { |
| 10741 | switch(kind2) { |
| 10742 | case PyUnicode_1BYTE_KIND: |
| 10743 | COMPARE(Py_UCS4, Py_UCS1); |
| 10744 | break; |
| 10745 | case PyUnicode_2BYTE_KIND: |
| 10746 | COMPARE(Py_UCS4, Py_UCS2); |
| 10747 | break; |
| 10748 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10749 | { |
| 10750 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10751 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10752 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10753 | if (cmp < 0) |
| 10754 | return -1; |
| 10755 | if (cmp > 0) |
| 10756 | return 1; |
| 10757 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10758 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10759 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10760 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10761 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10762 | default: |
| 10763 | assert(0); |
| 10764 | } |
| 10765 | break; |
| 10766 | } |
| 10767 | default: |
| 10768 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10769 | } |
| 10770 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10771 | if (len1 == len2) |
| 10772 | return 0; |
| 10773 | if (len1 < len2) |
| 10774 | return -1; |
| 10775 | else |
| 10776 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10777 | |
| 10778 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10779 | } |
| 10780 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10781 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10782 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10783 | { |
| 10784 | int kind; |
| 10785 | void *data1, *data2; |
| 10786 | Py_ssize_t len; |
| 10787 | int cmp; |
| 10788 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10789 | len = PyUnicode_GET_LENGTH(str1); |
| 10790 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10791 | return 0; |
| 10792 | kind = PyUnicode_KIND(str1); |
| 10793 | if (PyUnicode_KIND(str2) != kind) |
| 10794 | return 0; |
| 10795 | data1 = PyUnicode_DATA(str1); |
| 10796 | data2 = PyUnicode_DATA(str2); |
| 10797 | |
| 10798 | cmp = memcmp(data1, data2, len * kind); |
| 10799 | return (cmp == 0); |
| 10800 | } |
| 10801 | |
| 10802 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10803 | int |
| 10804 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10805 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10806 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10807 | if (PyUnicode_READY(left) == -1 || |
| 10808 | PyUnicode_READY(right) == -1) |
| 10809 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10810 | |
| 10811 | /* a string is equal to itself */ |
| 10812 | if (left == right) |
| 10813 | return 0; |
| 10814 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10815 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10816 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10817 | PyErr_Format(PyExc_TypeError, |
| 10818 | "Can't compare %.100s and %.100s", |
| 10819 | left->ob_type->tp_name, |
| 10820 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10821 | return -1; |
| 10822 | } |
| 10823 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10824 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10825 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10826 | { |
| 10827 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10828 | if (right_str == NULL) |
| 10829 | return -1; |
| 10830 | return PyUnicode_Compare(left, right_str); |
| 10831 | } |
| 10832 | |
| 10833 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10834 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10835 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10836 | Py_ssize_t i; |
| 10837 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10838 | Py_UCS4 chr; |
| 10839 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10840 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10841 | if (PyUnicode_READY(uni) == -1) |
| 10842 | return -1; |
| 10843 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10844 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10845 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10846 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10847 | size_t len, len2 = strlen(str); |
| 10848 | int cmp; |
| 10849 | |
| 10850 | len = Py_MIN(len1, len2); |
| 10851 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 10852 | if (cmp != 0) { |
| 10853 | if (cmp < 0) |
| 10854 | return -1; |
| 10855 | else |
| 10856 | return 1; |
| 10857 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10858 | if (len1 > len2) |
| 10859 | return 1; /* uni is longer */ |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10860 | if (len1 < len2) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10861 | return -1; /* str is longer */ |
| 10862 | return 0; |
| 10863 | } |
| 10864 | else { |
| 10865 | void *data = PyUnicode_DATA(uni); |
| 10866 | /* Compare Unicode string and source character set string */ |
| 10867 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
Victor Stinner | 12174a5 | 2014-08-15 23:17:38 +0200 | [diff] [blame] | 10868 | if (chr != (unsigned char)str[i]) |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10869 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10870 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10871 | to C strings identical up to that point. */ |
| 10872 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 10873 | return 1; /* uni is longer */ |
| 10874 | if (str[i]) |
| 10875 | return -1; /* str is longer */ |
| 10876 | return 0; |
| 10877 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10878 | } |
| 10879 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10880 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10881 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10882 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10883 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10884 | PyObject * |
| 10885 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10886 | { |
| 10887 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10888 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10889 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10890 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10891 | Py_RETURN_NOTIMPLEMENTED; |
| 10892 | |
| 10893 | if (PyUnicode_READY(left) == -1 || |
| 10894 | PyUnicode_READY(right) == -1) |
| 10895 | return NULL; |
| 10896 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 10897 | if (left == right) { |
| 10898 | switch (op) { |
| 10899 | case Py_EQ: |
| 10900 | case Py_LE: |
| 10901 | case Py_GE: |
| 10902 | /* a string is equal to itself */ |
| 10903 | v = Py_True; |
| 10904 | break; |
| 10905 | case Py_NE: |
| 10906 | case Py_LT: |
| 10907 | case Py_GT: |
| 10908 | v = Py_False; |
| 10909 | break; |
| 10910 | default: |
| 10911 | PyErr_BadArgument(); |
| 10912 | return NULL; |
| 10913 | } |
| 10914 | } |
| 10915 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10916 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10917 | result ^= (op == Py_NE); |
| 10918 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10919 | } |
| 10920 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10921 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10922 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10923 | /* Convert the return value to a Boolean */ |
| 10924 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10925 | case Py_LE: |
| 10926 | v = TEST_COND(result <= 0); |
| 10927 | break; |
| 10928 | case Py_GE: |
| 10929 | v = TEST_COND(result >= 0); |
| 10930 | break; |
| 10931 | case Py_LT: |
| 10932 | v = TEST_COND(result == -1); |
| 10933 | break; |
| 10934 | case Py_GT: |
| 10935 | v = TEST_COND(result == 1); |
| 10936 | break; |
| 10937 | default: |
| 10938 | PyErr_BadArgument(); |
| 10939 | return NULL; |
| 10940 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10941 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10942 | Py_INCREF(v); |
| 10943 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10944 | } |
| 10945 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10946 | int |
Raymond Hettinger | ac2ef65 | 2015-07-04 16:04:44 -0700 | [diff] [blame] | 10947 | _PyUnicode_EQ(PyObject *aa, PyObject *bb) |
| 10948 | { |
| 10949 | return unicode_eq(aa, bb); |
| 10950 | } |
| 10951 | |
| 10952 | int |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10953 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10954 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10955 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10956 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10957 | void *buf1, *buf2; |
| 10958 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10959 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10960 | |
| 10961 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10962 | sub = PyUnicode_FromObject(element); |
| 10963 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10964 | PyErr_Format(PyExc_TypeError, |
| 10965 | "'in <string>' requires string as left operand, not %s", |
| 10966 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10967 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10968 | } |
| 10969 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10970 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10971 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10972 | Py_DECREF(sub); |
| 10973 | return -1; |
| 10974 | } |
| 10975 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10976 | kind1 = PyUnicode_KIND(str); |
| 10977 | kind2 = PyUnicode_KIND(sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10978 | if (kind1 < kind2) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10979 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10980 | Py_DECREF(str); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10981 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10982 | } |
| 10983 | len1 = PyUnicode_GET_LENGTH(str); |
| 10984 | len2 = PyUnicode_GET_LENGTH(sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 10985 | if (len1 < len2) { |
| 10986 | Py_DECREF(sub); |
| 10987 | Py_DECREF(str); |
| 10988 | return 0; |
| 10989 | } |
| 10990 | buf1 = PyUnicode_DATA(str); |
| 10991 | buf2 = PyUnicode_DATA(sub); |
| 10992 | if (len2 == 1) { |
| 10993 | Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0); |
| 10994 | result = findchar((const char *)buf1, kind1, len1, ch, 1) != -1; |
| 10995 | Py_DECREF(sub); |
| 10996 | Py_DECREF(str); |
| 10997 | return result; |
| 10998 | } |
| 10999 | if (kind2 != kind1) { |
| 11000 | buf2 = _PyUnicode_AsKind(sub, kind1); |
| 11001 | if (!buf2) { |
| 11002 | Py_DECREF(sub); |
| 11003 | Py_DECREF(str); |
| 11004 | return -1; |
| 11005 | } |
| 11006 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11007 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11008 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11009 | case PyUnicode_1BYTE_KIND: |
| 11010 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11011 | break; |
| 11012 | case PyUnicode_2BYTE_KIND: |
| 11013 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11014 | break; |
| 11015 | case PyUnicode_4BYTE_KIND: |
| 11016 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 11017 | break; |
| 11018 | default: |
| 11019 | result = -1; |
| 11020 | assert(0); |
| 11021 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11022 | |
| 11023 | Py_DECREF(str); |
| 11024 | Py_DECREF(sub); |
| 11025 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 11026 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11027 | PyMem_Free(buf2); |
| 11028 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11029 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 11030 | } |
| 11031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11032 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 11033 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11034 | PyObject * |
| 11035 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11036 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11037 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11038 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11039 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11040 | |
| 11041 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11042 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11044 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11045 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11046 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11047 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11048 | |
| 11049 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11050 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11051 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11052 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11053 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11054 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11055 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11056 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11057 | } |
| 11058 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11059 | u_len = PyUnicode_GET_LENGTH(u); |
| 11060 | v_len = PyUnicode_GET_LENGTH(v); |
| 11061 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11062 | PyErr_SetString(PyExc_OverflowError, |
| 11063 | "strings are too large to concat"); |
| 11064 | goto onError; |
| 11065 | } |
| 11066 | new_len = u_len + v_len; |
| 11067 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11068 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11069 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11070 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11072 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11073 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11074 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11075 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11076 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 11077 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11078 | Py_DECREF(u); |
| 11079 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11080 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11081 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11082 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11083 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11084 | Py_XDECREF(u); |
| 11085 | Py_XDECREF(v); |
| 11086 | return NULL; |
| 11087 | } |
| 11088 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11089 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11090 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11091 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11092 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11093 | Py_UCS4 maxchar, maxchar2; |
| 11094 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11095 | |
| 11096 | if (p_left == NULL) { |
| 11097 | if (!PyErr_Occurred()) |
| 11098 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11099 | return; |
| 11100 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11101 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11102 | if (right == NULL || left == NULL |
| 11103 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11104 | if (!PyErr_Occurred()) |
| 11105 | PyErr_BadInternalCall(); |
| 11106 | goto error; |
| 11107 | } |
| 11108 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11109 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11110 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11111 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11112 | goto error; |
| 11113 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11114 | /* Shortcuts */ |
| 11115 | if (left == unicode_empty) { |
| 11116 | Py_DECREF(left); |
| 11117 | Py_INCREF(right); |
| 11118 | *p_left = right; |
| 11119 | return; |
| 11120 | } |
| 11121 | if (right == unicode_empty) |
| 11122 | return; |
| 11123 | |
| 11124 | left_len = PyUnicode_GET_LENGTH(left); |
| 11125 | right_len = PyUnicode_GET_LENGTH(right); |
| 11126 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11127 | PyErr_SetString(PyExc_OverflowError, |
| 11128 | "strings are too large to concat"); |
| 11129 | goto error; |
| 11130 | } |
| 11131 | new_len = left_len + right_len; |
| 11132 | |
| 11133 | if (unicode_modifiable(left) |
| 11134 | && PyUnicode_CheckExact(right) |
| 11135 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11136 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11137 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11138 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11139 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11140 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11141 | { |
| 11142 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11143 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11144 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11145 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11146 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11147 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11148 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11149 | else { |
| 11150 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11151 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11152 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11153 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11154 | /* Concat the two Unicode strings */ |
| 11155 | res = PyUnicode_New(new_len, maxchar); |
| 11156 | if (res == NULL) |
| 11157 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11158 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11159 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11160 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11161 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11162 | } |
| 11163 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11164 | return; |
| 11165 | |
| 11166 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11167 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11168 | } |
| 11169 | |
| 11170 | void |
| 11171 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11172 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11173 | PyUnicode_Append(pleft, right); |
| 11174 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11175 | } |
| 11176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11177 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11178 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11179 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11180 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11181 | 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] | 11182 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | |
| 11184 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11185 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11186 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11187 | PyObject *substring = NULL; /* initialize to fix a compiler warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11188 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11189 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11190 | PyObject *result; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11191 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11192 | void *buf1, *buf2; |
| 11193 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11194 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11195 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11196 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11197 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11198 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11199 | kind1 = PyUnicode_KIND(self); |
| 11200 | kind2 = PyUnicode_KIND(substring); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11201 | if (kind1 < kind2) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11202 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11203 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11204 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11205 | len1 = PyUnicode_GET_LENGTH(self); |
| 11206 | len2 = PyUnicode_GET_LENGTH(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11207 | ADJUST_INDICES(start, end, len1); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11208 | if (end - start < len2) { |
| 11209 | Py_DECREF(substring); |
| 11210 | return PyLong_FromLong(0); |
| 11211 | } |
| 11212 | buf1 = PyUnicode_DATA(self); |
| 11213 | buf2 = PyUnicode_DATA(substring); |
| 11214 | if (kind2 != kind1) { |
| 11215 | buf2 = _PyUnicode_AsKind(substring, kind1); |
| 11216 | if (!buf2) { |
| 11217 | Py_DECREF(substring); |
| 11218 | return NULL; |
| 11219 | } |
| 11220 | } |
| 11221 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11222 | case PyUnicode_1BYTE_KIND: |
| 11223 | iresult = ucs1lib_count( |
| 11224 | ((Py_UCS1*)buf1) + start, end - start, |
| 11225 | buf2, len2, PY_SSIZE_T_MAX |
| 11226 | ); |
| 11227 | break; |
| 11228 | case PyUnicode_2BYTE_KIND: |
| 11229 | iresult = ucs2lib_count( |
| 11230 | ((Py_UCS2*)buf1) + start, end - start, |
| 11231 | buf2, len2, PY_SSIZE_T_MAX |
| 11232 | ); |
| 11233 | break; |
| 11234 | case PyUnicode_4BYTE_KIND: |
| 11235 | iresult = ucs4lib_count( |
| 11236 | ((Py_UCS4*)buf1) + start, end - start, |
| 11237 | buf2, len2, PY_SSIZE_T_MAX |
| 11238 | ); |
| 11239 | break; |
| 11240 | default: |
| 11241 | assert(0); iresult = 0; |
| 11242 | } |
| 11243 | |
| 11244 | result = PyLong_FromSsize_t(iresult); |
| 11245 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 11246 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11247 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11248 | |
| 11249 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11251 | return result; |
| 11252 | } |
| 11253 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11254 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11255 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11256 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11257 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11258 | 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] | 11259 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11260 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11261 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11262 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11263 | |
| 11264 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11265 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11266 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11267 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11268 | char *encoding = NULL; |
| 11269 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11270 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11271 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11272 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11273 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11274 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11275 | } |
| 11276 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11277 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11278 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | \n\ |
| 11280 | 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] | 11281 | 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] | 11282 | |
| 11283 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11284 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11285 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11286 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11287 | Py_UCS4 ch; |
| 11288 | PyObject *u; |
| 11289 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11290 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11291 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11292 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11293 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11295 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11296 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11297 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11299 | if (PyUnicode_READY(self) == -1) |
| 11300 | return NULL; |
| 11301 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11302 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11303 | src_len = PyUnicode_GET_LENGTH(self); |
| 11304 | i = j = line_pos = 0; |
| 11305 | kind = PyUnicode_KIND(self); |
| 11306 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11307 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11308 | for (; i < src_len; i++) { |
| 11309 | ch = PyUnicode_READ(kind, src_data, i); |
| 11310 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11311 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11312 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11313 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11314 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11315 | goto overflow; |
| 11316 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11317 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11318 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11319 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11320 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11321 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11322 | goto overflow; |
| 11323 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11324 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11325 | if (ch == '\n' || ch == '\r') |
| 11326 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11327 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11328 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11329 | if (!found) |
| 11330 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11332 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11333 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11334 | if (!u) |
| 11335 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11336 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11337 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11338 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11340 | for (; i < src_len; i++) { |
| 11341 | ch = PyUnicode_READ(kind, src_data, i); |
| 11342 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11343 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11344 | incr = tabsize - (line_pos % tabsize); |
| 11345 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11346 | FILL(kind, dest_data, ' ', j, incr); |
| 11347 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11348 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11349 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11350 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11351 | line_pos++; |
| 11352 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11353 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11354 | if (ch == '\n' || ch == '\r') |
| 11355 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11356 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11357 | } |
| 11358 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11359 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11360 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11361 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11362 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11363 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11364 | } |
| 11365 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11366 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11367 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11368 | \n\ |
| 11369 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11370 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11371 | arguments start and end are interpreted as in slice notation.\n\ |
| 11372 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11373 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11374 | |
| 11375 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11376 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11377 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11378 | /* initialize variables to prevent gcc warning */ |
| 11379 | PyObject *substring = NULL; |
| 11380 | Py_ssize_t start = 0; |
| 11381 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11382 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11383 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11384 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11385 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11387 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11388 | if (PyUnicode_READY(self) == -1) { |
| 11389 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11390 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11391 | } |
| 11392 | if (PyUnicode_READY(substring) == -1) { |
| 11393 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11394 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11395 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11396 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11397 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11398 | |
| 11399 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11400 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | if (result == -2) |
| 11402 | return NULL; |
| 11403 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11404 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11405 | } |
| 11406 | |
| 11407 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11408 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11410 | void *data; |
| 11411 | enum PyUnicode_Kind kind; |
| 11412 | Py_UCS4 ch; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11413 | |
| 11414 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11415 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11416 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11417 | } |
| 11418 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11419 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11420 | return NULL; |
| 11421 | } |
| 11422 | kind = PyUnicode_KIND(self); |
| 11423 | data = PyUnicode_DATA(self); |
| 11424 | ch = PyUnicode_READ(kind, data, index); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 11425 | return unicode_char(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | } |
| 11427 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11428 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11429 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11430 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11431 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11432 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11433 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11434 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11435 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11436 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11437 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11438 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11439 | if (_PyUnicode_HASH(self) != -1) |
| 11440 | return _PyUnicode_HASH(self); |
| 11441 | if (PyUnicode_READY(self) == -1) |
| 11442 | return -1; |
| 11443 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11444 | /* |
| 11445 | We make the hash of the empty string be 0, rather than using |
| 11446 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11447 | */ |
| 11448 | if (len == 0) { |
| 11449 | _PyUnicode_HASH(self) = 0; |
| 11450 | return 0; |
| 11451 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11452 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11453 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11454 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11455 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | } |
| 11457 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11458 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11459 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11461 | 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] | 11462 | |
| 11463 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11465 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11466 | /* initialize variables to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11467 | Py_ssize_t result; |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 11468 | PyObject *substring = NULL; |
| 11469 | Py_ssize_t start = 0; |
| 11470 | Py_ssize_t end = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11472 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11473 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11474 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11476 | if (PyUnicode_READY(self) == -1) { |
| 11477 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11478 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11479 | } |
| 11480 | if (PyUnicode_READY(substring) == -1) { |
| 11481 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11482 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11483 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11484 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11485 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | |
| 11487 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11488 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11489 | if (result == -2) |
| 11490 | return NULL; |
| 11491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11492 | if (result < 0) { |
| 11493 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11494 | return NULL; |
| 11495 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11496 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11497 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | } |
| 11499 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11500 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11501 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11502 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11503 | 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] | 11504 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11505 | |
| 11506 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11507 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11508 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11509 | Py_ssize_t i, length; |
| 11510 | int kind; |
| 11511 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 | int cased; |
| 11513 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11514 | if (PyUnicode_READY(self) == -1) |
| 11515 | return NULL; |
| 11516 | length = PyUnicode_GET_LENGTH(self); |
| 11517 | kind = PyUnicode_KIND(self); |
| 11518 | data = PyUnicode_DATA(self); |
| 11519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11521 | if (length == 1) |
| 11522 | return PyBool_FromLong( |
| 11523 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11524 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11525 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11526 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11527 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11528 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11529 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11530 | for (i = 0; i < length; i++) { |
| 11531 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11532 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11533 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11534 | return PyBool_FromLong(0); |
| 11535 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11536 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11537 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11538 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11539 | } |
| 11540 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11541 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11542 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11543 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11544 | 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] | 11545 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11546 | |
| 11547 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11548 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11549 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11550 | Py_ssize_t i, length; |
| 11551 | int kind; |
| 11552 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11553 | int cased; |
| 11554 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11555 | if (PyUnicode_READY(self) == -1) |
| 11556 | return NULL; |
| 11557 | length = PyUnicode_GET_LENGTH(self); |
| 11558 | kind = PyUnicode_KIND(self); |
| 11559 | data = PyUnicode_DATA(self); |
| 11560 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11561 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | if (length == 1) |
| 11563 | return PyBool_FromLong( |
| 11564 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
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; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11571 | for (i = 0; i < length; i++) { |
| 11572 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11573 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11574 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11575 | return PyBool_FromLong(0); |
| 11576 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11577 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11578 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11579 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11580 | } |
| 11581 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11582 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11583 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11584 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11585 | Return True if S is a titlecased string and there is at least one\n\ |
| 11586 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11587 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11588 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11589 | |
| 11590 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11591 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11592 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11593 | Py_ssize_t i, length; |
| 11594 | int kind; |
| 11595 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11596 | int cased, previous_is_cased; |
| 11597 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11598 | if (PyUnicode_READY(self) == -1) |
| 11599 | return NULL; |
| 11600 | length = PyUnicode_GET_LENGTH(self); |
| 11601 | kind = PyUnicode_KIND(self); |
| 11602 | data = PyUnicode_DATA(self); |
| 11603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11604 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11605 | if (length == 1) { |
| 11606 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11607 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11608 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11609 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11610 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11611 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11612 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11613 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11614 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11615 | cased = 0; |
| 11616 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11617 | for (i = 0; i < length; i++) { |
| 11618 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11619 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11620 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11621 | if (previous_is_cased) |
| 11622 | return PyBool_FromLong(0); |
| 11623 | previous_is_cased = 1; |
| 11624 | cased = 1; |
| 11625 | } |
| 11626 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11627 | if (!previous_is_cased) |
| 11628 | return PyBool_FromLong(0); |
| 11629 | previous_is_cased = 1; |
| 11630 | cased = 1; |
| 11631 | } |
| 11632 | else |
| 11633 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11634 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11635 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11636 | } |
| 11637 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11638 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11639 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11640 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11641 | Return True if all characters in S are whitespace\n\ |
| 11642 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11643 | |
| 11644 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11645 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11646 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11647 | Py_ssize_t i, length; |
| 11648 | int kind; |
| 11649 | void *data; |
| 11650 | |
| 11651 | if (PyUnicode_READY(self) == -1) |
| 11652 | return NULL; |
| 11653 | length = PyUnicode_GET_LENGTH(self); |
| 11654 | kind = PyUnicode_KIND(self); |
| 11655 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11656 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11657 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11658 | if (length == 1) |
| 11659 | return PyBool_FromLong( |
| 11660 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11661 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11662 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11663 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11664 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11665 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11666 | for (i = 0; i < length; i++) { |
| 11667 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11668 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11669 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11670 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11671 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11672 | } |
| 11673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11674 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11675 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11676 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11677 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11678 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11679 | |
| 11680 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11681 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11682 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11683 | Py_ssize_t i, length; |
| 11684 | int kind; |
| 11685 | void *data; |
| 11686 | |
| 11687 | if (PyUnicode_READY(self) == -1) |
| 11688 | return NULL; |
| 11689 | length = PyUnicode_GET_LENGTH(self); |
| 11690 | kind = PyUnicode_KIND(self); |
| 11691 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11692 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11693 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11694 | if (length == 1) |
| 11695 | return PyBool_FromLong( |
| 11696 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11697 | |
| 11698 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11699 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11700 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11701 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11702 | for (i = 0; i < length; i++) { |
| 11703 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11704 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11705 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11706 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11707 | } |
| 11708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11709 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11710 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11711 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11712 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11713 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11714 | |
| 11715 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11716 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11717 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11718 | int kind; |
| 11719 | void *data; |
| 11720 | Py_ssize_t len, i; |
| 11721 | |
| 11722 | if (PyUnicode_READY(self) == -1) |
| 11723 | return NULL; |
| 11724 | |
| 11725 | kind = PyUnicode_KIND(self); |
| 11726 | data = PyUnicode_DATA(self); |
| 11727 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11728 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11729 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11730 | if (len == 1) { |
| 11731 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11732 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11733 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11734 | |
| 11735 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11736 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11737 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11739 | for (i = 0; i < len; i++) { |
| 11740 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11741 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11742 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11743 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11744 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11745 | } |
| 11746 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11747 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11748 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11750 | 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] | 11751 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11752 | |
| 11753 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11754 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11756 | Py_ssize_t i, length; |
| 11757 | int kind; |
| 11758 | void *data; |
| 11759 | |
| 11760 | if (PyUnicode_READY(self) == -1) |
| 11761 | return NULL; |
| 11762 | length = PyUnicode_GET_LENGTH(self); |
| 11763 | kind = PyUnicode_KIND(self); |
| 11764 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11766 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11767 | if (length == 1) |
| 11768 | return PyBool_FromLong( |
| 11769 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11770 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11771 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11772 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11773 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11774 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11775 | for (i = 0; i < length; i++) { |
| 11776 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11777 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11778 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11779 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | } |
| 11781 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11782 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11783 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11784 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11785 | Return True if all characters in S are digits\n\ |
| 11786 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11787 | |
| 11788 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11789 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11790 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11791 | Py_ssize_t i, length; |
| 11792 | int kind; |
| 11793 | void *data; |
| 11794 | |
| 11795 | if (PyUnicode_READY(self) == -1) |
| 11796 | return NULL; |
| 11797 | length = PyUnicode_GET_LENGTH(self); |
| 11798 | kind = PyUnicode_KIND(self); |
| 11799 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11801 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11802 | if (length == 1) { |
| 11803 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11804 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11805 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11807 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11808 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11809 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11810 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11811 | for (i = 0; i < length; i++) { |
| 11812 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11813 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11814 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11815 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11816 | } |
| 11817 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11818 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11819 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11820 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11821 | 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] | 11822 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11823 | |
| 11824 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11825 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11826 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11827 | Py_ssize_t i, length; |
| 11828 | int kind; |
| 11829 | void *data; |
| 11830 | |
| 11831 | if (PyUnicode_READY(self) == -1) |
| 11832 | return NULL; |
| 11833 | length = PyUnicode_GET_LENGTH(self); |
| 11834 | kind = PyUnicode_KIND(self); |
| 11835 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11836 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11837 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11838 | if (length == 1) |
| 11839 | return PyBool_FromLong( |
| 11840 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11841 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11842 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11843 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11844 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11845 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11846 | for (i = 0; i < length; i++) { |
| 11847 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11848 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11849 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11850 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11851 | } |
| 11852 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11853 | int |
| 11854 | PyUnicode_IsIdentifier(PyObject *self) |
| 11855 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11856 | int kind; |
| 11857 | void *data; |
| 11858 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11859 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11860 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11861 | if (PyUnicode_READY(self) == -1) { |
| 11862 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11863 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11864 | } |
| 11865 | |
| 11866 | /* Special case for empty strings */ |
| 11867 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11868 | return 0; |
| 11869 | kind = PyUnicode_KIND(self); |
| 11870 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11871 | |
| 11872 | /* PEP 3131 says that the first character must be in |
| 11873 | XID_Start and subsequent characters in XID_Continue, |
| 11874 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11875 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11876 | letters, digits, underscore). However, given the current |
| 11877 | definition of XID_Start and XID_Continue, it is sufficient |
| 11878 | to check just for these, except that _ must be allowed |
| 11879 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11880 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11881 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11882 | return 0; |
| 11883 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11884 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11885 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11886 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11887 | return 1; |
| 11888 | } |
| 11889 | |
| 11890 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11891 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11892 | \n\ |
| 11893 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11894 | to the language definition.\n\ |
| 11895 | \n\ |
| 11896 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11897 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11898 | |
| 11899 | static PyObject* |
| 11900 | unicode_isidentifier(PyObject *self) |
| 11901 | { |
| 11902 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11903 | } |
| 11904 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11905 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11906 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11907 | \n\ |
| 11908 | Return True if all characters in S are considered\n\ |
| 11909 | printable in repr() or S is empty, False otherwise."); |
| 11910 | |
| 11911 | static PyObject* |
| 11912 | unicode_isprintable(PyObject *self) |
| 11913 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11914 | Py_ssize_t i, length; |
| 11915 | int kind; |
| 11916 | void *data; |
| 11917 | |
| 11918 | if (PyUnicode_READY(self) == -1) |
| 11919 | return NULL; |
| 11920 | length = PyUnicode_GET_LENGTH(self); |
| 11921 | kind = PyUnicode_KIND(self); |
| 11922 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11923 | |
| 11924 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11925 | if (length == 1) |
| 11926 | return PyBool_FromLong( |
| 11927 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11928 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11929 | for (i = 0; i < length; i++) { |
| 11930 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11931 | Py_RETURN_FALSE; |
| 11932 | } |
| 11933 | } |
| 11934 | Py_RETURN_TRUE; |
| 11935 | } |
| 11936 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11937 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11938 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11939 | \n\ |
| 11940 | 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] | 11941 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11942 | |
| 11943 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11944 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11945 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11946 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11947 | } |
| 11948 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11949 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11950 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11952 | if (PyUnicode_READY(self) == -1) |
| 11953 | return -1; |
| 11954 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11955 | } |
| 11956 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11957 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11958 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11959 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11960 | 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] | 11961 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11962 | |
| 11963 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11964 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11965 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11966 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11967 | Py_UCS4 fillchar = ' '; |
| 11968 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11969 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11970 | return NULL; |
| 11971 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11972 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11973 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11974 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11975 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11976 | return unicode_result_unchanged(self); |
| 11977 | |
| 11978 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11979 | } |
| 11980 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11981 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11982 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11983 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11984 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11985 | |
| 11986 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11987 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11988 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11989 | if (PyUnicode_READY(self) == -1) |
| 11990 | return NULL; |
| 11991 | if (PyUnicode_IS_ASCII(self)) |
| 11992 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11993 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11994 | } |
| 11995 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11996 | #define LEFTSTRIP 0 |
| 11997 | #define RIGHTSTRIP 1 |
| 11998 | #define BOTHSTRIP 2 |
| 11999 | |
| 12000 | /* Arrays indexed by above */ |
| 12001 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 12002 | |
| 12003 | #define STRIPNAME(i) (stripformat[i]+3) |
| 12004 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12005 | /* externally visible for str.strip(unicode) */ |
| 12006 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12007 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12008 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12009 | void *data; |
| 12010 | int kind; |
| 12011 | Py_ssize_t i, j, len; |
| 12012 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12013 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12014 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12015 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 12016 | return NULL; |
| 12017 | |
| 12018 | kind = PyUnicode_KIND(self); |
| 12019 | data = PyUnicode_DATA(self); |
| 12020 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12021 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12022 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 12023 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12024 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12025 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12026 | i = 0; |
| 12027 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12028 | while (i < len) { |
| 12029 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12030 | if (!BLOOM(sepmask, ch)) |
| 12031 | break; |
| 12032 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12033 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12034 | i++; |
| 12035 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12036 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12037 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12038 | j = len; |
| 12039 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12040 | j--; |
| 12041 | while (j >= i) { |
| 12042 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12043 | if (!BLOOM(sepmask, ch)) |
| 12044 | break; |
| 12045 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12046 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12047 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12048 | } |
| 12049 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12050 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12051 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12052 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12053 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12054 | } |
| 12055 | |
| 12056 | PyObject* |
| 12057 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12058 | { |
| 12059 | unsigned char *data; |
| 12060 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12061 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12062 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12063 | if (PyUnicode_READY(self) == -1) |
| 12064 | return NULL; |
| 12065 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12066 | length = PyUnicode_GET_LENGTH(self); |
| 12067 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12068 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12069 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12070 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12071 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12072 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12073 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12074 | return NULL; |
| 12075 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12076 | if (start >= length || end < start) |
| 12077 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12078 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12079 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12080 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12081 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12082 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12083 | } |
| 12084 | else { |
| 12085 | kind = PyUnicode_KIND(self); |
| 12086 | data = PyUnicode_1BYTE_DATA(self); |
| 12087 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12088 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12089 | length); |
| 12090 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12091 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12092 | |
| 12093 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12094 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12095 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12096 | Py_ssize_t len, i, j; |
| 12097 | |
| 12098 | if (PyUnicode_READY(self) == -1) |
| 12099 | return NULL; |
| 12100 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12101 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12102 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12103 | if (PyUnicode_IS_ASCII(self)) { |
| 12104 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12105 | |
| 12106 | i = 0; |
| 12107 | if (striptype != RIGHTSTRIP) { |
| 12108 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12109 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12110 | if (!_Py_ascii_whitespace[ch]) |
| 12111 | break; |
| 12112 | i++; |
| 12113 | } |
| 12114 | } |
| 12115 | |
| 12116 | j = len; |
| 12117 | if (striptype != LEFTSTRIP) { |
| 12118 | j--; |
| 12119 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12120 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12121 | if (!_Py_ascii_whitespace[ch]) |
| 12122 | break; |
| 12123 | j--; |
| 12124 | } |
| 12125 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12126 | } |
| 12127 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12128 | else { |
| 12129 | int kind = PyUnicode_KIND(self); |
| 12130 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12131 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12132 | i = 0; |
| 12133 | if (striptype != RIGHTSTRIP) { |
| 12134 | while (i < len) { |
| 12135 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12136 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12137 | break; |
| 12138 | i++; |
| 12139 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12140 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12141 | |
| 12142 | j = len; |
| 12143 | if (striptype != LEFTSTRIP) { |
| 12144 | j--; |
| 12145 | while (j >= i) { |
| 12146 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12147 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12148 | break; |
| 12149 | j--; |
| 12150 | } |
| 12151 | j++; |
| 12152 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12153 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12154 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12155 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12156 | } |
| 12157 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12158 | |
| 12159 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12160 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12161 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12162 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12163 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12164 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12165 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12166 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12167 | if (sep != NULL && sep != Py_None) { |
| 12168 | if (PyUnicode_Check(sep)) |
| 12169 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12170 | else { |
| 12171 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12172 | "%s arg must be None or str", |
| 12173 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12174 | return NULL; |
| 12175 | } |
| 12176 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12177 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12178 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12179 | } |
| 12180 | |
| 12181 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12182 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12183 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12184 | \n\ |
| 12185 | Return a copy of the string S with leading and trailing\n\ |
| 12186 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12187 | 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] | 12188 | |
| 12189 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12190 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12191 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12192 | if (PyTuple_GET_SIZE(args) == 0) |
| 12193 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12194 | else |
| 12195 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12196 | } |
| 12197 | |
| 12198 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12199 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12200 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12201 | \n\ |
| 12202 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12203 | 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] | 12204 | |
| 12205 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12206 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12207 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12208 | if (PyTuple_GET_SIZE(args) == 0) |
| 12209 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12210 | else |
| 12211 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12212 | } |
| 12213 | |
| 12214 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12215 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12216 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12217 | \n\ |
| 12218 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12219 | 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] | 12220 | |
| 12221 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12222 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12223 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12224 | if (PyTuple_GET_SIZE(args) == 0) |
| 12225 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12226 | else |
| 12227 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12228 | } |
| 12229 | |
| 12230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12231 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12232 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12233 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12234 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12235 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12236 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12237 | if (len < 1) |
| 12238 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12239 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12240 | /* no repeat, return original string */ |
| 12241 | if (len == 1) |
| 12242 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12243 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12244 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12245 | return NULL; |
| 12246 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12247 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12248 | PyErr_SetString(PyExc_OverflowError, |
| 12249 | "repeated string is too long"); |
| 12250 | return NULL; |
| 12251 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12252 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12253 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12254 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12255 | if (!u) |
| 12256 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12257 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12258 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12259 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12260 | const int kind = PyUnicode_KIND(str); |
| 12261 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12262 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12263 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12264 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12265 | } |
| 12266 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12267 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12268 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12269 | ucs2[n] = fill_char; |
| 12270 | } else { |
| 12271 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12272 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12273 | for (n = 0; n < len; ++n) |
| 12274 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12275 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12276 | } |
| 12277 | else { |
| 12278 | /* number of characters copied this far */ |
| 12279 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12280 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12281 | char *to = (char *) PyUnicode_DATA(u); |
| 12282 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12283 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12285 | n = (done <= nchars-done) ? done : nchars-done; |
| 12286 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12287 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12288 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12289 | } |
| 12290 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12291 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12292 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12293 | } |
| 12294 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12295 | PyObject * |
| 12296 | PyUnicode_Replace(PyObject *obj, |
| 12297 | PyObject *subobj, |
| 12298 | PyObject *replobj, |
| 12299 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12300 | { |
| 12301 | PyObject *self; |
| 12302 | PyObject *str1; |
| 12303 | PyObject *str2; |
| 12304 | PyObject *result; |
| 12305 | |
| 12306 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12307 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12308 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12309 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12310 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12311 | Py_DECREF(self); |
| 12312 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12313 | } |
| 12314 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12315 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12316 | Py_DECREF(self); |
| 12317 | Py_DECREF(str1); |
| 12318 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12319 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12320 | if (PyUnicode_READY(self) == -1 || |
| 12321 | PyUnicode_READY(str1) == -1 || |
| 12322 | PyUnicode_READY(str2) == -1) |
| 12323 | result = NULL; |
| 12324 | else |
| 12325 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12326 | Py_DECREF(self); |
| 12327 | Py_DECREF(str1); |
| 12328 | Py_DECREF(str2); |
| 12329 | return result; |
| 12330 | } |
| 12331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12332 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12333 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12334 | \n\ |
| 12335 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12336 | old replaced by new. If the optional argument count is\n\ |
| 12337 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12338 | |
| 12339 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12340 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12341 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12342 | PyObject *str1; |
| 12343 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12344 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12345 | PyObject *result; |
| 12346 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12347 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12348 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12349 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12350 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12351 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12352 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12353 | return NULL; |
| 12354 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12355 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12356 | Py_DECREF(str1); |
| 12357 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12358 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12359 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12360 | result = NULL; |
| 12361 | else |
| 12362 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12363 | |
| 12364 | Py_DECREF(str1); |
| 12365 | Py_DECREF(str2); |
| 12366 | return result; |
| 12367 | } |
| 12368 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12369 | static PyObject * |
| 12370 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12371 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12372 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12373 | Py_ssize_t isize; |
| 12374 | Py_ssize_t osize, squote, dquote, i, o; |
| 12375 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12376 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12377 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12378 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12379 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12380 | return NULL; |
| 12381 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12382 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12383 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12384 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12385 | /* Compute length of output, quote characters, and |
| 12386 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12387 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12388 | max = 127; |
| 12389 | squote = dquote = 0; |
| 12390 | ikind = PyUnicode_KIND(unicode); |
| 12391 | for (i = 0; i < isize; i++) { |
| 12392 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12393 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12394 | switch (ch) { |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12395 | case '\'': squote++; break; |
| 12396 | case '"': dquote++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12397 | case '\\': case '\t': case '\r': case '\n': |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12398 | incr = 2; |
| 12399 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12400 | default: |
| 12401 | /* Fast-path ASCII */ |
| 12402 | if (ch < ' ' || ch == 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12403 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12404 | else if (ch < 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12405 | ; |
| 12406 | else if (Py_UNICODE_ISPRINTABLE(ch)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12407 | max = ch > max ? ch : max; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12408 | else if (ch < 0x100) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12409 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12410 | else if (ch < 0x10000) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12411 | incr = 6; /* \uHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12412 | else |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12413 | incr = 10; /* \uHHHHHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12414 | } |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12415 | if (osize > PY_SSIZE_T_MAX - incr) { |
| 12416 | PyErr_SetString(PyExc_OverflowError, |
| 12417 | "string is too long to generate repr"); |
| 12418 | return NULL; |
| 12419 | } |
| 12420 | osize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12421 | } |
| 12422 | |
| 12423 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12424 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12425 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12426 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12427 | if (dquote) |
| 12428 | /* Both squote and dquote present. Use squote, |
| 12429 | and escape them */ |
| 12430 | osize += squote; |
| 12431 | else |
| 12432 | quote = '"'; |
| 12433 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12434 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12435 | |
| 12436 | repr = PyUnicode_New(osize, max); |
| 12437 | if (repr == NULL) |
| 12438 | return NULL; |
| 12439 | okind = PyUnicode_KIND(repr); |
| 12440 | odata = PyUnicode_DATA(repr); |
| 12441 | |
| 12442 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12443 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12444 | if (unchanged) { |
| 12445 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12446 | unicode, 0, |
| 12447 | isize); |
| 12448 | } |
| 12449 | else { |
| 12450 | for (i = 0, o = 1; i < isize; i++) { |
| 12451 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12452 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12453 | /* Escape quotes and backslashes */ |
| 12454 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12455 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12456 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12457 | continue; |
| 12458 | } |
| 12459 | |
| 12460 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12461 | if (ch == '\t') { |
| 12462 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12463 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12464 | } |
| 12465 | else if (ch == '\n') { |
| 12466 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12467 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12468 | } |
| 12469 | else if (ch == '\r') { |
| 12470 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12471 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12472 | } |
| 12473 | |
| 12474 | /* Map non-printable US ASCII to '\xhh' */ |
| 12475 | else if (ch < ' ' || ch == 0x7F) { |
| 12476 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12477 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12478 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12479 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12480 | } |
| 12481 | |
| 12482 | /* Copy ASCII characters as-is */ |
| 12483 | else if (ch < 0x7F) { |
| 12484 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12485 | } |
| 12486 | |
| 12487 | /* Non-ASCII characters */ |
| 12488 | else { |
| 12489 | /* Map Unicode whitespace and control characters |
| 12490 | (categories Z* and C* except ASCII space) |
| 12491 | */ |
| 12492 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12493 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12494 | /* Map 8-bit characters to '\xhh' */ |
| 12495 | if (ch <= 0xff) { |
| 12496 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12497 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12498 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12499 | } |
| 12500 | /* Map 16-bit characters to '\uxxxx' */ |
| 12501 | else if (ch <= 0xffff) { |
| 12502 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12503 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12504 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12505 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12506 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12507 | } |
| 12508 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12509 | else { |
| 12510 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12511 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12512 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12513 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12514 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12515 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12516 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12517 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12518 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12519 | } |
| 12520 | } |
| 12521 | /* Copy characters as-is */ |
| 12522 | else { |
| 12523 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12524 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12525 | } |
| 12526 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12527 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12528 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12529 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12530 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12531 | } |
| 12532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12533 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12534 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12535 | \n\ |
| 12536 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12537 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12538 | arguments start and end are interpreted as in slice notation.\n\ |
| 12539 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12540 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12541 | |
| 12542 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12543 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12544 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12545 | /* initialize variables to prevent gcc warning */ |
| 12546 | PyObject *substring = NULL; |
| 12547 | Py_ssize_t start = 0; |
| 12548 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12549 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12550 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12551 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12552 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12553 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12554 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12555 | if (PyUnicode_READY(self) == -1) { |
| 12556 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12557 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12558 | } |
| 12559 | if (PyUnicode_READY(substring) == -1) { |
| 12560 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12561 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12562 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12563 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12564 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12565 | |
| 12566 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12567 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12568 | if (result == -2) |
| 12569 | return NULL; |
| 12570 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12571 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12572 | } |
| 12573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12574 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12575 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12576 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12577 | 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] | 12578 | |
| 12579 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12580 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12581 | { |
Victor Stinner | 0c39b1b | 2015-03-18 15:02:06 +0100 | [diff] [blame] | 12582 | /* initialize variables to prevent gcc warning */ |
| 12583 | PyObject *substring = NULL; |
| 12584 | Py_ssize_t start = 0; |
| 12585 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12586 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12587 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12588 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12589 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12590 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12591 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12592 | if (PyUnicode_READY(self) == -1) { |
| 12593 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12594 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12595 | } |
| 12596 | if (PyUnicode_READY(substring) == -1) { |
| 12597 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12598 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12599 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12600 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12601 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12602 | |
| 12603 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12604 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12605 | if (result == -2) |
| 12606 | return NULL; |
| 12607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12608 | if (result < 0) { |
| 12609 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12610 | return NULL; |
| 12611 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12612 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12613 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12614 | } |
| 12615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12616 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12617 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12618 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12619 | 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] | 12620 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12621 | |
| 12622 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12623 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12624 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12625 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12626 | Py_UCS4 fillchar = ' '; |
| 12627 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12628 | 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] | 12629 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12630 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12631 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12632 | return NULL; |
| 12633 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12634 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12635 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12636 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12637 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12638 | } |
| 12639 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12640 | PyObject * |
| 12641 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12642 | { |
| 12643 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12644 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12645 | s = PyUnicode_FromObject(s); |
| 12646 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12647 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12648 | if (sep != NULL) { |
| 12649 | sep = PyUnicode_FromObject(sep); |
| 12650 | if (sep == NULL) { |
| 12651 | Py_DECREF(s); |
| 12652 | return NULL; |
| 12653 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12654 | } |
| 12655 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12656 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12657 | |
| 12658 | Py_DECREF(s); |
| 12659 | Py_XDECREF(sep); |
| 12660 | return result; |
| 12661 | } |
| 12662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12663 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12664 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12665 | \n\ |
| 12666 | Return a list of the words in S, using sep as the\n\ |
| 12667 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12668 | 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] | 12669 | whitespace string is a separator and empty strings are\n\ |
| 12670 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12671 | |
| 12672 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12673 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12674 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12675 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12676 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12677 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12678 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12679 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12680 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12681 | return NULL; |
| 12682 | |
| 12683 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12684 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12685 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12686 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12687 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12688 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12689 | } |
| 12690 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12691 | PyObject * |
| 12692 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12693 | { |
| 12694 | PyObject* str_obj; |
| 12695 | PyObject* sep_obj; |
| 12696 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12697 | int kind1, kind2; |
| 12698 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12699 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12700 | |
| 12701 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12702 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12703 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12704 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12705 | if (!sep_obj) { |
| 12706 | Py_DECREF(str_obj); |
| 12707 | return NULL; |
| 12708 | } |
| 12709 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12710 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12711 | Py_DECREF(str_obj); |
| 12712 | return NULL; |
| 12713 | } |
| 12714 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12715 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12716 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12717 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12718 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12719 | if (kind1 < kind2 || len1 < len2) { |
| 12720 | _Py_INCREF_UNICODE_EMPTY(); |
| 12721 | if (!unicode_empty) |
| 12722 | out = NULL; |
| 12723 | else { |
| 12724 | out = PyTuple_Pack(3, str_obj, unicode_empty, unicode_empty); |
| 12725 | Py_DECREF(unicode_empty); |
| 12726 | } |
| 12727 | Py_DECREF(sep_obj); |
| 12728 | Py_DECREF(str_obj); |
| 12729 | return out; |
| 12730 | } |
| 12731 | buf1 = PyUnicode_DATA(str_obj); |
| 12732 | buf2 = PyUnicode_DATA(sep_obj); |
| 12733 | if (kind2 != kind1) { |
| 12734 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12735 | if (!buf2) |
| 12736 | goto onError; |
| 12737 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12739 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12740 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12741 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12742 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12743 | else |
| 12744 | 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] | 12745 | break; |
| 12746 | case PyUnicode_2BYTE_KIND: |
| 12747 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12748 | break; |
| 12749 | case PyUnicode_4BYTE_KIND: |
| 12750 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12751 | break; |
| 12752 | default: |
| 12753 | assert(0); |
| 12754 | out = 0; |
| 12755 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12756 | |
| 12757 | Py_DECREF(sep_obj); |
| 12758 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12759 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12760 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12761 | |
| 12762 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12763 | onError: |
| 12764 | Py_DECREF(sep_obj); |
| 12765 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12766 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12767 | PyMem_Free(buf2); |
| 12768 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12769 | } |
| 12770 | |
| 12771 | |
| 12772 | PyObject * |
| 12773 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12774 | { |
| 12775 | PyObject* str_obj; |
| 12776 | PyObject* sep_obj; |
| 12777 | PyObject* out; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12778 | int kind1, kind2; |
| 12779 | void *buf1, *buf2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12780 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12781 | |
| 12782 | str_obj = PyUnicode_FromObject(str_in); |
| 12783 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12784 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12785 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12786 | if (!sep_obj) { |
| 12787 | Py_DECREF(str_obj); |
| 12788 | return NULL; |
| 12789 | } |
| 12790 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12791 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12792 | kind2 = PyUnicode_KIND(sep_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12793 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12794 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12795 | if (kind1 < kind2 || len1 < len2) { |
| 12796 | _Py_INCREF_UNICODE_EMPTY(); |
| 12797 | if (!unicode_empty) |
| 12798 | out = NULL; |
| 12799 | else { |
| 12800 | out = PyTuple_Pack(3, unicode_empty, unicode_empty, str_obj); |
| 12801 | Py_DECREF(unicode_empty); |
| 12802 | } |
| 12803 | Py_DECREF(sep_obj); |
| 12804 | Py_DECREF(str_obj); |
| 12805 | return out; |
| 12806 | } |
| 12807 | buf1 = PyUnicode_DATA(str_obj); |
| 12808 | buf2 = PyUnicode_DATA(sep_obj); |
| 12809 | if (kind2 != kind1) { |
| 12810 | buf2 = _PyUnicode_AsKind(sep_obj, kind1); |
| 12811 | if (!buf2) |
| 12812 | goto onError; |
| 12813 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12814 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12815 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12816 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12817 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12818 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12819 | else |
| 12820 | 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] | 12821 | break; |
| 12822 | case PyUnicode_2BYTE_KIND: |
| 12823 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12824 | break; |
| 12825 | case PyUnicode_4BYTE_KIND: |
| 12826 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12827 | break; |
| 12828 | default: |
| 12829 | assert(0); |
| 12830 | out = 0; |
| 12831 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12832 | |
| 12833 | Py_DECREF(sep_obj); |
| 12834 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12835 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12836 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12837 | |
| 12838 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12839 | onError: |
| 12840 | Py_DECREF(sep_obj); |
| 12841 | Py_DECREF(str_obj); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 12842 | if (kind2 != kind1 && buf2) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12843 | PyMem_Free(buf2); |
| 12844 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12845 | } |
| 12846 | |
| 12847 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12848 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12849 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12850 | 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] | 12851 | 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] | 12852 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12853 | |
| 12854 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12855 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12856 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12857 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12858 | } |
| 12859 | |
| 12860 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12861 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12862 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12863 | 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] | 12864 | 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] | 12865 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12866 | |
| 12867 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12868 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12869 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12870 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12871 | } |
| 12872 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12873 | PyObject * |
| 12874 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12875 | { |
| 12876 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12877 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12878 | s = PyUnicode_FromObject(s); |
| 12879 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12880 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12881 | if (sep != NULL) { |
| 12882 | sep = PyUnicode_FromObject(sep); |
| 12883 | if (sep == NULL) { |
| 12884 | Py_DECREF(s); |
| 12885 | return NULL; |
| 12886 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12887 | } |
| 12888 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12889 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12890 | |
| 12891 | Py_DECREF(s); |
| 12892 | Py_XDECREF(sep); |
| 12893 | return result; |
| 12894 | } |
| 12895 | |
| 12896 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12897 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12898 | \n\ |
| 12899 | Return a list of the words in S, using sep as the\n\ |
| 12900 | delimiter string, starting at the end of the string and\n\ |
| 12901 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12902 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12903 | is a separator."); |
| 12904 | |
| 12905 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12906 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12907 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12908 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12909 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12910 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12911 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12912 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12913 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12914 | return NULL; |
| 12915 | |
| 12916 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12917 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12918 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12919 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12920 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12921 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12922 | } |
| 12923 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12924 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12925 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12926 | \n\ |
| 12927 | 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] | 12928 | 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] | 12929 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12930 | |
| 12931 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12932 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12933 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12934 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12935 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12936 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12937 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12938 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12939 | return NULL; |
| 12940 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12941 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12942 | } |
| 12943 | |
| 12944 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12945 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12946 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12947 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12948 | } |
| 12949 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12950 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12951 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12952 | \n\ |
| 12953 | 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] | 12954 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12955 | |
| 12956 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12957 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12958 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12959 | if (PyUnicode_READY(self) == -1) |
| 12960 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12961 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12962 | } |
| 12963 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 12964 | /*[clinic input] |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12965 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12966 | @staticmethod |
| 12967 | str.maketrans as unicode_maketrans |
| 12968 | |
| 12969 | x: object |
| 12970 | |
| 12971 | y: unicode=NULL |
| 12972 | |
| 12973 | z: unicode=NULL |
| 12974 | |
| 12975 | / |
| 12976 | |
| 12977 | Return a translation table usable for str.translate(). |
| 12978 | |
| 12979 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12980 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12981 | Character keys will be then converted to ordinals. |
| 12982 | If there are two arguments, they must be strings of equal length, and |
| 12983 | in the resulting dictionary, each character in x will be mapped to the |
| 12984 | character at the same position in y. If there is a third argument, it |
| 12985 | 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] | 12986 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12987 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12988 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12989 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 12990 | /*[clinic end generated code: output=a925c89452bd5881 input=7bfbf529a293c6c5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12991 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12992 | PyObject *new = NULL, *key, *value; |
| 12993 | Py_ssize_t i = 0; |
| 12994 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12995 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12996 | new = PyDict_New(); |
| 12997 | if (!new) |
| 12998 | return NULL; |
| 12999 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13000 | int x_kind, y_kind, z_kind; |
| 13001 | void *x_data, *y_data, *z_data; |
| 13002 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13003 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13004 | if (!PyUnicode_Check(x)) { |
| 13005 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 13006 | "be a string if there is a second argument"); |
| 13007 | goto err; |
| 13008 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13009 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13010 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 13011 | "arguments must have equal length"); |
| 13012 | goto err; |
| 13013 | } |
| 13014 | /* 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] | 13015 | x_kind = PyUnicode_KIND(x); |
| 13016 | y_kind = PyUnicode_KIND(y); |
| 13017 | x_data = PyUnicode_DATA(x); |
| 13018 | y_data = PyUnicode_DATA(y); |
| 13019 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 13020 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 13021 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13022 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 13023 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 13024 | if (!value) { |
| 13025 | Py_DECREF(key); |
| 13026 | goto err; |
| 13027 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13028 | res = PyDict_SetItem(new, key, value); |
| 13029 | Py_DECREF(key); |
| 13030 | Py_DECREF(value); |
| 13031 | if (res < 0) |
| 13032 | goto err; |
| 13033 | } |
| 13034 | /* create entries for deleting chars in z */ |
| 13035 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13036 | z_kind = PyUnicode_KIND(z); |
| 13037 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13038 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13039 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13040 | if (!key) |
| 13041 | goto err; |
| 13042 | res = PyDict_SetItem(new, key, Py_None); |
| 13043 | Py_DECREF(key); |
| 13044 | if (res < 0) |
| 13045 | goto err; |
| 13046 | } |
| 13047 | } |
| 13048 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13049 | int kind; |
| 13050 | void *data; |
| 13051 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13052 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 13053 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13054 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 13055 | "to maketrans it must be a dict"); |
| 13056 | goto err; |
| 13057 | } |
| 13058 | /* copy entries into the new dict, converting string keys to int keys */ |
| 13059 | while (PyDict_Next(x, &i, &key, &value)) { |
| 13060 | if (PyUnicode_Check(key)) { |
| 13061 | /* convert string keys to integer keys */ |
| 13062 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13063 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13064 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13065 | "table must be of length 1"); |
| 13066 | goto err; |
| 13067 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13068 | kind = PyUnicode_KIND(key); |
| 13069 | data = PyUnicode_DATA(key); |
| 13070 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13071 | if (!newkey) |
| 13072 | goto err; |
| 13073 | res = PyDict_SetItem(new, newkey, value); |
| 13074 | Py_DECREF(newkey); |
| 13075 | if (res < 0) |
| 13076 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13077 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13078 | /* just keep integer keys */ |
| 13079 | if (PyDict_SetItem(new, key, value) < 0) |
| 13080 | goto err; |
| 13081 | } else { |
| 13082 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13083 | "be strings or integers"); |
| 13084 | goto err; |
| 13085 | } |
| 13086 | } |
| 13087 | } |
| 13088 | return new; |
| 13089 | err: |
| 13090 | Py_DECREF(new); |
| 13091 | return NULL; |
| 13092 | } |
| 13093 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13094 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13095 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13096 | \n\ |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 13097 | Return a copy of the string S in which each character has been mapped\n\ |
| 13098 | through the given translation table. The table must implement\n\ |
| 13099 | lookup/indexing via __getitem__, for instance a dictionary or list,\n\ |
| 13100 | mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n\ |
| 13101 | this operation raises LookupError, the character is left untouched.\n\ |
| 13102 | Characters mapped to None are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13103 | |
| 13104 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13105 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13106 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13107 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13108 | } |
| 13109 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13110 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13111 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13112 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13113 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13114 | |
| 13115 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13116 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13117 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13118 | if (PyUnicode_READY(self) == -1) |
| 13119 | return NULL; |
| 13120 | if (PyUnicode_IS_ASCII(self)) |
| 13121 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13122 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13123 | } |
| 13124 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13125 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13126 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13127 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13128 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13129 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13130 | |
| 13131 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13132 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13133 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13134 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13135 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13136 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13137 | int kind; |
| 13138 | void *data; |
| 13139 | Py_UCS4 chr; |
| 13140 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13141 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13142 | return NULL; |
| 13143 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13144 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13145 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13146 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13147 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13148 | return unicode_result_unchanged(self); |
| 13149 | |
| 13150 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13151 | |
| 13152 | u = pad(self, fill, 0, '0'); |
| 13153 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13154 | if (u == NULL) |
| 13155 | return NULL; |
| 13156 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13157 | kind = PyUnicode_KIND(u); |
| 13158 | data = PyUnicode_DATA(u); |
| 13159 | chr = PyUnicode_READ(kind, data, fill); |
| 13160 | |
| 13161 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13162 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13163 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13164 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13165 | } |
| 13166 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13167 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13168 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13169 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13170 | |
| 13171 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13172 | static PyObject * |
| 13173 | unicode__decimal2ascii(PyObject *self) |
| 13174 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13175 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13176 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13177 | #endif |
| 13178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13179 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13180 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13181 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13182 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13183 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13184 | With optional end, stop comparing S at that position.\n\ |
| 13185 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13186 | |
| 13187 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13188 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13189 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13190 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13191 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13192 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13193 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13194 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13195 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13196 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13197 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13198 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13199 | if (PyTuple_Check(subobj)) { |
| 13200 | Py_ssize_t i; |
| 13201 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13202 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13203 | if (substring == NULL) |
| 13204 | return NULL; |
| 13205 | result = tailmatch(self, substring, start, end, -1); |
| 13206 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13207 | if (result == -1) |
| 13208 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13209 | if (result) { |
| 13210 | Py_RETURN_TRUE; |
| 13211 | } |
| 13212 | } |
| 13213 | /* nothing matched */ |
| 13214 | Py_RETURN_FALSE; |
| 13215 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13216 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13217 | if (substring == NULL) { |
| 13218 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13219 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13220 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13221 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13222 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13223 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13224 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13225 | if (result == -1) |
| 13226 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13227 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13228 | } |
| 13229 | |
| 13230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13231 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13232 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13233 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13234 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13235 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13236 | With optional end, stop comparing S at that position.\n\ |
| 13237 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13238 | |
| 13239 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13240 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13241 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13242 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13243 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13244 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13245 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13246 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13247 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13248 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13249 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13250 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13251 | if (PyTuple_Check(subobj)) { |
| 13252 | Py_ssize_t i; |
| 13253 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13254 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13255 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13256 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13257 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13258 | result = tailmatch(self, substring, start, end, +1); |
| 13259 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13260 | if (result == -1) |
| 13261 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13262 | if (result) { |
| 13263 | Py_RETURN_TRUE; |
| 13264 | } |
| 13265 | } |
| 13266 | Py_RETURN_FALSE; |
| 13267 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13268 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13269 | if (substring == NULL) { |
| 13270 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13271 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13272 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13273 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13274 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13275 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13276 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13277 | if (result == -1) |
| 13278 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13279 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13280 | } |
| 13281 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13282 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13283 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13284 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13285 | if (!writer->readonly) |
| 13286 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 13287 | else { |
| 13288 | /* Copy-on-write mode: set buffer size to 0 so |
| 13289 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13290 | * next write. */ |
| 13291 | writer->size = 0; |
| 13292 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13293 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13294 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13295 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13296 | } |
| 13297 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13298 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13299 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13300 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13301 | memset(writer, 0, sizeof(*writer)); |
| 13302 | #ifdef Py_DEBUG |
| 13303 | writer->kind = 5; /* invalid kind */ |
| 13304 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13305 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13306 | } |
| 13307 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13308 | int |
| 13309 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13310 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13311 | { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13312 | #ifdef MS_WINDOWS |
| 13313 | /* On Windows, overallocate by 50% is the best factor */ |
| 13314 | # define OVERALLOCATE_FACTOR 2 |
| 13315 | #else |
| 13316 | /* On Linux, overallocate by 25% is the best factor */ |
| 13317 | # define OVERALLOCATE_FACTOR 4 |
| 13318 | #endif |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13319 | Py_ssize_t newlen; |
| 13320 | PyObject *newbuffer; |
| 13321 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13322 | assert(length > 0); |
| 13323 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13324 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13325 | PyErr_NoMemory(); |
| 13326 | return -1; |
| 13327 | } |
| 13328 | newlen = writer->pos + length; |
| 13329 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13330 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13331 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13332 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13333 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13334 | if (writer->overallocate |
| 13335 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13336 | /* overallocate to limit the number of realloc() */ |
| 13337 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13338 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13339 | if (newlen < writer->min_length) |
| 13340 | newlen = writer->min_length; |
| 13341 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13342 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13343 | if (writer->buffer == NULL) |
| 13344 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13345 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13346 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13347 | if (writer->overallocate |
| 13348 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13349 | /* overallocate to limit the number of realloc() */ |
| 13350 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13351 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13352 | if (newlen < writer->min_length) |
| 13353 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13354 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13355 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13356 | /* resize + widen */ |
| 13357 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13358 | if (newbuffer == NULL) |
| 13359 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13360 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13361 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13362 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13363 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13364 | } |
| 13365 | else { |
| 13366 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13367 | if (newbuffer == NULL) |
| 13368 | return -1; |
| 13369 | } |
| 13370 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13371 | } |
| 13372 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13373 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13374 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13375 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13376 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13377 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13378 | writer->buffer, 0, writer->pos); |
| 13379 | Py_DECREF(writer->buffer); |
| 13380 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13381 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13382 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13383 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13384 | |
| 13385 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13386 | } |
| 13387 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13388 | Py_LOCAL_INLINE(int) |
| 13389 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13390 | { |
| 13391 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13392 | return -1; |
| 13393 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13394 | writer->pos++; |
| 13395 | return 0; |
| 13396 | } |
| 13397 | |
| 13398 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13399 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13400 | { |
| 13401 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13402 | } |
| 13403 | |
| 13404 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13405 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13406 | { |
| 13407 | Py_UCS4 maxchar; |
| 13408 | Py_ssize_t len; |
| 13409 | |
| 13410 | if (PyUnicode_READY(str) == -1) |
| 13411 | return -1; |
| 13412 | len = PyUnicode_GET_LENGTH(str); |
| 13413 | if (len == 0) |
| 13414 | return 0; |
| 13415 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13416 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13417 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 1912b39 | 2015-03-26 09:37:23 +0100 | [diff] [blame] | 13418 | assert(_PyUnicode_CheckConsistency(str, 1)); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13419 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13420 | Py_INCREF(str); |
| 13421 | writer->buffer = str; |
| 13422 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13423 | writer->pos += len; |
| 13424 | return 0; |
| 13425 | } |
| 13426 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13427 | return -1; |
| 13428 | } |
| 13429 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13430 | str, 0, len); |
| 13431 | writer->pos += len; |
| 13432 | return 0; |
| 13433 | } |
| 13434 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13435 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13436 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13437 | Py_ssize_t start, Py_ssize_t end) |
| 13438 | { |
| 13439 | Py_UCS4 maxchar; |
| 13440 | Py_ssize_t len; |
| 13441 | |
| 13442 | if (PyUnicode_READY(str) == -1) |
| 13443 | return -1; |
| 13444 | |
| 13445 | assert(0 <= start); |
| 13446 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13447 | assert(start <= end); |
| 13448 | |
| 13449 | if (end == 0) |
| 13450 | return 0; |
| 13451 | |
| 13452 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13453 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13454 | |
| 13455 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13456 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13457 | else |
| 13458 | maxchar = writer->maxchar; |
| 13459 | len = end - start; |
| 13460 | |
| 13461 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13462 | return -1; |
| 13463 | |
| 13464 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13465 | str, start, len); |
| 13466 | writer->pos += len; |
| 13467 | return 0; |
| 13468 | } |
| 13469 | |
| 13470 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13471 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13472 | const char *ascii, Py_ssize_t len) |
| 13473 | { |
| 13474 | if (len == -1) |
| 13475 | len = strlen(ascii); |
| 13476 | |
| 13477 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13478 | |
| 13479 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13480 | PyObject *str; |
| 13481 | |
| 13482 | str = _PyUnicode_FromASCII(ascii, len); |
| 13483 | if (str == NULL) |
| 13484 | return -1; |
| 13485 | |
| 13486 | writer->readonly = 1; |
| 13487 | writer->buffer = str; |
| 13488 | _PyUnicodeWriter_Update(writer); |
| 13489 | writer->pos += len; |
| 13490 | return 0; |
| 13491 | } |
| 13492 | |
| 13493 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13494 | return -1; |
| 13495 | |
| 13496 | switch (writer->kind) |
| 13497 | { |
| 13498 | case PyUnicode_1BYTE_KIND: |
| 13499 | { |
| 13500 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13501 | Py_UCS1 *data = writer->data; |
| 13502 | |
| 13503 | Py_MEMCPY(data + writer->pos, str, len); |
| 13504 | break; |
| 13505 | } |
| 13506 | case PyUnicode_2BYTE_KIND: |
| 13507 | { |
| 13508 | _PyUnicode_CONVERT_BYTES( |
| 13509 | Py_UCS1, Py_UCS2, |
| 13510 | ascii, ascii + len, |
| 13511 | (Py_UCS2 *)writer->data + writer->pos); |
| 13512 | break; |
| 13513 | } |
| 13514 | case PyUnicode_4BYTE_KIND: |
| 13515 | { |
| 13516 | _PyUnicode_CONVERT_BYTES( |
| 13517 | Py_UCS1, Py_UCS4, |
| 13518 | ascii, ascii + len, |
| 13519 | (Py_UCS4 *)writer->data + writer->pos); |
| 13520 | break; |
| 13521 | } |
| 13522 | default: |
| 13523 | assert(0); |
| 13524 | } |
| 13525 | |
| 13526 | writer->pos += len; |
| 13527 | return 0; |
| 13528 | } |
| 13529 | |
| 13530 | int |
| 13531 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13532 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13533 | { |
| 13534 | Py_UCS4 maxchar; |
| 13535 | |
| 13536 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13537 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13538 | return -1; |
| 13539 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13540 | writer->pos += len; |
| 13541 | return 0; |
| 13542 | } |
| 13543 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13544 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13545 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13546 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13547 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13548 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13549 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13550 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13551 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13552 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13553 | str = writer->buffer; |
| 13554 | writer->buffer = NULL; |
| 13555 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13556 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13557 | } |
| 13558 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13559 | PyObject *newbuffer; |
| 13560 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13561 | if (newbuffer == NULL) { |
Serhiy Storchaka | dfe98a1 | 2014-02-09 13:46:20 +0200 | [diff] [blame] | 13562 | Py_CLEAR(writer->buffer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13563 | return NULL; |
| 13564 | } |
| 13565 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13566 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13567 | str = writer->buffer; |
| 13568 | writer->buffer = NULL; |
| 13569 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13570 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13571 | } |
| 13572 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13573 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13574 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13575 | { |
| 13576 | Py_CLEAR(writer->buffer); |
| 13577 | } |
| 13578 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13579 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13580 | |
| 13581 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13582 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13583 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13584 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13585 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13586 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13587 | PyDoc_STRVAR(format_map__doc__, |
| 13588 | "S.format_map(mapping) -> str\n\ |
| 13589 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13590 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13591 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13592 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13593 | static PyObject * |
| 13594 | unicode__format__(PyObject* self, PyObject* args) |
| 13595 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13596 | PyObject *format_spec; |
| 13597 | _PyUnicodeWriter writer; |
| 13598 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13599 | |
| 13600 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13601 | return NULL; |
| 13602 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13603 | if (PyUnicode_READY(self) == -1) |
| 13604 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13605 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13606 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13607 | self, format_spec, 0, |
| 13608 | PyUnicode_GET_LENGTH(format_spec)); |
| 13609 | if (ret == -1) { |
| 13610 | _PyUnicodeWriter_Dealloc(&writer); |
| 13611 | return NULL; |
| 13612 | } |
| 13613 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13614 | } |
| 13615 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13616 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13617 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13618 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13619 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13620 | |
| 13621 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13622 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13623 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13624 | Py_ssize_t size; |
| 13625 | |
| 13626 | /* If it's a compact object, account for base structure + |
| 13627 | character data. */ |
| 13628 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13629 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13630 | else if (PyUnicode_IS_COMPACT(v)) |
| 13631 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13632 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13633 | else { |
| 13634 | /* If it is a two-block object, account for base object, and |
| 13635 | for character block if present. */ |
| 13636 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13637 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13638 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13639 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13640 | } |
| 13641 | /* 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] | 13642 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13643 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13644 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13645 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13646 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13647 | |
| 13648 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13649 | } |
| 13650 | |
| 13651 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13652 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13653 | |
| 13654 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13655 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13656 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13657 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13658 | if (!copy) |
| 13659 | return NULL; |
| 13660 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13661 | } |
| 13662 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13663 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13664 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13665 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13666 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13667 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13668 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13669 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13670 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13671 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13672 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13673 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13674 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13675 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13676 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13677 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13678 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13679 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13680 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13681 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13682 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13683 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13684 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13685 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13686 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13687 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13688 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13689 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13690 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13691 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13692 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13693 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13694 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13695 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13696 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13697 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13698 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13699 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13700 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13701 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13702 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13703 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13704 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13705 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13706 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13707 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13708 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13709 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13710 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13711 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13712 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13713 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13714 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13715 | #endif |
| 13716 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13717 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13718 | {NULL, NULL} |
| 13719 | }; |
| 13720 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13721 | static PyObject * |
| 13722 | unicode_mod(PyObject *v, PyObject *w) |
| 13723 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13724 | if (!PyUnicode_Check(v)) |
| 13725 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13726 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13727 | } |
| 13728 | |
| 13729 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13730 | 0, /*nb_add*/ |
| 13731 | 0, /*nb_subtract*/ |
| 13732 | 0, /*nb_multiply*/ |
| 13733 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13734 | }; |
| 13735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13736 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13737 | (lenfunc) unicode_length, /* sq_length */ |
| 13738 | PyUnicode_Concat, /* sq_concat */ |
| 13739 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13740 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13741 | 0, /* sq_slice */ |
| 13742 | 0, /* sq_ass_item */ |
| 13743 | 0, /* sq_ass_slice */ |
| 13744 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13745 | }; |
| 13746 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13747 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13748 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13749 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13750 | if (PyUnicode_READY(self) == -1) |
| 13751 | return NULL; |
| 13752 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13753 | if (PyIndex_Check(item)) { |
| 13754 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13755 | if (i == -1 && PyErr_Occurred()) |
| 13756 | return NULL; |
| 13757 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13758 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13759 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13760 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13761 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13762 | PyObject *result; |
| 13763 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13764 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13765 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13766 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13767 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13768 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13769 | return NULL; |
| 13770 | } |
| 13771 | |
| 13772 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13773 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13774 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13775 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13776 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13777 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13778 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13779 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13780 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13781 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13782 | src_kind = PyUnicode_KIND(self); |
| 13783 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13784 | if (!PyUnicode_IS_ASCII(self)) { |
| 13785 | kind_limit = kind_maxchar_limit(src_kind); |
| 13786 | max_char = 0; |
| 13787 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13788 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13789 | if (ch > max_char) { |
| 13790 | max_char = ch; |
| 13791 | if (max_char >= kind_limit) |
| 13792 | break; |
| 13793 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13794 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13795 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13796 | else |
| 13797 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13798 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13799 | if (result == NULL) |
| 13800 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13801 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13802 | dest_data = PyUnicode_DATA(result); |
| 13803 | |
| 13804 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13805 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13806 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13807 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13808 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13809 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13810 | } else { |
| 13811 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13812 | return NULL; |
| 13813 | } |
| 13814 | } |
| 13815 | |
| 13816 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13817 | (lenfunc)unicode_length, /* mp_length */ |
| 13818 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13819 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13820 | }; |
| 13821 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13822 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13823 | /* Helpers for PyUnicode_Format() */ |
| 13824 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13825 | struct unicode_formatter_t { |
| 13826 | PyObject *args; |
| 13827 | int args_owned; |
| 13828 | Py_ssize_t arglen, argidx; |
| 13829 | PyObject *dict; |
| 13830 | |
| 13831 | enum PyUnicode_Kind fmtkind; |
| 13832 | Py_ssize_t fmtcnt, fmtpos; |
| 13833 | void *fmtdata; |
| 13834 | PyObject *fmtstr; |
| 13835 | |
| 13836 | _PyUnicodeWriter writer; |
| 13837 | }; |
| 13838 | |
| 13839 | struct unicode_format_arg_t { |
| 13840 | Py_UCS4 ch; |
| 13841 | int flags; |
| 13842 | Py_ssize_t width; |
| 13843 | int prec; |
| 13844 | int sign; |
| 13845 | }; |
| 13846 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13847 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13848 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13849 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13850 | Py_ssize_t argidx = ctx->argidx; |
| 13851 | |
| 13852 | if (argidx < ctx->arglen) { |
| 13853 | ctx->argidx++; |
| 13854 | if (ctx->arglen < 0) |
| 13855 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13856 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13857 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13858 | } |
| 13859 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13860 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13861 | return NULL; |
| 13862 | } |
| 13863 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13864 | /* 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] | 13865 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13866 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13867 | otherwise. |
| 13868 | |
| 13869 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13870 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13871 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13872 | PyObject **p_output, |
| 13873 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13874 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13875 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13876 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13877 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13878 | int prec; |
| 13879 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13880 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13881 | x = PyFloat_AsDouble(v); |
| 13882 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13883 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13884 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13885 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13886 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13887 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13888 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13889 | if (arg->flags & F_ALT) |
| 13890 | dtoa_flags = Py_DTSF_ALT; |
| 13891 | else |
| 13892 | dtoa_flags = 0; |
| 13893 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13894 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13895 | return -1; |
| 13896 | len = strlen(p); |
| 13897 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13898 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13899 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13900 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13901 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13902 | } |
| 13903 | else |
| 13904 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13905 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13906 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13907 | } |
| 13908 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13909 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13910 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13911 | * Python's regular ints. |
| 13912 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13913 | * The output string is of the form |
| 13914 | * "-"? ("0x" | "0X")? digit+ |
| 13915 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13916 | * set in flags. The case of hex digits will be correct, |
| 13917 | * There will be at least prec digits, zero-filled on the left if |
| 13918 | * necessary to get that many. |
| 13919 | * val object to be converted |
| 13920 | * flags bitmask of format flags; only F_ALT is looked at |
| 13921 | * prec minimum number of digits; 0-fill on left if needed |
| 13922 | * type a character in [duoxX]; u acts the same as d |
| 13923 | * |
| 13924 | * CAUTION: o, x and X conversions on regular ints can never |
| 13925 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13926 | */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13927 | PyObject * |
| 13928 | _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13929 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13930 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13931 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13932 | Py_ssize_t i; |
| 13933 | int sign; /* 1 if '-', else 0 */ |
| 13934 | int len; /* number of characters */ |
| 13935 | Py_ssize_t llen; |
| 13936 | int numdigits; /* len == numnondigits + numdigits */ |
| 13937 | int numnondigits = 0; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13938 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13939 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13940 | if (prec > INT_MAX-3) { |
| 13941 | PyErr_SetString(PyExc_OverflowError, |
| 13942 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13943 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13944 | } |
| 13945 | |
| 13946 | assert(PyLong_Check(val)); |
| 13947 | |
| 13948 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13949 | default: |
| 13950 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13951 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13952 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13953 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13954 | /* int and int subclasses should print numerically when a numeric */ |
| 13955 | /* format code is used (see issue18780) */ |
| 13956 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13957 | break; |
| 13958 | case 'o': |
| 13959 | numnondigits = 2; |
| 13960 | result = PyNumber_ToBase(val, 8); |
| 13961 | break; |
| 13962 | case 'x': |
| 13963 | case 'X': |
| 13964 | numnondigits = 2; |
| 13965 | result = PyNumber_ToBase(val, 16); |
| 13966 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13967 | } |
| 13968 | if (!result) |
| 13969 | return NULL; |
| 13970 | |
| 13971 | assert(unicode_modifiable(result)); |
| 13972 | assert(PyUnicode_IS_READY(result)); |
| 13973 | assert(PyUnicode_IS_ASCII(result)); |
| 13974 | |
| 13975 | /* To modify the string in-place, there can only be one reference. */ |
| 13976 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13977 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13978 | PyErr_BadInternalCall(); |
| 13979 | return NULL; |
| 13980 | } |
| 13981 | buf = PyUnicode_DATA(result); |
| 13982 | llen = PyUnicode_GET_LENGTH(result); |
| 13983 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13984 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13985 | PyErr_SetString(PyExc_ValueError, |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13986 | "string too large in _PyUnicode_FormatLong"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13987 | return NULL; |
| 13988 | } |
| 13989 | len = (int)llen; |
| 13990 | sign = buf[0] == '-'; |
| 13991 | numnondigits += sign; |
| 13992 | numdigits = len - numnondigits; |
| 13993 | assert(numdigits > 0); |
| 13994 | |
| 13995 | /* Get rid of base marker unless F_ALT */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 13996 | if (((alt) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13997 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13998 | assert(buf[sign] == '0'); |
| 13999 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 14000 | buf[sign+1] == 'o'); |
| 14001 | numnondigits -= 2; |
| 14002 | buf += 2; |
| 14003 | len -= 2; |
| 14004 | if (sign) |
| 14005 | buf[0] = '-'; |
| 14006 | assert(len == numnondigits + numdigits); |
| 14007 | assert(numdigits > 0); |
| 14008 | } |
| 14009 | |
| 14010 | /* Fill with leading zeroes to meet minimum width. */ |
| 14011 | if (prec > numdigits) { |
| 14012 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 14013 | numnondigits + prec); |
| 14014 | char *b1; |
| 14015 | if (!r1) { |
| 14016 | Py_DECREF(result); |
| 14017 | return NULL; |
| 14018 | } |
| 14019 | b1 = PyBytes_AS_STRING(r1); |
| 14020 | for (i = 0; i < numnondigits; ++i) |
| 14021 | *b1++ = *buf++; |
| 14022 | for (i = 0; i < prec - numdigits; i++) |
| 14023 | *b1++ = '0'; |
| 14024 | for (i = 0; i < numdigits; i++) |
| 14025 | *b1++ = *buf++; |
| 14026 | *b1 = '\0'; |
| 14027 | Py_DECREF(result); |
| 14028 | result = r1; |
| 14029 | buf = PyBytes_AS_STRING(result); |
| 14030 | len = numnondigits + prec; |
| 14031 | } |
| 14032 | |
| 14033 | /* Fix up case for hex conversions. */ |
| 14034 | if (type == 'X') { |
| 14035 | /* Need to convert all lower case letters to upper case. |
| 14036 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 14037 | for (i = 0; i < len; i++) |
| 14038 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 14039 | buf[i] -= 'a'-'A'; |
| 14040 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14041 | if (!PyUnicode_Check(result) |
| 14042 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14043 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14044 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14045 | Py_DECREF(result); |
| 14046 | result = unicode; |
| 14047 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14048 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 14049 | if (PyUnicode_Resize(&result, len) < 0) |
| 14050 | Py_CLEAR(result); |
| 14051 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14052 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14053 | } |
| 14054 | |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14055 | /* Format an integer or a float as an integer. |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14056 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14057 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14058 | * -1 and raise an exception on error */ |
| 14059 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14060 | mainformatlong(PyObject *v, |
| 14061 | struct unicode_format_arg_t *arg, |
| 14062 | PyObject **p_output, |
| 14063 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14064 | { |
| 14065 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14066 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14067 | |
| 14068 | if (!PyNumber_Check(v)) |
| 14069 | goto wrongtype; |
| 14070 | |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14071 | /* 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] | 14072 | if (!PyLong_Check(v)) { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14073 | if (type == 'o' || type == 'x' || type == 'X') { |
| 14074 | iobj = PyNumber_Index(v); |
| 14075 | if (iobj == NULL) { |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14076 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14077 | goto wrongtype; |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14078 | return -1; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14079 | } |
| 14080 | } |
| 14081 | else { |
| 14082 | iobj = PyNumber_Long(v); |
| 14083 | if (iobj == NULL ) { |
| 14084 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14085 | goto wrongtype; |
| 14086 | return -1; |
| 14087 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14088 | } |
| 14089 | assert(PyLong_Check(iobj)); |
| 14090 | } |
| 14091 | else { |
| 14092 | iobj = v; |
| 14093 | Py_INCREF(iobj); |
| 14094 | } |
| 14095 | |
| 14096 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14097 | && arg->width == -1 && arg->prec == -1 |
| 14098 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14099 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14100 | { |
| 14101 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14102 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14103 | int base; |
| 14104 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14105 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14106 | { |
| 14107 | default: |
| 14108 | assert(0 && "'type' not in [diuoxX]"); |
| 14109 | case 'd': |
| 14110 | case 'i': |
| 14111 | case 'u': |
| 14112 | base = 10; |
| 14113 | break; |
| 14114 | case 'o': |
| 14115 | base = 8; |
| 14116 | break; |
| 14117 | case 'x': |
| 14118 | case 'X': |
| 14119 | base = 16; |
| 14120 | break; |
| 14121 | } |
| 14122 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14123 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14124 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14125 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14126 | } |
| 14127 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14128 | return 1; |
| 14129 | } |
| 14130 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 14131 | res = _PyUnicode_FormatLong(iobj, arg->flags & F_ALT, arg->prec, type); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14132 | Py_DECREF(iobj); |
| 14133 | if (res == NULL) |
| 14134 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14135 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14136 | return 0; |
| 14137 | |
| 14138 | wrongtype: |
Ethan Furman | 9ab7480 | 2014-03-21 06:38:46 -0700 | [diff] [blame] | 14139 | switch(type) |
| 14140 | { |
| 14141 | case 'o': |
| 14142 | case 'x': |
| 14143 | case 'X': |
| 14144 | PyErr_Format(PyExc_TypeError, |
| 14145 | "%%%c format: an integer is required, " |
| 14146 | "not %.200s", |
| 14147 | type, Py_TYPE(v)->tp_name); |
| 14148 | break; |
| 14149 | default: |
| 14150 | PyErr_Format(PyExc_TypeError, |
| 14151 | "%%%c format: a number is required, " |
| 14152 | "not %.200s", |
| 14153 | type, Py_TYPE(v)->tp_name); |
| 14154 | break; |
| 14155 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14156 | return -1; |
| 14157 | } |
| 14158 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14159 | static Py_UCS4 |
| 14160 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14161 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14162 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14163 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14164 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14165 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14166 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14167 | goto onError; |
| 14168 | } |
| 14169 | else { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14170 | PyObject *iobj; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14171 | long x; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14172 | /* make sure number is a type of integer */ |
| 14173 | if (!PyLong_Check(v)) { |
| 14174 | iobj = PyNumber_Index(v); |
| 14175 | if (iobj == NULL) { |
Ethan Furman | 38d872e | 2014-03-19 08:38:52 -0700 | [diff] [blame] | 14176 | goto onError; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14177 | } |
| 14178 | v = iobj; |
| 14179 | Py_DECREF(iobj); |
| 14180 | } |
| 14181 | /* Integer input truncated to a character */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14182 | x = PyLong_AsLong(v); |
| 14183 | if (x == -1 && PyErr_Occurred()) |
| 14184 | goto onError; |
| 14185 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14186 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14187 | PyErr_SetString(PyExc_OverflowError, |
| 14188 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14189 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14190 | } |
| 14191 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14192 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14193 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14194 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14195 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14196 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14197 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14198 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14199 | } |
| 14200 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14201 | /* Parse options of an argument: flags, width, precision. |
| 14202 | Handle also "%(name)" syntax. |
| 14203 | |
| 14204 | Return 0 if the argument has been formatted into arg->str. |
| 14205 | Return 1 if the argument has been written into ctx->writer, |
| 14206 | Raise an exception and return -1 on error. */ |
| 14207 | static int |
| 14208 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14209 | struct unicode_format_arg_t *arg) |
| 14210 | { |
| 14211 | #define FORMAT_READ(ctx) \ |
| 14212 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14213 | |
| 14214 | PyObject *v; |
| 14215 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14216 | if (arg->ch == '(') { |
| 14217 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14218 | Py_ssize_t keystart; |
| 14219 | Py_ssize_t keylen; |
| 14220 | PyObject *key; |
| 14221 | int pcount = 1; |
| 14222 | |
| 14223 | if (ctx->dict == NULL) { |
| 14224 | PyErr_SetString(PyExc_TypeError, |
| 14225 | "format requires a mapping"); |
| 14226 | return -1; |
| 14227 | } |
| 14228 | ++ctx->fmtpos; |
| 14229 | --ctx->fmtcnt; |
| 14230 | keystart = ctx->fmtpos; |
| 14231 | /* Skip over balanced parentheses */ |
| 14232 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14233 | arg->ch = FORMAT_READ(ctx); |
| 14234 | if (arg->ch == ')') |
| 14235 | --pcount; |
| 14236 | else if (arg->ch == '(') |
| 14237 | ++pcount; |
| 14238 | ctx->fmtpos++; |
| 14239 | } |
| 14240 | keylen = ctx->fmtpos - keystart - 1; |
| 14241 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14242 | PyErr_SetString(PyExc_ValueError, |
| 14243 | "incomplete format key"); |
| 14244 | return -1; |
| 14245 | } |
| 14246 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14247 | keystart, keystart + keylen); |
| 14248 | if (key == NULL) |
| 14249 | return -1; |
| 14250 | if (ctx->args_owned) { |
| 14251 | Py_DECREF(ctx->args); |
| 14252 | ctx->args_owned = 0; |
| 14253 | } |
| 14254 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14255 | Py_DECREF(key); |
| 14256 | if (ctx->args == NULL) |
| 14257 | return -1; |
| 14258 | ctx->args_owned = 1; |
| 14259 | ctx->arglen = -1; |
| 14260 | ctx->argidx = -2; |
| 14261 | } |
| 14262 | |
| 14263 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14264 | while (--ctx->fmtcnt >= 0) { |
| 14265 | arg->ch = FORMAT_READ(ctx); |
| 14266 | ctx->fmtpos++; |
| 14267 | switch (arg->ch) { |
| 14268 | case '-': arg->flags |= F_LJUST; continue; |
| 14269 | case '+': arg->flags |= F_SIGN; continue; |
| 14270 | case ' ': arg->flags |= F_BLANK; continue; |
| 14271 | case '#': arg->flags |= F_ALT; continue; |
| 14272 | case '0': arg->flags |= F_ZERO; continue; |
| 14273 | } |
| 14274 | break; |
| 14275 | } |
| 14276 | |
| 14277 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14278 | if (arg->ch == '*') { |
| 14279 | v = unicode_format_getnextarg(ctx); |
| 14280 | if (v == NULL) |
| 14281 | return -1; |
| 14282 | if (!PyLong_Check(v)) { |
| 14283 | PyErr_SetString(PyExc_TypeError, |
| 14284 | "* wants int"); |
| 14285 | return -1; |
| 14286 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14287 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14288 | if (arg->width == -1 && PyErr_Occurred()) |
| 14289 | return -1; |
| 14290 | if (arg->width < 0) { |
| 14291 | arg->flags |= F_LJUST; |
| 14292 | arg->width = -arg->width; |
| 14293 | } |
| 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->width = 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 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14307 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14308 | '0' and '9', casting to int is safe. */ |
| 14309 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14310 | PyErr_SetString(PyExc_ValueError, |
| 14311 | "width too big"); |
| 14312 | return -1; |
| 14313 | } |
| 14314 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14315 | } |
| 14316 | } |
| 14317 | |
| 14318 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14319 | if (arg->ch == '.') { |
| 14320 | arg->prec = 0; |
| 14321 | if (--ctx->fmtcnt >= 0) { |
| 14322 | arg->ch = FORMAT_READ(ctx); |
| 14323 | ctx->fmtpos++; |
| 14324 | } |
| 14325 | if (arg->ch == '*') { |
| 14326 | v = unicode_format_getnextarg(ctx); |
| 14327 | if (v == NULL) |
| 14328 | return -1; |
| 14329 | if (!PyLong_Check(v)) { |
| 14330 | PyErr_SetString(PyExc_TypeError, |
| 14331 | "* wants int"); |
| 14332 | return -1; |
| 14333 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14334 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14335 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14336 | return -1; |
| 14337 | if (arg->prec < 0) |
| 14338 | arg->prec = 0; |
| 14339 | if (--ctx->fmtcnt >= 0) { |
| 14340 | arg->ch = FORMAT_READ(ctx); |
| 14341 | ctx->fmtpos++; |
| 14342 | } |
| 14343 | } |
| 14344 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14345 | arg->prec = arg->ch - '0'; |
| 14346 | while (--ctx->fmtcnt >= 0) { |
| 14347 | arg->ch = FORMAT_READ(ctx); |
| 14348 | ctx->fmtpos++; |
| 14349 | if (arg->ch < '0' || arg->ch > '9') |
| 14350 | break; |
| 14351 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14352 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14353 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14354 | return -1; |
| 14355 | } |
| 14356 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14357 | } |
| 14358 | } |
| 14359 | } |
| 14360 | |
| 14361 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14362 | if (ctx->fmtcnt >= 0) { |
| 14363 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14364 | if (--ctx->fmtcnt >= 0) { |
| 14365 | arg->ch = FORMAT_READ(ctx); |
| 14366 | ctx->fmtpos++; |
| 14367 | } |
| 14368 | } |
| 14369 | } |
| 14370 | if (ctx->fmtcnt < 0) { |
| 14371 | PyErr_SetString(PyExc_ValueError, |
| 14372 | "incomplete format"); |
| 14373 | return -1; |
| 14374 | } |
| 14375 | return 0; |
| 14376 | |
| 14377 | #undef FORMAT_READ |
| 14378 | } |
| 14379 | |
| 14380 | /* Format one argument. Supported conversion specifiers: |
| 14381 | |
| 14382 | - "s", "r", "a": any type |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14383 | - "i", "d", "u": int or float |
| 14384 | - "o", "x", "X": int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14385 | - "e", "E", "f", "F", "g", "G": float |
| 14386 | - "c": int or str (1 character) |
| 14387 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14388 | When possible, the output is written directly into the Unicode writer |
| 14389 | (ctx->writer). A string is created when padding is required. |
| 14390 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14391 | Return 0 if the argument has been formatted into *p_str, |
| 14392 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14393 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14394 | static int |
| 14395 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14396 | struct unicode_format_arg_t *arg, |
| 14397 | PyObject **p_str) |
| 14398 | { |
| 14399 | PyObject *v; |
| 14400 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14401 | |
| 14402 | if (ctx->fmtcnt == 0) |
| 14403 | ctx->writer.overallocate = 0; |
| 14404 | |
| 14405 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14406 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14407 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14408 | return 1; |
| 14409 | } |
| 14410 | |
| 14411 | v = unicode_format_getnextarg(ctx); |
| 14412 | if (v == NULL) |
| 14413 | return -1; |
| 14414 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14415 | |
| 14416 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14417 | case 's': |
| 14418 | case 'r': |
| 14419 | case 'a': |
| 14420 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14421 | /* Fast path */ |
| 14422 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14423 | return -1; |
| 14424 | return 1; |
| 14425 | } |
| 14426 | |
| 14427 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14428 | *p_str = v; |
| 14429 | Py_INCREF(*p_str); |
| 14430 | } |
| 14431 | else { |
| 14432 | if (arg->ch == 's') |
| 14433 | *p_str = PyObject_Str(v); |
| 14434 | else if (arg->ch == 'r') |
| 14435 | *p_str = PyObject_Repr(v); |
| 14436 | else |
| 14437 | *p_str = PyObject_ASCII(v); |
| 14438 | } |
| 14439 | break; |
| 14440 | |
| 14441 | case 'i': |
| 14442 | case 'd': |
| 14443 | case 'u': |
| 14444 | case 'o': |
| 14445 | case 'x': |
| 14446 | case 'X': |
| 14447 | { |
| 14448 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14449 | if (ret != 0) |
| 14450 | return ret; |
| 14451 | arg->sign = 1; |
| 14452 | break; |
| 14453 | } |
| 14454 | |
| 14455 | case 'e': |
| 14456 | case 'E': |
| 14457 | case 'f': |
| 14458 | case 'F': |
| 14459 | case 'g': |
| 14460 | case 'G': |
| 14461 | if (arg->width == -1 && arg->prec == -1 |
| 14462 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14463 | { |
| 14464 | /* Fast path */ |
| 14465 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14466 | return -1; |
| 14467 | return 1; |
| 14468 | } |
| 14469 | |
| 14470 | arg->sign = 1; |
| 14471 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14472 | return -1; |
| 14473 | break; |
| 14474 | |
| 14475 | case 'c': |
| 14476 | { |
| 14477 | Py_UCS4 ch = formatchar(v); |
| 14478 | if (ch == (Py_UCS4) -1) |
| 14479 | return -1; |
| 14480 | if (arg->width == -1 && arg->prec == -1) { |
| 14481 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14482 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14483 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14484 | return 1; |
| 14485 | } |
| 14486 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14487 | break; |
| 14488 | } |
| 14489 | |
| 14490 | default: |
| 14491 | PyErr_Format(PyExc_ValueError, |
| 14492 | "unsupported format character '%c' (0x%x) " |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 14493 | "at index %zd", |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14494 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14495 | (int)arg->ch, |
| 14496 | ctx->fmtpos - 1); |
| 14497 | return -1; |
| 14498 | } |
| 14499 | if (*p_str == NULL) |
| 14500 | return -1; |
| 14501 | assert (PyUnicode_Check(*p_str)); |
| 14502 | return 0; |
| 14503 | } |
| 14504 | |
| 14505 | static int |
| 14506 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14507 | struct unicode_format_arg_t *arg, |
| 14508 | PyObject *str) |
| 14509 | { |
| 14510 | Py_ssize_t len; |
| 14511 | enum PyUnicode_Kind kind; |
| 14512 | void *pbuf; |
| 14513 | Py_ssize_t pindex; |
| 14514 | Py_UCS4 signchar; |
| 14515 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14516 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14517 | Py_ssize_t sublen; |
| 14518 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14519 | Py_UCS4 fill; |
| 14520 | |
| 14521 | fill = ' '; |
| 14522 | if (arg->sign && arg->flags & F_ZERO) |
| 14523 | fill = '0'; |
| 14524 | |
| 14525 | if (PyUnicode_READY(str) == -1) |
| 14526 | return -1; |
| 14527 | |
| 14528 | len = PyUnicode_GET_LENGTH(str); |
| 14529 | if ((arg->width == -1 || arg->width <= len) |
| 14530 | && (arg->prec == -1 || arg->prec >= len) |
| 14531 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14532 | { |
| 14533 | /* Fast path */ |
| 14534 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14535 | return -1; |
| 14536 | return 0; |
| 14537 | } |
| 14538 | |
| 14539 | /* Truncate the string for "s", "r" and "a" formats |
| 14540 | if the precision is set */ |
| 14541 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14542 | if (arg->prec >= 0 && len > arg->prec) |
| 14543 | len = arg->prec; |
| 14544 | } |
| 14545 | |
| 14546 | /* Adjust sign and width */ |
| 14547 | kind = PyUnicode_KIND(str); |
| 14548 | pbuf = PyUnicode_DATA(str); |
| 14549 | pindex = 0; |
| 14550 | signchar = '\0'; |
| 14551 | if (arg->sign) { |
| 14552 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14553 | if (ch == '-' || ch == '+') { |
| 14554 | signchar = ch; |
| 14555 | len--; |
| 14556 | pindex++; |
| 14557 | } |
| 14558 | else if (arg->flags & F_SIGN) |
| 14559 | signchar = '+'; |
| 14560 | else if (arg->flags & F_BLANK) |
| 14561 | signchar = ' '; |
| 14562 | else |
| 14563 | arg->sign = 0; |
| 14564 | } |
| 14565 | if (arg->width < len) |
| 14566 | arg->width = len; |
| 14567 | |
| 14568 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14569 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14570 | if (!(arg->flags & F_LJUST)) { |
| 14571 | if (arg->sign) { |
| 14572 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14573 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14574 | } |
| 14575 | else { |
| 14576 | if (arg->width > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14577 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14578 | } |
| 14579 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14580 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14581 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14582 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14583 | } |
| 14584 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14585 | buflen = arg->width; |
| 14586 | if (arg->sign && len == arg->width) |
| 14587 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14588 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14589 | return -1; |
| 14590 | |
| 14591 | /* Write the sign if needed */ |
| 14592 | if (arg->sign) { |
| 14593 | if (fill != ' ') { |
| 14594 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14595 | writer->pos += 1; |
| 14596 | } |
| 14597 | if (arg->width > len) |
| 14598 | arg->width--; |
| 14599 | } |
| 14600 | |
| 14601 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14602 | if the alternate form is used. |
| 14603 | For example, write "0x" for the "%#x" format. */ |
| 14604 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14605 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14606 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14607 | if (fill != ' ') { |
| 14608 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14609 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14610 | writer->pos += 2; |
| 14611 | pindex += 2; |
| 14612 | } |
| 14613 | arg->width -= 2; |
| 14614 | if (arg->width < 0) |
| 14615 | arg->width = 0; |
| 14616 | len -= 2; |
| 14617 | } |
| 14618 | |
| 14619 | /* Pad left with the fill character if needed */ |
| 14620 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14621 | sublen = arg->width - len; |
| 14622 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14623 | writer->pos += sublen; |
| 14624 | arg->width = len; |
| 14625 | } |
| 14626 | |
| 14627 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14628 | the alternate form is used */ |
| 14629 | if (fill == ' ') { |
| 14630 | if (arg->sign) { |
| 14631 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14632 | writer->pos += 1; |
| 14633 | } |
| 14634 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14635 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14636 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14637 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14638 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14639 | writer->pos += 2; |
| 14640 | pindex += 2; |
| 14641 | } |
| 14642 | } |
| 14643 | |
| 14644 | /* Write characters */ |
| 14645 | if (len) { |
| 14646 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14647 | str, pindex, len); |
| 14648 | writer->pos += len; |
| 14649 | } |
| 14650 | |
| 14651 | /* Pad right with the fill character if needed */ |
| 14652 | if (arg->width > len) { |
| 14653 | sublen = arg->width - len; |
| 14654 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14655 | writer->pos += sublen; |
| 14656 | } |
| 14657 | return 0; |
| 14658 | } |
| 14659 | |
| 14660 | /* Helper of PyUnicode_Format(): format one arg. |
| 14661 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14662 | static int |
| 14663 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14664 | { |
| 14665 | struct unicode_format_arg_t arg; |
| 14666 | PyObject *str; |
| 14667 | int ret; |
| 14668 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14669 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14670 | arg.flags = 0; |
| 14671 | arg.width = -1; |
| 14672 | arg.prec = -1; |
| 14673 | arg.sign = 0; |
| 14674 | str = NULL; |
| 14675 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14676 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14677 | if (ret == -1) |
| 14678 | return -1; |
| 14679 | |
| 14680 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14681 | if (ret == -1) |
| 14682 | return -1; |
| 14683 | |
| 14684 | if (ret != 1) { |
| 14685 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14686 | Py_DECREF(str); |
| 14687 | if (ret == -1) |
| 14688 | return -1; |
| 14689 | } |
| 14690 | |
| 14691 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14692 | PyErr_SetString(PyExc_TypeError, |
| 14693 | "not all arguments converted during string formatting"); |
| 14694 | return -1; |
| 14695 | } |
| 14696 | return 0; |
| 14697 | } |
| 14698 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14699 | PyObject * |
| 14700 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14701 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14702 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14704 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14705 | PyErr_BadInternalCall(); |
| 14706 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14707 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14708 | |
| 14709 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14710 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14711 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14712 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14713 | Py_DECREF(ctx.fmtstr); |
| 14714 | return NULL; |
| 14715 | } |
| 14716 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14717 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14718 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14719 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14720 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14721 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14722 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14723 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14725 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14726 | ctx.arglen = PyTuple_Size(args); |
| 14727 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14728 | } |
| 14729 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14730 | ctx.arglen = -1; |
| 14731 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14732 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14733 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14734 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14735 | ctx.dict = args; |
| 14736 | else |
| 14737 | ctx.dict = NULL; |
| 14738 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14739 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14740 | while (--ctx.fmtcnt >= 0) { |
| 14741 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14742 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14743 | |
| 14744 | nonfmtpos = ctx.fmtpos++; |
| 14745 | while (ctx.fmtcnt >= 0 && |
| 14746 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14747 | ctx.fmtpos++; |
| 14748 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14749 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14750 | if (ctx.fmtcnt < 0) { |
| 14751 | ctx.fmtpos--; |
| 14752 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14753 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14754 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14755 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14756 | nonfmtpos, ctx.fmtpos) < 0) |
| 14757 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14758 | } |
| 14759 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14760 | ctx.fmtpos++; |
| 14761 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14762 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14763 | } |
| 14764 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14765 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14766 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14767 | PyErr_SetString(PyExc_TypeError, |
| 14768 | "not all arguments converted during string formatting"); |
| 14769 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14770 | } |
| 14771 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14772 | if (ctx.args_owned) { |
| 14773 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14774 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14775 | Py_DECREF(ctx.fmtstr); |
| 14776 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14777 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14778 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14779 | Py_DECREF(ctx.fmtstr); |
| 14780 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14781 | if (ctx.args_owned) { |
| 14782 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14783 | } |
| 14784 | return NULL; |
| 14785 | } |
| 14786 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14787 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14788 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14789 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14790 | static PyObject * |
| 14791 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14792 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14793 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14794 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14795 | char *encoding = NULL; |
| 14796 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14797 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14798 | if (type != &PyUnicode_Type) |
| 14799 | return unicode_subtype_new(type, args, kwds); |
| 14800 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14801 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14802 | return NULL; |
| 14803 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14804 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14805 | if (encoding == NULL && errors == NULL) |
| 14806 | return PyObject_Str(x); |
| 14807 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14808 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14809 | } |
| 14810 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14811 | static PyObject * |
| 14812 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14813 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14814 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14815 | Py_ssize_t length, char_size; |
| 14816 | int share_wstr, share_utf8; |
| 14817 | unsigned int kind; |
| 14818 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14819 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14820 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14821 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14822 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14823 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14824 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14825 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14826 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14827 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14828 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14829 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14830 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14831 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14832 | if (self == NULL) { |
| 14833 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14834 | return NULL; |
| 14835 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14836 | kind = PyUnicode_KIND(unicode); |
| 14837 | length = PyUnicode_GET_LENGTH(unicode); |
| 14838 | |
| 14839 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14840 | #ifdef Py_DEBUG |
| 14841 | _PyUnicode_HASH(self) = -1; |
| 14842 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14843 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14844 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14845 | _PyUnicode_STATE(self).interned = 0; |
| 14846 | _PyUnicode_STATE(self).kind = kind; |
| 14847 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14848 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14849 | _PyUnicode_STATE(self).ready = 1; |
| 14850 | _PyUnicode_WSTR(self) = NULL; |
| 14851 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14852 | _PyUnicode_UTF8(self) = NULL; |
| 14853 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14854 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14855 | |
| 14856 | share_utf8 = 0; |
| 14857 | share_wstr = 0; |
| 14858 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14859 | char_size = 1; |
| 14860 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14861 | share_utf8 = 1; |
| 14862 | } |
| 14863 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14864 | char_size = 2; |
| 14865 | if (sizeof(wchar_t) == 2) |
| 14866 | share_wstr = 1; |
| 14867 | } |
| 14868 | else { |
| 14869 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14870 | char_size = 4; |
| 14871 | if (sizeof(wchar_t) == 4) |
| 14872 | share_wstr = 1; |
| 14873 | } |
| 14874 | |
| 14875 | /* Ensure we won't overflow the length. */ |
| 14876 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14877 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14878 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14879 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14880 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14881 | if (data == NULL) { |
| 14882 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14883 | goto onError; |
| 14884 | } |
| 14885 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14886 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14887 | if (share_utf8) { |
| 14888 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14889 | _PyUnicode_UTF8(self) = data; |
| 14890 | } |
| 14891 | if (share_wstr) { |
| 14892 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14893 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14894 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14895 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14896 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14897 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14898 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14899 | #ifdef Py_DEBUG |
| 14900 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14901 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14902 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14903 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14904 | |
| 14905 | onError: |
| 14906 | Py_DECREF(unicode); |
| 14907 | Py_DECREF(self); |
| 14908 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14909 | } |
| 14910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14911 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14912 | "str(object='') -> str\n\ |
| 14913 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14914 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14915 | Create a new string object from the given object. If encoding or\n\ |
| 14916 | errors is specified, then the object must expose a data buffer\n\ |
| 14917 | that will be decoded using the given encoding and error handler.\n\ |
| 14918 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14919 | or repr(object).\n\ |
| 14920 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14921 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14922 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14923 | static PyObject *unicode_iter(PyObject *seq); |
| 14924 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14925 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14926 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14927 | "str", /* tp_name */ |
| 14928 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14929 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14930 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14931 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14932 | 0, /* tp_print */ |
| 14933 | 0, /* tp_getattr */ |
| 14934 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14935 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14936 | unicode_repr, /* tp_repr */ |
| 14937 | &unicode_as_number, /* tp_as_number */ |
| 14938 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14939 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14940 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14941 | 0, /* tp_call*/ |
| 14942 | (reprfunc) unicode_str, /* tp_str */ |
| 14943 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14944 | 0, /* tp_setattro */ |
| 14945 | 0, /* tp_as_buffer */ |
| 14946 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14947 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14948 | unicode_doc, /* tp_doc */ |
| 14949 | 0, /* tp_traverse */ |
| 14950 | 0, /* tp_clear */ |
| 14951 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14952 | 0, /* tp_weaklistoffset */ |
| 14953 | unicode_iter, /* tp_iter */ |
| 14954 | 0, /* tp_iternext */ |
| 14955 | unicode_methods, /* tp_methods */ |
| 14956 | 0, /* tp_members */ |
| 14957 | 0, /* tp_getset */ |
| 14958 | &PyBaseObject_Type, /* tp_base */ |
| 14959 | 0, /* tp_dict */ |
| 14960 | 0, /* tp_descr_get */ |
| 14961 | 0, /* tp_descr_set */ |
| 14962 | 0, /* tp_dictoffset */ |
| 14963 | 0, /* tp_init */ |
| 14964 | 0, /* tp_alloc */ |
| 14965 | unicode_new, /* tp_new */ |
| 14966 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14967 | }; |
| 14968 | |
| 14969 | /* Initialize the Unicode implementation */ |
| 14970 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14971 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14972 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14973 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14974 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14975 | 0x000A, /* LINE FEED */ |
| 14976 | 0x000D, /* CARRIAGE RETURN */ |
| 14977 | 0x001C, /* FILE SEPARATOR */ |
| 14978 | 0x001D, /* GROUP SEPARATOR */ |
| 14979 | 0x001E, /* RECORD SEPARATOR */ |
| 14980 | 0x0085, /* NEXT LINE */ |
| 14981 | 0x2028, /* LINE SEPARATOR */ |
| 14982 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14983 | }; |
| 14984 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14985 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14986 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14987 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14988 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14989 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14990 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14991 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14992 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14993 | |
| 14994 | /* initialize the linebreak bloom filter */ |
| 14995 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14996 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14997 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14998 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14999 | if (PyType_Ready(&EncodingMapType) < 0) |
| 15000 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15001 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 15002 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 15003 | Py_FatalError("Can't initialize field name iterator type"); |
| 15004 | |
| 15005 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 15006 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 15007 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15008 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15009 | } |
| 15010 | |
| 15011 | /* Finalize the Unicode implementation */ |
| 15012 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15013 | int |
| 15014 | PyUnicode_ClearFreeList(void) |
| 15015 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15016 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15017 | } |
| 15018 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15019 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 15020 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15021 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 15022 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15023 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15024 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 15025 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15026 | for (i = 0; i < 256; i++) |
| 15027 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 15028 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15029 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15030 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 15031 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15032 | void |
| 15033 | PyUnicode_InternInPlace(PyObject **p) |
| 15034 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15035 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15036 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15037 | #ifdef Py_DEBUG |
| 15038 | assert(s != NULL); |
| 15039 | assert(_PyUnicode_CHECK(s)); |
| 15040 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15041 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15042 | return; |
| 15043 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15044 | /* If it's a subclass, we don't really know what putting |
| 15045 | it in the interned dict might do. */ |
| 15046 | if (!PyUnicode_CheckExact(s)) |
| 15047 | return; |
| 15048 | if (PyUnicode_CHECK_INTERNED(s)) |
| 15049 | return; |
| 15050 | if (interned == NULL) { |
| 15051 | interned = PyDict_New(); |
| 15052 | if (interned == NULL) { |
| 15053 | PyErr_Clear(); /* Don't leave an exception */ |
| 15054 | return; |
| 15055 | } |
| 15056 | } |
| 15057 | /* It might be that the GetItem call fails even |
| 15058 | though the key is present in the dictionary, |
| 15059 | namely when this happens during a stack overflow. */ |
| 15060 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15061 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15062 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15063 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15064 | if (t) { |
| 15065 | Py_INCREF(t); |
| 15066 | Py_DECREF(*p); |
| 15067 | *p = t; |
| 15068 | return; |
| 15069 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15070 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15071 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15072 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15073 | PyErr_Clear(); |
| 15074 | PyThreadState_GET()->recursion_critical = 0; |
| 15075 | return; |
| 15076 | } |
| 15077 | PyThreadState_GET()->recursion_critical = 0; |
| 15078 | /* The two references in interned are not counted by refcnt. |
| 15079 | The deallocator will take care of this */ |
| 15080 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15081 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15082 | } |
| 15083 | |
| 15084 | void |
| 15085 | PyUnicode_InternImmortal(PyObject **p) |
| 15086 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15087 | PyUnicode_InternInPlace(p); |
| 15088 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 15089 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15090 | Py_INCREF(*p); |
| 15091 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15092 | } |
| 15093 | |
| 15094 | PyObject * |
| 15095 | PyUnicode_InternFromString(const char *cp) |
| 15096 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15097 | PyObject *s = PyUnicode_FromString(cp); |
| 15098 | if (s == NULL) |
| 15099 | return NULL; |
| 15100 | PyUnicode_InternInPlace(&s); |
| 15101 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15102 | } |
| 15103 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15104 | void |
| 15105 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15106 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15107 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15108 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15109 | Py_ssize_t i, n; |
| 15110 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15111 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15112 | if (interned == NULL || !PyDict_Check(interned)) |
| 15113 | return; |
| 15114 | keys = PyDict_Keys(interned); |
| 15115 | if (keys == NULL || !PyList_Check(keys)) { |
| 15116 | PyErr_Clear(); |
| 15117 | return; |
| 15118 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15119 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15120 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15121 | detector, interned unicode strings are not forcibly deallocated; |
| 15122 | rather, we give them their stolen references back, and then clear |
| 15123 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15124 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15125 | n = PyList_GET_SIZE(keys); |
| 15126 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15127 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15128 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15129 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15130 | if (PyUnicode_READY(s) == -1) { |
| 15131 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15132 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15133 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15134 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15135 | case SSTATE_NOT_INTERNED: |
| 15136 | /* XXX Shouldn't happen */ |
| 15137 | break; |
| 15138 | case SSTATE_INTERNED_IMMORTAL: |
| 15139 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15140 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15141 | break; |
| 15142 | case SSTATE_INTERNED_MORTAL: |
| 15143 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15144 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15145 | break; |
| 15146 | default: |
| 15147 | Py_FatalError("Inconsistent interned string state."); |
| 15148 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15149 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15150 | } |
| 15151 | fprintf(stderr, "total size of all interned strings: " |
| 15152 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15153 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15154 | Py_DECREF(keys); |
| 15155 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15156 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15157 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15158 | |
| 15159 | |
| 15160 | /********************* Unicode Iterator **************************/ |
| 15161 | |
| 15162 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15163 | PyObject_HEAD |
| 15164 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15165 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15166 | } unicodeiterobject; |
| 15167 | |
| 15168 | static void |
| 15169 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15170 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15171 | _PyObject_GC_UNTRACK(it); |
| 15172 | Py_XDECREF(it->it_seq); |
| 15173 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15174 | } |
| 15175 | |
| 15176 | static int |
| 15177 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15178 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15179 | Py_VISIT(it->it_seq); |
| 15180 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15181 | } |
| 15182 | |
| 15183 | static PyObject * |
| 15184 | unicodeiter_next(unicodeiterobject *it) |
| 15185 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15186 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15187 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15188 | assert(it != NULL); |
| 15189 | seq = it->it_seq; |
| 15190 | if (seq == NULL) |
| 15191 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15192 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15193 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15194 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15195 | int kind = PyUnicode_KIND(seq); |
| 15196 | void *data = PyUnicode_DATA(seq); |
| 15197 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15198 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15199 | if (item != NULL) |
| 15200 | ++it->it_index; |
| 15201 | return item; |
| 15202 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15203 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15204 | Py_DECREF(seq); |
| 15205 | it->it_seq = NULL; |
| 15206 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15207 | } |
| 15208 | |
| 15209 | static PyObject * |
| 15210 | unicodeiter_len(unicodeiterobject *it) |
| 15211 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15212 | Py_ssize_t len = 0; |
| 15213 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15214 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15215 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15216 | } |
| 15217 | |
| 15218 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15219 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15220 | static PyObject * |
| 15221 | unicodeiter_reduce(unicodeiterobject *it) |
| 15222 | { |
| 15223 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15224 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15225 | it->it_seq, it->it_index); |
| 15226 | } else { |
| 15227 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15228 | if (u == NULL) |
| 15229 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15230 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15231 | } |
| 15232 | } |
| 15233 | |
| 15234 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15235 | |
| 15236 | static PyObject * |
| 15237 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15238 | { |
| 15239 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15240 | if (index == -1 && PyErr_Occurred()) |
| 15241 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 15242 | if (it->it_seq != NULL) { |
| 15243 | if (index < 0) |
| 15244 | index = 0; |
| 15245 | else if (index > PyUnicode_GET_LENGTH(it->it_seq)) |
| 15246 | index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */ |
| 15247 | it->it_index = index; |
| 15248 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15249 | Py_RETURN_NONE; |
| 15250 | } |
| 15251 | |
| 15252 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15253 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15254 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15255 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15256 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15257 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15258 | reduce_doc}, |
| 15259 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15260 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15261 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15262 | }; |
| 15263 | |
| 15264 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15265 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15266 | "str_iterator", /* tp_name */ |
| 15267 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15268 | 0, /* tp_itemsize */ |
| 15269 | /* methods */ |
| 15270 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15271 | 0, /* tp_print */ |
| 15272 | 0, /* tp_getattr */ |
| 15273 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15274 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15275 | 0, /* tp_repr */ |
| 15276 | 0, /* tp_as_number */ |
| 15277 | 0, /* tp_as_sequence */ |
| 15278 | 0, /* tp_as_mapping */ |
| 15279 | 0, /* tp_hash */ |
| 15280 | 0, /* tp_call */ |
| 15281 | 0, /* tp_str */ |
| 15282 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15283 | 0, /* tp_setattro */ |
| 15284 | 0, /* tp_as_buffer */ |
| 15285 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15286 | 0, /* tp_doc */ |
| 15287 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15288 | 0, /* tp_clear */ |
| 15289 | 0, /* tp_richcompare */ |
| 15290 | 0, /* tp_weaklistoffset */ |
| 15291 | PyObject_SelfIter, /* tp_iter */ |
| 15292 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15293 | unicodeiter_methods, /* tp_methods */ |
| 15294 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15295 | }; |
| 15296 | |
| 15297 | static PyObject * |
| 15298 | unicode_iter(PyObject *seq) |
| 15299 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15300 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15301 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15302 | if (!PyUnicode_Check(seq)) { |
| 15303 | PyErr_BadInternalCall(); |
| 15304 | return NULL; |
| 15305 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15306 | if (PyUnicode_READY(seq) == -1) |
| 15307 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15308 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15309 | if (it == NULL) |
| 15310 | return NULL; |
| 15311 | it->it_index = 0; |
| 15312 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15313 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15314 | _PyObject_GC_TRACK(it); |
| 15315 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15316 | } |
| 15317 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15318 | |
| 15319 | size_t |
| 15320 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15321 | { |
| 15322 | int res = 0; |
| 15323 | while(*u++) |
| 15324 | res++; |
| 15325 | return res; |
| 15326 | } |
| 15327 | |
| 15328 | Py_UNICODE* |
| 15329 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15330 | { |
| 15331 | Py_UNICODE *u = s1; |
| 15332 | while ((*u++ = *s2++)); |
| 15333 | return s1; |
| 15334 | } |
| 15335 | |
| 15336 | Py_UNICODE* |
| 15337 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15338 | { |
| 15339 | Py_UNICODE *u = s1; |
| 15340 | while ((*u++ = *s2++)) |
| 15341 | if (n-- == 0) |
| 15342 | break; |
| 15343 | return s1; |
| 15344 | } |
| 15345 | |
| 15346 | Py_UNICODE* |
| 15347 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15348 | { |
| 15349 | Py_UNICODE *u1 = s1; |
| 15350 | u1 += Py_UNICODE_strlen(u1); |
| 15351 | Py_UNICODE_strcpy(u1, s2); |
| 15352 | return s1; |
| 15353 | } |
| 15354 | |
| 15355 | int |
| 15356 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15357 | { |
| 15358 | while (*s1 && *s2 && *s1 == *s2) |
| 15359 | s1++, s2++; |
| 15360 | if (*s1 && *s2) |
| 15361 | return (*s1 < *s2) ? -1 : +1; |
| 15362 | if (*s1) |
| 15363 | return 1; |
| 15364 | if (*s2) |
| 15365 | return -1; |
| 15366 | return 0; |
| 15367 | } |
| 15368 | |
| 15369 | int |
| 15370 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15371 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15372 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15373 | for (; n != 0; n--) { |
| 15374 | u1 = *s1; |
| 15375 | u2 = *s2; |
| 15376 | if (u1 != u2) |
| 15377 | return (u1 < u2) ? -1 : +1; |
| 15378 | if (u1 == '\0') |
| 15379 | return 0; |
| 15380 | s1++; |
| 15381 | s2++; |
| 15382 | } |
| 15383 | return 0; |
| 15384 | } |
| 15385 | |
| 15386 | Py_UNICODE* |
| 15387 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15388 | { |
| 15389 | const Py_UNICODE *p; |
| 15390 | for (p = s; *p; p++) |
| 15391 | if (*p == c) |
| 15392 | return (Py_UNICODE*)p; |
| 15393 | return NULL; |
| 15394 | } |
| 15395 | |
| 15396 | Py_UNICODE* |
| 15397 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15398 | { |
| 15399 | const Py_UNICODE *p; |
| 15400 | p = s + Py_UNICODE_strlen(s); |
| 15401 | while (p != s) { |
| 15402 | p--; |
| 15403 | if (*p == c) |
| 15404 | return (Py_UNICODE*)p; |
| 15405 | } |
| 15406 | return NULL; |
| 15407 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15408 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15409 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15410 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15411 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15412 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15413 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15415 | if (!PyUnicode_Check(unicode)) { |
| 15416 | PyErr_BadArgument(); |
| 15417 | return NULL; |
| 15418 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15419 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15420 | if (u == NULL) |
| 15421 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15422 | /* Ensure we won't overflow the size. */ |
Gregory P. Smith | 8486f9b | 2014-09-30 00:33:24 -0700 | [diff] [blame] | 15423 | 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] | 15424 | PyErr_NoMemory(); |
| 15425 | return NULL; |
| 15426 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15427 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15428 | size *= sizeof(Py_UNICODE); |
| 15429 | copy = PyMem_Malloc(size); |
| 15430 | if (copy == NULL) { |
| 15431 | PyErr_NoMemory(); |
| 15432 | return NULL; |
| 15433 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15434 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15435 | return copy; |
| 15436 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15437 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15438 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15439 | to the string.Formatter class implemented in Python. */ |
| 15440 | |
| 15441 | static PyMethodDef _string_methods[] = { |
| 15442 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15443 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15444 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15445 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15446 | {NULL, NULL} |
| 15447 | }; |
| 15448 | |
| 15449 | static struct PyModuleDef _string_module = { |
| 15450 | PyModuleDef_HEAD_INIT, |
| 15451 | "_string", |
| 15452 | PyDoc_STR("string helper module"), |
| 15453 | 0, |
| 15454 | _string_methods, |
| 15455 | NULL, |
| 15456 | NULL, |
| 15457 | NULL, |
| 15458 | NULL |
| 15459 | }; |
| 15460 | |
| 15461 | PyMODINIT_FUNC |
| 15462 | PyInit__string(void) |
| 15463 | { |
| 15464 | return PyModule_Create(&_string_module); |
| 15465 | } |
| 15466 | |
| 15467 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15468 | #ifdef __cplusplus |
| 15469 | } |
| 15470 | #endif |