Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Benjamin Peterson | 31616ea | 2011-10-01 00:11:09 -0400 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com>. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6 | Major speed upgrades to the method implementations at the Reykjavik |
| 7 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 8 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 11 | -------------------------------------------------------------------- |
| 12 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14 | Copyright (c) 1999 by Secret Labs AB |
| 15 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 16 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 17 | By obtaining, using, and/or copying this software and/or its |
| 18 | associated documentation, you agree that you have read, understood, |
| 19 | and will comply with the following terms and conditions: |
| 20 | |
| 21 | Permission to use, copy, modify, and distribute this software and its |
| 22 | associated documentation for any purpose and without fee is hereby |
| 23 | granted, provided that the above copyright notice appears in all |
| 24 | copies, and that both that copyright notice and this permission notice |
| 25 | appear in supporting documentation, and that the name of Secret Labs |
| 26 | AB or the author not be used in advertising or publicity pertaining to |
| 27 | distribution of the software without specific, written prior |
| 28 | permission. |
| 29 | |
| 30 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 31 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 32 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 33 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 34 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 35 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 36 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 37 | -------------------------------------------------------------------- |
| 38 | |
| 39 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 42 | #include "Python.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 43 | #include "ucnhash.h" |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 46 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 47 | #include <windows.h> |
| 48 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 49 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 50 | /*[clinic input] |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 51 | class str "PyUnicodeObject *" "&PyUnicode_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 52 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 53 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=604e916854800fa8]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 54 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 55 | /* --- Globals ------------------------------------------------------------ |
| 56 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 57 | NOTE: In the interpreter's initialization phase, some globals are currently |
| 58 | initialized dynamically as needed. In the process Unicode objects may |
| 59 | be created before the Unicode type is ready. |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 60 | |
| 61 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 63 | |
| 64 | #ifdef __cplusplus |
| 65 | extern "C" { |
| 66 | #endif |
| 67 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 68 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 69 | #define MAX_UNICODE 0x10ffff |
| 70 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 71 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 72 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 73 | #else |
| 74 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 75 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 76 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 77 | #define _PyUnicode_UTF8(op) \ |
| 78 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 79 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 80 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 81 | assert(PyUnicode_IS_READY(op)), \ |
| 82 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 83 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 84 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 85 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 86 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 87 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 88 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 89 | assert(PyUnicode_IS_READY(op)), \ |
| 90 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 91 | ((PyASCIIObject*)(op))->length : \ |
| 92 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 93 | #define _PyUnicode_WSTR(op) \ |
| 94 | (((PyASCIIObject*)(op))->wstr) |
| 95 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 96 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 97 | #define _PyUnicode_LENGTH(op) \ |
| 98 | (((PyASCIIObject *)(op))->length) |
| 99 | #define _PyUnicode_STATE(op) \ |
| 100 | (((PyASCIIObject *)(op))->state) |
| 101 | #define _PyUnicode_HASH(op) \ |
| 102 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 103 | #define _PyUnicode_KIND(op) \ |
| 104 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 105 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 106 | #define _PyUnicode_GET_LENGTH(op) \ |
| 107 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 108 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 109 | #define _PyUnicode_DATA_ANY(op) \ |
| 110 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 111 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 112 | #undef PyUnicode_READY |
| 113 | #define PyUnicode_READY(op) \ |
| 114 | (assert(_PyUnicode_CHECK(op)), \ |
| 115 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 116 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 117 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 118 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 119 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 120 | (assert(_PyUnicode_CHECK(op)), \ |
| 121 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 122 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 123 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 124 | (assert(_PyUnicode_CHECK(op)), \ |
| 125 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 126 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 127 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 128 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 129 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 130 | ((!PyUnicode_IS_COMPACT_ASCII(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 131 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 132 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 133 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 134 | /* true if the Unicode object has an allocated wstr memory block |
| 135 | (not shared with other data) */ |
| 136 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
Victor Stinner | e699e5a | 2013-07-15 18:22:47 +0200 | [diff] [blame] | 137 | ((_PyUnicode_WSTR(op) && \ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 138 | (!PyUnicode_IS_READY(op) || \ |
| 139 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 140 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 141 | /* Generic helper macro to convert characters of different types. |
| 142 | from_type and to_type have to be valid type names, begin and end |
| 143 | are pointers to the source characters which should be of type |
| 144 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 145 | buffer where the result characters are written to. */ |
| 146 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 147 | do { \ |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 148 | to_type *_to = (to_type *)(to); \ |
| 149 | const from_type *_iter = (from_type *)(begin); \ |
| 150 | const from_type *_end = (from_type *)(end); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 151 | Py_ssize_t n = (_end) - (_iter); \ |
| 152 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 153 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 154 | while (_iter < (_unrolled_end)) { \ |
| 155 | _to[0] = (to_type) _iter[0]; \ |
| 156 | _to[1] = (to_type) _iter[1]; \ |
| 157 | _to[2] = (to_type) _iter[2]; \ |
| 158 | _to[3] = (to_type) _iter[3]; \ |
| 159 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 160 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 161 | while (_iter < (_end)) \ |
| 162 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 163 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 164 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 165 | /* This dictionary holds all interned unicode strings. Note that references |
| 166 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 167 | When the interned string reaches a refcnt of 0 the string deallocation |
| 168 | function will delete the reference from this dictionary. |
| 169 | |
| 170 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 171 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 172 | */ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 173 | static PyObject *interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 174 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 175 | /* The empty Unicode object is shared to improve performance. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 176 | static PyObject *unicode_empty = NULL; |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 177 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 178 | #define _Py_INCREF_UNICODE_EMPTY() \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 179 | do { \ |
| 180 | if (unicode_empty != NULL) \ |
| 181 | Py_INCREF(unicode_empty); \ |
| 182 | else { \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 183 | unicode_empty = PyUnicode_New(0, 0); \ |
| 184 | if (unicode_empty != NULL) { \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 185 | Py_INCREF(unicode_empty); \ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 186 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); \ |
| 187 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 188 | } \ |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 189 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 190 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 191 | #define _Py_RETURN_UNICODE_EMPTY() \ |
| 192 | do { \ |
| 193 | _Py_INCREF_UNICODE_EMPTY(); \ |
| 194 | return unicode_empty; \ |
| 195 | } while (0) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 196 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 197 | /* Forward declaration */ |
| 198 | Py_LOCAL_INLINE(int) |
| 199 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch); |
| 200 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 201 | /* List of static strings. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 202 | static _Py_Identifier *static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 203 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 204 | /* Single character Unicode strings in the Latin-1 range are being |
| 205 | shared as well. */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 206 | static PyObject *unicode_latin1[256] = {NULL}; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 207 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 208 | /* Fast detection of the most frequent whitespace characters */ |
| 209 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 211 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 212 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 213 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 214 | /* case 0x000C: * FORM FEED */ |
| 215 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 216 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 217 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 218 | /* case 0x001C: * FILE SEPARATOR */ |
| 219 | /* case 0x001D: * GROUP SEPARATOR */ |
| 220 | /* case 0x001E: * RECORD SEPARATOR */ |
| 221 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 222 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 223 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 224 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 225 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 226 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 227 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 228 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 229 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 230 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 231 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 232 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 233 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 234 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 235 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 236 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 239 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 240 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 241 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 242 | static int unicode_modifiable(PyObject *unicode); |
| 243 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 245 | static PyObject * |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 246 | _PyUnicode_FromUCS1(const Py_UCS1 *s, Py_ssize_t size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 247 | static PyObject * |
| 248 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 249 | static PyObject * |
| 250 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 251 | |
| 252 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 253 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 254 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 255 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 256 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 257 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 258 | static void |
| 259 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 260 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 261 | PyObject *unicode, |
| 262 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 263 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 264 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 265 | /* Same for linebreaks */ |
| 266 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 267 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 268 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 269 | /* 0x000B, * LINE TABULATION */ |
| 270 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 271 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 272 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 273 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 274 | /* 0x001C, * FILE SEPARATOR */ |
| 275 | /* 0x001D, * GROUP SEPARATOR */ |
| 276 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 277 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 278 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 279 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 280 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 281 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 282 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 283 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 284 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 286 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 287 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 288 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 289 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 290 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 293 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 294 | 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] | 295 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 296 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 297 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 298 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 299 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 300 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 301 | /* This is actually an illegal character, so it should |
| 302 | not be passed to unichr. */ |
| 303 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 304 | #endif |
| 305 | } |
| 306 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 308 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 309 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 310 | { |
| 311 | PyASCIIObject *ascii; |
| 312 | unsigned int kind; |
| 313 | |
| 314 | assert(PyUnicode_Check(op)); |
| 315 | |
| 316 | ascii = (PyASCIIObject *)op; |
| 317 | kind = ascii->state.kind; |
| 318 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 319 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 320 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 321 | assert(ascii->state.ready == 1); |
| 322 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 323 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 324 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 325 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 326 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 327 | if (ascii->state.compact == 1) { |
| 328 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 329 | assert(kind == PyUnicode_1BYTE_KIND |
| 330 | || kind == PyUnicode_2BYTE_KIND |
| 331 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 332 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 333 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 334 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 335 | } |
| 336 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 337 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 338 | |
| 339 | data = unicode->data.any; |
| 340 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 341 | assert(ascii->length == 0); |
| 342 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 343 | assert(ascii->state.compact == 0); |
| 344 | assert(ascii->state.ascii == 0); |
| 345 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 346 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 347 | assert(ascii->wstr != NULL); |
| 348 | assert(data == NULL); |
| 349 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 350 | } |
| 351 | else { |
| 352 | assert(kind == PyUnicode_1BYTE_KIND |
| 353 | || kind == PyUnicode_2BYTE_KIND |
| 354 | || kind == PyUnicode_4BYTE_KIND); |
| 355 | assert(ascii->state.compact == 0); |
| 356 | assert(ascii->state.ready == 1); |
| 357 | assert(data != NULL); |
| 358 | if (ascii->state.ascii) { |
| 359 | assert (compact->utf8 == data); |
| 360 | assert (compact->utf8_length == ascii->length); |
| 361 | } |
| 362 | else |
| 363 | assert (compact->utf8 != data); |
| 364 | } |
| 365 | } |
| 366 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 367 | if ( |
| 368 | #if SIZEOF_WCHAR_T == 2 |
| 369 | kind == PyUnicode_2BYTE_KIND |
| 370 | #else |
| 371 | kind == PyUnicode_4BYTE_KIND |
| 372 | #endif |
| 373 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 374 | { |
| 375 | assert(ascii->wstr == data); |
| 376 | assert(compact->wstr_length == ascii->length); |
| 377 | } else |
| 378 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 379 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 380 | |
| 381 | if (compact->utf8 == NULL) |
| 382 | assert(compact->utf8_length == 0); |
| 383 | if (ascii->wstr == NULL) |
| 384 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 385 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 386 | /* check that the best kind is used */ |
| 387 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 388 | { |
| 389 | Py_ssize_t i; |
| 390 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 391 | void *data; |
| 392 | Py_UCS4 ch; |
| 393 | |
| 394 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 395 | for (i=0; i < ascii->length; i++) |
| 396 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 397 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 398 | if (ch > maxchar) |
| 399 | maxchar = ch; |
| 400 | } |
| 401 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 402 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 403 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 404 | assert(maxchar <= 255); |
| 405 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 406 | else |
| 407 | assert(maxchar < 128); |
| 408 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 409 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 410 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 411 | assert(maxchar <= 0xFFFF); |
| 412 | } |
| 413 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 414 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 415 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 416 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 417 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 418 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 419 | return 1; |
| 420 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 421 | #endif |
| 422 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 423 | static PyObject* |
| 424 | unicode_result_wchar(PyObject *unicode) |
| 425 | { |
| 426 | #ifndef Py_DEBUG |
| 427 | Py_ssize_t len; |
| 428 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 429 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 430 | if (len == 0) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 431 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 432 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if (len == 1) { |
| 436 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 437 | if ((Py_UCS4)ch < 256) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 438 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 439 | Py_DECREF(unicode); |
| 440 | return latin1_char; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | if (_PyUnicode_Ready(unicode) < 0) { |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 445 | Py_DECREF(unicode); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 446 | return NULL; |
| 447 | } |
| 448 | #else |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 449 | assert(Py_REFCNT(unicode) == 1); |
| 450 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 451 | /* don't make the result ready in debug mode to ensure that the caller |
| 452 | makes the string ready before using it */ |
| 453 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 454 | #endif |
| 455 | return unicode; |
| 456 | } |
| 457 | |
| 458 | static PyObject* |
| 459 | unicode_result_ready(PyObject *unicode) |
| 460 | { |
| 461 | Py_ssize_t length; |
| 462 | |
| 463 | length = PyUnicode_GET_LENGTH(unicode); |
| 464 | if (length == 0) { |
| 465 | if (unicode != unicode_empty) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 466 | Py_DECREF(unicode); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 467 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 468 | } |
| 469 | return unicode_empty; |
| 470 | } |
| 471 | |
| 472 | if (length == 1) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 473 | void *data = PyUnicode_DATA(unicode); |
| 474 | int kind = PyUnicode_KIND(unicode); |
| 475 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 476 | if (ch < 256) { |
| 477 | PyObject *latin1_char = unicode_latin1[ch]; |
| 478 | if (latin1_char != NULL) { |
| 479 | if (unicode != latin1_char) { |
| 480 | Py_INCREF(latin1_char); |
| 481 | Py_DECREF(unicode); |
| 482 | } |
| 483 | return latin1_char; |
| 484 | } |
| 485 | else { |
| 486 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 487 | Py_INCREF(unicode); |
| 488 | unicode_latin1[ch] = unicode; |
| 489 | return unicode; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 495 | return unicode; |
| 496 | } |
| 497 | |
| 498 | static PyObject* |
| 499 | unicode_result(PyObject *unicode) |
| 500 | { |
| 501 | assert(_PyUnicode_CHECK(unicode)); |
| 502 | if (PyUnicode_IS_READY(unicode)) |
| 503 | return unicode_result_ready(unicode); |
| 504 | else |
| 505 | return unicode_result_wchar(unicode); |
| 506 | } |
| 507 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 508 | static PyObject* |
| 509 | unicode_result_unchanged(PyObject *unicode) |
| 510 | { |
| 511 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 512 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 513 | return NULL; |
| 514 | Py_INCREF(unicode); |
| 515 | return unicode; |
| 516 | } |
| 517 | else |
| 518 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 519 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 522 | #ifdef HAVE_MBCS |
| 523 | static OSVERSIONINFOEX winver; |
| 524 | #endif |
| 525 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 526 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 527 | |
| 528 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 529 | to keep things simple, we use a single bitmask, using the least 5 |
| 530 | bits from each unicode characters as the bit index. */ |
| 531 | |
| 532 | /* the linebreak mask is set up by Unicode_Init below */ |
| 533 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 534 | #if LONG_BIT >= 128 |
| 535 | #define BLOOM_WIDTH 128 |
| 536 | #elif LONG_BIT >= 64 |
| 537 | #define BLOOM_WIDTH 64 |
| 538 | #elif LONG_BIT >= 32 |
| 539 | #define BLOOM_WIDTH 32 |
| 540 | #else |
| 541 | #error "LONG_BIT is smaller than 32" |
| 542 | #endif |
| 543 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 544 | #define BLOOM_MASK unsigned long |
| 545 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 546 | static BLOOM_MASK bloom_linebreak = ~(BLOOM_MASK)0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 548 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 549 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 550 | #define BLOOM_LINEBREAK(ch) \ |
| 551 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 552 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 553 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 554 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 555 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 556 | { |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 557 | #define BLOOM_UPDATE(TYPE, MASK, PTR, LEN) \ |
| 558 | do { \ |
| 559 | TYPE *data = (TYPE *)PTR; \ |
| 560 | TYPE *end = data + LEN; \ |
| 561 | Py_UCS4 ch; \ |
| 562 | for (; data != end; data++) { \ |
| 563 | ch = *data; \ |
| 564 | MASK |= (1UL << (ch & (BLOOM_WIDTH - 1))); \ |
| 565 | } \ |
| 566 | break; \ |
| 567 | } while (0) |
| 568 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 569 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 570 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 571 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 572 | |
| 573 | mask = 0; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 574 | switch (kind) { |
| 575 | case PyUnicode_1BYTE_KIND: |
| 576 | BLOOM_UPDATE(Py_UCS1, mask, ptr, len); |
| 577 | break; |
| 578 | case PyUnicode_2BYTE_KIND: |
| 579 | BLOOM_UPDATE(Py_UCS2, mask, ptr, len); |
| 580 | break; |
| 581 | case PyUnicode_4BYTE_KIND: |
| 582 | BLOOM_UPDATE(Py_UCS4, mask, ptr, len); |
| 583 | break; |
| 584 | default: |
| 585 | assert(0); |
| 586 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 587 | return mask; |
Victor Stinner | a85af50 | 2013-04-09 21:53:54 +0200 | [diff] [blame] | 588 | |
| 589 | #undef BLOOM_UPDATE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 592 | /* Compilation of templated routines */ |
| 593 | |
| 594 | #include "stringlib/asciilib.h" |
| 595 | #include "stringlib/fastsearch.h" |
| 596 | #include "stringlib/partition.h" |
| 597 | #include "stringlib/split.h" |
| 598 | #include "stringlib/count.h" |
| 599 | #include "stringlib/find.h" |
| 600 | #include "stringlib/find_max_char.h" |
| 601 | #include "stringlib/localeutil.h" |
| 602 | #include "stringlib/undef.h" |
| 603 | |
| 604 | #include "stringlib/ucs1lib.h" |
| 605 | #include "stringlib/fastsearch.h" |
| 606 | #include "stringlib/partition.h" |
| 607 | #include "stringlib/split.h" |
| 608 | #include "stringlib/count.h" |
| 609 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 610 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 611 | #include "stringlib/find_max_char.h" |
| 612 | #include "stringlib/localeutil.h" |
| 613 | #include "stringlib/undef.h" |
| 614 | |
| 615 | #include "stringlib/ucs2lib.h" |
| 616 | #include "stringlib/fastsearch.h" |
| 617 | #include "stringlib/partition.h" |
| 618 | #include "stringlib/split.h" |
| 619 | #include "stringlib/count.h" |
| 620 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 621 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 622 | #include "stringlib/find_max_char.h" |
| 623 | #include "stringlib/localeutil.h" |
| 624 | #include "stringlib/undef.h" |
| 625 | |
| 626 | #include "stringlib/ucs4lib.h" |
| 627 | #include "stringlib/fastsearch.h" |
| 628 | #include "stringlib/partition.h" |
| 629 | #include "stringlib/split.h" |
| 630 | #include "stringlib/count.h" |
| 631 | #include "stringlib/find.h" |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 632 | #include "stringlib/replace.h" |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 633 | #include "stringlib/find_max_char.h" |
| 634 | #include "stringlib/localeutil.h" |
| 635 | #include "stringlib/undef.h" |
| 636 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 637 | #include "stringlib/unicodedefs.h" |
| 638 | #include "stringlib/fastsearch.h" |
| 639 | #include "stringlib/count.h" |
| 640 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 641 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 642 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 643 | /* --- Unicode Object ----------------------------------------------------- */ |
| 644 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 645 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 646 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 647 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 648 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 649 | Py_ssize_t size, Py_UCS4 ch, |
| 650 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 651 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 652 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 653 | |
| 654 | switch (kind) { |
| 655 | case PyUnicode_1BYTE_KIND: |
| 656 | { |
| 657 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 658 | if (ch1 == ch) |
| 659 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 660 | else |
| 661 | return -1; |
| 662 | } |
| 663 | case PyUnicode_2BYTE_KIND: |
| 664 | { |
| 665 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 666 | if (ch2 == ch) |
| 667 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 668 | else |
| 669 | return -1; |
| 670 | } |
| 671 | case PyUnicode_4BYTE_KIND: |
| 672 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 673 | default: |
| 674 | assert(0); |
| 675 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 676 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 679 | #ifdef Py_DEBUG |
| 680 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 681 | earlier. |
| 682 | |
| 683 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 684 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 685 | invalid character in Unicode 6.0. */ |
| 686 | static void |
| 687 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 688 | { |
| 689 | int kind = PyUnicode_KIND(unicode); |
| 690 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 691 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 692 | if (length <= old_length) |
| 693 | return; |
| 694 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 695 | } |
| 696 | #endif |
| 697 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 698 | static PyObject* |
| 699 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 700 | { |
| 701 | Py_ssize_t char_size; |
| 702 | Py_ssize_t struct_size; |
| 703 | Py_ssize_t new_size; |
| 704 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 705 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 706 | #ifdef Py_DEBUG |
| 707 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 708 | #endif |
| 709 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 710 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 711 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 712 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 713 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 714 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 715 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 716 | struct_size = sizeof(PyASCIIObject); |
| 717 | else |
| 718 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 719 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 720 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 721 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 722 | PyErr_NoMemory(); |
| 723 | return NULL; |
| 724 | } |
| 725 | new_size = (struct_size + (length + 1) * char_size); |
| 726 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 727 | _Py_DEC_REFTOTAL; |
| 728 | _Py_ForgetReference(unicode); |
| 729 | |
| 730 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 731 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 732 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 733 | PyErr_NoMemory(); |
| 734 | return NULL; |
| 735 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 736 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 737 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 738 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 739 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 740 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 741 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 742 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 743 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 744 | } |
Victor Stinner | bbbac2e | 2013-02-07 23:12:46 +0100 | [diff] [blame] | 745 | else if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) { |
| 746 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
| 747 | _PyUnicode_WSTR(unicode) = NULL; |
| 748 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 749 | #ifdef Py_DEBUG |
| 750 | unicode_fill_invalid(unicode, old_length); |
| 751 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 752 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 753 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 754 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 755 | return unicode; |
| 756 | } |
| 757 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 758 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 759 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 760 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 761 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 762 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 763 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 764 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 765 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 766 | if (PyUnicode_IS_READY(unicode)) { |
| 767 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 768 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 769 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 770 | #ifdef Py_DEBUG |
| 771 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 772 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 773 | |
| 774 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 775 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 776 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 777 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 778 | |
| 779 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 780 | PyErr_NoMemory(); |
| 781 | return -1; |
| 782 | } |
| 783 | new_size = (length + 1) * char_size; |
| 784 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 785 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 786 | { |
| 787 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 788 | _PyUnicode_UTF8(unicode) = NULL; |
| 789 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 790 | } |
| 791 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 792 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 793 | if (data == NULL) { |
| 794 | PyErr_NoMemory(); |
| 795 | return -1; |
| 796 | } |
| 797 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 798 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 799 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 800 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 801 | } |
| 802 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 803 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 804 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 805 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 806 | _PyUnicode_LENGTH(unicode) = length; |
| 807 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 808 | #ifdef Py_DEBUG |
| 809 | unicode_fill_invalid(unicode, old_length); |
| 810 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 811 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 812 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 813 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 814 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 815 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 816 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 817 | |
| 818 | /* check for integer overflow */ |
| 819 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 820 | PyErr_NoMemory(); |
| 821 | return -1; |
| 822 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 823 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 824 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 825 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 826 | if (!wstr) { |
| 827 | PyErr_NoMemory(); |
| 828 | return -1; |
| 829 | } |
| 830 | _PyUnicode_WSTR(unicode) = wstr; |
| 831 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 832 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 833 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 837 | static PyObject* |
| 838 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 839 | { |
| 840 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 841 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 842 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 843 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 844 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 845 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 846 | |
| 847 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 848 | if (copy == NULL) |
| 849 | return NULL; |
| 850 | |
| 851 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 852 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 853 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 854 | } |
| 855 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 856 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 857 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 858 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 859 | if (w == NULL) |
| 860 | return NULL; |
| 861 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 862 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 863 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 864 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 865 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 869 | /* 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] | 870 | Ux0000 terminated; some code (e.g. new_identifier) |
| 871 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 872 | |
| 873 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 874 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 875 | |
| 876 | */ |
| 877 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 878 | static PyUnicodeObject * |
| 879 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 880 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 881 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 882 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 883 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 884 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 885 | if (length == 0 && unicode_empty != NULL) { |
| 886 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 887 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 890 | /* Ensure we won't overflow the size. */ |
| 891 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 892 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 893 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 894 | if (length < 0) { |
| 895 | PyErr_SetString(PyExc_SystemError, |
| 896 | "Negative size passed to _PyUnicode_New"); |
| 897 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 900 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 901 | if (unicode == NULL) |
| 902 | return NULL; |
| 903 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 904 | |
| 905 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 906 | _PyUnicode_HASH(unicode) = -1; |
| 907 | _PyUnicode_STATE(unicode).interned = 0; |
| 908 | _PyUnicode_STATE(unicode).kind = 0; |
| 909 | _PyUnicode_STATE(unicode).compact = 0; |
| 910 | _PyUnicode_STATE(unicode).ready = 0; |
| 911 | _PyUnicode_STATE(unicode).ascii = 0; |
| 912 | _PyUnicode_DATA_ANY(unicode) = NULL; |
| 913 | _PyUnicode_LENGTH(unicode) = 0; |
| 914 | _PyUnicode_UTF8(unicode) = NULL; |
| 915 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 917 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 918 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 919 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 920 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 921 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 922 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 924 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 925 | * the caller fails before initializing str -- unicode_resize() |
| 926 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 927 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 928 | * We don't want unicode_resize to read uninitialized memory in |
| 929 | * that case. |
| 930 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 931 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 932 | _PyUnicode_WSTR(unicode)[length] = 0; |
Victor Stinner | 68b674c | 2013-10-29 19:31:43 +0100 | [diff] [blame] | 933 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 934 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 935 | return unicode; |
| 936 | } |
| 937 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 938 | static const char* |
| 939 | unicode_kind_name(PyObject *unicode) |
| 940 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 941 | /* don't check consistency: unicode_kind_name() is called from |
| 942 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 943 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 944 | { |
| 945 | if (!PyUnicode_IS_READY(unicode)) |
| 946 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 947 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 948 | { |
| 949 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 950 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 951 | return "legacy ascii"; |
| 952 | else |
| 953 | return "legacy latin1"; |
| 954 | case PyUnicode_2BYTE_KIND: |
| 955 | return "legacy UCS2"; |
| 956 | case PyUnicode_4BYTE_KIND: |
| 957 | return "legacy UCS4"; |
| 958 | default: |
| 959 | return "<legacy invalid kind>"; |
| 960 | } |
| 961 | } |
| 962 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 963 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 964 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 965 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 966 | return "ascii"; |
| 967 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 968 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 969 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 970 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 971 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 972 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 973 | default: |
| 974 | return "<invalid compact kind>"; |
| 975 | } |
| 976 | } |
| 977 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 978 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 979 | /* Functions wrapping macros for use in debugger */ |
| 980 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 981 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | void *_PyUnicode_compact_data(void *unicode) { |
| 985 | return _PyUnicode_COMPACT_DATA(unicode); |
| 986 | } |
| 987 | void *_PyUnicode_data(void *unicode){ |
| 988 | printf("obj %p\n", unicode); |
| 989 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 990 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 991 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 992 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 993 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 994 | return PyUnicode_DATA(unicode); |
| 995 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 996 | |
| 997 | void |
| 998 | _PyUnicode_Dump(PyObject *op) |
| 999 | { |
| 1000 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1001 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 1002 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 1003 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1004 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1005 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1006 | { |
| 1007 | if (ascii->state.ascii) |
| 1008 | data = (ascii + 1); |
| 1009 | else |
| 1010 | data = (compact + 1); |
| 1011 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1012 | else |
| 1013 | data = unicode->data.any; |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1014 | printf("%s: len=%" PY_FORMAT_SIZE_T "u, ", |
| 1015 | unicode_kind_name(op), ascii->length); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1016 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1017 | if (ascii->wstr == data) |
| 1018 | printf("shared "); |
| 1019 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 1020 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1021 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1022 | printf(" (%" PY_FORMAT_SIZE_T "u), ", compact->wstr_length); |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1023 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 1024 | printf("shared "); |
Victor Stinner | 293f3f5 | 2014-07-01 08:57:10 +0200 | [diff] [blame] | 1025 | printf("utf8=%p (%" PY_FORMAT_SIZE_T "u)", |
| 1026 | compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1027 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 1028 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 1029 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1030 | #endif |
| 1031 | |
| 1032 | PyObject * |
| 1033 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 1034 | { |
| 1035 | PyObject *obj; |
| 1036 | PyCompactUnicodeObject *unicode; |
| 1037 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1038 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1039 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1040 | Py_ssize_t char_size; |
| 1041 | Py_ssize_t struct_size; |
| 1042 | |
| 1043 | /* Optimization for empty strings */ |
| 1044 | if (size == 0 && unicode_empty != NULL) { |
| 1045 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1046 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1049 | is_ascii = 0; |
| 1050 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1051 | struct_size = sizeof(PyCompactUnicodeObject); |
| 1052 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1053 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1054 | char_size = 1; |
| 1055 | is_ascii = 1; |
| 1056 | struct_size = sizeof(PyASCIIObject); |
| 1057 | } |
| 1058 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1059 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1060 | char_size = 1; |
| 1061 | } |
| 1062 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1063 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1064 | char_size = 2; |
| 1065 | if (sizeof(wchar_t) == 2) |
| 1066 | is_sharing = 1; |
| 1067 | } |
| 1068 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1069 | if (maxchar > MAX_UNICODE) { |
| 1070 | PyErr_SetString(PyExc_SystemError, |
| 1071 | "invalid maximum character passed to PyUnicode_New"); |
| 1072 | return NULL; |
| 1073 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1074 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1075 | char_size = 4; |
| 1076 | if (sizeof(wchar_t) == 4) |
| 1077 | is_sharing = 1; |
| 1078 | } |
| 1079 | |
| 1080 | /* Ensure we won't overflow the size. */ |
| 1081 | if (size < 0) { |
| 1082 | PyErr_SetString(PyExc_SystemError, |
| 1083 | "Negative size passed to PyUnicode_New"); |
| 1084 | return NULL; |
| 1085 | } |
| 1086 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1087 | return PyErr_NoMemory(); |
| 1088 | |
| 1089 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1090 | * PyObject_New() so we are able to allocate space for the object and |
| 1091 | * it's data buffer. |
| 1092 | */ |
| 1093 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1094 | if (obj == NULL) |
| 1095 | return PyErr_NoMemory(); |
| 1096 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1097 | if (obj == NULL) |
| 1098 | return NULL; |
| 1099 | |
| 1100 | unicode = (PyCompactUnicodeObject *)obj; |
| 1101 | if (is_ascii) |
| 1102 | data = ((PyASCIIObject*)obj) + 1; |
| 1103 | else |
| 1104 | data = unicode + 1; |
| 1105 | _PyUnicode_LENGTH(unicode) = size; |
| 1106 | _PyUnicode_HASH(unicode) = -1; |
| 1107 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1108 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1109 | _PyUnicode_STATE(unicode).compact = 1; |
| 1110 | _PyUnicode_STATE(unicode).ready = 1; |
| 1111 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1112 | if (is_ascii) { |
| 1113 | ((char*)data)[size] = 0; |
| 1114 | _PyUnicode_WSTR(unicode) = NULL; |
| 1115 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1116 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1117 | ((char*)data)[size] = 0; |
| 1118 | _PyUnicode_WSTR(unicode) = NULL; |
| 1119 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1121 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1122 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1123 | else { |
| 1124 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1125 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1126 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1127 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1128 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1129 | ((Py_UCS4*)data)[size] = 0; |
| 1130 | if (is_sharing) { |
| 1131 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1132 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1133 | } |
| 1134 | else { |
| 1135 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1136 | _PyUnicode_WSTR(unicode) = NULL; |
| 1137 | } |
| 1138 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1139 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1140 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1141 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1142 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1143 | return obj; |
| 1144 | } |
| 1145 | |
| 1146 | #if SIZEOF_WCHAR_T == 2 |
| 1147 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1148 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1149 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | |
| 1151 | This function assumes that unicode can hold one more code point than wstr |
| 1152 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1153 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1154 | 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] | 1155 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | { |
| 1157 | const wchar_t *iter; |
| 1158 | Py_UCS4 *ucs4_out; |
| 1159 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1160 | assert(unicode != NULL); |
| 1161 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1162 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1163 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1164 | |
| 1165 | for (iter = begin; iter < end; ) { |
| 1166 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1167 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1168 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1169 | && (iter+1) < end |
| 1170 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1171 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1172 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1173 | iter += 2; |
| 1174 | } |
| 1175 | else { |
| 1176 | *ucs4_out++ = *iter; |
| 1177 | iter++; |
| 1178 | } |
| 1179 | } |
| 1180 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1181 | _PyUnicode_GET_LENGTH(unicode))); |
| 1182 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1183 | } |
| 1184 | #endif |
| 1185 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1186 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1187 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1188 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1189 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1190 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1191 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1192 | return -1; |
| 1193 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1194 | return 0; |
| 1195 | } |
| 1196 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1197 | static int |
| 1198 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1199 | PyObject *from, Py_ssize_t from_start, |
| 1200 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1201 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1202 | unsigned int from_kind, to_kind; |
| 1203 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1204 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1205 | assert(0 <= how_many); |
| 1206 | assert(0 <= from_start); |
| 1207 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1208 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1209 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1210 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1211 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1212 | assert(PyUnicode_Check(to)); |
| 1213 | assert(PyUnicode_IS_READY(to)); |
| 1214 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1215 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1216 | if (how_many == 0) |
| 1217 | return 0; |
| 1218 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1219 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1220 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1221 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1222 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1223 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1224 | #ifdef Py_DEBUG |
| 1225 | if (!check_maxchar |
| 1226 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1227 | { |
| 1228 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1229 | Py_UCS4 ch; |
| 1230 | Py_ssize_t i; |
| 1231 | for (i=0; i < how_many; i++) { |
| 1232 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1233 | assert(ch <= to_maxchar); |
| 1234 | } |
| 1235 | } |
| 1236 | #endif |
| 1237 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1238 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1239 | if (check_maxchar |
| 1240 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1241 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1242 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1243 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1244 | Py_UCS4 max_char; |
| 1245 | max_char = ucs1lib_find_max_char(from_data, |
| 1246 | (Py_UCS1*)from_data + how_many); |
| 1247 | if (max_char >= 128) |
| 1248 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1249 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1250 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1251 | (char*)from_data + from_kind * from_start, |
| 1252 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1253 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1254 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1255 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1256 | { |
| 1257 | _PyUnicode_CONVERT_BYTES( |
| 1258 | Py_UCS1, Py_UCS2, |
| 1259 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1260 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1261 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1262 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1263 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1264 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1265 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1266 | { |
| 1267 | _PyUnicode_CONVERT_BYTES( |
| 1268 | Py_UCS1, Py_UCS4, |
| 1269 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1270 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1271 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1272 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1273 | } |
| 1274 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1275 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1276 | { |
| 1277 | _PyUnicode_CONVERT_BYTES( |
| 1278 | Py_UCS2, Py_UCS4, |
| 1279 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1280 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1281 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1282 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1283 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1284 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1285 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1286 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1287 | if (!check_maxchar) { |
| 1288 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1289 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1290 | { |
| 1291 | _PyUnicode_CONVERT_BYTES( |
| 1292 | Py_UCS2, Py_UCS1, |
| 1293 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1294 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1295 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1296 | ); |
| 1297 | } |
| 1298 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1299 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1300 | { |
| 1301 | _PyUnicode_CONVERT_BYTES( |
| 1302 | Py_UCS4, Py_UCS1, |
| 1303 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1304 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1305 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1306 | ); |
| 1307 | } |
| 1308 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1309 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1310 | { |
| 1311 | _PyUnicode_CONVERT_BYTES( |
| 1312 | Py_UCS4, Py_UCS2, |
| 1313 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1314 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1315 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1316 | ); |
| 1317 | } |
| 1318 | else { |
| 1319 | assert(0); |
| 1320 | return -1; |
| 1321 | } |
| 1322 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1323 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1324 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1325 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1326 | Py_ssize_t i; |
| 1327 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1328 | for (i=0; i < how_many; i++) { |
| 1329 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1330 | if (ch > to_maxchar) |
| 1331 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1332 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1333 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1334 | } |
| 1335 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1336 | return 0; |
| 1337 | } |
| 1338 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1339 | void |
| 1340 | _PyUnicode_FastCopyCharacters( |
| 1341 | PyObject *to, Py_ssize_t to_start, |
| 1342 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1343 | { |
| 1344 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1345 | } |
| 1346 | |
| 1347 | Py_ssize_t |
| 1348 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1349 | PyObject *from, Py_ssize_t from_start, |
| 1350 | Py_ssize_t how_many) |
| 1351 | { |
| 1352 | int err; |
| 1353 | |
| 1354 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1355 | PyErr_BadInternalCall(); |
| 1356 | return -1; |
| 1357 | } |
| 1358 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1359 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1360 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1361 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1362 | return -1; |
| 1363 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1364 | if (from_start < 0) { |
| 1365 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1366 | return -1; |
| 1367 | } |
| 1368 | if (to_start < 0) { |
| 1369 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1370 | return -1; |
| 1371 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1372 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1373 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1374 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 1375 | "Cannot write %zi characters at %zi " |
| 1376 | "in a string of %zi characters", |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1377 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1378 | return -1; |
| 1379 | } |
| 1380 | |
| 1381 | if (how_many == 0) |
| 1382 | return 0; |
| 1383 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1384 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1385 | return -1; |
| 1386 | |
| 1387 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1388 | if (err) { |
| 1389 | PyErr_Format(PyExc_SystemError, |
| 1390 | "Cannot copy %s characters " |
| 1391 | "into a string of %s characters", |
| 1392 | unicode_kind_name(from), |
| 1393 | unicode_kind_name(to)); |
| 1394 | return -1; |
| 1395 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1396 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1399 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1400 | correct string length can be computed before converting a string to UCS4. |
| 1401 | This function counts single surrogates as a character and not as a pair. |
| 1402 | |
| 1403 | Return 0 on success, or -1 on error. */ |
| 1404 | static int |
| 1405 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1406 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1407 | { |
| 1408 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1409 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1410 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1411 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1412 | *num_surrogates = 0; |
| 1413 | *maxchar = 0; |
| 1414 | |
| 1415 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1416 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | cf77da9 | 2013-03-06 01:09:24 +0100 | [diff] [blame] | 1417 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1418 | && (iter+1) < end |
| 1419 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
| 1420 | { |
| 1421 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
| 1422 | ++(*num_surrogates); |
| 1423 | iter += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1424 | } |
| 1425 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1426 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1427 | { |
| 1428 | ch = *iter; |
| 1429 | iter++; |
| 1430 | } |
| 1431 | if (ch > *maxchar) { |
| 1432 | *maxchar = ch; |
| 1433 | if (*maxchar > MAX_UNICODE) { |
| 1434 | PyErr_Format(PyExc_ValueError, |
| 1435 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1436 | ch); |
| 1437 | return -1; |
| 1438 | } |
| 1439 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1440 | } |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1444 | int |
| 1445 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1446 | { |
| 1447 | wchar_t *end; |
| 1448 | Py_UCS4 maxchar = 0; |
| 1449 | Py_ssize_t num_surrogates; |
| 1450 | #if SIZEOF_WCHAR_T == 2 |
| 1451 | Py_ssize_t length_wo_surrogates; |
| 1452 | #endif |
| 1453 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1454 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1455 | strings were created using _PyObject_New() and where no canonical |
| 1456 | representation (the str field) has been set yet aka strings |
| 1457 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1458 | assert(_PyUnicode_CHECK(unicode)); |
| 1459 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1460 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1461 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1462 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1463 | /* Actually, it should neither be interned nor be anything else: */ |
| 1464 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1466 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1467 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1468 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1469 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1470 | |
| 1471 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1472 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1473 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1474 | PyErr_NoMemory(); |
| 1475 | return -1; |
| 1476 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1477 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1478 | _PyUnicode_WSTR(unicode), end, |
| 1479 | PyUnicode_1BYTE_DATA(unicode)); |
| 1480 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1481 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1482 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1483 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1484 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1485 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1486 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1487 | } |
| 1488 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1489 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1490 | _PyUnicode_UTF8(unicode) = NULL; |
| 1491 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1492 | } |
| 1493 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1494 | _PyUnicode_WSTR(unicode) = NULL; |
| 1495 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1496 | } |
| 1497 | /* In this case we might have to convert down from 4-byte native |
| 1498 | wchar_t to 2-byte unicode. */ |
| 1499 | else if (maxchar < 65536) { |
| 1500 | assert(num_surrogates == 0 && |
| 1501 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1502 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1503 | #if SIZEOF_WCHAR_T == 2 |
| 1504 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1505 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1506 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1507 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1508 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1509 | _PyUnicode_UTF8(unicode) = NULL; |
| 1510 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1511 | #else |
| 1512 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1513 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1514 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1515 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1516 | PyErr_NoMemory(); |
| 1517 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1518 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1519 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1520 | _PyUnicode_WSTR(unicode), end, |
| 1521 | PyUnicode_2BYTE_DATA(unicode)); |
| 1522 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1523 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1524 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1525 | _PyUnicode_UTF8(unicode) = NULL; |
| 1526 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1527 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1528 | _PyUnicode_WSTR(unicode) = NULL; |
| 1529 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1530 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1531 | } |
| 1532 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1533 | else { |
| 1534 | #if SIZEOF_WCHAR_T == 2 |
| 1535 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1536 | new normalized 4-byte version. */ |
| 1537 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 1538 | if (length_wo_surrogates > PY_SSIZE_T_MAX / 4 - 1) { |
| 1539 | PyErr_NoMemory(); |
| 1540 | return -1; |
| 1541 | } |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1542 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1543 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1544 | PyErr_NoMemory(); |
| 1545 | return -1; |
| 1546 | } |
| 1547 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1548 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1549 | _PyUnicode_UTF8(unicode) = NULL; |
| 1550 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1551 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1552 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1553 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1554 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1555 | _PyUnicode_WSTR(unicode) = NULL; |
| 1556 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1557 | #else |
| 1558 | assert(num_surrogates == 0); |
| 1559 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1560 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1561 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1562 | _PyUnicode_UTF8(unicode) = NULL; |
| 1563 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1564 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1565 | #endif |
| 1566 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1567 | } |
| 1568 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1569 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
| 1572 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1573 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1574 | unicode_dealloc(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1575 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1576 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1577 | case SSTATE_NOT_INTERNED: |
| 1578 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1579 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1580 | case SSTATE_INTERNED_MORTAL: |
| 1581 | /* revive dead object temporarily for DelItem */ |
| 1582 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1583 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1584 | Py_FatalError( |
| 1585 | "deletion of interned string failed"); |
| 1586 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1587 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1588 | case SSTATE_INTERNED_IMMORTAL: |
| 1589 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1590 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1591 | default: |
| 1592 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1595 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1596 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1597 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1598 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1599 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1600 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1601 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1602 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1605 | #ifdef Py_DEBUG |
| 1606 | static int |
| 1607 | unicode_is_singleton(PyObject *unicode) |
| 1608 | { |
| 1609 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1610 | if (unicode == unicode_empty) |
| 1611 | return 1; |
| 1612 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1613 | { |
| 1614 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1615 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1616 | return 1; |
| 1617 | } |
| 1618 | return 0; |
| 1619 | } |
| 1620 | #endif |
| 1621 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1622 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1623 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1624 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1625 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1626 | if (Py_REFCNT(unicode) != 1) |
| 1627 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1628 | if (_PyUnicode_HASH(unicode) != -1) |
| 1629 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1630 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1631 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1632 | if (!PyUnicode_CheckExact(unicode)) |
| 1633 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1634 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1635 | /* singleton refcount is greater than 1 */ |
| 1636 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1637 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1638 | return 1; |
| 1639 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1640 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1641 | static int |
| 1642 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1643 | { |
| 1644 | PyObject *unicode; |
| 1645 | Py_ssize_t old_length; |
| 1646 | |
| 1647 | assert(p_unicode != NULL); |
| 1648 | unicode = *p_unicode; |
| 1649 | |
| 1650 | assert(unicode != NULL); |
| 1651 | assert(PyUnicode_Check(unicode)); |
| 1652 | assert(0 <= length); |
| 1653 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1654 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1655 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1656 | else |
| 1657 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1658 | if (old_length == length) |
| 1659 | return 0; |
| 1660 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1661 | if (length == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1662 | _Py_INCREF_UNICODE_EMPTY(); |
| 1663 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1664 | return -1; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1665 | Py_DECREF(*p_unicode); |
| 1666 | *p_unicode = unicode_empty; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1667 | return 0; |
| 1668 | } |
| 1669 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1670 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1671 | PyObject *copy = resize_copy(unicode, length); |
| 1672 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1673 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1674 | Py_DECREF(*p_unicode); |
| 1675 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1676 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1679 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1680 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1681 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1682 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1683 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1684 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1685 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1686 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1689 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1690 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1691 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1692 | PyObject *unicode; |
| 1693 | if (p_unicode == NULL) { |
| 1694 | PyErr_BadInternalCall(); |
| 1695 | return -1; |
| 1696 | } |
| 1697 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1698 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1699 | { |
| 1700 | PyErr_BadInternalCall(); |
| 1701 | return -1; |
| 1702 | } |
| 1703 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1704 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1705 | |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame^] | 1706 | /* Copy an ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1707 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1708 | WARNING: The function doesn't copy the terminating null character and |
| 1709 | doesn't check the maximum character (may write a latin1 character in an |
| 1710 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1711 | static void |
| 1712 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1713 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1714 | { |
| 1715 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1716 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1717 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1718 | |
| 1719 | switch (kind) { |
| 1720 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1721 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1722 | #ifdef Py_DEBUG |
| 1723 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1724 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1725 | (const Py_UCS1*)str, |
| 1726 | (const Py_UCS1*)str + len); |
| 1727 | assert(maxchar < 128); |
| 1728 | } |
| 1729 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1730 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1731 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1732 | } |
| 1733 | case PyUnicode_2BYTE_KIND: { |
| 1734 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1735 | Py_UCS2 *ucs2 = start; |
| 1736 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1737 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1738 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1739 | *ucs2 = (Py_UCS2)*str; |
| 1740 | |
| 1741 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1742 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1743 | } |
| 1744 | default: { |
| 1745 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1746 | Py_UCS4 *ucs4 = start; |
| 1747 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1748 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1749 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1750 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1751 | *ucs4 = (Py_UCS4)*str; |
| 1752 | |
| 1753 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | static PyObject* |
| 1759 | get_latin1_char(unsigned char ch) |
| 1760 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1761 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1763 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1764 | if (!unicode) |
| 1765 | return NULL; |
| 1766 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1767 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1768 | unicode_latin1[ch] = unicode; |
| 1769 | } |
| 1770 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1771 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1774 | static PyObject* |
| 1775 | unicode_char(Py_UCS4 ch) |
| 1776 | { |
| 1777 | PyObject *unicode; |
| 1778 | |
| 1779 | assert(ch <= MAX_UNICODE); |
| 1780 | |
Victor Stinner | f3b46b4 | 2014-01-03 13:16:00 +0100 | [diff] [blame] | 1781 | if (ch < 256) |
| 1782 | return get_latin1_char(ch); |
| 1783 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1784 | unicode = PyUnicode_New(1, ch); |
| 1785 | if (unicode == NULL) |
| 1786 | return NULL; |
| 1787 | switch (PyUnicode_KIND(unicode)) { |
| 1788 | case PyUnicode_1BYTE_KIND: |
| 1789 | PyUnicode_1BYTE_DATA(unicode)[0] = (Py_UCS1)ch; |
| 1790 | break; |
| 1791 | case PyUnicode_2BYTE_KIND: |
| 1792 | PyUnicode_2BYTE_DATA(unicode)[0] = (Py_UCS2)ch; |
| 1793 | break; |
| 1794 | default: |
| 1795 | assert(PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1796 | PyUnicode_4BYTE_DATA(unicode)[0] = ch; |
| 1797 | } |
| 1798 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1799 | return unicode; |
| 1800 | } |
| 1801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1802 | PyObject * |
| 1803 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1804 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1805 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1806 | Py_UCS4 maxchar = 0; |
| 1807 | Py_ssize_t num_surrogates; |
| 1808 | |
| 1809 | if (u == NULL) |
| 1810 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1811 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1812 | /* If the Unicode data is known at construction time, we can apply |
| 1813 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1815 | /* Optimization for empty strings */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1816 | if (size == 0) |
| 1817 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1818 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1819 | /* Single character Unicode objects in the Latin-1 range are |
| 1820 | shared when using this constructor */ |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1821 | if (size == 1 && (Py_UCS4)*u < 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1822 | return get_latin1_char((unsigned char)*u); |
| 1823 | |
| 1824 | /* If not empty and not single character, copy the Unicode data |
| 1825 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1826 | if (find_maxchar_surrogates(u, u + size, |
| 1827 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1828 | return NULL; |
| 1829 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1830 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1831 | if (!unicode) |
| 1832 | return NULL; |
| 1833 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1834 | switch (PyUnicode_KIND(unicode)) { |
| 1835 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1836 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1837 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1838 | break; |
| 1839 | case PyUnicode_2BYTE_KIND: |
| 1840 | #if Py_UNICODE_SIZE == 2 |
| 1841 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1842 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1843 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1844 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1845 | #endif |
| 1846 | break; |
| 1847 | case PyUnicode_4BYTE_KIND: |
| 1848 | #if SIZEOF_WCHAR_T == 2 |
| 1849 | /* This is the only case which has to process surrogates, thus |
| 1850 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1851 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1852 | #else |
| 1853 | assert(num_surrogates == 0); |
| 1854 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1855 | #endif |
| 1856 | break; |
| 1857 | default: |
| 1858 | assert(0 && "Impossible state"); |
| 1859 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1860 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1861 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1864 | PyObject * |
| 1865 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1866 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1867 | if (size < 0) { |
| 1868 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1869 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1870 | return NULL; |
| 1871 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1872 | if (u != NULL) |
| 1873 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1874 | else |
| 1875 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1878 | PyObject * |
| 1879 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1880 | { |
| 1881 | size_t size = strlen(u); |
| 1882 | if (size > PY_SSIZE_T_MAX) { |
| 1883 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1884 | return NULL; |
| 1885 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1886 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1889 | PyObject * |
| 1890 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1891 | { |
| 1892 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1893 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1894 | strlen(id->string), |
| 1895 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1896 | if (!id->object) |
| 1897 | return NULL; |
| 1898 | PyUnicode_InternInPlace(&id->object); |
| 1899 | assert(!id->next); |
| 1900 | id->next = static_strings; |
| 1901 | static_strings = id; |
| 1902 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1903 | return id->object; |
| 1904 | } |
| 1905 | |
| 1906 | void |
| 1907 | _PyUnicode_ClearStaticStrings() |
| 1908 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1909 | _Py_Identifier *tmp, *s = static_strings; |
| 1910 | while (s) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 1911 | Py_CLEAR(s->object); |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1912 | tmp = s->next; |
| 1913 | s->next = NULL; |
| 1914 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1915 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1916 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1917 | } |
| 1918 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1919 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1920 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1921 | PyObject* |
| 1922 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1923 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1924 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1925 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1926 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1927 | #ifdef Py_DEBUG |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1928 | assert((unsigned char)s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1929 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1930 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1931 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1932 | unicode = PyUnicode_New(size, 127); |
| 1933 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1934 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1935 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1936 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1937 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1938 | } |
| 1939 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1940 | static Py_UCS4 |
| 1941 | kind_maxchar_limit(unsigned int kind) |
| 1942 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1943 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1944 | case PyUnicode_1BYTE_KIND: |
| 1945 | return 0x80; |
| 1946 | case PyUnicode_2BYTE_KIND: |
| 1947 | return 0x100; |
| 1948 | case PyUnicode_4BYTE_KIND: |
| 1949 | return 0x10000; |
| 1950 | default: |
| 1951 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1952 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1953 | } |
| 1954 | } |
| 1955 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1956 | Py_LOCAL_INLINE(Py_UCS4) |
| 1957 | align_maxchar(Py_UCS4 maxchar) |
| 1958 | { |
| 1959 | if (maxchar <= 127) |
| 1960 | return 127; |
| 1961 | else if (maxchar <= 255) |
| 1962 | return 255; |
| 1963 | else if (maxchar <= 65535) |
| 1964 | return 65535; |
| 1965 | else |
| 1966 | return MAX_UNICODE; |
| 1967 | } |
| 1968 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1969 | static PyObject* |
Victor Stinner | d21b58c | 2013-02-26 00:15:54 +0100 | [diff] [blame] | 1970 | _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1971 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1972 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1973 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1974 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1975 | if (size == 0) |
| 1976 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1977 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1978 | if (size == 1) |
| 1979 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1980 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1981 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1982 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1983 | if (!res) |
| 1984 | return NULL; |
| 1985 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1986 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1987 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1990 | static PyObject* |
| 1991 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | { |
| 1993 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1994 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1995 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 1996 | if (size == 0) |
| 1997 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1998 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 1999 | if (size == 1) |
| 2000 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2001 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2002 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2003 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2004 | if (!res) |
| 2005 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2006 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2007 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2008 | else { |
| 2009 | _PyUnicode_CONVERT_BYTES( |
| 2010 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 2011 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2012 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2013 | return res; |
| 2014 | } |
| 2015 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2016 | static PyObject* |
| 2017 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2018 | { |
| 2019 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2020 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2021 | |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2022 | if (size == 0) |
| 2023 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2024 | assert(size > 0); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2025 | if (size == 1) |
| 2026 | return unicode_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2027 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2028 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2029 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2030 | if (!res) |
| 2031 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2032 | if (max_char < 256) |
| 2033 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 2034 | PyUnicode_1BYTE_DATA(res)); |
| 2035 | else if (max_char < 0x10000) |
| 2036 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 2037 | PyUnicode_2BYTE_DATA(res)); |
| 2038 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2039 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2040 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2041 | return res; |
| 2042 | } |
| 2043 | |
| 2044 | PyObject* |
| 2045 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 2046 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 2047 | if (size < 0) { |
| 2048 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 2049 | return NULL; |
| 2050 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2051 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2052 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2053 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2054 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2055 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2056 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2057 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2058 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2059 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2060 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2061 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2062 | } |
| 2063 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2064 | Py_UCS4 |
| 2065 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2066 | { |
| 2067 | enum PyUnicode_Kind kind; |
| 2068 | void *startptr, *endptr; |
| 2069 | |
| 2070 | assert(PyUnicode_IS_READY(unicode)); |
| 2071 | assert(0 <= start); |
| 2072 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2073 | assert(start <= end); |
| 2074 | |
| 2075 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2076 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2077 | |
| 2078 | if (start == end) |
| 2079 | return 127; |
| 2080 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2081 | if (PyUnicode_IS_ASCII(unicode)) |
| 2082 | return 127; |
| 2083 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2084 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2085 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2086 | endptr = (char *)startptr + end * kind; |
| 2087 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2088 | switch(kind) { |
| 2089 | case PyUnicode_1BYTE_KIND: |
| 2090 | return ucs1lib_find_max_char(startptr, endptr); |
| 2091 | case PyUnicode_2BYTE_KIND: |
| 2092 | return ucs2lib_find_max_char(startptr, endptr); |
| 2093 | case PyUnicode_4BYTE_KIND: |
| 2094 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2095 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2096 | assert(0); |
| 2097 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2098 | } |
| 2099 | } |
| 2100 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2101 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2102 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2103 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2104 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2105 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2106 | { |
| 2107 | PyObject *unicode, *copy; |
| 2108 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2109 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2110 | unsigned int kind; |
| 2111 | |
| 2112 | assert(p_unicode != NULL); |
| 2113 | unicode = *p_unicode; |
| 2114 | assert(PyUnicode_IS_READY(unicode)); |
| 2115 | if (PyUnicode_IS_ASCII(unicode)) |
| 2116 | return; |
| 2117 | |
| 2118 | len = PyUnicode_GET_LENGTH(unicode); |
| 2119 | kind = PyUnicode_KIND(unicode); |
| 2120 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2121 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2122 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2123 | if (max_char >= 128) |
| 2124 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2125 | } |
| 2126 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2127 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2128 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2129 | if (max_char >= 256) |
| 2130 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2131 | } |
| 2132 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2133 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2134 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2135 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2136 | if (max_char >= 0x10000) |
| 2137 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2138 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2139 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2140 | if (copy != NULL) |
| 2141 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2142 | Py_DECREF(unicode); |
| 2143 | *p_unicode = copy; |
| 2144 | } |
| 2145 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2146 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2147 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2148 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2149 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2150 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2151 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2152 | if (!PyUnicode_Check(unicode)) { |
| 2153 | PyErr_BadInternalCall(); |
| 2154 | return NULL; |
| 2155 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2156 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2157 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2158 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2159 | length = PyUnicode_GET_LENGTH(unicode); |
| 2160 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2161 | if (!copy) |
| 2162 | return NULL; |
| 2163 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2164 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2165 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2166 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2167 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2168 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2169 | } |
| 2170 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2172 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2173 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2174 | |
| 2175 | void* |
| 2176 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2177 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2178 | Py_ssize_t len; |
| 2179 | void *result; |
| 2180 | unsigned int skind; |
| 2181 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2182 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2183 | return NULL; |
| 2184 | |
| 2185 | len = PyUnicode_GET_LENGTH(s); |
| 2186 | skind = PyUnicode_KIND(s); |
| 2187 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2188 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2189 | return NULL; |
| 2190 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2191 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2192 | case PyUnicode_2BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2193 | result = PyMem_New(Py_UCS2, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2194 | if (!result) |
| 2195 | return PyErr_NoMemory(); |
| 2196 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2197 | _PyUnicode_CONVERT_BYTES( |
| 2198 | Py_UCS1, Py_UCS2, |
| 2199 | PyUnicode_1BYTE_DATA(s), |
| 2200 | PyUnicode_1BYTE_DATA(s) + len, |
| 2201 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2202 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2203 | case PyUnicode_4BYTE_KIND: |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2204 | result = PyMem_New(Py_UCS4, len); |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2205 | if (!result) |
| 2206 | return PyErr_NoMemory(); |
| 2207 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2208 | _PyUnicode_CONVERT_BYTES( |
| 2209 | Py_UCS2, Py_UCS4, |
| 2210 | PyUnicode_2BYTE_DATA(s), |
| 2211 | PyUnicode_2BYTE_DATA(s) + len, |
| 2212 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2213 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2214 | else { |
| 2215 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2216 | _PyUnicode_CONVERT_BYTES( |
| 2217 | Py_UCS1, Py_UCS4, |
| 2218 | PyUnicode_1BYTE_DATA(s), |
| 2219 | PyUnicode_1BYTE_DATA(s) + len, |
| 2220 | result); |
| 2221 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2222 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2223 | default: |
| 2224 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2225 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2226 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2227 | return NULL; |
| 2228 | } |
| 2229 | |
| 2230 | static Py_UCS4* |
| 2231 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2232 | int copy_null) |
| 2233 | { |
| 2234 | int kind; |
| 2235 | void *data; |
| 2236 | Py_ssize_t len, targetlen; |
| 2237 | if (PyUnicode_READY(string) == -1) |
| 2238 | return NULL; |
| 2239 | kind = PyUnicode_KIND(string); |
| 2240 | data = PyUnicode_DATA(string); |
| 2241 | len = PyUnicode_GET_LENGTH(string); |
| 2242 | targetlen = len; |
| 2243 | if (copy_null) |
| 2244 | targetlen++; |
| 2245 | if (!target) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2246 | target = PyMem_New(Py_UCS4, targetlen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2247 | if (!target) { |
| 2248 | PyErr_NoMemory(); |
| 2249 | return NULL; |
| 2250 | } |
| 2251 | } |
| 2252 | else { |
| 2253 | if (targetsize < targetlen) { |
| 2254 | PyErr_Format(PyExc_SystemError, |
| 2255 | "string is longer than the buffer"); |
| 2256 | if (copy_null && 0 < targetsize) |
| 2257 | target[0] = 0; |
| 2258 | return NULL; |
| 2259 | } |
| 2260 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2261 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2262 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2263 | _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] | 2264 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2265 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2266 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2267 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2268 | } |
| 2269 | else { |
| 2270 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2271 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2272 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2273 | if (copy_null) |
| 2274 | target[len] = 0; |
| 2275 | return target; |
| 2276 | } |
| 2277 | |
| 2278 | Py_UCS4* |
| 2279 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2280 | int copy_null) |
| 2281 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2282 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2283 | PyErr_BadInternalCall(); |
| 2284 | return NULL; |
| 2285 | } |
| 2286 | return as_ucs4(string, target, targetsize, copy_null); |
| 2287 | } |
| 2288 | |
| 2289 | Py_UCS4* |
| 2290 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2291 | { |
| 2292 | return as_ucs4(string, NULL, 0, 1); |
| 2293 | } |
| 2294 | |
| 2295 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2296 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2297 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2298 | PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2299 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | if (w == NULL) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2301 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 2302 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2303 | PyErr_BadInternalCall(); |
| 2304 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2307 | if (size == -1) { |
| 2308 | size = wcslen(w); |
| 2309 | } |
| 2310 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2311 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2314 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2315 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2316 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2317 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2318 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2319 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2320 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2321 | if (longflag) |
| 2322 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2323 | else if (longlongflag) { |
| 2324 | /* longlongflag should only ever be nonzero on machines with |
| 2325 | HAVE_LONG_LONG defined */ |
| 2326 | #ifdef HAVE_LONG_LONG |
| 2327 | char *f = PY_FORMAT_LONG_LONG; |
| 2328 | while (*f) |
| 2329 | *fmt++ = *f++; |
| 2330 | #else |
| 2331 | /* we shouldn't ever get here */ |
| 2332 | assert(0); |
| 2333 | *fmt++ = 'l'; |
| 2334 | #endif |
| 2335 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2336 | else if (size_tflag) { |
| 2337 | char *f = PY_FORMAT_SIZE_T; |
| 2338 | while (*f) |
| 2339 | *fmt++ = *f++; |
| 2340 | } |
| 2341 | *fmt++ = c; |
| 2342 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2345 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2346 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2347 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2348 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2349 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2350 | static int |
| 2351 | unicode_fromformat_write_str(_PyUnicodeWriter *writer, PyObject *str, |
| 2352 | Py_ssize_t width, Py_ssize_t precision) |
| 2353 | { |
| 2354 | Py_ssize_t length, fill, arglen; |
| 2355 | Py_UCS4 maxchar; |
| 2356 | |
| 2357 | if (PyUnicode_READY(str) == -1) |
| 2358 | return -1; |
| 2359 | |
| 2360 | length = PyUnicode_GET_LENGTH(str); |
| 2361 | if ((precision == -1 || precision >= length) |
| 2362 | && width <= length) |
| 2363 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 2364 | |
| 2365 | if (precision != -1) |
| 2366 | length = Py_MIN(precision, length); |
| 2367 | |
| 2368 | arglen = Py_MAX(length, width); |
| 2369 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 2370 | maxchar = _PyUnicode_FindMaxChar(str, 0, length); |
| 2371 | else |
| 2372 | maxchar = writer->maxchar; |
| 2373 | |
| 2374 | if (_PyUnicodeWriter_Prepare(writer, arglen, maxchar) == -1) |
| 2375 | return -1; |
| 2376 | |
| 2377 | if (width > length) { |
| 2378 | fill = width - length; |
| 2379 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, ' ') == -1) |
| 2380 | return -1; |
| 2381 | writer->pos += fill; |
| 2382 | } |
| 2383 | |
| 2384 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 2385 | str, 0, length); |
| 2386 | writer->pos += length; |
| 2387 | return 0; |
| 2388 | } |
| 2389 | |
| 2390 | static int |
| 2391 | unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str, |
| 2392 | Py_ssize_t width, Py_ssize_t precision) |
| 2393 | { |
| 2394 | /* UTF-8 */ |
| 2395 | Py_ssize_t length; |
| 2396 | PyObject *unicode; |
| 2397 | int res; |
| 2398 | |
| 2399 | length = strlen(str); |
| 2400 | if (precision != -1) |
| 2401 | length = Py_MIN(length, precision); |
| 2402 | unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL); |
| 2403 | if (unicode == NULL) |
| 2404 | return -1; |
| 2405 | |
| 2406 | res = unicode_fromformat_write_str(writer, unicode, width, -1); |
| 2407 | Py_DECREF(unicode); |
| 2408 | return res; |
| 2409 | } |
| 2410 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2411 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2412 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2413 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2414 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2415 | const char *p; |
| 2416 | Py_ssize_t len; |
| 2417 | int zeropad; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2418 | Py_ssize_t width; |
| 2419 | Py_ssize_t precision; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2420 | int longflag; |
| 2421 | int longlongflag; |
| 2422 | int size_tflag; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2423 | Py_ssize_t fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2424 | |
| 2425 | p = f; |
| 2426 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2427 | zeropad = 0; |
| 2428 | if (*f == '0') { |
| 2429 | zeropad = 1; |
| 2430 | f++; |
| 2431 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2432 | |
| 2433 | /* 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] | 2434 | width = -1; |
| 2435 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2436 | width = *f - '0'; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2437 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2438 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2439 | if (width > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2440 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2441 | "width too big"); |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2442 | return NULL; |
| 2443 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2444 | width = (width * 10) + (*f - '0'); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2445 | f++; |
| 2446 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2447 | } |
| 2448 | precision = -1; |
| 2449 | if (*f == '.') { |
| 2450 | f++; |
| 2451 | if (Py_ISDIGIT((unsigned)*f)) { |
| 2452 | precision = (*f - '0'); |
| 2453 | f++; |
| 2454 | while (Py_ISDIGIT((unsigned)*f)) { |
| 2455 | if (precision > (PY_SSIZE_T_MAX - ((int)*f - '0')) / 10) { |
| 2456 | PyErr_SetString(PyExc_ValueError, |
| 2457 | "precision too big"); |
| 2458 | return NULL; |
| 2459 | } |
| 2460 | precision = (precision * 10) + (*f - '0'); |
| 2461 | f++; |
| 2462 | } |
| 2463 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2464 | if (*f == '%') { |
| 2465 | /* "%.3%s" => f points to "3" */ |
| 2466 | f--; |
| 2467 | } |
| 2468 | } |
| 2469 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2470 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2471 | f--; |
| 2472 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2473 | |
| 2474 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2475 | longflag = 0; |
| 2476 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2477 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2478 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2479 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2480 | longflag = 1; |
| 2481 | ++f; |
| 2482 | } |
| 2483 | #ifdef HAVE_LONG_LONG |
| 2484 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2485 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2486 | longlongflag = 1; |
| 2487 | f += 2; |
| 2488 | } |
| 2489 | #endif |
| 2490 | } |
| 2491 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2492 | 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] | 2493 | size_tflag = 1; |
| 2494 | ++f; |
| 2495 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2496 | |
| 2497 | if (f[1] == '\0') |
| 2498 | writer->overallocate = 0; |
| 2499 | |
| 2500 | switch (*f) { |
| 2501 | case 'c': |
| 2502 | { |
| 2503 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2504 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Serhiy Storchaka | c89533f | 2013-06-23 20:21:16 +0300 | [diff] [blame] | 2505 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2506 | "character argument not in range(0x110000)"); |
| 2507 | return NULL; |
| 2508 | } |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2509 | if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2510 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2511 | break; |
| 2512 | } |
| 2513 | |
| 2514 | case 'i': |
| 2515 | case 'd': |
| 2516 | case 'u': |
| 2517 | case 'x': |
| 2518 | { |
| 2519 | /* used by sprintf */ |
| 2520 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2521 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2522 | Py_ssize_t arglen; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2523 | |
| 2524 | if (*f == 'u') { |
| 2525 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2526 | |
| 2527 | if (longflag) |
| 2528 | len = sprintf(buffer, fmt, |
| 2529 | va_arg(*vargs, unsigned long)); |
| 2530 | #ifdef HAVE_LONG_LONG |
| 2531 | else if (longlongflag) |
| 2532 | len = sprintf(buffer, fmt, |
| 2533 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2534 | #endif |
| 2535 | else if (size_tflag) |
| 2536 | len = sprintf(buffer, fmt, |
| 2537 | va_arg(*vargs, size_t)); |
| 2538 | else |
| 2539 | len = sprintf(buffer, fmt, |
| 2540 | va_arg(*vargs, unsigned int)); |
| 2541 | } |
| 2542 | else if (*f == 'x') { |
| 2543 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2544 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2545 | } |
| 2546 | else { |
| 2547 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2548 | |
| 2549 | if (longflag) |
| 2550 | len = sprintf(buffer, fmt, |
| 2551 | va_arg(*vargs, long)); |
| 2552 | #ifdef HAVE_LONG_LONG |
| 2553 | else if (longlongflag) |
| 2554 | len = sprintf(buffer, fmt, |
| 2555 | va_arg(*vargs, PY_LONG_LONG)); |
| 2556 | #endif |
| 2557 | else if (size_tflag) |
| 2558 | len = sprintf(buffer, fmt, |
| 2559 | va_arg(*vargs, Py_ssize_t)); |
| 2560 | else |
| 2561 | len = sprintf(buffer, fmt, |
| 2562 | va_arg(*vargs, int)); |
| 2563 | } |
| 2564 | assert(len >= 0); |
| 2565 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2566 | if (precision < len) |
| 2567 | precision = len; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2568 | |
| 2569 | arglen = Py_MAX(precision, width); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2570 | if (_PyUnicodeWriter_Prepare(writer, arglen, 127) == -1) |
| 2571 | return NULL; |
| 2572 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2573 | if (width > precision) { |
| 2574 | Py_UCS4 fillchar; |
| 2575 | fill = width - precision; |
| 2576 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2577 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2578 | return NULL; |
| 2579 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2580 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2581 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2582 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2583 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2584 | return NULL; |
| 2585 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2586 | } |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2587 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2588 | if (_PyUnicodeWriter_WriteASCIIString(writer, buffer, len) < 0) |
| 2589 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | } |
| 2592 | |
| 2593 | case 'p': |
| 2594 | { |
| 2595 | char number[MAX_LONG_LONG_CHARS]; |
| 2596 | |
| 2597 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2598 | assert(len >= 0); |
| 2599 | |
| 2600 | /* %p is ill-defined: ensure leading 0x. */ |
| 2601 | if (number[1] == 'X') |
| 2602 | number[1] = 'x'; |
| 2603 | else if (number[1] != 'x') { |
| 2604 | memmove(number + 2, number, |
| 2605 | strlen(number) + 1); |
| 2606 | number[0] = '0'; |
| 2607 | number[1] = 'x'; |
| 2608 | len += 2; |
| 2609 | } |
| 2610 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2611 | if (_PyUnicodeWriter_WriteASCIIString(writer, number, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2612 | return NULL; |
| 2613 | break; |
| 2614 | } |
| 2615 | |
| 2616 | case 's': |
| 2617 | { |
| 2618 | /* UTF-8 */ |
| 2619 | const char *s = va_arg(*vargs, const char*); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2620 | if (unicode_fromformat_write_cstr(writer, s, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2621 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2622 | break; |
| 2623 | } |
| 2624 | |
| 2625 | case 'U': |
| 2626 | { |
| 2627 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2628 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2629 | |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2630 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2631 | return NULL; |
| 2632 | break; |
| 2633 | } |
| 2634 | |
| 2635 | case 'V': |
| 2636 | { |
| 2637 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2638 | const char *str = va_arg(*vargs, const char *); |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2639 | if (obj) { |
| 2640 | assert(_PyUnicode_CHECK(obj)); |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2641 | if (unicode_fromformat_write_str(writer, obj, width, precision) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2642 | return NULL; |
| 2643 | } |
| 2644 | else { |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2645 | assert(str != NULL); |
| 2646 | if (unicode_fromformat_write_cstr(writer, str, width, precision) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2647 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2648 | } |
| 2649 | break; |
| 2650 | } |
| 2651 | |
| 2652 | case 'S': |
| 2653 | { |
| 2654 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2655 | PyObject *str; |
| 2656 | assert(obj); |
| 2657 | str = PyObject_Str(obj); |
| 2658 | if (!str) |
| 2659 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2660 | if (unicode_fromformat_write_str(writer, str, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2661 | Py_DECREF(str); |
| 2662 | return NULL; |
| 2663 | } |
| 2664 | Py_DECREF(str); |
| 2665 | break; |
| 2666 | } |
| 2667 | |
| 2668 | case 'R': |
| 2669 | { |
| 2670 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2671 | PyObject *repr; |
| 2672 | assert(obj); |
| 2673 | repr = PyObject_Repr(obj); |
| 2674 | if (!repr) |
| 2675 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2676 | if (unicode_fromformat_write_str(writer, repr, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2677 | Py_DECREF(repr); |
| 2678 | return NULL; |
| 2679 | } |
| 2680 | Py_DECREF(repr); |
| 2681 | break; |
| 2682 | } |
| 2683 | |
| 2684 | case 'A': |
| 2685 | { |
| 2686 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2687 | PyObject *ascii; |
| 2688 | assert(obj); |
| 2689 | ascii = PyObject_ASCII(obj); |
| 2690 | if (!ascii) |
| 2691 | return NULL; |
Victor Stinner | 8cecc8c | 2013-05-06 23:11:54 +0200 | [diff] [blame] | 2692 | if (unicode_fromformat_write_str(writer, ascii, width, precision) == -1) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2693 | Py_DECREF(ascii); |
| 2694 | return NULL; |
| 2695 | } |
| 2696 | Py_DECREF(ascii); |
| 2697 | break; |
| 2698 | } |
| 2699 | |
| 2700 | case '%': |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 2701 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2702 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2703 | break; |
| 2704 | |
| 2705 | default: |
| 2706 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2707 | of the format string to the output string. (we cannot just |
| 2708 | skip the code, since there's no way to know what's in the |
| 2709 | argument list) */ |
| 2710 | len = strlen(p); |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2711 | if (_PyUnicodeWriter_WriteLatin1String(writer, p, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2712 | return NULL; |
| 2713 | f = p+len; |
| 2714 | return f; |
| 2715 | } |
| 2716 | |
| 2717 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2718 | return f; |
| 2719 | } |
| 2720 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2721 | PyObject * |
| 2722 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2723 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2724 | va_list vargs2; |
| 2725 | const char *f; |
| 2726 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2727 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 2728 | _PyUnicodeWriter_Init(&writer); |
| 2729 | writer.min_length = strlen(format) + 100; |
| 2730 | writer.overallocate = 1; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2731 | |
| 2732 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2733 | Copy it to be able to pass a reference to a subfunction. */ |
| 2734 | Py_VA_COPY(vargs2, vargs); |
| 2735 | |
| 2736 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2737 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2738 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2739 | if (f == NULL) |
| 2740 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2741 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2742 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2743 | const char *p; |
| 2744 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2745 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2746 | p = f; |
| 2747 | do |
| 2748 | { |
| 2749 | if ((unsigned char)*p > 127) { |
| 2750 | PyErr_Format(PyExc_ValueError, |
| 2751 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2752 | "string, got a non-ASCII byte: 0x%02x", |
| 2753 | (unsigned char)*p); |
| 2754 | return NULL; |
| 2755 | } |
| 2756 | p++; |
| 2757 | } |
| 2758 | while (*p != '\0' && *p != '%'); |
| 2759 | len = p - f; |
| 2760 | |
| 2761 | if (*p == '\0') |
| 2762 | writer.overallocate = 0; |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 2763 | |
| 2764 | if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2765 | goto fail; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2766 | |
| 2767 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2768 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2769 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2770 | return _PyUnicodeWriter_Finish(&writer); |
| 2771 | |
| 2772 | fail: |
| 2773 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2774 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2777 | PyObject * |
| 2778 | PyUnicode_FromFormat(const char *format, ...) |
| 2779 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2780 | PyObject* ret; |
| 2781 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2782 | |
| 2783 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2784 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2785 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2786 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2787 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2788 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2789 | va_end(vargs); |
| 2790 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2793 | #ifdef HAVE_WCHAR_H |
| 2794 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2795 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2796 | convert a Unicode object to a wide character string. |
| 2797 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2798 | - 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] | 2799 | character) required to convert the unicode object. Ignore size argument. |
| 2800 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2801 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2802 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2803 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2804 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2805 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2806 | wchar_t *w, |
| 2807 | Py_ssize_t size) |
| 2808 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2809 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2810 | const wchar_t *wstr; |
| 2811 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2812 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2813 | if (wstr == NULL) |
| 2814 | return -1; |
| 2815 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2816 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2817 | if (size > res) |
| 2818 | size = res + 1; |
| 2819 | else |
| 2820 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2821 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2822 | return res; |
| 2823 | } |
| 2824 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2825 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2829 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2830 | wchar_t *w, |
| 2831 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2832 | { |
| 2833 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2834 | PyErr_BadInternalCall(); |
| 2835 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2836 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2837 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2840 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2841 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2842 | Py_ssize_t *size) |
| 2843 | { |
| 2844 | wchar_t* buffer; |
| 2845 | Py_ssize_t buflen; |
| 2846 | |
| 2847 | if (unicode == NULL) { |
| 2848 | PyErr_BadInternalCall(); |
| 2849 | return NULL; |
| 2850 | } |
| 2851 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2852 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2853 | if (buflen == -1) |
| 2854 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 2855 | buffer = PyMem_NEW(wchar_t, buflen); |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2856 | if (buffer == NULL) { |
| 2857 | PyErr_NoMemory(); |
| 2858 | return NULL; |
| 2859 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2860 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2861 | if (buflen == -1) { |
| 2862 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2863 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2864 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2865 | if (size != NULL) |
| 2866 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2867 | return buffer; |
| 2868 | } |
| 2869 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2870 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2871 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2872 | PyObject * |
| 2873 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2874 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2875 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2876 | PyErr_SetString(PyExc_ValueError, |
| 2877 | "chr() arg not in range(0x110000)"); |
| 2878 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2879 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2880 | |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 2881 | return unicode_char((Py_UCS4)ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2884 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2885 | PyUnicode_FromObject(PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2886 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2887 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2888 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2889 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2890 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2891 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2892 | Py_INCREF(obj); |
| 2893 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2894 | } |
| 2895 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2896 | /* For a Unicode subtype that's not a Unicode object, |
| 2897 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2898 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2899 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2900 | PyErr_Format(PyExc_TypeError, |
| 2901 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2902 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2903 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2906 | PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2907 | PyUnicode_FromEncodedObject(PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2908 | const char *encoding, |
| 2909 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2910 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2911 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2912 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2913 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2914 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2915 | PyErr_BadInternalCall(); |
| 2916 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2917 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2918 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2919 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2920 | if (PyBytes_Check(obj)) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2921 | if (PyBytes_GET_SIZE(obj) == 0) |
| 2922 | _Py_RETURN_UNICODE_EMPTY(); |
| 2923 | v = PyUnicode_Decode( |
| 2924 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2925 | encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2926 | return v; |
| 2927 | } |
| 2928 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2929 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2930 | PyErr_SetString(PyExc_TypeError, |
| 2931 | "decoding str is not supported"); |
| 2932 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2933 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2934 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2935 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2936 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2937 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 2938 | "coercing to str: need a bytes-like object, %.80s found", |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2939 | Py_TYPE(obj)->tp_name); |
| 2940 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2941 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2942 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2943 | if (buffer.len == 0) { |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2944 | PyBuffer_Release(&buffer); |
| 2945 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2946 | } |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2947 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 2948 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2949 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2950 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2953 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2954 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2955 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2956 | int |
| 2957 | _Py_normalize_encoding(const char *encoding, |
| 2958 | char *lower, |
| 2959 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2960 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2961 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2962 | char *l; |
| 2963 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2964 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2965 | if (encoding == NULL) { |
Victor Stinner | 66b3270 | 2013-11-07 23:12:23 +0100 | [diff] [blame] | 2966 | /* 6 == strlen("utf-8") + 1 */ |
Victor Stinner | df23e30 | 2013-11-07 13:33:36 +0100 | [diff] [blame] | 2967 | if (lower_len < 6) |
| 2968 | return 0; |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2969 | strcpy(lower, "utf-8"); |
| 2970 | return 1; |
| 2971 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2972 | e = encoding; |
| 2973 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2974 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2975 | while (*e) { |
| 2976 | if (l == l_end) |
| 2977 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2978 | if (Py_ISUPPER(*e)) { |
| 2979 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2980 | } |
| 2981 | else if (*e == '_') { |
| 2982 | *l++ = '-'; |
| 2983 | e++; |
| 2984 | } |
| 2985 | else { |
| 2986 | *l++ = *e++; |
| 2987 | } |
| 2988 | } |
| 2989 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2990 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2993 | PyObject * |
| 2994 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2995 | Py_ssize_t size, |
| 2996 | const char *encoding, |
| 2997 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2998 | { |
| 2999 | PyObject *buffer = NULL, *unicode; |
| 3000 | Py_buffer info; |
| 3001 | char lower[11]; /* Enough for any encoding shortcut */ |
| 3002 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3003 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3004 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3005 | if ((strcmp(lower, "utf-8") == 0) || |
| 3006 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3007 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3008 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3009 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3010 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3011 | (strcmp(lower, "iso8859-1") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3012 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3013 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3014 | else if (strcmp(lower, "mbcs") == 0) |
| 3015 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3016 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3017 | else if (strcmp(lower, "ascii") == 0) |
| 3018 | return PyUnicode_DecodeASCII(s, size, errors); |
| 3019 | else if (strcmp(lower, "utf-16") == 0) |
| 3020 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 3021 | else if (strcmp(lower, "utf-32") == 0) |
| 3022 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 3023 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3024 | |
| 3025 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 3026 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 3027 | 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] | 3028 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 3029 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3030 | if (buffer == NULL) |
| 3031 | goto onError; |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3032 | unicode = _PyCodec_DecodeText(buffer, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3033 | if (unicode == NULL) |
| 3034 | goto onError; |
| 3035 | if (!PyUnicode_Check(unicode)) { |
| 3036 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3037 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3038 | "use codecs.decode() to decode to arbitrary types", |
| 3039 | encoding, |
| 3040 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3041 | Py_DECREF(unicode); |
| 3042 | goto onError; |
| 3043 | } |
| 3044 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3045 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3046 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3047 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | Py_XDECREF(buffer); |
| 3049 | return NULL; |
| 3050 | } |
| 3051 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3052 | PyObject * |
| 3053 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3054 | const char *encoding, |
| 3055 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3056 | { |
| 3057 | PyObject *v; |
| 3058 | |
| 3059 | if (!PyUnicode_Check(unicode)) { |
| 3060 | PyErr_BadArgument(); |
| 3061 | goto onError; |
| 3062 | } |
| 3063 | |
| 3064 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3065 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3066 | |
| 3067 | /* Decode via the codec registry */ |
| 3068 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3069 | if (v == NULL) |
| 3070 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3071 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3072 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3073 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3074 | return NULL; |
| 3075 | } |
| 3076 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3077 | PyObject * |
| 3078 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3079 | const char *encoding, |
| 3080 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3081 | { |
| 3082 | PyObject *v; |
| 3083 | |
| 3084 | if (!PyUnicode_Check(unicode)) { |
| 3085 | PyErr_BadArgument(); |
| 3086 | goto onError; |
| 3087 | } |
| 3088 | |
| 3089 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3090 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3091 | |
| 3092 | /* Decode via the codec registry */ |
| 3093 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3094 | if (v == NULL) |
| 3095 | goto onError; |
| 3096 | if (!PyUnicode_Check(v)) { |
| 3097 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3098 | "'%.400s' decoder returned '%.400s' instead of 'str'; " |
| 3099 | "use codecs.decode() to decode to arbitrary types", |
| 3100 | encoding, |
| 3101 | Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3102 | Py_DECREF(v); |
| 3103 | goto onError; |
| 3104 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3105 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3106 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3107 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3108 | return NULL; |
| 3109 | } |
| 3110 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3111 | PyObject * |
| 3112 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3113 | Py_ssize_t size, |
| 3114 | const char *encoding, |
| 3115 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3116 | { |
| 3117 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3118 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3119 | unicode = PyUnicode_FromUnicode(s, size); |
| 3120 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3121 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3122 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3123 | Py_DECREF(unicode); |
| 3124 | return v; |
| 3125 | } |
| 3126 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3127 | PyObject * |
| 3128 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3129 | const char *encoding, |
| 3130 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3131 | { |
| 3132 | PyObject *v; |
| 3133 | |
| 3134 | if (!PyUnicode_Check(unicode)) { |
| 3135 | PyErr_BadArgument(); |
| 3136 | goto onError; |
| 3137 | } |
| 3138 | |
| 3139 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3140 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3141 | |
| 3142 | /* Encode via the codec registry */ |
| 3143 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3144 | if (v == NULL) |
| 3145 | goto onError; |
| 3146 | return v; |
| 3147 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3148 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3149 | return NULL; |
| 3150 | } |
| 3151 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3152 | static size_t |
| 3153 | wcstombs_errorpos(const wchar_t *wstr) |
| 3154 | { |
| 3155 | size_t len; |
| 3156 | #if SIZEOF_WCHAR_T == 2 |
| 3157 | wchar_t buf[3]; |
| 3158 | #else |
| 3159 | wchar_t buf[2]; |
| 3160 | #endif |
| 3161 | char outbuf[MB_LEN_MAX]; |
| 3162 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3163 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3164 | #if SIZEOF_WCHAR_T == 2 |
| 3165 | buf[2] = 0; |
| 3166 | #else |
| 3167 | buf[1] = 0; |
| 3168 | #endif |
| 3169 | start = wstr; |
| 3170 | while (*wstr != L'\0') |
| 3171 | { |
| 3172 | previous = wstr; |
| 3173 | #if SIZEOF_WCHAR_T == 2 |
| 3174 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3175 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3176 | { |
| 3177 | buf[0] = wstr[0]; |
| 3178 | buf[1] = wstr[1]; |
| 3179 | wstr += 2; |
| 3180 | } |
| 3181 | else { |
| 3182 | buf[0] = *wstr; |
| 3183 | buf[1] = 0; |
| 3184 | wstr++; |
| 3185 | } |
| 3186 | #else |
| 3187 | buf[0] = *wstr; |
| 3188 | wstr++; |
| 3189 | #endif |
| 3190 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3191 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3192 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3196 | return 0; |
| 3197 | } |
| 3198 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3199 | static int |
| 3200 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3201 | { |
| 3202 | if (errors == NULL) { |
| 3203 | *surrogateescape = 0; |
| 3204 | return 0; |
| 3205 | } |
| 3206 | |
| 3207 | if (strcmp(errors, "strict") == 0) { |
| 3208 | *surrogateescape = 0; |
| 3209 | return 0; |
| 3210 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3211 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3212 | *surrogateescape = 1; |
| 3213 | return 0; |
| 3214 | } |
| 3215 | PyErr_Format(PyExc_ValueError, |
| 3216 | "only 'strict' and 'surrogateescape' error handlers " |
| 3217 | "are supported, not '%s'", |
| 3218 | errors); |
| 3219 | return -1; |
| 3220 | } |
| 3221 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3222 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3223 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3224 | { |
| 3225 | Py_ssize_t wlen, wlen2; |
| 3226 | wchar_t *wstr; |
| 3227 | PyObject *bytes = NULL; |
| 3228 | char *errmsg; |
Raymond Hettinger | e56666d | 2013-08-04 11:51:03 -0700 | [diff] [blame] | 3229 | PyObject *reason = NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3230 | PyObject *exc; |
| 3231 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3232 | int surrogateescape; |
| 3233 | |
| 3234 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3235 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3236 | |
| 3237 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3238 | if (wstr == NULL) |
| 3239 | return NULL; |
| 3240 | |
| 3241 | wlen2 = wcslen(wstr); |
| 3242 | if (wlen2 != wlen) { |
| 3243 | PyMem_Free(wstr); |
| 3244 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3245 | return NULL; |
| 3246 | } |
| 3247 | |
| 3248 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3249 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3250 | char *str; |
| 3251 | |
| 3252 | str = _Py_wchar2char(wstr, &error_pos); |
| 3253 | if (str == NULL) { |
| 3254 | if (error_pos == (size_t)-1) { |
| 3255 | PyErr_NoMemory(); |
| 3256 | PyMem_Free(wstr); |
| 3257 | return NULL; |
| 3258 | } |
| 3259 | else { |
| 3260 | goto encode_error; |
| 3261 | } |
| 3262 | } |
| 3263 | PyMem_Free(wstr); |
| 3264 | |
| 3265 | bytes = PyBytes_FromString(str); |
| 3266 | PyMem_Free(str); |
| 3267 | } |
| 3268 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3269 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3270 | size_t len, len2; |
| 3271 | |
| 3272 | len = wcstombs(NULL, wstr, 0); |
| 3273 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3274 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3275 | goto encode_error; |
| 3276 | } |
| 3277 | |
| 3278 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3279 | if (bytes == NULL) { |
| 3280 | PyMem_Free(wstr); |
| 3281 | return NULL; |
| 3282 | } |
| 3283 | |
| 3284 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3285 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3286 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3287 | goto encode_error; |
| 3288 | } |
| 3289 | PyMem_Free(wstr); |
| 3290 | } |
| 3291 | return bytes; |
| 3292 | |
| 3293 | encode_error: |
| 3294 | errmsg = strerror(errno); |
| 3295 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3296 | |
| 3297 | if (error_pos == (size_t)-1) |
| 3298 | error_pos = wcstombs_errorpos(wstr); |
| 3299 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3300 | PyMem_Free(wstr); |
| 3301 | Py_XDECREF(bytes); |
| 3302 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3303 | if (errmsg != NULL) { |
| 3304 | size_t errlen; |
| 3305 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3306 | if (wstr != NULL) { |
| 3307 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3308 | PyMem_RawFree(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3309 | } else |
| 3310 | errmsg = NULL; |
| 3311 | } |
| 3312 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3313 | reason = PyUnicode_FromString( |
| 3314 | "wcstombs() encountered an unencodable " |
| 3315 | "wide character"); |
| 3316 | if (reason == NULL) |
| 3317 | return NULL; |
| 3318 | |
| 3319 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3320 | "locale", unicode, |
| 3321 | (Py_ssize_t)error_pos, |
| 3322 | (Py_ssize_t)(error_pos+1), |
| 3323 | reason); |
| 3324 | Py_DECREF(reason); |
| 3325 | if (exc != NULL) { |
| 3326 | PyCodec_StrictErrors(exc); |
| 3327 | Py_XDECREF(exc); |
| 3328 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3329 | return NULL; |
| 3330 | } |
| 3331 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3332 | PyObject * |
| 3333 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3334 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3335 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3336 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3337 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3338 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3339 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3340 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3341 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3342 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3343 | the Python codec requires to encode at least its own filename. Use the C |
| 3344 | version of the locale codec until the codec registry is initialized and |
| 3345 | the Python codec is loaded. |
| 3346 | |
| 3347 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3348 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3349 | subinterpreters. */ |
| 3350 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3351 | return PyUnicode_AsEncodedString(unicode, |
| 3352 | Py_FileSystemDefaultEncoding, |
| 3353 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3354 | } |
| 3355 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3356 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3357 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3358 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3361 | PyObject * |
| 3362 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3363 | const char *encoding, |
| 3364 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3365 | { |
| 3366 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3367 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3368 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3369 | if (!PyUnicode_Check(unicode)) { |
| 3370 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3371 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3372 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3373 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3374 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3375 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3376 | if ((strcmp(lower, "utf-8") == 0) || |
| 3377 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3378 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3379 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3380 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3381 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3382 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3383 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3384 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3385 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | fa3ba4c | 2013-10-29 11:34:05 +0100 | [diff] [blame] | 3386 | (strcmp(lower, "iso-8859-1") == 0) || |
| 3387 | (strcmp(lower, "iso8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3388 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3389 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3390 | else if (strcmp(lower, "mbcs") == 0) |
| 3391 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3392 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3393 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3394 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3395 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3396 | |
| 3397 | /* Encode via the codec registry */ |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 3398 | v = _PyCodec_EncodeText(unicode, encoding, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3399 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3400 | return NULL; |
| 3401 | |
| 3402 | /* The normal path */ |
| 3403 | if (PyBytes_Check(v)) |
| 3404 | return v; |
| 3405 | |
| 3406 | /* 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] | 3407 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3408 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3409 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3410 | |
| 3411 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3412 | "encoder %s returned bytearray instead of bytes; " |
| 3413 | "use codecs.encode() to encode to arbitrary types", |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3414 | encoding); |
| 3415 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3416 | Py_DECREF(v); |
| 3417 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3418 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3419 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3420 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3421 | Py_DECREF(v); |
| 3422 | return b; |
| 3423 | } |
| 3424 | |
| 3425 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3426 | "'%.400s' encoder returned '%.400s' instead of 'bytes'; " |
| 3427 | "use codecs.encode() to encode to arbitrary types", |
| 3428 | encoding, |
| 3429 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3430 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3431 | return NULL; |
| 3432 | } |
| 3433 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3434 | PyObject * |
| 3435 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3436 | const char *encoding, |
| 3437 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3438 | { |
| 3439 | PyObject *v; |
| 3440 | |
| 3441 | if (!PyUnicode_Check(unicode)) { |
| 3442 | PyErr_BadArgument(); |
| 3443 | goto onError; |
| 3444 | } |
| 3445 | |
| 3446 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3447 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3448 | |
| 3449 | /* Encode via the codec registry */ |
| 3450 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3451 | if (v == NULL) |
| 3452 | goto onError; |
| 3453 | if (!PyUnicode_Check(v)) { |
| 3454 | PyErr_Format(PyExc_TypeError, |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 3455 | "'%.400s' encoder returned '%.400s' instead of 'str'; " |
| 3456 | "use codecs.encode() to encode to arbitrary types", |
| 3457 | encoding, |
| 3458 | Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3459 | Py_DECREF(v); |
| 3460 | goto onError; |
| 3461 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3462 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3463 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3464 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3465 | return NULL; |
| 3466 | } |
| 3467 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3468 | static size_t |
| 3469 | mbstowcs_errorpos(const char *str, size_t len) |
| 3470 | { |
| 3471 | #ifdef HAVE_MBRTOWC |
| 3472 | const char *start = str; |
| 3473 | mbstate_t mbs; |
| 3474 | size_t converted; |
| 3475 | wchar_t ch; |
| 3476 | |
| 3477 | memset(&mbs, 0, sizeof mbs); |
| 3478 | while (len) |
| 3479 | { |
| 3480 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3481 | if (converted == 0) |
| 3482 | /* Reached end of string */ |
| 3483 | break; |
| 3484 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3485 | /* Conversion error or incomplete character */ |
| 3486 | return str - start; |
| 3487 | } |
| 3488 | else { |
| 3489 | str += converted; |
| 3490 | len -= converted; |
| 3491 | } |
| 3492 | } |
| 3493 | /* failed to find the undecodable byte sequence */ |
| 3494 | return 0; |
| 3495 | #endif |
| 3496 | return 0; |
| 3497 | } |
| 3498 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3499 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3500 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3501 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3502 | { |
| 3503 | wchar_t smallbuf[256]; |
| 3504 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3505 | wchar_t *wstr; |
| 3506 | size_t wlen, wlen2; |
| 3507 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3508 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3509 | size_t error_pos; |
| 3510 | char *errmsg; |
| 3511 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3512 | |
| 3513 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3514 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3515 | |
| 3516 | if (str[len] != '\0' || len != strlen(str)) { |
| 3517 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3518 | return NULL; |
| 3519 | } |
| 3520 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3521 | if (surrogateescape) { |
| 3522 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3523 | wstr = _Py_char2wchar(str, &wlen); |
| 3524 | if (wstr == NULL) { |
| 3525 | if (wlen == (size_t)-1) |
| 3526 | PyErr_NoMemory(); |
| 3527 | else |
| 3528 | PyErr_SetFromErrno(PyExc_OSError); |
| 3529 | return NULL; |
| 3530 | } |
| 3531 | |
| 3532 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3533 | PyMem_RawFree(wstr); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3534 | } |
| 3535 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3536 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3537 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3538 | wlen = mbstowcs(NULL, str, 0); |
| 3539 | #else |
| 3540 | wlen = len; |
| 3541 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3542 | if (wlen == (size_t)-1) |
| 3543 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3544 | if (wlen+1 <= smallbuf_len) { |
| 3545 | wstr = smallbuf; |
| 3546 | } |
| 3547 | else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 3548 | wstr = PyMem_New(wchar_t, wlen+1); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3549 | if (!wstr) |
| 3550 | return PyErr_NoMemory(); |
| 3551 | } |
| 3552 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3553 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3554 | if (wlen2 == (size_t)-1) { |
| 3555 | if (wstr != smallbuf) |
| 3556 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3557 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3558 | } |
| 3559 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3560 | assert(wlen2 == wlen); |
| 3561 | #endif |
| 3562 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3563 | if (wstr != smallbuf) |
| 3564 | PyMem_Free(wstr); |
| 3565 | } |
| 3566 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3567 | |
| 3568 | decode_error: |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3569 | reason = NULL; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3570 | errmsg = strerror(errno); |
| 3571 | assert(errmsg != NULL); |
| 3572 | |
| 3573 | error_pos = mbstowcs_errorpos(str, len); |
| 3574 | if (errmsg != NULL) { |
| 3575 | size_t errlen; |
| 3576 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3577 | if (wstr != NULL) { |
| 3578 | reason = PyUnicode_FromWideChar(wstr, errlen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 3579 | PyMem_RawFree(wstr); |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3580 | } |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3581 | } |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 3582 | if (reason == NULL) |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3583 | reason = PyUnicode_FromString( |
| 3584 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3585 | if (reason == NULL) |
| 3586 | return NULL; |
| 3587 | |
| 3588 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3589 | "locale", str, len, |
| 3590 | (Py_ssize_t)error_pos, |
| 3591 | (Py_ssize_t)(error_pos+1), |
| 3592 | reason); |
| 3593 | Py_DECREF(reason); |
| 3594 | if (exc != NULL) { |
| 3595 | PyCodec_StrictErrors(exc); |
| 3596 | Py_XDECREF(exc); |
| 3597 | } |
| 3598 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3602 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3603 | { |
| 3604 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3605 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3606 | } |
| 3607 | |
| 3608 | |
| 3609 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3610 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3611 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3612 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3613 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3614 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3615 | PyObject* |
| 3616 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3617 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3618 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3619 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3620 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3621 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3622 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3623 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3624 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3625 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3626 | the Python codec requires to encode at least its own filename. Use the C |
| 3627 | version of the locale codec until the codec registry is initialized and |
| 3628 | the Python codec is loaded. |
| 3629 | |
| 3630 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3631 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3632 | subinterpreters. */ |
| 3633 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3634 | return PyUnicode_Decode(s, size, |
| 3635 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3636 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3637 | } |
| 3638 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3639 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3640 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3641 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3644 | |
| 3645 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3646 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3647 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3648 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3649 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3650 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3651 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3652 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3653 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3654 | if (pos == -1) |
| 3655 | return 0; |
| 3656 | else |
| 3657 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3658 | } |
| 3659 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3660 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3661 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3662 | { |
| 3663 | PyObject *output = NULL; |
| 3664 | Py_ssize_t size; |
| 3665 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3666 | if (arg == NULL) { |
| 3667 | Py_DECREF(*(PyObject**)addr); |
| 3668 | return 1; |
| 3669 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3670 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3671 | output = arg; |
| 3672 | Py_INCREF(output); |
| 3673 | } |
| 3674 | else { |
| 3675 | arg = PyUnicode_FromObject(arg); |
| 3676 | if (!arg) |
| 3677 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3678 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3679 | Py_DECREF(arg); |
| 3680 | if (!output) |
| 3681 | return 0; |
| 3682 | if (!PyBytes_Check(output)) { |
| 3683 | Py_DECREF(output); |
| 3684 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3685 | return 0; |
| 3686 | } |
| 3687 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3688 | size = PyBytes_GET_SIZE(output); |
| 3689 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3690 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3691 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3692 | Py_DECREF(output); |
| 3693 | return 0; |
| 3694 | } |
| 3695 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3696 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3700 | int |
| 3701 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3702 | { |
| 3703 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3704 | if (arg == NULL) { |
| 3705 | Py_DECREF(*(PyObject**)addr); |
| 3706 | return 1; |
| 3707 | } |
| 3708 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3709 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3710 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3711 | output = arg; |
| 3712 | Py_INCREF(output); |
| 3713 | } |
| 3714 | else { |
| 3715 | arg = PyBytes_FromObject(arg); |
| 3716 | if (!arg) |
| 3717 | return 0; |
| 3718 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3719 | PyBytes_GET_SIZE(arg)); |
| 3720 | Py_DECREF(arg); |
| 3721 | if (!output) |
| 3722 | return 0; |
| 3723 | if (!PyUnicode_Check(output)) { |
| 3724 | Py_DECREF(output); |
| 3725 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3726 | return 0; |
| 3727 | } |
| 3728 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3729 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3730 | Py_DECREF(output); |
| 3731 | return 0; |
| 3732 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3733 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3734 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3735 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3736 | Py_DECREF(output); |
| 3737 | return 0; |
| 3738 | } |
| 3739 | *(PyObject**)addr = output; |
| 3740 | return Py_CLEANUP_SUPPORTED; |
| 3741 | } |
| 3742 | |
| 3743 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3744 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3745 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3746 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3747 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3748 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 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_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3754 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3755 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3756 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3757 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3758 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3759 | if (bytes == NULL) |
| 3760 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3761 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3762 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Victor Stinner | a5afb58 | 2013-10-29 01:28:23 +0100 | [diff] [blame] | 3763 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3764 | Py_DECREF(bytes); |
| 3765 | return NULL; |
| 3766 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3767 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3768 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3769 | PyBytes_AS_STRING(bytes), |
| 3770 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3771 | Py_DECREF(bytes); |
| 3772 | } |
| 3773 | |
| 3774 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3775 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3776 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3780 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3781 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3782 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3783 | } |
| 3784 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3785 | Py_UNICODE * |
| 3786 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3787 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3788 | const unsigned char *one_byte; |
| 3789 | #if SIZEOF_WCHAR_T == 4 |
| 3790 | const Py_UCS2 *two_bytes; |
| 3791 | #else |
| 3792 | const Py_UCS4 *four_bytes; |
| 3793 | const Py_UCS4 *ucs4_end; |
| 3794 | Py_ssize_t num_surrogates; |
| 3795 | #endif |
| 3796 | wchar_t *w; |
| 3797 | wchar_t *wchar_end; |
| 3798 | |
| 3799 | if (!PyUnicode_Check(unicode)) { |
| 3800 | PyErr_BadArgument(); |
| 3801 | return NULL; |
| 3802 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3803 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3804 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3805 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3806 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3807 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3808 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3809 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3810 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3811 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3812 | num_surrogates = 0; |
| 3813 | |
| 3814 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3815 | if (*four_bytes > 0xFFFF) |
| 3816 | ++num_surrogates; |
| 3817 | } |
| 3818 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3819 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3820 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3821 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3822 | PyErr_NoMemory(); |
| 3823 | return NULL; |
| 3824 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3825 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3826 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3827 | w = _PyUnicode_WSTR(unicode); |
| 3828 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3829 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3830 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3831 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3832 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3833 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3834 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3835 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3836 | } |
| 3837 | else |
| 3838 | *w = *four_bytes; |
| 3839 | |
| 3840 | if (w > wchar_end) { |
| 3841 | assert(0 && "Miscalculated string end"); |
| 3842 | } |
| 3843 | } |
| 3844 | *w = 0; |
| 3845 | #else |
| 3846 | /* sizeof(wchar_t) == 4 */ |
| 3847 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3848 | "should share memory already."); |
| 3849 | return NULL; |
| 3850 | #endif |
| 3851 | } |
| 3852 | else { |
Serhiy Storchaka | e55181f | 2015-02-20 21:34:06 +0200 | [diff] [blame] | 3853 | if ((size_t)_PyUnicode_LENGTH(unicode) > |
| 3854 | PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 3855 | PyErr_NoMemory(); |
| 3856 | return NULL; |
| 3857 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3858 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3859 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3860 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3861 | PyErr_NoMemory(); |
| 3862 | return NULL; |
| 3863 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3864 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3865 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3866 | w = _PyUnicode_WSTR(unicode); |
| 3867 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3868 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3869 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3870 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3871 | for (; w < wchar_end; ++one_byte, ++w) |
| 3872 | *w = *one_byte; |
| 3873 | /* null-terminate the wstr */ |
| 3874 | *w = 0; |
| 3875 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3876 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3877 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3878 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3879 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3880 | *w = *two_bytes; |
| 3881 | /* null-terminate the wstr */ |
| 3882 | *w = 0; |
| 3883 | #else |
| 3884 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3885 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3886 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3887 | Py_FatalError("Impossible unicode object state, wstr " |
| 3888 | "and str should share memory already."); |
| 3889 | return NULL; |
| 3890 | #endif |
| 3891 | } |
| 3892 | else { |
| 3893 | assert(0 && "This should never happen."); |
| 3894 | } |
| 3895 | } |
| 3896 | } |
| 3897 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3898 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3899 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3902 | Py_UNICODE * |
| 3903 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3904 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3905 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3908 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3909 | Py_ssize_t |
| 3910 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3911 | { |
| 3912 | if (!PyUnicode_Check(unicode)) { |
| 3913 | PyErr_BadArgument(); |
| 3914 | goto onError; |
| 3915 | } |
| 3916 | return PyUnicode_GET_SIZE(unicode); |
| 3917 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3918 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3919 | return -1; |
| 3920 | } |
| 3921 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3922 | Py_ssize_t |
| 3923 | PyUnicode_GetLength(PyObject *unicode) |
| 3924 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3925 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3926 | PyErr_BadArgument(); |
| 3927 | return -1; |
| 3928 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3929 | if (PyUnicode_READY(unicode) == -1) |
| 3930 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3931 | return PyUnicode_GET_LENGTH(unicode); |
| 3932 | } |
| 3933 | |
| 3934 | Py_UCS4 |
| 3935 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3936 | { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3937 | void *data; |
| 3938 | int kind; |
| 3939 | |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3940 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3941 | PyErr_BadArgument(); |
| 3942 | return (Py_UCS4)-1; |
| 3943 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3944 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3945 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3946 | return (Py_UCS4)-1; |
| 3947 | } |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 3948 | data = PyUnicode_DATA(unicode); |
| 3949 | kind = PyUnicode_KIND(unicode); |
| 3950 | return PyUnicode_READ(kind, data, index); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | int |
| 3954 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3955 | { |
| 3956 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3957 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3958 | return -1; |
| 3959 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3960 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3961 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3962 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3963 | return -1; |
| 3964 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3965 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3966 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3967 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3968 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3969 | return -1; |
| 3970 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3971 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3972 | index, ch); |
| 3973 | return 0; |
| 3974 | } |
| 3975 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3976 | const char * |
| 3977 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3978 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3979 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3982 | /* create or adjust a UnicodeDecodeError */ |
| 3983 | static void |
| 3984 | make_decode_exception(PyObject **exceptionObject, |
| 3985 | const char *encoding, |
| 3986 | const char *input, Py_ssize_t length, |
| 3987 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3988 | const char *reason) |
| 3989 | { |
| 3990 | if (*exceptionObject == NULL) { |
| 3991 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3992 | encoding, input, length, startpos, endpos, reason); |
| 3993 | } |
| 3994 | else { |
| 3995 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3996 | goto onError; |
| 3997 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3998 | goto onError; |
| 3999 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 4000 | goto onError; |
| 4001 | } |
| 4002 | return; |
| 4003 | |
| 4004 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 4005 | Py_CLEAR(*exceptionObject); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4008 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4009 | /* error handling callback helper: |
| 4010 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4011 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4012 | and adjust various state variables. |
| 4013 | return 0 on success, -1 on error |
| 4014 | */ |
| 4015 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4016 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4017 | unicode_decode_call_errorhandler_wchar( |
| 4018 | const char *errors, PyObject **errorHandler, |
| 4019 | const char *encoding, const char *reason, |
| 4020 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4021 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4022 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4023 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4024 | 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] | 4025 | |
| 4026 | PyObject *restuple = NULL; |
| 4027 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4028 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4029 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4030 | Py_ssize_t requiredsize; |
| 4031 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4032 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4033 | wchar_t *repwstr; |
| 4034 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4035 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4036 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 4037 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 4038 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4039 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4040 | *errorHandler = PyCodec_LookupError(errors); |
| 4041 | if (*errorHandler == NULL) |
| 4042 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 4045 | make_decode_exception(exceptionObject, |
| 4046 | encoding, |
| 4047 | *input, *inend - *input, |
| 4048 | *startinpos, *endinpos, |
| 4049 | reason); |
| 4050 | if (*exceptionObject == NULL) |
| 4051 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4052 | |
| 4053 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4054 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4055 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4056 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 4057 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4058 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4059 | } |
| 4060 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4061 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4062 | |
| 4063 | /* Copy back the bytes variables, which might have been modified by the |
| 4064 | callback */ |
| 4065 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4066 | if (!inputobj) |
| 4067 | goto onError; |
| 4068 | if (!PyBytes_Check(inputobj)) { |
| 4069 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 4070 | } |
| 4071 | *input = PyBytes_AS_STRING(inputobj); |
| 4072 | insize = PyBytes_GET_SIZE(inputobj); |
| 4073 | *inend = *input + insize; |
| 4074 | /* we can DECREF safely, as the exception has another reference, |
| 4075 | so the object won't go away. */ |
| 4076 | Py_DECREF(inputobj); |
| 4077 | |
| 4078 | if (newpos<0) |
| 4079 | newpos = insize+newpos; |
| 4080 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4081 | 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] | 4082 | goto onError; |
| 4083 | } |
| 4084 | |
| 4085 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 4086 | if (repwstr == NULL) |
| 4087 | goto onError; |
| 4088 | /* need more space? (at least enough for what we |
| 4089 | have+the replacement+the rest of the string (starting |
| 4090 | at the new input position), so we won't have to check space |
| 4091 | when there are no errors in the rest of the string) */ |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4092 | requiredsize = *outpos; |
| 4093 | if (requiredsize > PY_SSIZE_T_MAX - repwlen) |
| 4094 | goto overflow; |
| 4095 | requiredsize += repwlen; |
| 4096 | if (requiredsize > PY_SSIZE_T_MAX - (insize - newpos)) |
| 4097 | goto overflow; |
| 4098 | requiredsize += insize - newpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4099 | if (requiredsize > outsize) { |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4100 | if (outsize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*outsize) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4101 | requiredsize = 2*outsize; |
| 4102 | if (unicode_resize(output, requiredsize) < 0) |
| 4103 | goto onError; |
| 4104 | } |
| 4105 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 4106 | *outpos += repwlen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4107 | *endinpos = newpos; |
| 4108 | *inptr = *input + newpos; |
| 4109 | |
| 4110 | /* we made it! */ |
| 4111 | Py_XDECREF(restuple); |
| 4112 | return 0; |
| 4113 | |
Benjamin Peterson | 2b76ce6 | 2014-09-29 18:50:06 -0400 | [diff] [blame] | 4114 | overflow: |
| 4115 | PyErr_SetString(PyExc_OverflowError, |
| 4116 | "decoded result is too long for a Python string"); |
| 4117 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4118 | onError: |
| 4119 | Py_XDECREF(restuple); |
| 4120 | return -1; |
| 4121 | } |
| 4122 | #endif /* HAVE_MBCS */ |
| 4123 | |
| 4124 | static int |
| 4125 | unicode_decode_call_errorhandler_writer( |
| 4126 | const char *errors, PyObject **errorHandler, |
| 4127 | const char *encoding, const char *reason, |
| 4128 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4129 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4130 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4131 | { |
| 4132 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4133 | |
| 4134 | PyObject *restuple = NULL; |
| 4135 | PyObject *repunicode = NULL; |
| 4136 | Py_ssize_t insize; |
| 4137 | Py_ssize_t newpos; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4138 | Py_ssize_t replen; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4139 | PyObject *inputobj = NULL; |
| 4140 | |
| 4141 | if (*errorHandler == NULL) { |
| 4142 | *errorHandler = PyCodec_LookupError(errors); |
| 4143 | if (*errorHandler == NULL) |
| 4144 | goto onError; |
| 4145 | } |
| 4146 | |
| 4147 | make_decode_exception(exceptionObject, |
| 4148 | encoding, |
| 4149 | *input, *inend - *input, |
| 4150 | *startinpos, *endinpos, |
| 4151 | reason); |
| 4152 | if (*exceptionObject == NULL) |
| 4153 | goto onError; |
| 4154 | |
| 4155 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4156 | if (restuple == NULL) |
| 4157 | goto onError; |
| 4158 | if (!PyTuple_Check(restuple)) { |
| 4159 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4160 | goto onError; |
| 4161 | } |
| 4162 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4163 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4164 | |
| 4165 | /* Copy back the bytes variables, which might have been modified by the |
| 4166 | callback */ |
| 4167 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4168 | if (!inputobj) |
| 4169 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4170 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4171 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4172 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4173 | *input = PyBytes_AS_STRING(inputobj); |
| 4174 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4175 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4176 | /* we can DECREF safely, as the exception has another reference, |
| 4177 | so the object won't go away. */ |
| 4178 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4179 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4180 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4181 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4182 | if (newpos<0 || newpos>insize) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 4183 | 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] | 4184 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4185 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4186 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4187 | if (PyUnicode_READY(repunicode) < 0) |
| 4188 | goto onError; |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4189 | replen = PyUnicode_GET_LENGTH(repunicode); |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4190 | if (replen > 1) { |
| 4191 | writer->min_length += replen - 1; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4192 | writer->overallocate = 1; |
Serhiy Storchaka | 7e4b905 | 2015-01-26 01:22:54 +0200 | [diff] [blame] | 4193 | if (_PyUnicodeWriter_Prepare(writer, writer->min_length, |
| 4194 | PyUnicode_MAX_CHAR_VALUE(repunicode)) == -1) |
| 4195 | goto onError; |
| 4196 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4197 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
Victor Stinner | 376cfa1 | 2013-04-17 23:58:16 +0200 | [diff] [blame] | 4198 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4199 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4201 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4202 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4203 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4204 | Py_XDECREF(restuple); |
| 4205 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4206 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4207 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4208 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4209 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4212 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4213 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4214 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4215 | |
| 4216 | /* Three simple macros defining base-64. */ |
| 4217 | |
| 4218 | /* Is c a base-64 character? */ |
| 4219 | |
| 4220 | #define IS_BASE64(c) \ |
| 4221 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4222 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4223 | ((c) >= '0' && (c) <= '9') || \ |
| 4224 | (c) == '+' || (c) == '/') |
| 4225 | |
| 4226 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4227 | |
| 4228 | #define FROM_BASE64(c) \ |
| 4229 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4230 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4231 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4232 | (c) == '+' ? 62 : 63) |
| 4233 | |
| 4234 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4235 | |
| 4236 | #define TO_BASE64(n) \ |
| 4237 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4238 | |
| 4239 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4240 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4241 | * byte not decoding to itself is the + which begins a base64 |
| 4242 | * string. */ |
| 4243 | |
| 4244 | #define DECODE_DIRECT(c) \ |
| 4245 | ((c) <= 127 && (c) != '+') |
| 4246 | |
| 4247 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4248 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4249 | * the above). See RFC2152. This array identifies these different |
| 4250 | * sets: |
| 4251 | * 0 : "Set D" |
| 4252 | * alphanumeric and '(),-./:? |
| 4253 | * 1 : "Set O" |
| 4254 | * !"#$%&*;<=>@[]^_`{|} |
| 4255 | * 2 : "whitespace" |
| 4256 | * ht nl cr sp |
| 4257 | * 3 : special (must be base64 encoded) |
| 4258 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4259 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4260 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4261 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4262 | char utf7_category[128] = { |
| 4263 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4264 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4265 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4266 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4267 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4268 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4269 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4270 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4271 | /* @ A B C D E F G H I J K L M N O */ |
| 4272 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4273 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4274 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4275 | /* ` a b c d e f g h i j k l m n o */ |
| 4276 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4277 | /* p q r s t u v w x y z { | } ~ del */ |
| 4278 | 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] | 4279 | }; |
| 4280 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4281 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4282 | * answer depends on whether we are encoding set O as itself, and also |
| 4283 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4284 | * clear that the answers to these questions vary between |
| 4285 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4286 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4287 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4288 | ((c) < 128 && (c) > 0 && \ |
| 4289 | ((utf7_category[(c)] == 0) || \ |
| 4290 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4291 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4292 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4293 | PyObject * |
| 4294 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4295 | Py_ssize_t size, |
| 4296 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4297 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4298 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4299 | } |
| 4300 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4301 | /* The decoder. The only state we preserve is our read position, |
| 4302 | * i.e. how many characters we have consumed. So if we end in the |
| 4303 | * middle of a shift sequence we have to back off the read position |
| 4304 | * and the output to the beginning of the sequence, otherwise we lose |
| 4305 | * all the shift state (seen bits, number of bits seen, high |
| 4306 | * surrogate). */ |
| 4307 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4308 | PyObject * |
| 4309 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4310 | Py_ssize_t size, |
| 4311 | const char *errors, |
| 4312 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4313 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4314 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4315 | Py_ssize_t startinpos; |
| 4316 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4317 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4318 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4319 | const char *errmsg = ""; |
| 4320 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4321 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4322 | unsigned int base64bits = 0; |
| 4323 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4324 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4325 | PyObject *errorHandler = NULL; |
| 4326 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4327 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4328 | if (size == 0) { |
| 4329 | if (consumed) |
| 4330 | *consumed = 0; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 4331 | _Py_RETURN_UNICODE_EMPTY(); |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4332 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4333 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4334 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4335 | _PyUnicodeWriter_Init(&writer); |
| 4336 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4337 | |
| 4338 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4339 | e = s + size; |
| 4340 | |
| 4341 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4342 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4343 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4344 | ch = (unsigned char) *s; |
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 | if (inShift) { /* in a base-64 section */ |
| 4347 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4348 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4349 | base64bits += 6; |
| 4350 | s++; |
| 4351 | if (base64bits >= 16) { |
| 4352 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4353 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 | base64bits -= 16; |
| 4355 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4356 | assert(outCh <= 0xffff); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4357 | if (surrogate) { |
| 4358 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4359 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4360 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4361 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4362 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4363 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4364 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4365 | } |
| 4366 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4367 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4368 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4369 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4370 | } |
| 4371 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4372 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4373 | /* first surrogate */ |
| 4374 | surrogate = outCh; |
| 4375 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4376 | else { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4377 | if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4378 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4383 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4384 | if (base64bits > 0) { /* left-over bits */ |
| 4385 | if (base64bits >= 6) { |
| 4386 | /* We've seen at least one base-64 character */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4387 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4388 | errmsg = "partial character in shift sequence"; |
| 4389 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4390 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4391 | else { |
| 4392 | /* Some bits remain; they should be zero */ |
| 4393 | if (base64buffer != 0) { |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4394 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4395 | errmsg = "non-zero padding bits in shift sequence"; |
| 4396 | goto utf7Error; |
| 4397 | } |
| 4398 | } |
| 4399 | } |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4400 | if (surrogate && DECODE_DIRECT(ch)) { |
| 4401 | if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0) |
| 4402 | goto onError; |
| 4403 | } |
| 4404 | surrogate = 0; |
| 4405 | if (ch == '-') { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4406 | /* '-' is absorbed; other terminating |
| 4407 | characters are preserved */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4408 | s++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4409 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4410 | } |
| 4411 | } |
| 4412 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4413 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4414 | s++; /* consume '+' */ |
| 4415 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4416 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4417 | if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4418 | goto onError; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4419 | } |
| 4420 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4421 | inShift = 1; |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4422 | surrogate = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4423 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4424 | base64bits = 0; |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 4425 | base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4426 | } |
| 4427 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4428 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4429 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4430 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4431 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4432 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4433 | else { |
| 4434 | startinpos = s-starts; |
| 4435 | s++; |
| 4436 | errmsg = "unexpected special character"; |
| 4437 | goto utf7Error; |
| 4438 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4439 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4440 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4441 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4442 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4443 | errors, &errorHandler, |
| 4444 | "utf7", errmsg, |
| 4445 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4446 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4447 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4450 | /* end of string */ |
| 4451 | |
| 4452 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4453 | /* if we're in an inconsistent state, that's an error */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 4454 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4455 | if (surrogate || |
| 4456 | (base64bits >= 6) || |
| 4457 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4458 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4459 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4460 | errors, &errorHandler, |
| 4461 | "utf7", "unterminated shift sequence", |
| 4462 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4463 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4464 | goto onError; |
| 4465 | if (s < e) |
| 4466 | goto restart; |
| 4467 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4468 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4469 | |
| 4470 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4471 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4472 | if (inShift) { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4473 | *consumed = startinpos; |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4474 | if (writer.pos != shiftOutStart && writer.maxchar > 127) { |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4475 | PyObject *result = PyUnicode_FromKindAndData( |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4476 | writer.kind, writer.data, shiftOutStart); |
| 4477 | Py_XDECREF(errorHandler); |
| 4478 | Py_XDECREF(exc); |
| 4479 | _PyUnicodeWriter_Dealloc(&writer); |
| 4480 | return result; |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 4481 | } |
Serhiy Storchaka | 6cbf151 | 2014-02-08 14:06:33 +0200 | [diff] [blame] | 4482 | writer.pos = shiftOutStart; /* back off output */ |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4483 | } |
| 4484 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4485 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4486 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4487 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4488 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4489 | Py_XDECREF(errorHandler); |
| 4490 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4491 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4492 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4493 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4494 | Py_XDECREF(errorHandler); |
| 4495 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4496 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4497 | return NULL; |
| 4498 | } |
| 4499 | |
| 4500 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4501 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4502 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4503 | int base64SetO, |
| 4504 | int base64WhiteSpace, |
| 4505 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4506 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4507 | int kind; |
| 4508 | void *data; |
| 4509 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4510 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4511 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4512 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4513 | unsigned int base64bits = 0; |
| 4514 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4515 | char * out; |
| 4516 | char * start; |
| 4517 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4518 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4519 | return NULL; |
| 4520 | kind = PyUnicode_KIND(str); |
| 4521 | data = PyUnicode_DATA(str); |
| 4522 | len = PyUnicode_GET_LENGTH(str); |
| 4523 | |
| 4524 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4525 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4526 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4527 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4528 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4529 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4530 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4531 | if (v == NULL) |
| 4532 | return NULL; |
| 4533 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4534 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4535 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4536 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4537 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4538 | if (inShift) { |
| 4539 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4540 | /* shifting out */ |
| 4541 | if (base64bits) { /* output remaining bits */ |
| 4542 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4543 | base64buffer = 0; |
| 4544 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4545 | } |
| 4546 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4547 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4548 | so no '-' is required, except if the character is itself a '-' */ |
| 4549 | if (IS_BASE64(ch) || ch == '-') { |
| 4550 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4551 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4552 | *out++ = (char) ch; |
| 4553 | } |
| 4554 | else { |
| 4555 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4556 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4557 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4558 | else { /* not in a shift sequence */ |
| 4559 | if (ch == '+') { |
| 4560 | *out++ = '+'; |
| 4561 | *out++ = '-'; |
| 4562 | } |
| 4563 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4564 | *out++ = (char) ch; |
| 4565 | } |
| 4566 | else { |
| 4567 | *out++ = '+'; |
| 4568 | inShift = 1; |
| 4569 | goto encode_char; |
| 4570 | } |
| 4571 | } |
| 4572 | continue; |
| 4573 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4574 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4575 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4576 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4577 | /* code first surrogate */ |
| 4578 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4579 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4580 | while (base64bits >= 6) { |
| 4581 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4582 | base64bits -= 6; |
| 4583 | } |
| 4584 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4585 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4586 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4587 | base64bits += 16; |
| 4588 | base64buffer = (base64buffer << 16) | ch; |
| 4589 | while (base64bits >= 6) { |
| 4590 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4591 | base64bits -= 6; |
| 4592 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4593 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4594 | if (base64bits) |
| 4595 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4596 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4597 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4598 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4599 | return NULL; |
| 4600 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4601 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4602 | PyObject * |
| 4603 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4604 | Py_ssize_t size, |
| 4605 | int base64SetO, |
| 4606 | int base64WhiteSpace, |
| 4607 | const char *errors) |
| 4608 | { |
| 4609 | PyObject *result; |
| 4610 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4611 | if (tmp == NULL) |
| 4612 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4613 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4614 | base64WhiteSpace, errors); |
| 4615 | Py_DECREF(tmp); |
| 4616 | return result; |
| 4617 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4618 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4619 | #undef IS_BASE64 |
| 4620 | #undef FROM_BASE64 |
| 4621 | #undef TO_BASE64 |
| 4622 | #undef DECODE_DIRECT |
| 4623 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4624 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4625 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4626 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4627 | PyObject * |
| 4628 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4629 | Py_ssize_t size, |
| 4630 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4631 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4632 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4633 | } |
| 4634 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4635 | #include "stringlib/asciilib.h" |
| 4636 | #include "stringlib/codecs.h" |
| 4637 | #include "stringlib/undef.h" |
| 4638 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4639 | #include "stringlib/ucs1lib.h" |
| 4640 | #include "stringlib/codecs.h" |
| 4641 | #include "stringlib/undef.h" |
| 4642 | |
| 4643 | #include "stringlib/ucs2lib.h" |
| 4644 | #include "stringlib/codecs.h" |
| 4645 | #include "stringlib/undef.h" |
| 4646 | |
| 4647 | #include "stringlib/ucs4lib.h" |
| 4648 | #include "stringlib/codecs.h" |
| 4649 | #include "stringlib/undef.h" |
| 4650 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4651 | /* Mask to quickly check whether a C 'long' contains a |
| 4652 | non-ASCII, UTF8-encoded char. */ |
| 4653 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4654 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4655 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4656 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4657 | #else |
| 4658 | # error C 'long' size should be either 4 or 8! |
| 4659 | #endif |
| 4660 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4661 | static Py_ssize_t |
| 4662 | 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] | 4663 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4664 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4665 | 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] | 4666 | |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4667 | /* |
| 4668 | * Issue #17237: m68k is a bit different from most architectures in |
| 4669 | * that objects do not use "natural alignment" - for example, int and |
| 4670 | * long are only aligned at 2-byte boundaries. Therefore the assert() |
| 4671 | * won't work; also, tests have shown that skipping the "optimised |
| 4672 | * version" will even speed up m68k. |
| 4673 | */ |
| 4674 | #if !defined(__m68k__) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4675 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4676 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4677 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4678 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4679 | an explanation. */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4680 | /* Help allocation */ |
| 4681 | const char *_p = p; |
| 4682 | Py_UCS1 * q = dest; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4683 | while (_p < aligned_end) { |
| 4684 | unsigned long value = *(const unsigned long *) _p; |
| 4685 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4686 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4687 | *((unsigned long *)q) = value; |
| 4688 | _p += SIZEOF_LONG; |
| 4689 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4690 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4691 | p = _p; |
| 4692 | while (p < end) { |
| 4693 | if ((unsigned char)*p & 0x80) |
| 4694 | break; |
| 4695 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4696 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4697 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4698 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4699 | #endif |
Antoine Pitrou | 8b0e984 | 2013-05-11 15:58:34 +0200 | [diff] [blame] | 4700 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4701 | while (p < end) { |
| 4702 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4703 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4704 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4705 | /* Help allocation */ |
| 4706 | const char *_p = p; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4707 | while (_p < aligned_end) { |
| 4708 | unsigned long value = *(unsigned long *) _p; |
| 4709 | if (value & ASCII_CHAR_MASK) |
| 4710 | break; |
| 4711 | _p += SIZEOF_LONG; |
| 4712 | } |
| 4713 | p = _p; |
| 4714 | if (_p == end) |
| 4715 | break; |
| 4716 | } |
| 4717 | if ((unsigned char)*p & 0x80) |
| 4718 | break; |
| 4719 | ++p; |
| 4720 | } |
| 4721 | memcpy(dest, start, p - start); |
| 4722 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4723 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4724 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4725 | PyObject * |
| 4726 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4727 | Py_ssize_t size, |
| 4728 | const char *errors, |
| 4729 | Py_ssize_t *consumed) |
| 4730 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4731 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4732 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4733 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4734 | |
| 4735 | Py_ssize_t startinpos; |
| 4736 | Py_ssize_t endinpos; |
| 4737 | const char *errmsg = ""; |
| 4738 | PyObject *errorHandler = NULL; |
| 4739 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4740 | |
| 4741 | if (size == 0) { |
| 4742 | if (consumed) |
| 4743 | *consumed = 0; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 4744 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4745 | } |
| 4746 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4747 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4748 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4749 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4750 | *consumed = 1; |
| 4751 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4752 | } |
| 4753 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 4754 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 4755 | writer.min_length = size; |
| 4756 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4757 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4758 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4759 | writer.pos = ascii_decode(s, end, writer.data); |
| 4760 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4761 | while (s < end) { |
| 4762 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4763 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4764 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4765 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4766 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4767 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4768 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4769 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4770 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4771 | } else { |
| 4772 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4773 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | switch (ch) { |
| 4777 | case 0: |
| 4778 | if (s == end || consumed) |
| 4779 | goto End; |
| 4780 | errmsg = "unexpected end of data"; |
| 4781 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4782 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4783 | break; |
| 4784 | case 1: |
| 4785 | errmsg = "invalid start byte"; |
| 4786 | startinpos = s - starts; |
| 4787 | endinpos = startinpos + 1; |
| 4788 | break; |
| 4789 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4790 | case 3: |
| 4791 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4792 | errmsg = "invalid continuation byte"; |
| 4793 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4794 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4795 | break; |
| 4796 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 4797 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4798 | goto onError; |
| 4799 | continue; |
| 4800 | } |
| 4801 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4802 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4803 | errors, &errorHandler, |
| 4804 | "utf-8", errmsg, |
| 4805 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4806 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4807 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4808 | } |
| 4809 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4810 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4811 | if (consumed) |
| 4812 | *consumed = s - starts; |
| 4813 | |
| 4814 | Py_XDECREF(errorHandler); |
| 4815 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4816 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4817 | |
| 4818 | onError: |
| 4819 | Py_XDECREF(errorHandler); |
| 4820 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4821 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4822 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4823 | } |
| 4824 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4825 | #ifdef __APPLE__ |
| 4826 | |
| 4827 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4828 | used to decode the command line arguments on Mac OS X. |
| 4829 | |
| 4830 | Return a pointer to a newly allocated wide character string (use |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4831 | PyMem_RawFree() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4832 | |
| 4833 | wchar_t* |
| 4834 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4835 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4836 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4837 | wchar_t *unicode; |
| 4838 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4839 | |
| 4840 | /* Note: size will always be longer than the resulting Unicode |
| 4841 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4842 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4843 | return NULL; |
Victor Stinner | 6f8eeee | 2013-07-07 22:57:45 +0200 | [diff] [blame] | 4844 | unicode = PyMem_RawMalloc((size + 1) * sizeof(wchar_t)); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4845 | if (!unicode) |
| 4846 | return NULL; |
| 4847 | |
| 4848 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4849 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4850 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4851 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4852 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4853 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4854 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4855 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4856 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4857 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4858 | if (ch > 0xFF) { |
| 4859 | #if SIZEOF_WCHAR_T == 4 |
| 4860 | assert(0); |
| 4861 | #else |
| 4862 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4863 | /* compute and append the two surrogates: */ |
| 4864 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4865 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4866 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4867 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4868 | else { |
| 4869 | if (!ch && s == e) |
| 4870 | break; |
| 4871 | /* surrogateescape */ |
| 4872 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4873 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4874 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4875 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4876 | return unicode; |
| 4877 | } |
| 4878 | |
| 4879 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4880 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4881 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4882 | |
| 4883 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4884 | and allocate exactly as much space needed at the end. Else allocate the |
| 4885 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4886 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4887 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4888 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4889 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4890 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4891 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4892 | void *data; |
| 4893 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4894 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4895 | if (!PyUnicode_Check(unicode)) { |
| 4896 | PyErr_BadArgument(); |
| 4897 | return NULL; |
| 4898 | } |
| 4899 | |
| 4900 | if (PyUnicode_READY(unicode) == -1) |
| 4901 | return NULL; |
| 4902 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4903 | if (PyUnicode_UTF8(unicode)) |
| 4904 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4905 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4906 | |
| 4907 | kind = PyUnicode_KIND(unicode); |
| 4908 | data = PyUnicode_DATA(unicode); |
| 4909 | size = PyUnicode_GET_LENGTH(unicode); |
| 4910 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4911 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4912 | default: |
| 4913 | assert(0); |
| 4914 | case PyUnicode_1BYTE_KIND: |
| 4915 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4916 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4917 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4918 | case PyUnicode_2BYTE_KIND: |
| 4919 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4920 | case PyUnicode_4BYTE_KIND: |
| 4921 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4922 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4925 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4926 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4927 | Py_ssize_t size, |
| 4928 | const char *errors) |
| 4929 | { |
| 4930 | PyObject *v, *unicode; |
| 4931 | |
| 4932 | unicode = PyUnicode_FromUnicode(s, size); |
| 4933 | if (unicode == NULL) |
| 4934 | return NULL; |
| 4935 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4936 | Py_DECREF(unicode); |
| 4937 | return v; |
| 4938 | } |
| 4939 | |
| 4940 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4941 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4942 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4943 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4946 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4947 | |
| 4948 | PyObject * |
| 4949 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4950 | Py_ssize_t size, |
| 4951 | const char *errors, |
| 4952 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4953 | { |
| 4954 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4955 | } |
| 4956 | |
| 4957 | PyObject * |
| 4958 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4959 | Py_ssize_t size, |
| 4960 | const char *errors, |
| 4961 | int *byteorder, |
| 4962 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4963 | { |
| 4964 | const char *starts = s; |
| 4965 | Py_ssize_t startinpos; |
| 4966 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4967 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4968 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4969 | int le, bo = 0; /* assume native ordering by default */ |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 4970 | const char *encoding; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4971 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4972 | PyObject *errorHandler = NULL; |
| 4973 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4974 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4975 | q = (unsigned char *)s; |
| 4976 | e = q + size; |
| 4977 | |
| 4978 | if (byteorder) |
| 4979 | bo = *byteorder; |
| 4980 | |
| 4981 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4982 | byte order setting accordingly. In native mode, the leading BOM |
| 4983 | mark is skipped, in all other modes, it is copied to the output |
| 4984 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4985 | if (bo == 0 && size >= 4) { |
| 4986 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4987 | if (bom == 0x0000FEFF) { |
| 4988 | bo = -1; |
| 4989 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4990 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4991 | else if (bom == 0xFFFE0000) { |
| 4992 | bo = 1; |
| 4993 | q += 4; |
| 4994 | } |
| 4995 | if (byteorder) |
| 4996 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4999 | if (q == e) { |
| 5000 | if (consumed) |
| 5001 | *consumed = size; |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5002 | _Py_RETURN_UNICODE_EMPTY(); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5005 | #ifdef WORDS_BIGENDIAN |
| 5006 | le = bo < 0; |
| 5007 | #else |
| 5008 | le = bo <= 0; |
| 5009 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5010 | encoding = le ? "utf-32-le" : "utf-32-be"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5011 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5012 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5013 | writer.min_length = (e - q + 3) / 4; |
| 5014 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5015 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5016 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5017 | while (1) { |
| 5018 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5019 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 5020 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5021 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5022 | enum PyUnicode_Kind kind = writer.kind; |
| 5023 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5024 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5025 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5026 | if (le) { |
| 5027 | do { |
| 5028 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 5029 | if (ch > maxch) |
| 5030 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5031 | if (kind != PyUnicode_1BYTE_KIND && |
| 5032 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5033 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5034 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5035 | q += 4; |
| 5036 | } while (q <= last); |
| 5037 | } |
| 5038 | else { |
| 5039 | do { |
| 5040 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 5041 | if (ch > maxch) |
| 5042 | break; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5043 | if (kind != PyUnicode_1BYTE_KIND && |
| 5044 | Py_UNICODE_IS_SURROGATE(ch)) |
| 5045 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5046 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5047 | q += 4; |
| 5048 | } while (q <= last); |
| 5049 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5050 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5051 | } |
| 5052 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5053 | if (Py_UNICODE_IS_SURROGATE(ch)) { |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5054 | errmsg = "code point in surrogate code point range(0xd800, 0xe000)"; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5055 | startinpos = ((const char *)q) - starts; |
| 5056 | endinpos = startinpos + 4; |
| 5057 | } |
| 5058 | else if (ch <= maxch) { |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5059 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5060 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5061 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5062 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5063 | startinpos = ((const char *)q) - starts; |
| 5064 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5066 | else { |
| 5067 | if (ch < 0x110000) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5068 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5069 | goto onError; |
| 5070 | q += 4; |
| 5071 | continue; |
| 5072 | } |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 5073 | errmsg = "code point not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5074 | startinpos = ((const char *)q) - starts; |
| 5075 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5076 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 5077 | |
| 5078 | /* The remaining input chars are ignored if the callback |
| 5079 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5080 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5081 | errors, &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5082 | encoding, errmsg, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5083 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5084 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5085 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5088 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5089 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5090 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5091 | Py_XDECREF(errorHandler); |
| 5092 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5093 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5094 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5096 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5097 | Py_XDECREF(errorHandler); |
| 5098 | Py_XDECREF(exc); |
| 5099 | return NULL; |
| 5100 | } |
| 5101 | |
| 5102 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5103 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 5104 | const char *errors, |
| 5105 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5106 | { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5107 | int kind; |
| 5108 | void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5109 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5110 | PyObject *v; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5111 | unsigned char *p; |
| 5112 | Py_ssize_t nsize, i; |
| 5113 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5114 | #if PY_LITTLE_ENDIAN |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5115 | int iorder[] = {0, 1, 2, 3}; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5116 | #else |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5117 | int iorder[] = {3, 2, 1, 0}; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5118 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5119 | const char *encoding; |
| 5120 | PyObject *errorHandler = NULL; |
| 5121 | PyObject *exc = NULL; |
| 5122 | PyObject *rep = NULL; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5123 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5124 | #define STORECHAR(CH) \ |
| 5125 | do { \ |
| 5126 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 5127 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 5128 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 5129 | p[iorder[0]] = (CH) & 0xff; \ |
| 5130 | p += 4; \ |
| 5131 | } while(0) |
| 5132 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5133 | if (!PyUnicode_Check(str)) { |
| 5134 | PyErr_BadArgument(); |
| 5135 | return NULL; |
| 5136 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5137 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5138 | return NULL; |
| 5139 | kind = PyUnicode_KIND(str); |
| 5140 | data = PyUnicode_DATA(str); |
| 5141 | len = PyUnicode_GET_LENGTH(str); |
| 5142 | |
Serhiy Storchaka | 583a939 | 2014-01-04 19:25:37 +0200 | [diff] [blame] | 5143 | nsize = len + (byteorder == 0); |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5144 | if (nsize > PY_SSIZE_T_MAX / 4) |
| 5145 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5146 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5147 | if (v == NULL) |
| 5148 | return NULL; |
| 5149 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5150 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5151 | if (byteorder == 0) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5152 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5153 | if (len == 0) |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5154 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5155 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5156 | if (byteorder == -1) { |
| 5157 | /* force LE */ |
| 5158 | iorder[0] = 0; |
| 5159 | iorder[1] = 1; |
| 5160 | iorder[2] = 2; |
| 5161 | iorder[3] = 3; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5162 | encoding = "utf-32-le"; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5163 | } |
| 5164 | else if (byteorder == 1) { |
| 5165 | /* force BE */ |
| 5166 | iorder[0] = 3; |
| 5167 | iorder[1] = 2; |
| 5168 | iorder[2] = 1; |
| 5169 | iorder[3] = 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5170 | encoding = "utf-32-be"; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5171 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5172 | else |
| 5173 | encoding = "utf-32"; |
| 5174 | |
| 5175 | if (kind == PyUnicode_1BYTE_KIND) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5176 | for (i = 0; i < len; i++) |
| 5177 | STORECHAR(PyUnicode_READ(kind, data, i)); |
| 5178 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5181 | for (i = 0; i < len;) { |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5182 | Py_ssize_t repsize, moreunits; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5183 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 5184 | i++; |
| 5185 | assert(ch <= MAX_UNICODE); |
| 5186 | if (!Py_UNICODE_IS_SURROGATE(ch)) { |
| 5187 | STORECHAR(ch); |
| 5188 | continue; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5189 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5190 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5191 | rep = unicode_encode_call_errorhandler( |
| 5192 | errors, &errorHandler, |
| 5193 | encoding, "surrogates not allowed", |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5194 | str, &exc, i-1, i, &i); |
| 5195 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5196 | if (!rep) |
| 5197 | goto error; |
| 5198 | |
| 5199 | if (PyBytes_Check(rep)) { |
| 5200 | repsize = PyBytes_GET_SIZE(rep); |
| 5201 | if (repsize & 3) { |
| 5202 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5203 | str, i - 1, i, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5204 | "surrogates not allowed"); |
| 5205 | goto error; |
| 5206 | } |
| 5207 | moreunits = repsize / 4; |
| 5208 | } |
| 5209 | else { |
| 5210 | assert(PyUnicode_Check(rep)); |
| 5211 | if (PyUnicode_READY(rep) < 0) |
| 5212 | goto error; |
| 5213 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5214 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5215 | raise_encode_exception(&exc, encoding, |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5216 | str, i - 1, i, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5217 | "surrogates not allowed"); |
| 5218 | goto error; |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | /* four bytes are reserved for each surrogate */ |
| 5223 | if (moreunits > 1) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5224 | Py_ssize_t outpos = p - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5225 | Py_ssize_t morebytes = 4 * (moreunits - 1); |
| 5226 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5227 | /* integer overflow */ |
| 5228 | PyErr_NoMemory(); |
| 5229 | goto error; |
| 5230 | } |
| 5231 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5232 | goto error; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5233 | p = (unsigned char*) PyBytes_AS_STRING(v) + outpos; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5234 | } |
| 5235 | |
| 5236 | if (PyBytes_Check(rep)) { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5237 | Py_MEMCPY(p, PyBytes_AS_STRING(rep), repsize); |
| 5238 | p += repsize; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5239 | } else /* rep is unicode */ { |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5240 | const Py_UCS1 *repdata; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5241 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5242 | repdata = PyUnicode_1BYTE_DATA(rep); |
| 5243 | while (repsize--) { |
| 5244 | Py_UCS4 ch = *repdata++; |
| 5245 | STORECHAR(ch); |
| 5246 | } |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5247 | } |
| 5248 | |
| 5249 | Py_CLEAR(rep); |
| 5250 | } |
| 5251 | |
| 5252 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5253 | encoding of a string containing isolated surrogates and the 'ignore' |
| 5254 | handler is used. */ |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5255 | nsize = p - (unsigned char*) PyBytes_AS_STRING(v); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5256 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5257 | _PyBytes_Resize(&v, nsize); |
| 5258 | Py_XDECREF(errorHandler); |
| 5259 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5260 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5261 | error: |
| 5262 | Py_XDECREF(rep); |
| 5263 | Py_XDECREF(errorHandler); |
| 5264 | Py_XDECREF(exc); |
| 5265 | Py_XDECREF(v); |
| 5266 | return NULL; |
Serhiy Storchaka | 3079328 | 2014-01-04 22:44:01 +0200 | [diff] [blame] | 5267 | #undef STORECHAR |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5270 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5271 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5272 | Py_ssize_t size, |
| 5273 | const char *errors, |
| 5274 | int byteorder) |
| 5275 | { |
| 5276 | PyObject *result; |
| 5277 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5278 | if (tmp == NULL) |
| 5279 | return NULL; |
| 5280 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5281 | Py_DECREF(tmp); |
| 5282 | return result; |
| 5283 | } |
| 5284 | |
| 5285 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5286 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5287 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5288 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5291 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5292 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5293 | PyObject * |
| 5294 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5295 | Py_ssize_t size, |
| 5296 | const char *errors, |
| 5297 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5298 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5299 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5300 | } |
| 5301 | |
| 5302 | PyObject * |
| 5303 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5304 | Py_ssize_t size, |
| 5305 | const char *errors, |
| 5306 | int *byteorder, |
| 5307 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5308 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5309 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5310 | Py_ssize_t startinpos; |
| 5311 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5312 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5313 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5314 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5315 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5316 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5317 | PyObject *errorHandler = NULL; |
| 5318 | PyObject *exc = NULL; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5319 | const char *encoding; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5320 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5321 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5322 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | |
| 5324 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5325 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5326 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5327 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5328 | byte order setting accordingly. In native mode, the leading BOM |
| 5329 | mark is skipped, in all other modes, it is copied to the output |
| 5330 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5331 | if (bo == 0 && size >= 2) { |
| 5332 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5333 | if (bom == 0xFEFF) { |
| 5334 | q += 2; |
| 5335 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5336 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5337 | else if (bom == 0xFFFE) { |
| 5338 | q += 2; |
| 5339 | bo = 1; |
| 5340 | } |
| 5341 | if (byteorder) |
| 5342 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5343 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5344 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5345 | if (q == e) { |
| 5346 | if (consumed) |
| 5347 | *consumed = size; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 5348 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5349 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5350 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5351 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5352 | native_ordering = bo <= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5353 | encoding = bo <= 0 ? "utf-16-le" : "utf-16-be"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5354 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5355 | native_ordering = bo >= 0; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5356 | encoding = bo >= 0 ? "utf-16-be" : "utf-16-le"; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5357 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5358 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5359 | /* Note: size will always be longer than the resulting Unicode |
| 5360 | character count */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5361 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 5362 | writer.min_length = (e - q + 1) / 2; |
| 5363 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5364 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5365 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5366 | while (1) { |
| 5367 | Py_UCS4 ch = 0; |
| 5368 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5369 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5370 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5371 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5372 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5373 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5374 | native_ordering); |
| 5375 | else |
| 5376 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5377 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5378 | native_ordering); |
| 5379 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5380 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5381 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5382 | native_ordering); |
| 5383 | } else { |
| 5384 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5385 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5386 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5387 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5388 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5389 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5390 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5391 | switch (ch) |
| 5392 | { |
| 5393 | case 0: |
| 5394 | /* remaining byte at the end? (size should be even) */ |
| 5395 | if (q == e || consumed) |
| 5396 | goto End; |
| 5397 | errmsg = "truncated data"; |
| 5398 | startinpos = ((const char *)q) - starts; |
| 5399 | endinpos = ((const char *)e) - starts; |
| 5400 | break; |
| 5401 | /* The remaining input chars are ignored if the callback |
| 5402 | chooses to skip the input */ |
| 5403 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5404 | q -= 2; |
| 5405 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5406 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5407 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5408 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5409 | endinpos = ((const char *)e) - starts; |
| 5410 | break; |
| 5411 | case 2: |
| 5412 | errmsg = "illegal encoding"; |
| 5413 | startinpos = ((const char *)q) - 2 - starts; |
| 5414 | endinpos = startinpos + 2; |
| 5415 | break; |
| 5416 | case 3: |
| 5417 | errmsg = "illegal UTF-16 surrogate"; |
| 5418 | startinpos = ((const char *)q) - 4 - starts; |
| 5419 | endinpos = startinpos + 2; |
| 5420 | break; |
| 5421 | default: |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5422 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5423 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5424 | continue; |
| 5425 | } |
| 5426 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5427 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5428 | errors, |
| 5429 | &errorHandler, |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5430 | encoding, errmsg, |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5431 | &starts, |
| 5432 | (const char **)&e, |
| 5433 | &startinpos, |
| 5434 | &endinpos, |
| 5435 | &exc, |
| 5436 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5437 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5438 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5441 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5442 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5443 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5444 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5445 | Py_XDECREF(errorHandler); |
| 5446 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5447 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5449 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5450 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5451 | Py_XDECREF(errorHandler); |
| 5452 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | return NULL; |
| 5454 | } |
| 5455 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5456 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5457 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5458 | const char *errors, |
| 5459 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5460 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5461 | enum PyUnicode_Kind kind; |
| 5462 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5463 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5464 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5465 | unsigned short *out; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5466 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5467 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5468 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5469 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5470 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5471 | #endif |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5472 | const char *encoding; |
| 5473 | Py_ssize_t nsize, pos; |
| 5474 | PyObject *errorHandler = NULL; |
| 5475 | PyObject *exc = NULL; |
| 5476 | PyObject *rep = NULL; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5477 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5478 | if (!PyUnicode_Check(str)) { |
| 5479 | PyErr_BadArgument(); |
| 5480 | return NULL; |
| 5481 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5482 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5483 | return NULL; |
| 5484 | kind = PyUnicode_KIND(str); |
| 5485 | data = PyUnicode_DATA(str); |
| 5486 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5487 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5488 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5489 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5490 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5491 | const Py_UCS4 *end = in + len; |
| 5492 | while (in < end) |
| 5493 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5494 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5495 | } |
| 5496 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5497 | return PyErr_NoMemory(); |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5498 | nsize = len + pairs + (byteorder == 0); |
| 5499 | v = PyBytes_FromStringAndSize(NULL, nsize * 2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5500 | if (v == NULL) |
| 5501 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5503 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5504 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5505 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5507 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5508 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5509 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5510 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5511 | if (kind == PyUnicode_1BYTE_KIND) { |
| 5512 | ucs1lib_utf16_encode((const Py_UCS1 *)data, len, &out, native_ordering); |
| 5513 | goto done; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5514 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5515 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5516 | if (byteorder < 0) |
| 5517 | encoding = "utf-16-le"; |
| 5518 | else if (byteorder > 0) |
| 5519 | encoding = "utf-16-be"; |
| 5520 | else |
| 5521 | encoding = "utf-16"; |
| 5522 | |
| 5523 | pos = 0; |
| 5524 | while (pos < len) { |
| 5525 | Py_ssize_t repsize, moreunits; |
| 5526 | |
| 5527 | if (kind == PyUnicode_2BYTE_KIND) { |
| 5528 | pos += ucs2lib_utf16_encode((const Py_UCS2 *)data + pos, len - pos, |
| 5529 | &out, native_ordering); |
| 5530 | } |
| 5531 | else { |
| 5532 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5533 | pos += ucs4lib_utf16_encode((const Py_UCS4 *)data + pos, len - pos, |
| 5534 | &out, native_ordering); |
| 5535 | } |
| 5536 | if (pos == len) |
| 5537 | break; |
| 5538 | |
| 5539 | rep = unicode_encode_call_errorhandler( |
| 5540 | errors, &errorHandler, |
| 5541 | encoding, "surrogates not allowed", |
| 5542 | str, &exc, pos, pos + 1, &pos); |
| 5543 | if (!rep) |
| 5544 | goto error; |
| 5545 | |
| 5546 | if (PyBytes_Check(rep)) { |
| 5547 | repsize = PyBytes_GET_SIZE(rep); |
| 5548 | if (repsize & 1) { |
| 5549 | raise_encode_exception(&exc, encoding, |
| 5550 | str, pos - 1, pos, |
| 5551 | "surrogates not allowed"); |
| 5552 | goto error; |
| 5553 | } |
| 5554 | moreunits = repsize / 2; |
| 5555 | } |
| 5556 | else { |
| 5557 | assert(PyUnicode_Check(rep)); |
| 5558 | if (PyUnicode_READY(rep) < 0) |
| 5559 | goto error; |
| 5560 | moreunits = repsize = PyUnicode_GET_LENGTH(rep); |
| 5561 | if (!PyUnicode_IS_ASCII(rep)) { |
| 5562 | raise_encode_exception(&exc, encoding, |
| 5563 | str, pos - 1, pos, |
| 5564 | "surrogates not allowed"); |
| 5565 | goto error; |
| 5566 | } |
| 5567 | } |
| 5568 | |
| 5569 | /* two bytes are reserved for each surrogate */ |
| 5570 | if (moreunits > 1) { |
| 5571 | Py_ssize_t outpos = out - (unsigned short*) PyBytes_AS_STRING(v); |
| 5572 | Py_ssize_t morebytes = 2 * (moreunits - 1); |
| 5573 | if (PyBytes_GET_SIZE(v) > PY_SSIZE_T_MAX - morebytes) { |
| 5574 | /* integer overflow */ |
| 5575 | PyErr_NoMemory(); |
| 5576 | goto error; |
| 5577 | } |
| 5578 | if (_PyBytes_Resize(&v, PyBytes_GET_SIZE(v) + morebytes) < 0) |
| 5579 | goto error; |
| 5580 | out = (unsigned short*) PyBytes_AS_STRING(v) + outpos; |
| 5581 | } |
| 5582 | |
| 5583 | if (PyBytes_Check(rep)) { |
| 5584 | Py_MEMCPY(out, PyBytes_AS_STRING(rep), repsize); |
| 5585 | out += moreunits; |
| 5586 | } else /* rep is unicode */ { |
| 5587 | assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND); |
| 5588 | ucs1lib_utf16_encode(PyUnicode_1BYTE_DATA(rep), repsize, |
| 5589 | &out, native_ordering); |
| 5590 | } |
| 5591 | |
| 5592 | Py_CLEAR(rep); |
| 5593 | } |
| 5594 | |
| 5595 | /* Cut back to size actually needed. This is necessary for, for example, |
| 5596 | encoding of a string containing isolated surrogates and the 'ignore' handler |
| 5597 | is used. */ |
| 5598 | nsize = (unsigned char*) out - (unsigned char*) PyBytes_AS_STRING(v); |
| 5599 | if (nsize != PyBytes_GET_SIZE(v)) |
| 5600 | _PyBytes_Resize(&v, nsize); |
| 5601 | Py_XDECREF(errorHandler); |
| 5602 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5603 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5604 | return v; |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 5605 | error: |
| 5606 | Py_XDECREF(rep); |
| 5607 | Py_XDECREF(errorHandler); |
| 5608 | Py_XDECREF(exc); |
| 5609 | Py_XDECREF(v); |
| 5610 | return NULL; |
| 5611 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5614 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5615 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5616 | Py_ssize_t size, |
| 5617 | const char *errors, |
| 5618 | int byteorder) |
| 5619 | { |
| 5620 | PyObject *result; |
| 5621 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5622 | if (tmp == NULL) |
| 5623 | return NULL; |
| 5624 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5625 | Py_DECREF(tmp); |
| 5626 | return result; |
| 5627 | } |
| 5628 | |
| 5629 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5630 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5632 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5637 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5638 | if all the escapes in the string make it still a valid ASCII string. |
| 5639 | Returns -1 if any escapes were found which cause the string to |
| 5640 | pop out of ASCII range. Otherwise returns the length of the |
| 5641 | required buffer to hold the string. |
| 5642 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5643 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5644 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5645 | { |
| 5646 | const unsigned char *p = (const unsigned char *)s; |
| 5647 | const unsigned char *end = p + size; |
| 5648 | Py_ssize_t length = 0; |
| 5649 | |
| 5650 | if (size < 0) |
| 5651 | return -1; |
| 5652 | |
| 5653 | for (; p < end; ++p) { |
| 5654 | if (*p > 127) { |
| 5655 | /* Non-ASCII */ |
| 5656 | return -1; |
| 5657 | } |
| 5658 | else if (*p != '\\') { |
| 5659 | /* Normal character */ |
| 5660 | ++length; |
| 5661 | } |
| 5662 | else { |
| 5663 | /* Backslash-escape, check next char */ |
| 5664 | ++p; |
| 5665 | /* Escape sequence reaches till end of string or |
| 5666 | non-ASCII follow-up. */ |
| 5667 | if (p >= end || *p > 127) |
| 5668 | return -1; |
| 5669 | switch (*p) { |
| 5670 | case '\n': |
| 5671 | /* backslash + \n result in zero characters */ |
| 5672 | break; |
| 5673 | case '\\': case '\'': case '\"': |
| 5674 | case 'b': case 'f': case 't': |
| 5675 | case 'n': case 'r': case 'v': case 'a': |
| 5676 | ++length; |
| 5677 | break; |
| 5678 | case '0': case '1': case '2': case '3': |
| 5679 | case '4': case '5': case '6': case '7': |
| 5680 | case 'x': case 'u': case 'U': case 'N': |
| 5681 | /* these do not guarantee ASCII characters */ |
| 5682 | return -1; |
| 5683 | default: |
| 5684 | /* count the backslash + the other character */ |
| 5685 | length += 2; |
| 5686 | } |
| 5687 | } |
| 5688 | } |
| 5689 | return length; |
| 5690 | } |
| 5691 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5692 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5693 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5694 | PyObject * |
| 5695 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5696 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5697 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5699 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5700 | Py_ssize_t startinpos; |
| 5701 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5702 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5703 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5704 | char* message; |
| 5705 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5706 | PyObject *errorHandler = NULL; |
| 5707 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5708 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5709 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5710 | len = length_of_escaped_ascii_string(s, size); |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 5711 | if (len == 0) |
| 5712 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5713 | |
| 5714 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5715 | either the string is pure ASCII with named escapes like \n, etc. |
| 5716 | and we determined it's exact size (common case) |
| 5717 | or it contains \x, \u, ... escape sequences. then we create a |
| 5718 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5719 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5720 | if (len > 0) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5721 | writer.min_length = len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5722 | } |
| 5723 | else { |
| 5724 | /* Escaped strings will always be longer than the resulting |
| 5725 | Unicode string, so we start with size here and then reduce the |
| 5726 | length after conversion to the true value. |
| 5727 | (but if the error callback returns a long replacement string |
| 5728 | we'll have to allocate more space) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 5729 | writer.min_length = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5730 | } |
| 5731 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5733 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | while (s < end) { |
| 5737 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5738 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5739 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5740 | |
| 5741 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5742 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5743 | x = (unsigned char)*s; |
| 5744 | s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5745 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5746 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5747 | continue; |
| 5748 | } |
| 5749 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5750 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | /* \ - Escapes */ |
| 5752 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5753 | c = *s++; |
| 5754 | if (s > end) |
| 5755 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5756 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5757 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5758 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5759 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5760 | #define WRITECHAR(ch) \ |
| 5761 | do { \ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 5762 | if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5763 | goto onError; \ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5764 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5767 | case '\\': WRITECHAR('\\'); break; |
| 5768 | case '\'': WRITECHAR('\''); break; |
| 5769 | case '\"': WRITECHAR('\"'); break; |
| 5770 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5771 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5772 | case 'f': WRITECHAR('\014'); break; |
| 5773 | case 't': WRITECHAR('\t'); break; |
| 5774 | case 'n': WRITECHAR('\n'); break; |
| 5775 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5776 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5777 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5778 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5779 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5781 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | case '0': case '1': case '2': case '3': |
| 5783 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5784 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5785 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5786 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5787 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5788 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5790 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | break; |
| 5792 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5793 | /* hex escapes */ |
| 5794 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5796 | digits = 2; |
| 5797 | message = "truncated \\xXX escape"; |
| 5798 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5799 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5800 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5801 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5802 | digits = 4; |
| 5803 | message = "truncated \\uXXXX escape"; |
| 5804 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5805 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5806 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5807 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5808 | digits = 8; |
| 5809 | message = "truncated \\UXXXXXXXX escape"; |
| 5810 | hexescape: |
| 5811 | chr = 0; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5812 | if (end - s < digits) { |
| 5813 | /* count only hex digits */ |
| 5814 | for (; s < end; ++s) { |
| 5815 | c = (unsigned char)*s; |
| 5816 | if (!Py_ISXDIGIT(c)) |
| 5817 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5818 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5819 | goto error; |
| 5820 | } |
| 5821 | for (; digits--; ++s) { |
| 5822 | c = (unsigned char)*s; |
| 5823 | if (!Py_ISXDIGIT(c)) |
| 5824 | goto error; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5825 | chr = (chr<<4) & ~0xF; |
| 5826 | if (c >= '0' && c <= '9') |
| 5827 | chr += c - '0'; |
| 5828 | else if (c >= 'a' && c <= 'f') |
| 5829 | chr += 10 + c - 'a'; |
| 5830 | else |
| 5831 | chr += 10 + c - 'A'; |
| 5832 | } |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5833 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5834 | /* _decoding_error will have already written into the |
| 5835 | target buffer. */ |
| 5836 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5837 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5838 | /* when we get here, chr is a 32-bit unicode character */ |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5839 | message = "illegal Unicode character"; |
| 5840 | if (chr > MAX_UNICODE) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5841 | goto error; |
Serhiy Storchaka | 24193de | 2013-01-29 10:28:07 +0200 | [diff] [blame] | 5842 | WRITECHAR(chr); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5843 | break; |
| 5844 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5845 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5846 | case 'N': |
| 5847 | message = "malformed \\N character escape"; |
| 5848 | if (ucnhash_CAPI == NULL) { |
| 5849 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5850 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5851 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5852 | if (ucnhash_CAPI == NULL) |
| 5853 | goto ucnhashError; |
| 5854 | } |
| 5855 | if (*s == '{') { |
| 5856 | const char *start = s+1; |
| 5857 | /* look for the closing brace */ |
| 5858 | while (*s != '}' && s < end) |
| 5859 | s++; |
| 5860 | if (s > start && s < end && *s == '}') { |
| 5861 | /* found a name. look it up in the unicode database */ |
| 5862 | message = "unknown Unicode character name"; |
| 5863 | s++; |
Serhiy Storchaka | 4f5f0e5 | 2013-01-21 11:38:00 +0200 | [diff] [blame] | 5864 | if (s - start - 1 <= INT_MAX && |
Serhiy Storchaka | c35f3a9 | 2013-01-21 11:42:57 +0200 | [diff] [blame] | 5865 | ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5866 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5867 | goto store; |
| 5868 | } |
| 5869 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5870 | goto error; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5871 | |
| 5872 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5873 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5874 | message = "\\ at end of string"; |
| 5875 | s--; |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5876 | goto error; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5877 | } |
| 5878 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5879 | WRITECHAR('\\'); |
Serhiy Storchaka | 73e3880 | 2013-01-25 23:52:21 +0200 | [diff] [blame] | 5880 | WRITECHAR((unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5881 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5882 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | } |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5884 | continue; |
| 5885 | |
| 5886 | error: |
| 5887 | endinpos = s-starts; |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5888 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5889 | errors, &errorHandler, |
| 5890 | "unicodeescape", message, |
| 5891 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 8fe5a9f | 2013-01-29 10:37:39 +0200 | [diff] [blame] | 5892 | &writer)) |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 5893 | goto onError; |
| 5894 | continue; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5895 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5896 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5897 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5898 | Py_XDECREF(errorHandler); |
| 5899 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5900 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5901 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5902 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5903 | PyErr_SetString( |
| 5904 | PyExc_UnicodeError, |
| 5905 | "\\N escapes not supported (can't load unicodedata module)" |
| 5906 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5907 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5908 | Py_XDECREF(errorHandler); |
| 5909 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5910 | return NULL; |
| 5911 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5912 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5913 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5914 | Py_XDECREF(errorHandler); |
| 5915 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5916 | return NULL; |
| 5917 | } |
| 5918 | |
| 5919 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5920 | |
| 5921 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5922 | appropriate. |
| 5923 | |
| 5924 | */ |
| 5925 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5926 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5927 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5929 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5930 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5932 | int kind; |
| 5933 | void *data; |
| 5934 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5936 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5937 | escape. |
| 5938 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5939 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5940 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5941 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5942 | */ |
| 5943 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5944 | if (!PyUnicode_Check(unicode)) { |
| 5945 | PyErr_BadArgument(); |
| 5946 | return NULL; |
| 5947 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5948 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5949 | return NULL; |
| 5950 | len = PyUnicode_GET_LENGTH(unicode); |
| 5951 | kind = PyUnicode_KIND(unicode); |
| 5952 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5953 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5954 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5955 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5956 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5957 | } |
| 5958 | |
| 5959 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5960 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5961 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5962 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5963 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5964 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5965 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5966 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5967 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5968 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | if (repr == NULL) |
| 5970 | return NULL; |
| 5971 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5972 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5974 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5975 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5976 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5977 | /* Escape backslashes */ |
| 5978 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5979 | *p++ = '\\'; |
| 5980 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5981 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5982 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5983 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5984 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5985 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5986 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5987 | *p++ = '\\'; |
| 5988 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5989 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5990 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5991 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5992 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5993 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5994 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5995 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5996 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5997 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5998 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 6001 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6002 | *p++ = '\\'; |
| 6003 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6004 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 6005 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 6006 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6007 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6009 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6010 | /* Map special whitespace to '\t', \n', '\r' */ |
| 6011 | else if (ch == '\t') { |
| 6012 | *p++ = '\\'; |
| 6013 | *p++ = 't'; |
| 6014 | } |
| 6015 | else if (ch == '\n') { |
| 6016 | *p++ = '\\'; |
| 6017 | *p++ = 'n'; |
| 6018 | } |
| 6019 | else if (ch == '\r') { |
| 6020 | *p++ = '\\'; |
| 6021 | *p++ = 'r'; |
| 6022 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6023 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6024 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 6025 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6026 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 6027 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6028 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 6029 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6030 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 6031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6032 | /* Copy everything else as-is */ |
| 6033 | else |
| 6034 | *p++ = (char) ch; |
| 6035 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6037 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 6038 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 6039 | return NULL; |
| 6040 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | } |
| 6042 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6043 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6044 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 6045 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6047 | PyObject *result; |
| 6048 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6049 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6051 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 6052 | Py_DECREF(tmp); |
| 6053 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 6057 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6058 | PyObject * |
| 6059 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6060 | Py_ssize_t size, |
| 6061 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6063 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6064 | Py_ssize_t startinpos; |
| 6065 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6066 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | const char *end; |
| 6068 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6069 | PyObject *errorHandler = NULL; |
| 6070 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6071 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6072 | if (size == 0) |
| 6073 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | /* Escaped strings will always be longer than the resulting |
| 6076 | 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] | 6077 | length after conversion to the true value. (But decoding error |
| 6078 | handler might have to resize the string) */ |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6079 | _PyUnicodeWriter_Init(&writer); |
| 6080 | writer.min_length = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6081 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | end = s + size; |
| 6083 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6084 | unsigned char c; |
| 6085 | Py_UCS4 x; |
| 6086 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6087 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6089 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 6090 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6091 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6092 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6093 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6094 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6095 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6096 | startinpos = s-starts; |
| 6097 | |
| 6098 | /* \u-escapes are only interpreted iff the number of leading |
| 6099 | backslashes if odd */ |
| 6100 | bs = s; |
| 6101 | for (;s < end;) { |
| 6102 | if (*s != '\\') |
| 6103 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6104 | x = (unsigned char)*s++; |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6105 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6106 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6107 | } |
| 6108 | if (((s - bs) & 1) == 0 || |
| 6109 | s >= end || |
| 6110 | (*s != 'u' && *s != 'U')) { |
| 6111 | continue; |
| 6112 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6113 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6114 | count = *s=='u' ? 4 : 8; |
| 6115 | s++; |
| 6116 | |
| 6117 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 6119 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 6120 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6121 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6122 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6123 | errors, &errorHandler, |
| 6124 | "rawunicodeescape", "truncated \\uXXXX", |
| 6125 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6126 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6127 | goto onError; |
| 6128 | goto nextByte; |
| 6129 | } |
| 6130 | x = (x<<4) & ~0xF; |
| 6131 | if (c >= '0' && c <= '9') |
| 6132 | x += c - '0'; |
| 6133 | else if (c >= 'a' && c <= 'f') |
| 6134 | x += 10 + c - 'a'; |
| 6135 | else |
| 6136 | x += 10 + c - 'A'; |
| 6137 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6138 | if (x <= MAX_UNICODE) { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6139 | if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6140 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6141 | } |
| 6142 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 6143 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6144 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6145 | errors, &errorHandler, |
| 6146 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6147 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6148 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6150 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6151 | nextByte: |
| 6152 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6154 | Py_XDECREF(errorHandler); |
| 6155 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6156 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6157 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6158 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6159 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6160 | Py_XDECREF(errorHandler); |
| 6161 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | return NULL; |
| 6163 | } |
| 6164 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6165 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6166 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6167 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6169 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | char *p; |
| 6171 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6172 | Py_ssize_t expandsize, pos; |
| 6173 | int kind; |
| 6174 | void *data; |
| 6175 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6176 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6177 | if (!PyUnicode_Check(unicode)) { |
| 6178 | PyErr_BadArgument(); |
| 6179 | return NULL; |
| 6180 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6181 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6182 | return NULL; |
| 6183 | kind = PyUnicode_KIND(unicode); |
| 6184 | data = PyUnicode_DATA(unicode); |
| 6185 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 6186 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 6187 | bytes, and 1 byte characters 4. */ |
| 6188 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 6189 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6190 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6192 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6193 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | if (repr == NULL) |
| 6195 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6196 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6197 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6198 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6199 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6200 | for (pos = 0; pos < len; pos++) { |
| 6201 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 6203 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6204 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 6205 | *p++ = '\\'; |
| 6206 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6207 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 6208 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 6209 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 6210 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 6211 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6212 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6213 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6214 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6215 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6216 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6217 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | *p++ = '\\'; |
| 6219 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 6220 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 6221 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 6222 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 6223 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6225 | /* Copy everything else as-is */ |
| 6226 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6227 | *p++ = (char) ch; |
| 6228 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6229 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6230 | assert(p > q); |
| 6231 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6232 | return NULL; |
| 6233 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6236 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6237 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 6238 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6239 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6240 | PyObject *result; |
| 6241 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 6242 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6243 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 6244 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 6245 | Py_DECREF(tmp); |
| 6246 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6249 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6250 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6251 | PyObject * |
| 6252 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6253 | Py_ssize_t size, |
| 6254 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6255 | { |
| 6256 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6257 | Py_ssize_t startinpos; |
| 6258 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6259 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6260 | const char *end; |
| 6261 | const char *reason; |
| 6262 | PyObject *errorHandler = NULL; |
| 6263 | PyObject *exc = NULL; |
| 6264 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6265 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 6266 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6267 | 1)) |
| 6268 | return NULL; |
| 6269 | |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 6270 | if (size == 0) |
| 6271 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6272 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6273 | _PyUnicodeWriter_Init(&writer); |
| 6274 | if (size / Py_UNICODE_SIZE > PY_SSIZE_T_MAX - 1) { |
| 6275 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6276 | goto onError; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6277 | } |
| 6278 | writer.min_length = (size + (Py_UNICODE_SIZE - 1)) / Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6279 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6280 | end = s + size; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6281 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6282 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6283 | Py_UCS4 ch; |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6284 | if (end - s < Py_UNICODE_SIZE) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6285 | endinpos = end-starts; |
| 6286 | reason = "truncated input"; |
| 6287 | goto error; |
| 6288 | } |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6289 | /* We copy the raw representation one byte at a time because the |
| 6290 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6291 | ((char *) &uch)[0] = s[0]; |
| 6292 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6293 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6294 | ((char *) &uch)[2] = s[2]; |
| 6295 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6296 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6297 | ch = uch; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6298 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6299 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6300 | some malformed UCS-4 data. */ |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6301 | if (ch > 0x10ffff) { |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6302 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6303 | reason = "illegal code point (> 0x10FFFF)"; |
| 6304 | goto error; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6305 | } |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6306 | #endif |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6307 | s += Py_UNICODE_SIZE; |
| 6308 | #ifndef Py_UNICODE_WIDE |
Serhiy Storchaka | 03ee12e | 2013-02-07 16:25:25 +0200 | [diff] [blame] | 6309 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && end - s >= Py_UNICODE_SIZE) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6310 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6311 | Py_UNICODE uch2; |
| 6312 | ((char *) &uch2)[0] = s[0]; |
| 6313 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6314 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6315 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6316 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6317 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6318 | } |
| 6319 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6320 | #endif |
| 6321 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 6322 | if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6323 | goto onError; |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6324 | continue; |
| 6325 | |
| 6326 | error: |
| 6327 | startinpos = s - starts; |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6328 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6329 | errors, &errorHandler, |
| 6330 | "unicode_internal", reason, |
| 6331 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | d0c79dc | 2013-02-07 16:26:55 +0200 | [diff] [blame] | 6332 | &writer)) |
Serhiy Storchaka | 3fd4ab3 | 2013-02-07 16:23:21 +0200 | [diff] [blame] | 6333 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6334 | } |
| 6335 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6336 | Py_XDECREF(errorHandler); |
| 6337 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6338 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6339 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6340 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6341 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6342 | Py_XDECREF(errorHandler); |
| 6343 | Py_XDECREF(exc); |
| 6344 | return NULL; |
| 6345 | } |
| 6346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6347 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6348 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6349 | PyObject * |
| 6350 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6351 | Py_ssize_t size, |
| 6352 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6353 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6355 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6356 | } |
| 6357 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6358 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6359 | static void |
| 6360 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6361 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6362 | PyObject *unicode, |
| 6363 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6364 | const char *reason) |
| 6365 | { |
| 6366 | if (*exceptionObject == NULL) { |
| 6367 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6368 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6369 | encoding, unicode, startpos, endpos, reason); |
| 6370 | } |
| 6371 | else { |
| 6372 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6373 | goto onError; |
| 6374 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6375 | goto onError; |
| 6376 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6377 | goto onError; |
| 6378 | return; |
| 6379 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 6380 | Py_CLEAR(*exceptionObject); |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6381 | } |
| 6382 | } |
| 6383 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6384 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6385 | static void |
| 6386 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6387 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6388 | PyObject *unicode, |
| 6389 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6390 | const char *reason) |
| 6391 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6392 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6393 | encoding, unicode, startpos, endpos, reason); |
| 6394 | if (*exceptionObject != NULL) |
| 6395 | PyCodec_StrictErrors(*exceptionObject); |
| 6396 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6397 | |
| 6398 | /* error handling callback helper: |
| 6399 | build arguments, call the callback and check the arguments, |
| 6400 | put the result into newpos and return the replacement string, which |
| 6401 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6402 | static PyObject * |
| 6403 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6404 | PyObject **errorHandler, |
| 6405 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6406 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6407 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6408 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6409 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6410 | 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] | 6411 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6412 | PyObject *restuple; |
| 6413 | PyObject *resunicode; |
| 6414 | |
| 6415 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6416 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6417 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6418 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6421 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6422 | return NULL; |
| 6423 | len = PyUnicode_GET_LENGTH(unicode); |
| 6424 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6425 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6426 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6427 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6428 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6429 | |
| 6430 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6431 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6432 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6433 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6434 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6435 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6436 | Py_DECREF(restuple); |
| 6437 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6438 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6439 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6440 | &resunicode, newpos)) { |
| 6441 | Py_DECREF(restuple); |
| 6442 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6443 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6444 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6445 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6446 | Py_DECREF(restuple); |
| 6447 | return NULL; |
| 6448 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6449 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6450 | *newpos = len + *newpos; |
| 6451 | if (*newpos<0 || *newpos>len) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 6452 | 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] | 6453 | Py_DECREF(restuple); |
| 6454 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6455 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6456 | Py_INCREF(resunicode); |
| 6457 | Py_DECREF(restuple); |
| 6458 | return resunicode; |
| 6459 | } |
| 6460 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6461 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6462 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6463 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6464 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6465 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6466 | /* input state */ |
| 6467 | Py_ssize_t pos=0, size; |
| 6468 | int kind; |
| 6469 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6470 | /* output object */ |
| 6471 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6472 | /* pointer into the output */ |
| 6473 | char *str; |
| 6474 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6475 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6476 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6477 | 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] | 6478 | PyObject *errorHandler = NULL; |
| 6479 | PyObject *exc = NULL; |
| 6480 | /* the following variable is used for caching string comparisons |
| 6481 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6482 | int known_errorHandler = -1; |
| 6483 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6484 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6485 | return NULL; |
| 6486 | size = PyUnicode_GET_LENGTH(unicode); |
| 6487 | kind = PyUnicode_KIND(unicode); |
| 6488 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6489 | /* allocate enough for a simple encoding without |
| 6490 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6491 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6492 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6493 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6494 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6495 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6496 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6497 | ressize = size; |
| 6498 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6499 | while (pos < size) { |
| 6500 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6501 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6502 | /* can we encode this? */ |
| 6503 | if (c<limit) { |
| 6504 | /* no overflow check, because we know that the space is enough */ |
| 6505 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6506 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6507 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6508 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6509 | Py_ssize_t requiredsize; |
| 6510 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6511 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6512 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6513 | Py_ssize_t collstart = pos; |
| 6514 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6515 | /* find all unecodable characters */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6516 | while ((collend < size) && (PyUnicode_READ(kind, data, collend) >= limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6517 | ++collend; |
| 6518 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6519 | if (known_errorHandler==-1) { |
| 6520 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6521 | known_errorHandler = 1; |
| 6522 | else if (!strcmp(errors, "replace")) |
| 6523 | known_errorHandler = 2; |
| 6524 | else if (!strcmp(errors, "ignore")) |
| 6525 | known_errorHandler = 3; |
| 6526 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6527 | known_errorHandler = 4; |
| 6528 | else |
| 6529 | known_errorHandler = 0; |
| 6530 | } |
| 6531 | switch (known_errorHandler) { |
| 6532 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6533 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6534 | goto onError; |
| 6535 | case 2: /* replace */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6536 | while (collstart++ < collend) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6537 | *str++ = '?'; /* fall through */ |
| 6538 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6539 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6540 | break; |
| 6541 | case 4: /* xmlcharrefreplace */ |
| 6542 | respos = str - PyBytes_AS_STRING(res); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6543 | requiredsize = respos; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6544 | /* determine replacement size */ |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6545 | for (i = collstart; i < collend; ++i) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6546 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6547 | Py_ssize_t incr; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6548 | if (ch < 10) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6549 | incr = 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6550 | else if (ch < 100) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6551 | incr = 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6552 | else if (ch < 1000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6553 | incr = 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6554 | else if (ch < 10000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6555 | incr = 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6556 | else if (ch < 100000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6557 | incr = 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6558 | else if (ch < 1000000) |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6559 | incr = 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6560 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6561 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6562 | incr = 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6563 | } |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6564 | if (requiredsize > PY_SSIZE_T_MAX - incr) |
| 6565 | goto overflow; |
| 6566 | requiredsize += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6567 | } |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6568 | if (requiredsize > PY_SSIZE_T_MAX - (size - collend)) |
| 6569 | goto overflow; |
| 6570 | requiredsize += size - collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6571 | if (requiredsize > ressize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6572 | if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6573 | requiredsize = 2*ressize; |
| 6574 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6575 | goto onError; |
| 6576 | str = PyBytes_AS_STRING(res) + respos; |
| 6577 | ressize = requiredsize; |
| 6578 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6579 | /* generate replacement */ |
| 6580 | for (i = collstart; i < collend; ++i) { |
| 6581 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6582 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6583 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6584 | break; |
| 6585 | default: |
| 6586 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6587 | encoding, reason, unicode, &exc, |
| 6588 | collstart, collend, &newpos); |
| 6589 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6590 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6591 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6592 | if (PyBytes_Check(repunicode)) { |
| 6593 | /* Directly copy bytes result to output. */ |
| 6594 | repsize = PyBytes_Size(repunicode); |
| 6595 | if (repsize > 1) { |
| 6596 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6597 | respos = str - PyBytes_AS_STRING(res); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6598 | if (ressize > PY_SSIZE_T_MAX - repsize - 1) { |
| 6599 | Py_DECREF(repunicode); |
| 6600 | goto overflow; |
| 6601 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6602 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6603 | Py_DECREF(repunicode); |
| 6604 | goto onError; |
| 6605 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6606 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6607 | ressize += repsize-1; |
| 6608 | } |
| 6609 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6610 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6611 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6612 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6613 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6614 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | /* need more space? (at least enough for what we |
| 6616 | have+the replacement+the rest of the string, so |
| 6617 | we won't have to check space for encodable characters) */ |
| 6618 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6619 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6620 | requiredsize = respos; |
| 6621 | if (requiredsize > PY_SSIZE_T_MAX - repsize) |
| 6622 | goto overflow; |
| 6623 | requiredsize += repsize; |
| 6624 | if (requiredsize > PY_SSIZE_T_MAX - (size - collend)) |
| 6625 | goto overflow; |
| 6626 | requiredsize += size - collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6627 | if (requiredsize > ressize) { |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6628 | if (ressize <= PY_SSIZE_T_MAX/2 && requiredsize < 2*ressize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6629 | requiredsize = 2*ressize; |
| 6630 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6631 | Py_DECREF(repunicode); |
| 6632 | goto onError; |
| 6633 | } |
| 6634 | str = PyBytes_AS_STRING(res) + respos; |
| 6635 | ressize = requiredsize; |
| 6636 | } |
| 6637 | /* check if there is anything unencodable in the replacement |
| 6638 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6639 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6640 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6641 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6642 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6643 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6644 | Py_DECREF(repunicode); |
| 6645 | goto onError; |
| 6646 | } |
| 6647 | *str = (char)c; |
| 6648 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6649 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6650 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6651 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6652 | } |
| 6653 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6654 | /* Resize if we allocated to much */ |
| 6655 | size = str - PyBytes_AS_STRING(res); |
| 6656 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6657 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6658 | if (_PyBytes_Resize(&res, size) < 0) |
| 6659 | goto onError; |
| 6660 | } |
| 6661 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6662 | Py_XDECREF(errorHandler); |
| 6663 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6664 | return res; |
| 6665 | |
Benjamin Peterson | a1c1be4 | 2014-09-29 18:18:57 -0400 | [diff] [blame] | 6666 | overflow: |
| 6667 | PyErr_SetString(PyExc_OverflowError, |
| 6668 | "encoded result is too long for a Python string"); |
| 6669 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6670 | onError: |
| 6671 | Py_XDECREF(res); |
| 6672 | Py_XDECREF(errorHandler); |
| 6673 | Py_XDECREF(exc); |
| 6674 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6675 | } |
| 6676 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6677 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6678 | PyObject * |
| 6679 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6680 | Py_ssize_t size, |
| 6681 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6683 | PyObject *result; |
| 6684 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6685 | if (unicode == NULL) |
| 6686 | return NULL; |
| 6687 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6688 | Py_DECREF(unicode); |
| 6689 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | } |
| 6691 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6692 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6693 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | { |
| 6695 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6696 | PyErr_BadArgument(); |
| 6697 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6699 | if (PyUnicode_READY(unicode) == -1) |
| 6700 | return NULL; |
| 6701 | /* Fast path: if it is a one-byte string, construct |
| 6702 | bytes object directly. */ |
| 6703 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6704 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6705 | PyUnicode_GET_LENGTH(unicode)); |
| 6706 | /* Non-Latin-1 characters present. Defer to above function to |
| 6707 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6708 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6709 | } |
| 6710 | |
| 6711 | PyObject* |
| 6712 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6713 | { |
| 6714 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6715 | } |
| 6716 | |
| 6717 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6718 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6719 | PyObject * |
| 6720 | PyUnicode_DecodeASCII(const char *s, |
| 6721 | Py_ssize_t size, |
| 6722 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6724 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6725 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6726 | int kind; |
| 6727 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6728 | Py_ssize_t startinpos; |
| 6729 | Py_ssize_t endinpos; |
| 6730 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6731 | const char *e; |
| 6732 | PyObject *errorHandler = NULL; |
| 6733 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | if (size == 0) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 6736 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6739 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6740 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6741 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6742 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 6743 | writer.min_length = size; |
| 6744 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) < 0) |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 6745 | return NULL; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6746 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6747 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6748 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6749 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6750 | writer.pos = outpos; |
| 6751 | if (writer.pos == size) |
| 6752 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6753 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6754 | s += writer.pos; |
| 6755 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6756 | while (s < e) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 6757 | unsigned char c = (unsigned char)*s; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6758 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6759 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6760 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6761 | ++s; |
| 6762 | } |
| 6763 | else { |
| 6764 | startinpos = s-starts; |
| 6765 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6766 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | errors, &errorHandler, |
| 6768 | "ascii", "ordinal not in range(128)", |
| 6769 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6770 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6771 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6772 | kind = writer.kind; |
| 6773 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6774 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6776 | Py_XDECREF(errorHandler); |
| 6777 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6778 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6779 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6780 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6781 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6782 | Py_XDECREF(errorHandler); |
| 6783 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | return NULL; |
| 6785 | } |
| 6786 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6787 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6788 | PyObject * |
| 6789 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6790 | Py_ssize_t size, |
| 6791 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6793 | PyObject *result; |
| 6794 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6795 | if (unicode == NULL) |
| 6796 | return NULL; |
| 6797 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6798 | Py_DECREF(unicode); |
| 6799 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6800 | } |
| 6801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6802 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6803 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | { |
| 6805 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6806 | PyErr_BadArgument(); |
| 6807 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6808 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6809 | if (PyUnicode_READY(unicode) == -1) |
| 6810 | return NULL; |
| 6811 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6812 | directly. Else defer to above function to raise the exception. */ |
Victor Stinner | af03757 | 2013-04-14 18:44:10 +0200 | [diff] [blame] | 6813 | if (PyUnicode_IS_ASCII(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6814 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6815 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6816 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6817 | } |
| 6818 | |
| 6819 | PyObject * |
| 6820 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6821 | { |
| 6822 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | } |
| 6824 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6825 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6826 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6827 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6828 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6829 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6830 | #define NEED_RETRY |
| 6831 | #endif |
| 6832 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6833 | #ifndef WC_ERR_INVALID_CHARS |
| 6834 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6835 | #endif |
| 6836 | |
| 6837 | static char* |
| 6838 | code_page_name(UINT code_page, PyObject **obj) |
| 6839 | { |
| 6840 | *obj = NULL; |
| 6841 | if (code_page == CP_ACP) |
| 6842 | return "mbcs"; |
| 6843 | if (code_page == CP_UTF7) |
| 6844 | return "CP_UTF7"; |
| 6845 | if (code_page == CP_UTF8) |
| 6846 | return "CP_UTF8"; |
| 6847 | |
| 6848 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6849 | if (*obj == NULL) |
| 6850 | return NULL; |
| 6851 | return PyBytes_AS_STRING(*obj); |
| 6852 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6853 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6854 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6855 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6856 | { |
| 6857 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6858 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6859 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6860 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6861 | return 0; |
| 6862 | |
| 6863 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6864 | if (prev == curr) |
| 6865 | return 1; |
| 6866 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6867 | as it assumes an incomplete character consists of a single |
| 6868 | byte. */ |
| 6869 | if (curr - prev == 2) |
| 6870 | return 1; |
| 6871 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6872 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6873 | return 0; |
| 6874 | } |
| 6875 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6876 | static DWORD |
| 6877 | decode_code_page_flags(UINT code_page) |
| 6878 | { |
| 6879 | if (code_page == CP_UTF7) { |
| 6880 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6881 | return 0; |
| 6882 | } |
| 6883 | else |
| 6884 | return MB_ERR_INVALID_CHARS; |
| 6885 | } |
| 6886 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6887 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6888 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6889 | * mode. |
| 6890 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6891 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6892 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6893 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6894 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6895 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6896 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6897 | const char *in, |
| 6898 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6899 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6900 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6901 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6902 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6903 | |
| 6904 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6905 | assert(insize > 0); |
| 6906 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6907 | if (outsize <= 0) |
| 6908 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6909 | |
| 6910 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6911 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6912 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6913 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6914 | if (*v == NULL) |
| 6915 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6916 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6917 | } |
| 6918 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6919 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6920 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6921 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6922 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6923 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
| 6926 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6927 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6928 | if (outsize <= 0) |
| 6929 | goto error; |
| 6930 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6931 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6932 | error: |
| 6933 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6934 | return -2; |
| 6935 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6936 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6937 | } |
| 6938 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6939 | /* |
| 6940 | * Decode a byte string from a code page into unicode object with an error |
| 6941 | * handler. |
| 6942 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6943 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6944 | * UnicodeDecodeError exception and returns -1 on error. |
| 6945 | */ |
| 6946 | static int |
| 6947 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6948 | PyObject **v, |
| 6949 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6950 | const char *errors) |
| 6951 | { |
| 6952 | const char *startin = in; |
| 6953 | const char *endin = in + size; |
| 6954 | const DWORD flags = decode_code_page_flags(code_page); |
| 6955 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6956 | 2000 English version of the message. */ |
| 6957 | const char *reason = "No mapping for the Unicode character exists " |
| 6958 | "in the target code page."; |
| 6959 | /* each step cannot decode more than 1 character, but a character can be |
| 6960 | represented as a surrogate pair */ |
| 6961 | wchar_t buffer[2], *startout, *out; |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 6962 | int insize; |
| 6963 | Py_ssize_t outsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6964 | PyObject *errorHandler = NULL; |
| 6965 | PyObject *exc = NULL; |
| 6966 | PyObject *encoding_obj = NULL; |
| 6967 | char *encoding; |
| 6968 | DWORD err; |
| 6969 | int ret = -1; |
| 6970 | |
| 6971 | assert(size > 0); |
| 6972 | |
| 6973 | encoding = code_page_name(code_page, &encoding_obj); |
| 6974 | if (encoding == NULL) |
| 6975 | return -1; |
| 6976 | |
| 6977 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6978 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6979 | UnicodeDecodeError. */ |
| 6980 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6981 | if (exc != NULL) { |
| 6982 | PyCodec_StrictErrors(exc); |
| 6983 | Py_CLEAR(exc); |
| 6984 | } |
| 6985 | goto error; |
| 6986 | } |
| 6987 | |
| 6988 | if (*v == NULL) { |
| 6989 | /* Create unicode object */ |
| 6990 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6991 | PyErr_NoMemory(); |
| 6992 | goto error; |
| 6993 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6994 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6995 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6996 | if (*v == NULL) |
| 6997 | goto error; |
| 6998 | startout = PyUnicode_AS_UNICODE(*v); |
| 6999 | } |
| 7000 | else { |
| 7001 | /* Extend unicode object */ |
| 7002 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 7003 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 7004 | PyErr_NoMemory(); |
| 7005 | goto error; |
| 7006 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7007 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7008 | goto error; |
| 7009 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 7010 | } |
| 7011 | |
| 7012 | /* Decode the byte string character per character */ |
| 7013 | out = startout; |
| 7014 | while (in < endin) |
| 7015 | { |
| 7016 | /* Decode a character */ |
| 7017 | insize = 1; |
| 7018 | do |
| 7019 | { |
| 7020 | outsize = MultiByteToWideChar(code_page, flags, |
| 7021 | in, insize, |
| 7022 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 7023 | if (outsize > 0) |
| 7024 | break; |
| 7025 | err = GetLastError(); |
| 7026 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 7027 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 7028 | { |
| 7029 | PyErr_SetFromWindowsErr(0); |
| 7030 | goto error; |
| 7031 | } |
| 7032 | insize++; |
| 7033 | } |
| 7034 | /* 4=maximum length of a UTF-8 sequence */ |
| 7035 | while (insize <= 4 && (in + insize) <= endin); |
| 7036 | |
| 7037 | if (outsize <= 0) { |
| 7038 | Py_ssize_t startinpos, endinpos, outpos; |
| 7039 | |
| 7040 | startinpos = in - startin; |
| 7041 | endinpos = startinpos + 1; |
| 7042 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7043 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7044 | errors, &errorHandler, |
| 7045 | encoding, reason, |
| 7046 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7047 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7048 | { |
| 7049 | goto error; |
| 7050 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 7051 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7052 | } |
| 7053 | else { |
| 7054 | in += insize; |
| 7055 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 7056 | out += outsize; |
| 7057 | } |
| 7058 | } |
| 7059 | |
| 7060 | /* write a NUL character at the end */ |
| 7061 | *out = 0; |
| 7062 | |
| 7063 | /* Extend unicode object */ |
| 7064 | outsize = out - startout; |
| 7065 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 7066 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7067 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7068 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7069 | |
| 7070 | error: |
| 7071 | Py_XDECREF(encoding_obj); |
| 7072 | Py_XDECREF(errorHandler); |
| 7073 | Py_XDECREF(exc); |
| 7074 | return ret; |
| 7075 | } |
| 7076 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7077 | static PyObject * |
| 7078 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7079 | const char *s, Py_ssize_t size, |
| 7080 | const char *errors, Py_ssize_t *consumed) |
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 | PyObject *v = NULL; |
| 7083 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7084 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7085 | if (code_page < 0) { |
| 7086 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7087 | return NULL; |
| 7088 | } |
| 7089 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7090 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7091 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7092 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7093 | do |
| 7094 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7095 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7096 | if (size > INT_MAX) { |
| 7097 | chunk_size = INT_MAX; |
| 7098 | final = 0; |
| 7099 | done = 0; |
| 7100 | } |
| 7101 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7102 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7103 | { |
| 7104 | chunk_size = (int)size; |
| 7105 | final = (consumed == NULL); |
| 7106 | done = 1; |
| 7107 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7108 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7109 | /* Skip trailing lead-byte unless 'final' is set */ |
| 7110 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 7111 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7112 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7113 | if (chunk_size == 0 && done) { |
| 7114 | if (v != NULL) |
| 7115 | break; |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 7116 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7117 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7118 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7119 | |
| 7120 | converted = decode_code_page_strict(code_page, &v, |
| 7121 | s, chunk_size); |
| 7122 | if (converted == -2) |
| 7123 | converted = decode_code_page_errors(code_page, &v, |
| 7124 | s, chunk_size, |
| 7125 | errors); |
| 7126 | assert(converted != 0); |
| 7127 | |
| 7128 | if (converted < 0) { |
| 7129 | Py_XDECREF(v); |
| 7130 | return NULL; |
| 7131 | } |
| 7132 | |
| 7133 | if (consumed) |
| 7134 | *consumed += converted; |
| 7135 | |
| 7136 | s += converted; |
| 7137 | size -= converted; |
| 7138 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7139 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 7140 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7141 | } |
| 7142 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7143 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7144 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 7145 | const char *s, |
| 7146 | Py_ssize_t size, |
| 7147 | const char *errors, |
| 7148 | Py_ssize_t *consumed) |
| 7149 | { |
| 7150 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 7151 | } |
| 7152 | |
| 7153 | PyObject * |
| 7154 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 7155 | Py_ssize_t size, |
| 7156 | const char *errors, |
| 7157 | Py_ssize_t *consumed) |
| 7158 | { |
| 7159 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 7160 | } |
| 7161 | |
| 7162 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7163 | PyUnicode_DecodeMBCS(const char *s, |
| 7164 | Py_ssize_t size, |
| 7165 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7166 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7167 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 7168 | } |
| 7169 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7170 | static DWORD |
| 7171 | encode_code_page_flags(UINT code_page, const char *errors) |
| 7172 | { |
| 7173 | if (code_page == CP_UTF8) { |
| 7174 | if (winver.dwMajorVersion >= 6) |
| 7175 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 7176 | and later */ |
| 7177 | return WC_ERR_INVALID_CHARS; |
| 7178 | else |
| 7179 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 7180 | return 0; |
| 7181 | } |
| 7182 | else if (code_page == CP_UTF7) { |
| 7183 | /* CP_UTF7 only supports flags=0 */ |
| 7184 | return 0; |
| 7185 | } |
| 7186 | else { |
| 7187 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 7188 | return 0; |
| 7189 | else |
| 7190 | return WC_NO_BEST_FIT_CHARS; |
| 7191 | } |
| 7192 | } |
| 7193 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7194 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7195 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 7196 | * mode. |
| 7197 | * |
| 7198 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7199 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7200 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7201 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7202 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7203 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7204 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7205 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7206 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7207 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 7208 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7209 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 7210 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7211 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7212 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 7213 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7214 | /* Create a substring so that we can get the UTF-16 representation |
| 7215 | of just the slice under consideration. */ |
| 7216 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7217 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7218 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7219 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7220 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7221 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7222 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7223 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7224 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7225 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 7226 | if (substring == NULL) |
| 7227 | return -1; |
| 7228 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 7229 | if (p == NULL) { |
| 7230 | Py_DECREF(substring); |
| 7231 | return -1; |
| 7232 | } |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7233 | assert(size <= INT_MAX); |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7234 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7235 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7236 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7237 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7238 | NULL, 0, |
| 7239 | NULL, pusedDefaultChar); |
| 7240 | if (outsize <= 0) |
| 7241 | goto error; |
| 7242 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7243 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 7244 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7245 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7246 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7247 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7248 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7249 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7250 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7251 | if (*outbytes == NULL) { |
| 7252 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7253 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7254 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7255 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7256 | } |
| 7257 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7258 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7259 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7260 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 7261 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7262 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7263 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7264 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7265 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 7266 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7267 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7268 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7269 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7270 | } |
| 7271 | |
| 7272 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7273 | outsize = WideCharToMultiByte(code_page, flags, |
Victor Stinner | 9f067f4 | 2013-06-05 00:21:31 +0200 | [diff] [blame] | 7274 | p, (int)size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7275 | out, outsize, |
| 7276 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7277 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7278 | if (outsize <= 0) |
| 7279 | goto error; |
| 7280 | if (pusedDefaultChar && *pusedDefaultChar) |
| 7281 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7282 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7283 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7284 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7285 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7286 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 7287 | return -2; |
| 7288 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 7289 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7290 | } |
| 7291 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7292 | /* |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame^] | 7293 | * Encode a Unicode string to a Windows code page into a byte string using an |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7294 | * error handler. |
| 7295 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7296 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7297 | * -1 on other error. |
| 7298 | */ |
| 7299 | static int |
| 7300 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7301 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7302 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7303 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7304 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7305 | Py_ssize_t pos = unicode_offset; |
| 7306 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7307 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7308 | 2000 English version of the message. */ |
| 7309 | const char *reason = "invalid character"; |
| 7310 | /* 4=maximum length of a UTF-8 sequence */ |
| 7311 | char buffer[4]; |
| 7312 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7313 | Py_ssize_t outsize; |
| 7314 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7315 | PyObject *errorHandler = NULL; |
| 7316 | PyObject *exc = NULL; |
| 7317 | PyObject *encoding_obj = NULL; |
| 7318 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7319 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7320 | PyObject *rep; |
| 7321 | int ret = -1; |
| 7322 | |
| 7323 | assert(insize > 0); |
| 7324 | |
| 7325 | encoding = code_page_name(code_page, &encoding_obj); |
| 7326 | if (encoding == NULL) |
| 7327 | return -1; |
| 7328 | |
| 7329 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7330 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7331 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7332 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7333 | if (exc != NULL) { |
| 7334 | PyCodec_StrictErrors(exc); |
| 7335 | Py_DECREF(exc); |
| 7336 | } |
| 7337 | Py_XDECREF(encoding_obj); |
| 7338 | return -1; |
| 7339 | } |
| 7340 | |
| 7341 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7342 | pusedDefaultChar = &usedDefaultChar; |
| 7343 | else |
| 7344 | pusedDefaultChar = NULL; |
| 7345 | |
| 7346 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7347 | PyErr_NoMemory(); |
| 7348 | goto error; |
| 7349 | } |
| 7350 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7351 | |
| 7352 | if (*outbytes == NULL) { |
| 7353 | /* Create string object */ |
| 7354 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7355 | if (*outbytes == NULL) |
| 7356 | goto error; |
| 7357 | out = PyBytes_AS_STRING(*outbytes); |
| 7358 | } |
| 7359 | else { |
| 7360 | /* Extend string object */ |
| 7361 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7362 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7363 | PyErr_NoMemory(); |
| 7364 | goto error; |
| 7365 | } |
| 7366 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7367 | goto error; |
| 7368 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7369 | } |
| 7370 | |
| 7371 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7372 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7373 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7374 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7375 | wchar_t chars[2]; |
| 7376 | int charsize; |
| 7377 | if (ch < 0x10000) { |
| 7378 | chars[0] = (wchar_t)ch; |
| 7379 | charsize = 1; |
| 7380 | } |
| 7381 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7382 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7383 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7384 | charsize = 2; |
| 7385 | } |
| 7386 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7387 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7388 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7389 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7390 | NULL, pusedDefaultChar); |
| 7391 | if (outsize > 0) { |
| 7392 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7393 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7394 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7395 | memcpy(out, buffer, outsize); |
| 7396 | out += outsize; |
| 7397 | continue; |
| 7398 | } |
| 7399 | } |
| 7400 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7401 | PyErr_SetFromWindowsErr(0); |
| 7402 | goto error; |
| 7403 | } |
| 7404 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7405 | rep = unicode_encode_call_errorhandler( |
| 7406 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7407 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7408 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7409 | if (rep == NULL) |
| 7410 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7411 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7412 | |
| 7413 | if (PyBytes_Check(rep)) { |
| 7414 | outsize = PyBytes_GET_SIZE(rep); |
| 7415 | if (outsize != 1) { |
| 7416 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7417 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7418 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7419 | Py_DECREF(rep); |
| 7420 | goto error; |
| 7421 | } |
| 7422 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7423 | } |
| 7424 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7425 | out += outsize; |
| 7426 | } |
| 7427 | else { |
| 7428 | Py_ssize_t i; |
| 7429 | enum PyUnicode_Kind kind; |
| 7430 | void *data; |
| 7431 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7432 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7433 | Py_DECREF(rep); |
| 7434 | goto error; |
| 7435 | } |
| 7436 | |
| 7437 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7438 | if (outsize != 1) { |
| 7439 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7440 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7441 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7442 | Py_DECREF(rep); |
| 7443 | goto error; |
| 7444 | } |
| 7445 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7446 | } |
| 7447 | kind = PyUnicode_KIND(rep); |
| 7448 | data = PyUnicode_DATA(rep); |
| 7449 | for (i=0; i < outsize; i++) { |
| 7450 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7451 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7452 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7453 | encoding, unicode, |
| 7454 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7455 | "unable to encode error handler result to ASCII"); |
| 7456 | Py_DECREF(rep); |
| 7457 | goto error; |
| 7458 | } |
| 7459 | *out = (unsigned char)ch; |
| 7460 | out++; |
| 7461 | } |
| 7462 | } |
| 7463 | Py_DECREF(rep); |
| 7464 | } |
| 7465 | /* write a NUL byte */ |
| 7466 | *out = 0; |
| 7467 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7468 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7469 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7470 | goto error; |
| 7471 | ret = 0; |
| 7472 | |
| 7473 | error: |
| 7474 | Py_XDECREF(encoding_obj); |
| 7475 | Py_XDECREF(errorHandler); |
| 7476 | Py_XDECREF(exc); |
| 7477 | return ret; |
| 7478 | } |
| 7479 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7480 | static PyObject * |
| 7481 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7482 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7483 | const char *errors) |
| 7484 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7485 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7486 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7487 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7488 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7489 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7490 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7491 | return NULL; |
| 7492 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7493 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7494 | if (code_page < 0) { |
| 7495 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7496 | return NULL; |
| 7497 | } |
| 7498 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7499 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7500 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7501 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7502 | offset = 0; |
| 7503 | do |
| 7504 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7505 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7506 | /* 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] | 7507 | chunks. */ |
| 7508 | if (len > INT_MAX/2) { |
| 7509 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7510 | done = 0; |
| 7511 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7512 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7513 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7514 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7515 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7516 | done = 1; |
| 7517 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7518 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7519 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7520 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7521 | errors); |
| 7522 | if (ret == -2) |
| 7523 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7524 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7525 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7526 | if (ret < 0) { |
| 7527 | Py_XDECREF(outbytes); |
| 7528 | return NULL; |
| 7529 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7530 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7531 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7532 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7533 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7534 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7535 | return outbytes; |
| 7536 | } |
| 7537 | |
| 7538 | PyObject * |
| 7539 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7540 | Py_ssize_t size, |
| 7541 | const char *errors) |
| 7542 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7543 | PyObject *unicode, *res; |
| 7544 | unicode = PyUnicode_FromUnicode(p, size); |
| 7545 | if (unicode == NULL) |
| 7546 | return NULL; |
| 7547 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7548 | Py_DECREF(unicode); |
| 7549 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | PyObject * |
| 7553 | PyUnicode_EncodeCodePage(int code_page, |
| 7554 | PyObject *unicode, |
| 7555 | const char *errors) |
| 7556 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7557 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7558 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7559 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7560 | PyObject * |
| 7561 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7562 | { |
| 7563 | if (!PyUnicode_Check(unicode)) { |
| 7564 | PyErr_BadArgument(); |
| 7565 | return NULL; |
| 7566 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7567 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7568 | } |
| 7569 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7570 | #undef NEED_RETRY |
| 7571 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7572 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7575 | |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7576 | static int |
| 7577 | charmap_decode_string(const char *s, |
| 7578 | Py_ssize_t size, |
| 7579 | PyObject *mapping, |
| 7580 | const char *errors, |
| 7581 | _PyUnicodeWriter *writer) |
| 7582 | { |
| 7583 | const char *starts = s; |
| 7584 | const char *e; |
| 7585 | Py_ssize_t startinpos, endinpos; |
| 7586 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7587 | Py_ssize_t maplen; |
| 7588 | enum PyUnicode_Kind mapkind; |
| 7589 | void *mapdata; |
| 7590 | Py_UCS4 x; |
| 7591 | unsigned char ch; |
| 7592 | |
| 7593 | if (PyUnicode_READY(mapping) == -1) |
| 7594 | return -1; |
| 7595 | |
| 7596 | maplen = PyUnicode_GET_LENGTH(mapping); |
| 7597 | mapdata = PyUnicode_DATA(mapping); |
| 7598 | mapkind = PyUnicode_KIND(mapping); |
| 7599 | |
| 7600 | e = s + size; |
| 7601 | |
| 7602 | if (mapkind == PyUnicode_1BYTE_KIND && maplen >= 256) { |
| 7603 | /* fast-path for cp037, cp500 and iso8859_1 encodings. iso8859_1 |
| 7604 | * is disabled in encoding aliases, latin1 is preferred because |
| 7605 | * its implementation is faster. */ |
| 7606 | Py_UCS1 *mapdata_ucs1 = (Py_UCS1 *)mapdata; |
| 7607 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7608 | Py_UCS4 maxchar = writer->maxchar; |
| 7609 | |
| 7610 | assert (writer->kind == PyUnicode_1BYTE_KIND); |
| 7611 | while (s < e) { |
| 7612 | ch = *s; |
| 7613 | x = mapdata_ucs1[ch]; |
| 7614 | if (x > maxchar) { |
| 7615 | if (_PyUnicodeWriter_Prepare(writer, 1, 0xff) == -1) |
| 7616 | goto onError; |
| 7617 | maxchar = writer->maxchar; |
| 7618 | outdata = (Py_UCS1 *)writer->data; |
| 7619 | } |
| 7620 | outdata[writer->pos] = x; |
| 7621 | writer->pos++; |
| 7622 | ++s; |
| 7623 | } |
| 7624 | return 0; |
| 7625 | } |
| 7626 | |
| 7627 | while (s < e) { |
| 7628 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
| 7629 | enum PyUnicode_Kind outkind = writer->kind; |
| 7630 | Py_UCS2 *mapdata_ucs2 = (Py_UCS2 *)mapdata; |
| 7631 | if (outkind == PyUnicode_1BYTE_KIND) { |
| 7632 | Py_UCS1 *outdata = (Py_UCS1 *)writer->data; |
| 7633 | Py_UCS4 maxchar = writer->maxchar; |
| 7634 | while (s < e) { |
| 7635 | ch = *s; |
| 7636 | x = mapdata_ucs2[ch]; |
| 7637 | if (x > maxchar) |
| 7638 | goto Error; |
| 7639 | outdata[writer->pos] = x; |
| 7640 | writer->pos++; |
| 7641 | ++s; |
| 7642 | } |
| 7643 | break; |
| 7644 | } |
| 7645 | else if (outkind == PyUnicode_2BYTE_KIND) { |
| 7646 | Py_UCS2 *outdata = (Py_UCS2 *)writer->data; |
| 7647 | while (s < e) { |
| 7648 | ch = *s; |
| 7649 | x = mapdata_ucs2[ch]; |
| 7650 | if (x == 0xFFFE) |
| 7651 | goto Error; |
| 7652 | outdata[writer->pos] = x; |
| 7653 | writer->pos++; |
| 7654 | ++s; |
| 7655 | } |
| 7656 | break; |
| 7657 | } |
| 7658 | } |
| 7659 | ch = *s; |
| 7660 | |
| 7661 | if (ch < maplen) |
| 7662 | x = PyUnicode_READ(mapkind, mapdata, ch); |
| 7663 | else |
| 7664 | x = 0xfffe; /* invalid value */ |
| 7665 | Error: |
| 7666 | if (x == 0xfffe) |
| 7667 | { |
| 7668 | /* undefined mapping */ |
| 7669 | startinpos = s-starts; |
| 7670 | endinpos = startinpos+1; |
| 7671 | if (unicode_decode_call_errorhandler_writer( |
| 7672 | errors, &errorHandler, |
| 7673 | "charmap", "character maps to <undefined>", |
| 7674 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7675 | writer)) { |
| 7676 | goto onError; |
| 7677 | } |
| 7678 | continue; |
| 7679 | } |
| 7680 | |
| 7681 | if (_PyUnicodeWriter_WriteCharInline(writer, x) < 0) |
| 7682 | goto onError; |
| 7683 | ++s; |
| 7684 | } |
| 7685 | Py_XDECREF(errorHandler); |
| 7686 | Py_XDECREF(exc); |
| 7687 | return 0; |
| 7688 | |
| 7689 | onError: |
| 7690 | Py_XDECREF(errorHandler); |
| 7691 | Py_XDECREF(exc); |
| 7692 | return -1; |
| 7693 | } |
| 7694 | |
| 7695 | static int |
| 7696 | charmap_decode_mapping(const char *s, |
| 7697 | Py_ssize_t size, |
| 7698 | PyObject *mapping, |
| 7699 | const char *errors, |
| 7700 | _PyUnicodeWriter *writer) |
| 7701 | { |
| 7702 | const char *starts = s; |
| 7703 | const char *e; |
| 7704 | Py_ssize_t startinpos, endinpos; |
| 7705 | PyObject *errorHandler = NULL, *exc = NULL; |
| 7706 | unsigned char ch; |
Victor Stinner | f4f2424 | 2013-05-07 01:01:31 +0200 | [diff] [blame] | 7707 | PyObject *key, *item = NULL; |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7708 | |
| 7709 | e = s + size; |
| 7710 | |
| 7711 | while (s < e) { |
| 7712 | ch = *s; |
| 7713 | |
| 7714 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7715 | key = PyLong_FromLong((long)ch); |
| 7716 | if (key == NULL) |
| 7717 | goto onError; |
| 7718 | |
| 7719 | item = PyObject_GetItem(mapping, key); |
| 7720 | Py_DECREF(key); |
| 7721 | if (item == NULL) { |
| 7722 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7723 | /* No mapping found means: mapping is undefined. */ |
| 7724 | PyErr_Clear(); |
| 7725 | goto Undefined; |
| 7726 | } else |
| 7727 | goto onError; |
| 7728 | } |
| 7729 | |
| 7730 | /* Apply mapping */ |
| 7731 | if (item == Py_None) |
| 7732 | goto Undefined; |
| 7733 | if (PyLong_Check(item)) { |
| 7734 | long value = PyLong_AS_LONG(item); |
| 7735 | if (value == 0xFFFE) |
| 7736 | goto Undefined; |
| 7737 | if (value < 0 || value > MAX_UNICODE) { |
| 7738 | PyErr_Format(PyExc_TypeError, |
| 7739 | "character mapping must be in range(0x%lx)", |
| 7740 | (unsigned long)MAX_UNICODE + 1); |
| 7741 | goto onError; |
| 7742 | } |
| 7743 | |
| 7744 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7745 | goto onError; |
| 7746 | } |
| 7747 | else if (PyUnicode_Check(item)) { |
| 7748 | if (PyUnicode_READY(item) == -1) |
| 7749 | goto onError; |
| 7750 | if (PyUnicode_GET_LENGTH(item) == 1) { |
| 7751 | Py_UCS4 value = PyUnicode_READ_CHAR(item, 0); |
| 7752 | if (value == 0xFFFE) |
| 7753 | goto Undefined; |
| 7754 | if (_PyUnicodeWriter_WriteCharInline(writer, value) < 0) |
| 7755 | goto onError; |
| 7756 | } |
| 7757 | else { |
| 7758 | writer->overallocate = 1; |
| 7759 | if (_PyUnicodeWriter_WriteStr(writer, item) == -1) |
| 7760 | goto onError; |
| 7761 | } |
| 7762 | } |
| 7763 | else { |
| 7764 | /* wrong return value */ |
| 7765 | PyErr_SetString(PyExc_TypeError, |
| 7766 | "character mapping must return integer, None or str"); |
| 7767 | goto onError; |
| 7768 | } |
| 7769 | Py_CLEAR(item); |
| 7770 | ++s; |
| 7771 | continue; |
| 7772 | |
| 7773 | Undefined: |
| 7774 | /* undefined mapping */ |
| 7775 | Py_CLEAR(item); |
| 7776 | startinpos = s-starts; |
| 7777 | endinpos = startinpos+1; |
| 7778 | if (unicode_decode_call_errorhandler_writer( |
| 7779 | errors, &errorHandler, |
| 7780 | "charmap", "character maps to <undefined>", |
| 7781 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 7782 | writer)) { |
| 7783 | goto onError; |
| 7784 | } |
| 7785 | } |
| 7786 | Py_XDECREF(errorHandler); |
| 7787 | Py_XDECREF(exc); |
| 7788 | return 0; |
| 7789 | |
| 7790 | onError: |
| 7791 | Py_XDECREF(item); |
| 7792 | Py_XDECREF(errorHandler); |
| 7793 | Py_XDECREF(exc); |
| 7794 | return -1; |
| 7795 | } |
| 7796 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7797 | PyObject * |
| 7798 | PyUnicode_DecodeCharmap(const char *s, |
| 7799 | Py_ssize_t size, |
| 7800 | PyObject *mapping, |
| 7801 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7803 | _PyUnicodeWriter writer; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7804 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7805 | /* Default to Latin-1 */ |
| 7806 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7807 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7809 | if (size == 0) |
Serhiy Storchaka | ed3c412 | 2013-01-26 12:18:17 +0200 | [diff] [blame] | 7810 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 7811 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | 170ca6f | 2013-04-18 00:25:28 +0200 | [diff] [blame] | 7812 | writer.min_length = size; |
| 7813 | if (_PyUnicodeWriter_Prepare(&writer, writer.min_length, 127) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7814 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7815 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7816 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7817 | if (charmap_decode_string(s, size, mapping, errors, &writer) < 0) |
| 7818 | goto onError; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7819 | } |
| 7820 | else { |
Victor Stinner | fb161b1 | 2013-04-18 01:44:27 +0200 | [diff] [blame] | 7821 | if (charmap_decode_mapping(s, size, mapping, errors, &writer) < 0) |
| 7822 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7823 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7824 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7825 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7826 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7827 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | return NULL; |
| 7829 | } |
| 7830 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7831 | /* Charmap encoding: the lookup table */ |
| 7832 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7833 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7834 | PyObject_HEAD |
| 7835 | unsigned char level1[32]; |
| 7836 | int count2, count3; |
| 7837 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7838 | }; |
| 7839 | |
| 7840 | static PyObject* |
| 7841 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7842 | { |
| 7843 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7844 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7845 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7846 | } |
| 7847 | |
| 7848 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7849 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7850 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7851 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7852 | }; |
| 7853 | |
| 7854 | static void |
| 7855 | encoding_map_dealloc(PyObject* o) |
| 7856 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7857 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7858 | } |
| 7859 | |
| 7860 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7861 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7862 | "EncodingMap", /*tp_name*/ |
| 7863 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7864 | 0, /*tp_itemsize*/ |
| 7865 | /* methods */ |
| 7866 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7867 | 0, /*tp_print*/ |
| 7868 | 0, /*tp_getattr*/ |
| 7869 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7870 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7871 | 0, /*tp_repr*/ |
| 7872 | 0, /*tp_as_number*/ |
| 7873 | 0, /*tp_as_sequence*/ |
| 7874 | 0, /*tp_as_mapping*/ |
| 7875 | 0, /*tp_hash*/ |
| 7876 | 0, /*tp_call*/ |
| 7877 | 0, /*tp_str*/ |
| 7878 | 0, /*tp_getattro*/ |
| 7879 | 0, /*tp_setattro*/ |
| 7880 | 0, /*tp_as_buffer*/ |
| 7881 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7882 | 0, /*tp_doc*/ |
| 7883 | 0, /*tp_traverse*/ |
| 7884 | 0, /*tp_clear*/ |
| 7885 | 0, /*tp_richcompare*/ |
| 7886 | 0, /*tp_weaklistoffset*/ |
| 7887 | 0, /*tp_iter*/ |
| 7888 | 0, /*tp_iternext*/ |
| 7889 | encoding_map_methods, /*tp_methods*/ |
| 7890 | 0, /*tp_members*/ |
| 7891 | 0, /*tp_getset*/ |
| 7892 | 0, /*tp_base*/ |
| 7893 | 0, /*tp_dict*/ |
| 7894 | 0, /*tp_descr_get*/ |
| 7895 | 0, /*tp_descr_set*/ |
| 7896 | 0, /*tp_dictoffset*/ |
| 7897 | 0, /*tp_init*/ |
| 7898 | 0, /*tp_alloc*/ |
| 7899 | 0, /*tp_new*/ |
| 7900 | 0, /*tp_free*/ |
| 7901 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7902 | }; |
| 7903 | |
| 7904 | PyObject* |
| 7905 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7906 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7907 | PyObject *result; |
| 7908 | struct encoding_map *mresult; |
| 7909 | int i; |
| 7910 | int need_dict = 0; |
| 7911 | unsigned char level1[32]; |
| 7912 | unsigned char level2[512]; |
| 7913 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7914 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7915 | int kind; |
| 7916 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7917 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7918 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7919 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7920 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7921 | PyErr_BadArgument(); |
| 7922 | return NULL; |
| 7923 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7924 | kind = PyUnicode_KIND(string); |
| 7925 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7926 | length = PyUnicode_GET_LENGTH(string); |
| 7927 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7928 | memset(level1, 0xFF, sizeof level1); |
| 7929 | memset(level2, 0xFF, sizeof level2); |
| 7930 | |
| 7931 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7932 | or if there are non-BMP characters, we need to use |
| 7933 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7934 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7935 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7936 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7937 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7938 | ch = PyUnicode_READ(kind, data, i); |
| 7939 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7940 | need_dict = 1; |
| 7941 | break; |
| 7942 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7943 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7944 | /* unmapped character */ |
| 7945 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7946 | l1 = ch >> 11; |
| 7947 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7948 | if (level1[l1] == 0xFF) |
| 7949 | level1[l1] = count2++; |
| 7950 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7951 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7952 | } |
| 7953 | |
| 7954 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7955 | need_dict = 1; |
| 7956 | |
| 7957 | if (need_dict) { |
| 7958 | PyObject *result = PyDict_New(); |
| 7959 | PyObject *key, *value; |
| 7960 | if (!result) |
| 7961 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7962 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7963 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7964 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7965 | if (!key || !value) |
| 7966 | goto failed1; |
| 7967 | if (PyDict_SetItem(result, key, value) == -1) |
| 7968 | goto failed1; |
| 7969 | Py_DECREF(key); |
| 7970 | Py_DECREF(value); |
| 7971 | } |
| 7972 | return result; |
| 7973 | failed1: |
| 7974 | Py_XDECREF(key); |
| 7975 | Py_XDECREF(value); |
| 7976 | Py_DECREF(result); |
| 7977 | return NULL; |
| 7978 | } |
| 7979 | |
| 7980 | /* Create a three-level trie */ |
| 7981 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7982 | 16*count2 + 128*count3 - 1); |
| 7983 | if (!result) |
| 7984 | return PyErr_NoMemory(); |
| 7985 | PyObject_Init(result, &EncodingMapType); |
| 7986 | mresult = (struct encoding_map*)result; |
| 7987 | mresult->count2 = count2; |
| 7988 | mresult->count3 = count3; |
| 7989 | mlevel1 = mresult->level1; |
| 7990 | mlevel2 = mresult->level23; |
| 7991 | mlevel3 = mresult->level23 + 16*count2; |
| 7992 | memcpy(mlevel1, level1, 32); |
| 7993 | memset(mlevel2, 0xFF, 16*count2); |
| 7994 | memset(mlevel3, 0, 128*count3); |
| 7995 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7996 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7997 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7998 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7999 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8000 | /* unmapped character */ |
| 8001 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8002 | o1 = ch>>11; |
| 8003 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8004 | i2 = 16*mlevel1[o1] + o2; |
| 8005 | if (mlevel2[i2] == 0xFF) |
| 8006 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 8007 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8008 | i3 = 128*mlevel2[i2] + o3; |
| 8009 | mlevel3[i3] = i; |
| 8010 | } |
| 8011 | return result; |
| 8012 | } |
| 8013 | |
| 8014 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8015 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8016 | { |
| 8017 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 8018 | int l1 = c>>11; |
| 8019 | int l2 = (c>>7) & 0xF; |
| 8020 | int l3 = c & 0x7F; |
| 8021 | int i; |
| 8022 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8023 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8024 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8025 | if (c == 0) |
| 8026 | return 0; |
| 8027 | /* level 1*/ |
| 8028 | i = map->level1[l1]; |
| 8029 | if (i == 0xFF) { |
| 8030 | return -1; |
| 8031 | } |
| 8032 | /* level 2*/ |
| 8033 | i = map->level23[16*i+l2]; |
| 8034 | if (i == 0xFF) { |
| 8035 | return -1; |
| 8036 | } |
| 8037 | /* level 3 */ |
| 8038 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 8039 | if (i == 0) { |
| 8040 | return -1; |
| 8041 | } |
| 8042 | return i; |
| 8043 | } |
| 8044 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8045 | /* Lookup the character ch in the mapping. If the character |
| 8046 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 8047 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8048 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8049 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8050 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8051 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8052 | PyObject *x; |
| 8053 | |
| 8054 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8055 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8056 | x = PyObject_GetItem(mapping, w); |
| 8057 | Py_DECREF(w); |
| 8058 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8059 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8060 | /* No mapping found means: mapping is undefined. */ |
| 8061 | PyErr_Clear(); |
| 8062 | x = Py_None; |
| 8063 | Py_INCREF(x); |
| 8064 | return x; |
| 8065 | } else |
| 8066 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8067 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 8068 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8069 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8070 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | long value = PyLong_AS_LONG(x); |
| 8072 | if (value < 0 || value > 255) { |
| 8073 | PyErr_SetString(PyExc_TypeError, |
| 8074 | "character mapping must be in range(256)"); |
| 8075 | Py_DECREF(x); |
| 8076 | return NULL; |
| 8077 | } |
| 8078 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8080 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8081 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8082 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8083 | /* wrong return value */ |
| 8084 | PyErr_Format(PyExc_TypeError, |
| 8085 | "character mapping must return integer, bytes or None, not %.400s", |
| 8086 | x->ob_type->tp_name); |
| 8087 | Py_DECREF(x); |
| 8088 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8089 | } |
| 8090 | } |
| 8091 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8092 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8093 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8094 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8095 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 8096 | /* exponentially overallocate to minimize reallocations */ |
| 8097 | if (requiredsize < 2*outsize) |
| 8098 | requiredsize = 2*outsize; |
| 8099 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 8100 | return -1; |
| 8101 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8102 | } |
| 8103 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8104 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8105 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8106 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8107 | /* 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] | 8108 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8109 | space is available. Return a new reference to the object that |
| 8110 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 8111 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 8112 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8113 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 8114 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8115 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8116 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8117 | PyObject *rep; |
| 8118 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8119 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8120 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8121 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8122 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8123 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8124 | if (res == -1) |
| 8125 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8126 | if (outsize<requiredsize) |
| 8127 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 8128 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8129 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8130 | outstart[(*outpos)++] = (char)res; |
| 8131 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8132 | } |
| 8133 | |
| 8134 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8135 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8136 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8137 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8138 | Py_DECREF(rep); |
| 8139 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8140 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8141 | if (PyLong_Check(rep)) { |
| 8142 | Py_ssize_t requiredsize = *outpos+1; |
| 8143 | if (outsize<requiredsize) |
| 8144 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8145 | Py_DECREF(rep); |
| 8146 | return enc_EXCEPTION; |
| 8147 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8148 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8149 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8150 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8151 | else { |
| 8152 | const char *repchars = PyBytes_AS_STRING(rep); |
| 8153 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 8154 | Py_ssize_t requiredsize = *outpos+repsize; |
| 8155 | if (outsize<requiredsize) |
| 8156 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 8157 | Py_DECREF(rep); |
| 8158 | return enc_EXCEPTION; |
| 8159 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8160 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8161 | memcpy(outstart + *outpos, repchars, repsize); |
| 8162 | *outpos += repsize; |
| 8163 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8164 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8165 | Py_DECREF(rep); |
| 8166 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8167 | } |
| 8168 | |
| 8169 | /* handle an error in PyUnicode_EncodeCharmap |
| 8170 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8171 | static int |
| 8172 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8173 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8174 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 8175 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8176 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8177 | { |
| 8178 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8179 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8180 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8181 | enum PyUnicode_Kind kind; |
| 8182 | void *data; |
| 8183 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8184 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8185 | Py_ssize_t collstartpos = *inpos; |
| 8186 | Py_ssize_t collendpos = *inpos+1; |
| 8187 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8188 | char *encoding = "charmap"; |
| 8189 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8190 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8191 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8192 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8193 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8194 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8195 | return -1; |
| 8196 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8197 | /* find all unencodable characters */ |
| 8198 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8199 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8200 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8201 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 8202 | val = encoding_map_lookup(ch, mapping); |
| 8203 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8204 | break; |
| 8205 | ++collendpos; |
| 8206 | continue; |
| 8207 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8208 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8209 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 8210 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8211 | if (rep==NULL) |
| 8212 | return -1; |
| 8213 | else if (rep!=Py_None) { |
| 8214 | Py_DECREF(rep); |
| 8215 | break; |
| 8216 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8217 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8218 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8219 | } |
| 8220 | /* cache callback name lookup |
| 8221 | * (if not done yet, i.e. it's the first error) */ |
| 8222 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8223 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8224 | *known_errorHandler = 1; |
| 8225 | else if (!strcmp(errors, "replace")) |
| 8226 | *known_errorHandler = 2; |
| 8227 | else if (!strcmp(errors, "ignore")) |
| 8228 | *known_errorHandler = 3; |
| 8229 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8230 | *known_errorHandler = 4; |
| 8231 | else |
| 8232 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8233 | } |
| 8234 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8235 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8236 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8237 | return -1; |
| 8238 | case 2: /* replace */ |
| 8239 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8240 | x = charmapencode_output('?', mapping, res, respos); |
| 8241 | if (x==enc_EXCEPTION) { |
| 8242 | return -1; |
| 8243 | } |
| 8244 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8245 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8246 | return -1; |
| 8247 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8248 | } |
| 8249 | /* fall through */ |
| 8250 | case 3: /* ignore */ |
| 8251 | *inpos = collendpos; |
| 8252 | break; |
| 8253 | case 4: /* xmlcharrefreplace */ |
| 8254 | /* generate replacement (temporarily (mis)uses p) */ |
| 8255 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8256 | char buffer[2+29+1+1]; |
| 8257 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8258 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8259 | for (cp = buffer; *cp; ++cp) { |
| 8260 | x = charmapencode_output(*cp, mapping, res, respos); |
| 8261 | if (x==enc_EXCEPTION) |
| 8262 | return -1; |
| 8263 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8264 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8265 | return -1; |
| 8266 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8267 | } |
| 8268 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8269 | *inpos = collendpos; |
| 8270 | break; |
| 8271 | default: |
| 8272 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8273 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8274 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8275 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8276 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8277 | if (PyBytes_Check(repunicode)) { |
| 8278 | /* Directly copy bytes result to output. */ |
| 8279 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 8280 | Py_ssize_t requiredsize; |
| 8281 | repsize = PyBytes_Size(repunicode); |
| 8282 | requiredsize = *respos + repsize; |
| 8283 | if (requiredsize > outsize) |
| 8284 | /* Make room for all additional bytes. */ |
| 8285 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 8286 | Py_DECREF(repunicode); |
| 8287 | return -1; |
| 8288 | } |
| 8289 | memcpy(PyBytes_AsString(*res) + *respos, |
| 8290 | PyBytes_AsString(repunicode), repsize); |
| 8291 | *respos += repsize; |
| 8292 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8293 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8294 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8295 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8296 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8297 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8298 | Py_DECREF(repunicode); |
| 8299 | return -1; |
| 8300 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 8301 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8302 | data = PyUnicode_DATA(repunicode); |
| 8303 | kind = PyUnicode_KIND(repunicode); |
| 8304 | for (index = 0; index < repsize; index++) { |
| 8305 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 8306 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8307 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 8308 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8309 | return -1; |
| 8310 | } |
| 8311 | else if (x==enc_FAILED) { |
| 8312 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 8313 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8314 | return -1; |
| 8315 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8316 | } |
| 8317 | *inpos = newpos; |
| 8318 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8319 | } |
| 8320 | return 0; |
| 8321 | } |
| 8322 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8323 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8324 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 8325 | PyObject *mapping, |
| 8326 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8328 | /* output object */ |
| 8329 | PyObject *res = NULL; |
| 8330 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8331 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8332 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8333 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8334 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8335 | PyObject *errorHandler = NULL; |
| 8336 | PyObject *exc = NULL; |
| 8337 | /* the following variable is used for caching string comparisons |
| 8338 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8339 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8340 | int known_errorHandler = -1; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8341 | void *data; |
| 8342 | int kind; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8344 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8345 | return NULL; |
| 8346 | size = PyUnicode_GET_LENGTH(unicode); |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8347 | data = PyUnicode_DATA(unicode); |
| 8348 | kind = PyUnicode_KIND(unicode); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8349 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8350 | /* Default to Latin-1 */ |
| 8351 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8352 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8354 | /* allocate enough for a simple encoding without |
| 8355 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8356 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8357 | if (res == NULL) |
| 8358 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 8359 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8360 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8361 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8362 | while (inpos<size) { |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 8363 | Py_UCS4 ch = PyUnicode_READ(kind, data, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8364 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8365 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8366 | if (x==enc_EXCEPTION) /* error */ |
| 8367 | goto onError; |
| 8368 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8369 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8370 | &exc, |
| 8371 | &known_errorHandler, &errorHandler, errors, |
| 8372 | &res, &respos)) { |
| 8373 | goto onError; |
| 8374 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8375 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8376 | else |
| 8377 | /* done with this character => adjust input position */ |
| 8378 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8379 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8380 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8381 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8382 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8383 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8384 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8385 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8386 | Py_XDECREF(exc); |
| 8387 | Py_XDECREF(errorHandler); |
| 8388 | return res; |
| 8389 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8390 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8391 | Py_XDECREF(res); |
| 8392 | Py_XDECREF(exc); |
| 8393 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8394 | return NULL; |
| 8395 | } |
| 8396 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8397 | /* Deprecated */ |
| 8398 | PyObject * |
| 8399 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8400 | Py_ssize_t size, |
| 8401 | PyObject *mapping, |
| 8402 | const char *errors) |
| 8403 | { |
| 8404 | PyObject *result; |
| 8405 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8406 | if (unicode == NULL) |
| 8407 | return NULL; |
| 8408 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8409 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8410 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8411 | } |
| 8412 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8413 | PyObject * |
| 8414 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8415 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8416 | { |
| 8417 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8418 | PyErr_BadArgument(); |
| 8419 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8420 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8421 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8422 | } |
| 8423 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8424 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8425 | static void |
| 8426 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8427 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8428 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8429 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8431 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8432 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8433 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | } |
| 8435 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8436 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8437 | goto onError; |
| 8438 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8439 | goto onError; |
| 8440 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8441 | goto onError; |
| 8442 | return; |
| 8443 | onError: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 8444 | Py_CLEAR(*exceptionObject); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8445 | } |
| 8446 | } |
| 8447 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8448 | /* error handling callback helper: |
| 8449 | build arguments, call the callback and check the arguments, |
| 8450 | put the result into newpos and return the replacement string, which |
| 8451 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8452 | static PyObject * |
| 8453 | unicode_translate_call_errorhandler(const char *errors, |
| 8454 | PyObject **errorHandler, |
| 8455 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8456 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8457 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8458 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8459 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8460 | 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] | 8461 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8462 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8463 | PyObject *restuple; |
| 8464 | PyObject *resunicode; |
| 8465 | |
| 8466 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8467 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8468 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8469 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8470 | } |
| 8471 | |
| 8472 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8473 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8474 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8475 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8476 | |
| 8477 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8478 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8479 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8480 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8481 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8482 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8483 | Py_DECREF(restuple); |
| 8484 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8485 | } |
| 8486 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8487 | &resunicode, &i_newpos)) { |
| 8488 | Py_DECREF(restuple); |
| 8489 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8490 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8491 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8492 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8493 | else |
| 8494 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8495 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 8496 | 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] | 8497 | Py_DECREF(restuple); |
| 8498 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8499 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8500 | Py_INCREF(resunicode); |
| 8501 | Py_DECREF(restuple); |
| 8502 | return resunicode; |
| 8503 | } |
| 8504 | |
| 8505 | /* Lookup the character ch in the mapping and put the result in result, |
| 8506 | which must be decrefed by the caller. |
| 8507 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8508 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8509 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8510 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8511 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8512 | PyObject *x; |
| 8513 | |
| 8514 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8515 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8516 | x = PyObject_GetItem(mapping, w); |
| 8517 | Py_DECREF(w); |
| 8518 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8519 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8520 | /* No mapping found means: use 1:1 mapping. */ |
| 8521 | PyErr_Clear(); |
| 8522 | *result = NULL; |
| 8523 | return 0; |
| 8524 | } else |
| 8525 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8526 | } |
| 8527 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8528 | *result = x; |
| 8529 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8530 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8531 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8532 | long value = PyLong_AS_LONG(x); |
| 8533 | long max = PyUnicode_GetMax(); |
| 8534 | if (value < 0 || value > max) { |
| 8535 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8536 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8537 | Py_DECREF(x); |
| 8538 | return -1; |
| 8539 | } |
| 8540 | *result = x; |
| 8541 | return 0; |
| 8542 | } |
| 8543 | else if (PyUnicode_Check(x)) { |
| 8544 | *result = x; |
| 8545 | return 0; |
| 8546 | } |
| 8547 | else { |
| 8548 | /* wrong return value */ |
| 8549 | PyErr_SetString(PyExc_TypeError, |
| 8550 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8551 | Py_DECREF(x); |
| 8552 | return -1; |
| 8553 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8554 | } |
| 8555 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8556 | if not reallocate and adjust various state variables. |
| 8557 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8558 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8559 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8560 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8561 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8562 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8563 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8564 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8565 | /* exponentially overallocate to minimize reallocations */ |
| 8566 | if (requiredsize < 2 * oldsize) |
| 8567 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8568 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8569 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8570 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8571 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8572 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8573 | } |
| 8574 | return 0; |
| 8575 | } |
| 8576 | /* lookup the character, put the result in the output string and adjust |
| 8577 | various state variables. Return a new reference to the object that |
| 8578 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8579 | undefined (in which case no character was written). |
| 8580 | The called must decref result. |
| 8581 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8582 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8583 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8584 | PyObject *mapping, Py_UCS4 **output, |
| 8585 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8586 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8587 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8588 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8589 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8590 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8591 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8592 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8593 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8594 | } |
| 8595 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8596 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8597 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8598 | /* no overflow check, because we know that the space is enough */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8599 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8600 | } |
| 8601 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8602 | Py_ssize_t repsize; |
| 8603 | if (PyUnicode_READY(*res) == -1) |
| 8604 | return -1; |
| 8605 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8606 | if (repsize==1) { |
| 8607 | /* no overflow check, because we know that the space is enough */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8608 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8609 | } |
| 8610 | else if (repsize!=0) { |
| 8611 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8612 | Py_ssize_t requiredsize = *opos + |
| 8613 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8614 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8615 | Py_ssize_t i; |
| 8616 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8617 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8618 | for(i = 0; i < repsize; i++) |
| 8619 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8620 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8621 | } |
| 8622 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8623 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8624 | return 0; |
| 8625 | } |
| 8626 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8627 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8628 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8629 | PyObject *mapping, |
| 8630 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8631 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8632 | /* input object */ |
| 8633 | char *idata; |
| 8634 | Py_ssize_t size, i; |
| 8635 | int kind; |
| 8636 | /* output buffer */ |
| 8637 | Py_UCS4 *output = NULL; |
| 8638 | Py_ssize_t osize; |
| 8639 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8640 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8641 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8642 | char *reason = "character maps to <undefined>"; |
| 8643 | PyObject *errorHandler = NULL; |
| 8644 | PyObject *exc = NULL; |
| 8645 | /* the following variable is used for caching string comparisons |
| 8646 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8647 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8648 | int known_errorHandler = -1; |
| 8649 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8650 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8651 | PyErr_BadArgument(); |
| 8652 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8653 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8654 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8655 | if (PyUnicode_READY(input) == -1) |
| 8656 | return NULL; |
| 8657 | idata = (char*)PyUnicode_DATA(input); |
| 8658 | kind = PyUnicode_KIND(input); |
| 8659 | size = PyUnicode_GET_LENGTH(input); |
| 8660 | i = 0; |
| 8661 | |
| 8662 | if (size == 0) { |
| 8663 | Py_INCREF(input); |
| 8664 | return input; |
| 8665 | } |
| 8666 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8667 | /* allocate enough for a simple 1:1 translation without |
| 8668 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8669 | osize = size; |
Benjamin Peterson | e5a853c | 2015-03-02 13:23:25 -0500 | [diff] [blame] | 8670 | output = PyMem_NEW(Py_UCS4, osize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8671 | opos = 0; |
| 8672 | if (output == NULL) { |
| 8673 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8674 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8675 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8676 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8678 | /* try to encode it */ |
| 8679 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8680 | if (charmaptranslate_output(input, i, mapping, |
| 8681 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8682 | Py_XDECREF(x); |
| 8683 | goto onError; |
| 8684 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8685 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8686 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8687 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8688 | else { /* untranslatable character */ |
| 8689 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8690 | Py_ssize_t repsize; |
| 8691 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8692 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8693 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8694 | Py_ssize_t collstart = i; |
| 8695 | Py_ssize_t collend = i+1; |
| 8696 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8697 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8698 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8699 | while (collend < size) { |
| 8700 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8701 | goto onError; |
| 8702 | Py_XDECREF(x); |
| 8703 | if (x!=Py_None) |
| 8704 | break; |
| 8705 | ++collend; |
| 8706 | } |
| 8707 | /* cache callback name lookup |
| 8708 | * (if not done yet, i.e. it's the first error) */ |
| 8709 | if (known_errorHandler==-1) { |
| 8710 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8711 | known_errorHandler = 1; |
| 8712 | else if (!strcmp(errors, "replace")) |
| 8713 | known_errorHandler = 2; |
| 8714 | else if (!strcmp(errors, "ignore")) |
| 8715 | known_errorHandler = 3; |
| 8716 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8717 | known_errorHandler = 4; |
| 8718 | else |
| 8719 | known_errorHandler = 0; |
| 8720 | } |
| 8721 | switch (known_errorHandler) { |
| 8722 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8723 | make_translate_exception(&exc, |
| 8724 | input, collstart, collend, reason); |
| 8725 | if (exc != NULL) |
| 8726 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8727 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8728 | case 2: /* replace */ |
| 8729 | /* No need to check for space, this is a 1:1 replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8730 | for (coll = collstart; coll<collend; coll++) |
| 8731 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8732 | /* fall through */ |
| 8733 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8734 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8735 | break; |
| 8736 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 | /* generate replacement (temporarily (mis)uses i) */ |
| 8738 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8739 | char buffer[2+29+1+1]; |
| 8740 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8741 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8742 | if (charmaptranslate_makespace(&output, &osize, |
| 8743 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8744 | goto onError; |
| 8745 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8747 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8748 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8749 | break; |
| 8750 | default: |
| 8751 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8752 | reason, input, &exc, |
| 8753 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8754 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8755 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8756 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8757 | Py_DECREF(repunicode); |
| 8758 | goto onError; |
| 8759 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8760 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8761 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8762 | if (charmaptranslate_makespace(&output, &osize, |
| 8763 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8764 | Py_DECREF(repunicode); |
| 8765 | goto onError; |
| 8766 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8767 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8768 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8769 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8770 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8771 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8772 | } |
| 8773 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8774 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8775 | if (!res) |
| 8776 | goto onError; |
| 8777 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8778 | Py_XDECREF(exc); |
| 8779 | Py_XDECREF(errorHandler); |
| 8780 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8781 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8782 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8783 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8784 | Py_XDECREF(exc); |
| 8785 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8786 | return NULL; |
| 8787 | } |
| 8788 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8789 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8790 | PyObject * |
| 8791 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8792 | Py_ssize_t size, |
| 8793 | PyObject *mapping, |
| 8794 | const char *errors) |
| 8795 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8796 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8797 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8798 | if (!unicode) |
| 8799 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8800 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8801 | Py_DECREF(unicode); |
| 8802 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8803 | } |
| 8804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8805 | PyObject * |
| 8806 | PyUnicode_Translate(PyObject *str, |
| 8807 | PyObject *mapping, |
| 8808 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8809 | { |
| 8810 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8812 | str = PyUnicode_FromObject(str); |
| 8813 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8814 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8815 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8816 | Py_DECREF(str); |
| 8817 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8818 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8819 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8820 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8821 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8822 | { |
| 8823 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8824 | called as a callback from fixup() which does it already. */ |
| 8825 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8826 | const int kind = PyUnicode_KIND(self); |
| 8827 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8828 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8829 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8830 | Py_ssize_t i; |
| 8831 | |
| 8832 | for (i = 0; i < len; ++i) { |
| 8833 | ch = PyUnicode_READ(kind, data, i); |
| 8834 | fixed = 0; |
| 8835 | if (ch > 127) { |
| 8836 | if (Py_UNICODE_ISSPACE(ch)) |
| 8837 | fixed = ' '; |
| 8838 | else { |
| 8839 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8840 | if (decimal >= 0) |
| 8841 | fixed = '0' + decimal; |
| 8842 | } |
| 8843 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8844 | modified = 1; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8845 | maxchar = Py_MAX(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8846 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8847 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8848 | else |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8849 | maxchar = Py_MAX(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8850 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8851 | } |
| 8852 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8853 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8854 | } |
| 8855 | |
| 8856 | PyObject * |
| 8857 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8858 | { |
| 8859 | if (!PyUnicode_Check(unicode)) { |
| 8860 | PyErr_BadInternalCall(); |
| 8861 | return NULL; |
| 8862 | } |
| 8863 | if (PyUnicode_READY(unicode) == -1) |
| 8864 | return NULL; |
| 8865 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8866 | /* If the string is already ASCII, just return the same string */ |
| 8867 | Py_INCREF(unicode); |
| 8868 | return unicode; |
| 8869 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8870 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8871 | } |
| 8872 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8873 | PyObject * |
| 8874 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8875 | Py_ssize_t length) |
| 8876 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8877 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8878 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8879 | Py_UCS4 maxchar; |
| 8880 | enum PyUnicode_Kind kind; |
| 8881 | void *data; |
| 8882 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8883 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8884 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8885 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8886 | if (ch > 127) { |
| 8887 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8888 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8889 | ch = '0' + decimal; |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 8890 | maxchar = Py_MAX(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8891 | } |
| 8892 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8893 | |
| 8894 | /* Copy to a new string */ |
| 8895 | decimal = PyUnicode_New(length, maxchar); |
| 8896 | if (decimal == NULL) |
| 8897 | return decimal; |
| 8898 | kind = PyUnicode_KIND(decimal); |
| 8899 | data = PyUnicode_DATA(decimal); |
| 8900 | /* Iterate over code points */ |
| 8901 | for (i = 0; i < length; i++) { |
| 8902 | Py_UNICODE ch = s[i]; |
| 8903 | if (ch > 127) { |
| 8904 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8905 | if (decimal >= 0) |
| 8906 | ch = '0' + decimal; |
| 8907 | } |
| 8908 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8909 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8910 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8911 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8912 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8913 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8914 | int |
| 8915 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8916 | Py_ssize_t length, |
| 8917 | char *output, |
| 8918 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8919 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8920 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8921 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8922 | enum PyUnicode_Kind kind; |
| 8923 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8924 | |
| 8925 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8926 | PyErr_BadArgument(); |
| 8927 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8928 | } |
| 8929 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8930 | unicode = PyUnicode_FromUnicode(s, length); |
| 8931 | if (unicode == NULL) |
| 8932 | return -1; |
| 8933 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8934 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8935 | Py_DECREF(unicode); |
| 8936 | return -1; |
| 8937 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8938 | kind = PyUnicode_KIND(unicode); |
| 8939 | data = PyUnicode_DATA(unicode); |
| 8940 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8941 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8942 | PyObject *exc; |
| 8943 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8944 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8945 | Py_ssize_t startpos; |
| 8946 | |
| 8947 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8948 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8949 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8950 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8951 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8952 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8953 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8954 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8955 | if (decimal >= 0) { |
| 8956 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8957 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8958 | continue; |
| 8959 | } |
| 8960 | if (0 < ch && ch < 256) { |
| 8961 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8962 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8963 | continue; |
| 8964 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8965 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8966 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8967 | exc = NULL; |
| 8968 | raise_encode_exception(&exc, "decimal", unicode, |
| 8969 | startpos, startpos+1, |
| 8970 | "invalid decimal Unicode string"); |
| 8971 | Py_XDECREF(exc); |
| 8972 | Py_DECREF(unicode); |
| 8973 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8974 | } |
| 8975 | /* 0-terminate the output string */ |
| 8976 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8977 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8978 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8979 | } |
| 8980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8981 | /* --- Helpers ------------------------------------------------------------ */ |
| 8982 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8983 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8984 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8985 | Py_ssize_t start, |
| 8986 | Py_ssize_t end) |
| 8987 | { |
| 8988 | int kind1, kind2, kind; |
| 8989 | void *buf1, *buf2; |
| 8990 | Py_ssize_t len1, len2, result; |
| 8991 | |
| 8992 | kind1 = PyUnicode_KIND(s1); |
| 8993 | kind2 = PyUnicode_KIND(s2); |
| 8994 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8995 | buf1 = PyUnicode_DATA(s1); |
| 8996 | buf2 = PyUnicode_DATA(s2); |
| 8997 | if (kind1 != kind) |
| 8998 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8999 | if (!buf1) |
| 9000 | return -2; |
| 9001 | if (kind2 != kind) |
| 9002 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 9003 | if (!buf2) { |
| 9004 | if (kind1 != kind) PyMem_Free(buf1); |
| 9005 | return -2; |
| 9006 | } |
| 9007 | len1 = PyUnicode_GET_LENGTH(s1); |
| 9008 | len2 = PyUnicode_GET_LENGTH(s2); |
| 9009 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9010 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9011 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9012 | case PyUnicode_1BYTE_KIND: |
| 9013 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9014 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9015 | else |
| 9016 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9017 | break; |
| 9018 | case PyUnicode_2BYTE_KIND: |
| 9019 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9020 | break; |
| 9021 | case PyUnicode_4BYTE_KIND: |
| 9022 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 9023 | break; |
| 9024 | default: |
| 9025 | assert(0); result = -2; |
| 9026 | } |
| 9027 | } |
| 9028 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9029 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9030 | case PyUnicode_1BYTE_KIND: |
| 9031 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 9032 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9033 | else |
| 9034 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9035 | break; |
| 9036 | case PyUnicode_2BYTE_KIND: |
| 9037 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9038 | break; |
| 9039 | case PyUnicode_4BYTE_KIND: |
| 9040 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 9041 | break; |
| 9042 | default: |
| 9043 | assert(0); result = -2; |
| 9044 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9045 | } |
| 9046 | |
| 9047 | if (kind1 != kind) |
| 9048 | PyMem_Free(buf1); |
| 9049 | if (kind2 != kind) |
| 9050 | PyMem_Free(buf2); |
| 9051 | |
| 9052 | return result; |
| 9053 | } |
| 9054 | |
| 9055 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9056 | _PyUnicode_InsertThousandsGrouping( |
| 9057 | PyObject *unicode, Py_ssize_t index, |
| 9058 | Py_ssize_t n_buffer, |
| 9059 | void *digits, Py_ssize_t n_digits, |
| 9060 | Py_ssize_t min_width, |
| 9061 | const char *grouping, PyObject *thousands_sep, |
| 9062 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9063 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9064 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9065 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9066 | Py_ssize_t thousands_sep_len; |
| 9067 | Py_ssize_t len; |
| 9068 | |
| 9069 | if (unicode != NULL) { |
| 9070 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9071 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9072 | } |
| 9073 | else { |
| 9074 | kind = PyUnicode_1BYTE_KIND; |
| 9075 | data = NULL; |
| 9076 | } |
| 9077 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 9078 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 9079 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 9080 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9081 | if (thousands_sep_kind < kind) { |
| 9082 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 9083 | if (!thousands_sep_data) |
| 9084 | return -1; |
| 9085 | } |
| 9086 | else { |
| 9087 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 9088 | if (!data) |
| 9089 | return -1; |
| 9090 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9091 | } |
| 9092 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9093 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9094 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9095 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9096 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9097 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9098 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9099 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9100 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9101 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9102 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9103 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9104 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9105 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9106 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9107 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9108 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9109 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9110 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9111 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9112 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9113 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9114 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9115 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 9116 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9117 | break; |
| 9118 | default: |
| 9119 | assert(0); |
| 9120 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9121 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 9122 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 9123 | if (thousands_sep_kind < kind) |
| 9124 | PyMem_Free(thousands_sep_data); |
| 9125 | else |
| 9126 | PyMem_Free(data); |
| 9127 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9128 | if (unicode == NULL) { |
| 9129 | *maxchar = 127; |
| 9130 | if (len != n_digits) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9131 | *maxchar = Py_MAX(*maxchar, |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9132 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 9133 | } |
| 9134 | } |
| 9135 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9136 | } |
| 9137 | |
| 9138 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9139 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9140 | #define ADJUST_INDICES(start, end, len) \ |
| 9141 | if (end > len) \ |
| 9142 | end = len; \ |
| 9143 | else if (end < 0) { \ |
| 9144 | end += len; \ |
| 9145 | if (end < 0) \ |
| 9146 | end = 0; \ |
| 9147 | } \ |
| 9148 | if (start < 0) { \ |
| 9149 | start += len; \ |
| 9150 | if (start < 0) \ |
| 9151 | start = 0; \ |
| 9152 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9153 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9154 | Py_ssize_t |
| 9155 | PyUnicode_Count(PyObject *str, |
| 9156 | PyObject *substr, |
| 9157 | Py_ssize_t start, |
| 9158 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9159 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9160 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9161 | PyObject* str_obj; |
| 9162 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9163 | int kind1, kind2, kind; |
| 9164 | void *buf1 = NULL, *buf2 = NULL; |
| 9165 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9166 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9167 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9168 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9169 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9170 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9171 | if (!sub_obj) { |
| 9172 | Py_DECREF(str_obj); |
| 9173 | return -1; |
| 9174 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 9175 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 9176 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9177 | Py_DECREF(str_obj); |
| 9178 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9179 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9180 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9181 | kind1 = PyUnicode_KIND(str_obj); |
| 9182 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9183 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9184 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9185 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9186 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9187 | if (kind2 > kind) { |
| 9188 | Py_DECREF(sub_obj); |
| 9189 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 9190 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 9191 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9192 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 9193 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9194 | if (!buf2) |
| 9195 | goto onError; |
| 9196 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 9197 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 9198 | |
| 9199 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9200 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9201 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9202 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 9203 | result = asciilib_count( |
| 9204 | ((Py_UCS1*)buf1) + start, end - start, |
| 9205 | buf2, len2, PY_SSIZE_T_MAX |
| 9206 | ); |
| 9207 | else |
| 9208 | result = ucs1lib_count( |
| 9209 | ((Py_UCS1*)buf1) + start, end - start, |
| 9210 | buf2, len2, PY_SSIZE_T_MAX |
| 9211 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9212 | break; |
| 9213 | case PyUnicode_2BYTE_KIND: |
| 9214 | result = ucs2lib_count( |
| 9215 | ((Py_UCS2*)buf1) + start, end - start, |
| 9216 | buf2, len2, PY_SSIZE_T_MAX |
| 9217 | ); |
| 9218 | break; |
| 9219 | case PyUnicode_4BYTE_KIND: |
| 9220 | result = ucs4lib_count( |
| 9221 | ((Py_UCS4*)buf1) + start, end - start, |
| 9222 | buf2, len2, PY_SSIZE_T_MAX |
| 9223 | ); |
| 9224 | break; |
| 9225 | default: |
| 9226 | assert(0); result = 0; |
| 9227 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9228 | |
| 9229 | Py_DECREF(sub_obj); |
| 9230 | Py_DECREF(str_obj); |
| 9231 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9232 | if (kind2 != kind) |
| 9233 | PyMem_Free(buf2); |
| 9234 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9235 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9236 | onError: |
| 9237 | Py_DECREF(sub_obj); |
| 9238 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9239 | if (kind2 != kind && buf2) |
| 9240 | PyMem_Free(buf2); |
| 9241 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9242 | } |
| 9243 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9244 | Py_ssize_t |
| 9245 | PyUnicode_Find(PyObject *str, |
| 9246 | PyObject *sub, |
| 9247 | Py_ssize_t start, |
| 9248 | Py_ssize_t end, |
| 9249 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9250 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9251 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9253 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9254 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9255 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9256 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9257 | if (!sub) { |
| 9258 | Py_DECREF(str); |
| 9259 | return -2; |
| 9260 | } |
| 9261 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 9262 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9263 | Py_DECREF(str); |
| 9264 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9265 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9266 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 9267 | result = any_find_slice(direction, |
| 9268 | str, sub, start, end |
| 9269 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9271 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9272 | Py_DECREF(sub); |
| 9273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9274 | return result; |
| 9275 | } |
| 9276 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | Py_ssize_t |
| 9278 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 9279 | Py_ssize_t start, Py_ssize_t end, |
| 9280 | int direction) |
| 9281 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9282 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9283 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9284 | if (PyUnicode_READY(str) == -1) |
| 9285 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 9286 | if (start < 0 || end < 0) { |
| 9287 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9288 | return -2; |
| 9289 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9290 | if (end > PyUnicode_GET_LENGTH(str)) |
| 9291 | end = PyUnicode_GET_LENGTH(str); |
| 9292 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9293 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 9294 | kind, end-start, ch, direction); |
| 9295 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9296 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 9297 | else |
| 9298 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9299 | } |
| 9300 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9301 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9302 | tailmatch(PyObject *self, |
| 9303 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9304 | Py_ssize_t start, |
| 9305 | Py_ssize_t end, |
| 9306 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9307 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9308 | int kind_self; |
| 9309 | int kind_sub; |
| 9310 | void *data_self; |
| 9311 | void *data_sub; |
| 9312 | Py_ssize_t offset; |
| 9313 | Py_ssize_t i; |
| 9314 | Py_ssize_t end_sub; |
| 9315 | |
| 9316 | if (PyUnicode_READY(self) == -1 || |
| 9317 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 9318 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9319 | |
| 9320 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9321 | return 1; |
| 9322 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9323 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 9324 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9325 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9326 | return 0; |
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 | kind_self = PyUnicode_KIND(self); |
| 9329 | data_self = PyUnicode_DATA(self); |
| 9330 | kind_sub = PyUnicode_KIND(substring); |
| 9331 | data_sub = PyUnicode_DATA(substring); |
| 9332 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 9333 | |
| 9334 | if (direction > 0) |
| 9335 | offset = end; |
| 9336 | else |
| 9337 | offset = start; |
| 9338 | |
| 9339 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 9340 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 9341 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 9342 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 9343 | /* If both are of the same kind, memcmp is sufficient */ |
| 9344 | if (kind_self == kind_sub) { |
| 9345 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9346 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9347 | data_sub, |
| 9348 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9349 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9350 | } |
| 9351 | /* otherwise we have to compare each character by first accesing it */ |
| 9352 | else { |
| 9353 | /* We do not need to compare 0 and len(substring)-1 because |
| 9354 | the if statement above ensured already that they are equal |
| 9355 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9356 | for (i = 1; i < end_sub; ++i) { |
| 9357 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 9358 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 9359 | return 0; |
| 9360 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9361 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9362 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9363 | } |
| 9364 | |
| 9365 | return 0; |
| 9366 | } |
| 9367 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9368 | Py_ssize_t |
| 9369 | PyUnicode_Tailmatch(PyObject *str, |
| 9370 | PyObject *substr, |
| 9371 | Py_ssize_t start, |
| 9372 | Py_ssize_t end, |
| 9373 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9374 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9375 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9376 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9377 | str = PyUnicode_FromObject(str); |
| 9378 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9379 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9380 | substr = PyUnicode_FromObject(substr); |
| 9381 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9382 | Py_DECREF(str); |
| 9383 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9384 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9385 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9386 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9387 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9388 | Py_DECREF(str); |
| 9389 | Py_DECREF(substr); |
| 9390 | return result; |
| 9391 | } |
| 9392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9393 | /* Apply fixfct filter to the Unicode object self and return a |
| 9394 | reference to the modified object */ |
| 9395 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9396 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9397 | fixup(PyObject *self, |
| 9398 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9399 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9400 | PyObject *u; |
| 9401 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9402 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9403 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9404 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9405 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9406 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9407 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9408 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9409 | /* fix functions return the new maximum character in a string, |
| 9410 | if the kind of the resulting unicode object does not change, |
| 9411 | everything is fine. Otherwise we need to change the string kind |
| 9412 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9413 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9414 | |
| 9415 | if (maxchar_new == 0) { |
| 9416 | /* no changes */; |
| 9417 | if (PyUnicode_CheckExact(self)) { |
| 9418 | Py_DECREF(u); |
| 9419 | Py_INCREF(self); |
| 9420 | return self; |
| 9421 | } |
| 9422 | else |
| 9423 | return u; |
| 9424 | } |
| 9425 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9426 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9427 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9428 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9429 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9430 | |
| 9431 | /* In case the maximum character changed, we need to |
| 9432 | convert the string to the new category. */ |
| 9433 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9434 | if (v == NULL) { |
| 9435 | Py_DECREF(u); |
| 9436 | return NULL; |
| 9437 | } |
| 9438 | if (maxchar_new > maxchar_old) { |
| 9439 | /* If the maxchar increased so that the kind changed, not all |
| 9440 | characters are representable anymore and we need to fix the |
| 9441 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9442 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9443 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9444 | maxchar_old = fixfct(v); |
| 9445 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9446 | } |
| 9447 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9448 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9449 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9450 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9451 | Py_DECREF(u); |
| 9452 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9453 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9454 | } |
| 9455 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9456 | static PyObject * |
| 9457 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9458 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9459 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9460 | char *resdata, *data = PyUnicode_DATA(self); |
| 9461 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9462 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9463 | res = PyUnicode_New(len, 127); |
| 9464 | if (res == NULL) |
| 9465 | return NULL; |
| 9466 | resdata = PyUnicode_DATA(res); |
| 9467 | if (lower) |
| 9468 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9469 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9470 | _Py_bytes_upper(resdata, data, len); |
| 9471 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9472 | } |
| 9473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9474 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9475 | 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] | 9476 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9477 | Py_ssize_t j; |
| 9478 | int final_sigma; |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 9479 | Py_UCS4 c = 0; |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9480 | /* 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] | 9481 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9482 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9483 | |
| 9484 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9485 | */ |
| 9486 | for (j = i - 1; j >= 0; j--) { |
| 9487 | c = PyUnicode_READ(kind, data, j); |
| 9488 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9489 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9490 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9491 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9492 | if (final_sigma) { |
| 9493 | for (j = i + 1; j < length; j++) { |
| 9494 | c = PyUnicode_READ(kind, data, j); |
| 9495 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9496 | break; |
| 9497 | } |
| 9498 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9499 | } |
| 9500 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9501 | } |
| 9502 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9503 | static int |
| 9504 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9505 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9506 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9507 | /* Obscure special case. */ |
| 9508 | if (c == 0x3A3) { |
| 9509 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9510 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9511 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9512 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9513 | } |
| 9514 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9515 | static Py_ssize_t |
| 9516 | 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] | 9517 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9518 | Py_ssize_t i, k = 0; |
| 9519 | int n_res, j; |
| 9520 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9521 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9522 | c = PyUnicode_READ(kind, data, 0); |
| 9523 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9524 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9525 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9526 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9527 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9528 | for (i = 1; i < length; i++) { |
| 9529 | c = PyUnicode_READ(kind, data, i); |
| 9530 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9531 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9532 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9533 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9534 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9535 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9536 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9537 | } |
| 9538 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9539 | static Py_ssize_t |
| 9540 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9541 | Py_ssize_t i, k = 0; |
| 9542 | |
| 9543 | for (i = 0; i < length; i++) { |
| 9544 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9545 | int n_res, j; |
| 9546 | if (Py_UNICODE_ISUPPER(c)) { |
| 9547 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9548 | } |
| 9549 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9550 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9551 | } |
| 9552 | else { |
| 9553 | n_res = 1; |
| 9554 | mapped[0] = c; |
| 9555 | } |
| 9556 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9557 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9558 | res[k++] = mapped[j]; |
| 9559 | } |
| 9560 | } |
| 9561 | return k; |
| 9562 | } |
| 9563 | |
| 9564 | static Py_ssize_t |
| 9565 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9566 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9567 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9568 | Py_ssize_t i, k = 0; |
| 9569 | |
| 9570 | for (i = 0; i < length; i++) { |
| 9571 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9572 | int n_res, j; |
| 9573 | if (lower) |
| 9574 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9575 | else |
| 9576 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9577 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9578 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9579 | res[k++] = mapped[j]; |
| 9580 | } |
| 9581 | } |
| 9582 | return k; |
| 9583 | } |
| 9584 | |
| 9585 | static Py_ssize_t |
| 9586 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9587 | { |
| 9588 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9589 | } |
| 9590 | |
| 9591 | static Py_ssize_t |
| 9592 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9593 | { |
| 9594 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9595 | } |
| 9596 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9597 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9598 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9599 | { |
| 9600 | Py_ssize_t i, k = 0; |
| 9601 | |
| 9602 | for (i = 0; i < length; i++) { |
| 9603 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9604 | Py_UCS4 mapped[3]; |
| 9605 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9606 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9607 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9608 | res[k++] = mapped[j]; |
| 9609 | } |
| 9610 | } |
| 9611 | return k; |
| 9612 | } |
| 9613 | |
| 9614 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9615 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9616 | { |
| 9617 | Py_ssize_t i, k = 0; |
| 9618 | int previous_is_cased; |
| 9619 | |
| 9620 | previous_is_cased = 0; |
| 9621 | for (i = 0; i < length; i++) { |
| 9622 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9623 | Py_UCS4 mapped[3]; |
| 9624 | int n_res, j; |
| 9625 | |
| 9626 | if (previous_is_cased) |
| 9627 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9628 | else |
| 9629 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9630 | |
| 9631 | for (j = 0; j < n_res; j++) { |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9632 | *maxchar = Py_MAX(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9633 | res[k++] = mapped[j]; |
| 9634 | } |
| 9635 | |
| 9636 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9637 | } |
| 9638 | return k; |
| 9639 | } |
| 9640 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9641 | static PyObject * |
| 9642 | case_operation(PyObject *self, |
| 9643 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9644 | { |
| 9645 | PyObject *res = NULL; |
| 9646 | Py_ssize_t length, newlength = 0; |
| 9647 | int kind, outkind; |
| 9648 | void *data, *outdata; |
| 9649 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9650 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9651 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9652 | |
| 9653 | kind = PyUnicode_KIND(self); |
| 9654 | data = PyUnicode_DATA(self); |
| 9655 | length = PyUnicode_GET_LENGTH(self); |
Antoine Pitrou | b6dc9b7 | 2014-10-15 23:14:53 +0200 | [diff] [blame] | 9656 | if ((size_t) length > PY_SSIZE_T_MAX / (3 * sizeof(Py_UCS4))) { |
Benjamin Peterson | e1bd38c | 2014-10-15 11:47:36 -0400 | [diff] [blame] | 9657 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 9658 | return NULL; |
| 9659 | } |
Benjamin Peterson | 1e211ff | 2014-10-15 12:17:21 -0400 | [diff] [blame] | 9660 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9661 | if (tmp == NULL) |
| 9662 | return PyErr_NoMemory(); |
| 9663 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9664 | res = PyUnicode_New(newlength, maxchar); |
| 9665 | if (res == NULL) |
| 9666 | goto leave; |
| 9667 | tmpend = tmp + newlength; |
| 9668 | outdata = PyUnicode_DATA(res); |
| 9669 | outkind = PyUnicode_KIND(res); |
| 9670 | switch (outkind) { |
| 9671 | case PyUnicode_1BYTE_KIND: |
| 9672 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9673 | break; |
| 9674 | case PyUnicode_2BYTE_KIND: |
| 9675 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9676 | break; |
| 9677 | case PyUnicode_4BYTE_KIND: |
| 9678 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9679 | break; |
| 9680 | default: |
| 9681 | assert(0); |
| 9682 | break; |
| 9683 | } |
| 9684 | leave: |
| 9685 | PyMem_FREE(tmp); |
| 9686 | return res; |
| 9687 | } |
| 9688 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9689 | PyObject * |
| 9690 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9691 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9692 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9693 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9694 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9695 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9696 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9697 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9698 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9699 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9700 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9701 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9702 | int use_memcpy; |
| 9703 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9704 | PyObject *last_obj; |
| 9705 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9706 | |
Benjamin Peterson | 9743b2c | 2014-02-15 13:02:52 -0500 | [diff] [blame] | 9707 | fseq = PySequence_Fast(seq, "can only join an iterable"); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9708 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9709 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9710 | } |
| 9711 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9712 | /* NOTE: the following code can't call back into Python code, |
| 9713 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9714 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9715 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9716 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9717 | /* If empty sequence, return u"". */ |
| 9718 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9719 | Py_DECREF(fseq); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 9720 | _Py_RETURN_UNICODE_EMPTY(); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9721 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9722 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9723 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9724 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9725 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9726 | if (seqlen == 1) { |
| 9727 | if (PyUnicode_CheckExact(items[0])) { |
| 9728 | res = items[0]; |
| 9729 | Py_INCREF(res); |
| 9730 | Py_DECREF(fseq); |
| 9731 | return res; |
| 9732 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9733 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9734 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9735 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9736 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9737 | /* Set up sep and seplen */ |
| 9738 | if (separator == NULL) { |
| 9739 | /* fall back to a blank space separator */ |
| 9740 | sep = PyUnicode_FromOrdinal(' '); |
| 9741 | if (!sep) |
| 9742 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9743 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9744 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9745 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9746 | else { |
| 9747 | if (!PyUnicode_Check(separator)) { |
| 9748 | PyErr_Format(PyExc_TypeError, |
| 9749 | "separator: expected str instance," |
| 9750 | " %.80s found", |
| 9751 | Py_TYPE(separator)->tp_name); |
| 9752 | goto onError; |
| 9753 | } |
| 9754 | if (PyUnicode_READY(separator)) |
| 9755 | goto onError; |
| 9756 | sep = separator; |
| 9757 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9758 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9759 | /* inc refcount to keep this code path symmetric with the |
| 9760 | above case of a blank separator */ |
| 9761 | Py_INCREF(sep); |
| 9762 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9763 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9764 | } |
| 9765 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9766 | /* There are at least two things to join, or else we have a subclass |
| 9767 | * of str in the sequence. |
| 9768 | * Do a pre-pass to figure out the total amount of space we'll |
| 9769 | * need (sz), and see whether all argument are strings. |
| 9770 | */ |
| 9771 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9772 | #ifdef Py_DEBUG |
| 9773 | use_memcpy = 0; |
| 9774 | #else |
| 9775 | use_memcpy = 1; |
| 9776 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9777 | for (i = 0; i < seqlen; i++) { |
| 9778 | const Py_ssize_t old_sz = sz; |
| 9779 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9780 | if (!PyUnicode_Check(item)) { |
| 9781 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 9782 | "sequence item %zd: expected str instance," |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9783 | " %.80s found", |
| 9784 | i, Py_TYPE(item)->tp_name); |
| 9785 | goto onError; |
| 9786 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9787 | if (PyUnicode_READY(item) == -1) |
| 9788 | goto onError; |
| 9789 | sz += PyUnicode_GET_LENGTH(item); |
| 9790 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9791 | maxchar = Py_MAX(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9792 | if (i != 0) |
| 9793 | sz += seplen; |
| 9794 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9795 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9796 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9797 | goto onError; |
| 9798 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9799 | if (use_memcpy && last_obj != NULL) { |
| 9800 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9801 | use_memcpy = 0; |
| 9802 | } |
| 9803 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9804 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9805 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9806 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9807 | if (res == NULL) |
| 9808 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9809 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9810 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9811 | #ifdef Py_DEBUG |
| 9812 | use_memcpy = 0; |
| 9813 | #else |
| 9814 | if (use_memcpy) { |
| 9815 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9816 | kind = PyUnicode_KIND(res); |
| 9817 | if (seplen != 0) |
| 9818 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9819 | } |
| 9820 | #endif |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9821 | if (use_memcpy) { |
| 9822 | for (i = 0; i < seqlen; ++i) { |
| 9823 | Py_ssize_t itemlen; |
| 9824 | item = items[i]; |
| 9825 | |
| 9826 | /* Copy item, and maybe the separator. */ |
| 9827 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9828 | Py_MEMCPY(res_data, |
| 9829 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9830 | kind * seplen); |
| 9831 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9832 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9833 | |
| 9834 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9835 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9836 | Py_MEMCPY(res_data, |
| 9837 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9838 | kind * itemlen); |
| 9839 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9840 | } |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9841 | } |
| 9842 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
| 9843 | + kind * PyUnicode_GET_LENGTH(res)); |
| 9844 | } |
| 9845 | else { |
| 9846 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
| 9847 | Py_ssize_t itemlen; |
| 9848 | item = items[i]; |
| 9849 | |
| 9850 | /* Copy item, and maybe the separator. */ |
| 9851 | if (i && seplen != 0) { |
| 9852 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
| 9853 | res_offset += seplen; |
| 9854 | } |
| 9855 | |
| 9856 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9857 | if (itemlen != 0) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9858 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9859 | res_offset += itemlen; |
| 9860 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9861 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9862 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Victor Stinner | 4560f9c | 2013-04-14 18:56:46 +0200 | [diff] [blame] | 9863 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9864 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9865 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9866 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9867 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9868 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9869 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9870 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9871 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9872 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9873 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9874 | return NULL; |
| 9875 | } |
| 9876 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9877 | #define FILL(kind, data, value, start, length) \ |
| 9878 | do { \ |
| 9879 | Py_ssize_t i_ = 0; \ |
| 9880 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9881 | switch ((kind)) { \ |
| 9882 | case PyUnicode_1BYTE_KIND: { \ |
| 9883 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9884 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9885 | break; \ |
| 9886 | } \ |
| 9887 | case PyUnicode_2BYTE_KIND: { \ |
| 9888 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9889 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9890 | break; \ |
| 9891 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9892 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9893 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9894 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9895 | break; \ |
| 9896 | } \ |
Serhiy Storchaka | 133b11b | 2014-12-01 18:56:28 +0200 | [diff] [blame] | 9897 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9898 | } \ |
| 9899 | } while (0) |
| 9900 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9901 | void |
| 9902 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9903 | Py_UCS4 fill_char) |
| 9904 | { |
| 9905 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9906 | const void *data = PyUnicode_DATA(unicode); |
| 9907 | assert(PyUnicode_IS_READY(unicode)); |
| 9908 | assert(unicode_modifiable(unicode)); |
| 9909 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9910 | assert(start >= 0); |
| 9911 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9912 | FILL(kind, data, fill_char, start, length); |
| 9913 | } |
| 9914 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9915 | Py_ssize_t |
| 9916 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9917 | Py_UCS4 fill_char) |
| 9918 | { |
| 9919 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9920 | |
| 9921 | if (!PyUnicode_Check(unicode)) { |
| 9922 | PyErr_BadInternalCall(); |
| 9923 | return -1; |
| 9924 | } |
| 9925 | if (PyUnicode_READY(unicode) == -1) |
| 9926 | return -1; |
| 9927 | if (unicode_check_modifiable(unicode)) |
| 9928 | return -1; |
| 9929 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9930 | if (start < 0) { |
| 9931 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9932 | return -1; |
| 9933 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9934 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9935 | PyErr_SetString(PyExc_ValueError, |
| 9936 | "fill character is bigger than " |
| 9937 | "the string maximum character"); |
| 9938 | return -1; |
| 9939 | } |
| 9940 | |
| 9941 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9942 | length = Py_MIN(maxlen, length); |
| 9943 | if (length <= 0) |
| 9944 | return 0; |
| 9945 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9946 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9947 | return length; |
| 9948 | } |
| 9949 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9950 | static PyObject * |
| 9951 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9952 | Py_ssize_t left, |
| 9953 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9954 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9955 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9956 | PyObject *u; |
| 9957 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9958 | int kind; |
| 9959 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9960 | |
| 9961 | if (left < 0) |
| 9962 | left = 0; |
| 9963 | if (right < 0) |
| 9964 | right = 0; |
| 9965 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9966 | if (left == 0 && right == 0) |
| 9967 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9968 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9969 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9970 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9971 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9972 | return NULL; |
| 9973 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9974 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 9975 | maxchar = Py_MAX(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9976 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9977 | if (!u) |
| 9978 | return NULL; |
| 9979 | |
| 9980 | kind = PyUnicode_KIND(u); |
| 9981 | data = PyUnicode_DATA(u); |
| 9982 | if (left) |
| 9983 | FILL(kind, data, fill, 0, left); |
| 9984 | if (right) |
| 9985 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9986 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9987 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9988 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9989 | } |
| 9990 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9991 | PyObject * |
| 9992 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9993 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9994 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9995 | |
| 9996 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9997 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9998 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9999 | if (PyUnicode_READY(string) == -1) { |
| 10000 | Py_DECREF(string); |
| 10001 | return NULL; |
| 10002 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10003 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10004 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10005 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10006 | if (PyUnicode_IS_ASCII(string)) |
| 10007 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10008 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10009 | PyUnicode_GET_LENGTH(string), keepends); |
| 10010 | else |
| 10011 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10012 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10013 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10014 | break; |
| 10015 | case PyUnicode_2BYTE_KIND: |
| 10016 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10017 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10018 | PyUnicode_GET_LENGTH(string), keepends); |
| 10019 | break; |
| 10020 | case PyUnicode_4BYTE_KIND: |
| 10021 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10022 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10023 | PyUnicode_GET_LENGTH(string), keepends); |
| 10024 | break; |
| 10025 | default: |
| 10026 | assert(0); |
| 10027 | list = 0; |
| 10028 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10029 | Py_DECREF(string); |
| 10030 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10031 | } |
| 10032 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10033 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10034 | split(PyObject *self, |
| 10035 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10036 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10037 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10038 | int kind1, kind2, kind; |
| 10039 | void *buf1, *buf2; |
| 10040 | Py_ssize_t len1, len2; |
| 10041 | PyObject* out; |
| 10042 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10043 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10044 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10045 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10046 | if (PyUnicode_READY(self) == -1) |
| 10047 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10048 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10049 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10050 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10051 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10052 | if (PyUnicode_IS_ASCII(self)) |
| 10053 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10054 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10055 | PyUnicode_GET_LENGTH(self), maxcount |
| 10056 | ); |
| 10057 | else |
| 10058 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10059 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10060 | PyUnicode_GET_LENGTH(self), maxcount |
| 10061 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10062 | case PyUnicode_2BYTE_KIND: |
| 10063 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10064 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10065 | PyUnicode_GET_LENGTH(self), maxcount |
| 10066 | ); |
| 10067 | case PyUnicode_4BYTE_KIND: |
| 10068 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10069 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10070 | PyUnicode_GET_LENGTH(self), maxcount |
| 10071 | ); |
| 10072 | default: |
| 10073 | assert(0); |
| 10074 | return NULL; |
| 10075 | } |
| 10076 | |
| 10077 | if (PyUnicode_READY(substring) == -1) |
| 10078 | return NULL; |
| 10079 | |
| 10080 | kind1 = PyUnicode_KIND(self); |
| 10081 | kind2 = PyUnicode_KIND(substring); |
| 10082 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10083 | buf1 = PyUnicode_DATA(self); |
| 10084 | buf2 = PyUnicode_DATA(substring); |
| 10085 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10086 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10087 | if (!buf1) |
| 10088 | return NULL; |
| 10089 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10090 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10091 | if (!buf2) { |
| 10092 | if (kind1 != kind) PyMem_Free(buf1); |
| 10093 | return NULL; |
| 10094 | } |
| 10095 | len1 = PyUnicode_GET_LENGTH(self); |
| 10096 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10097 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10098 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10099 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10100 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10101 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10102 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10103 | else |
| 10104 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10105 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10106 | break; |
| 10107 | case PyUnicode_2BYTE_KIND: |
| 10108 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10109 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10110 | break; |
| 10111 | case PyUnicode_4BYTE_KIND: |
| 10112 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10113 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10114 | break; |
| 10115 | default: |
| 10116 | out = NULL; |
| 10117 | } |
| 10118 | if (kind1 != kind) |
| 10119 | PyMem_Free(buf1); |
| 10120 | if (kind2 != kind) |
| 10121 | PyMem_Free(buf2); |
| 10122 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10123 | } |
| 10124 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10125 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10126 | rsplit(PyObject *self, |
| 10127 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10128 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10129 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | int kind1, kind2, kind; |
| 10131 | void *buf1, *buf2; |
| 10132 | Py_ssize_t len1, len2; |
| 10133 | PyObject* out; |
| 10134 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10135 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10136 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10137 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 | if (PyUnicode_READY(self) == -1) |
| 10139 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10140 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10141 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10142 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10143 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10144 | if (PyUnicode_IS_ASCII(self)) |
| 10145 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10146 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10147 | PyUnicode_GET_LENGTH(self), maxcount |
| 10148 | ); |
| 10149 | else |
| 10150 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10151 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10152 | PyUnicode_GET_LENGTH(self), maxcount |
| 10153 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10154 | case PyUnicode_2BYTE_KIND: |
| 10155 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10156 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | PyUnicode_GET_LENGTH(self), maxcount |
| 10158 | ); |
| 10159 | case PyUnicode_4BYTE_KIND: |
| 10160 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10161 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10162 | PyUnicode_GET_LENGTH(self), maxcount |
| 10163 | ); |
| 10164 | default: |
| 10165 | assert(0); |
| 10166 | return NULL; |
| 10167 | } |
| 10168 | |
| 10169 | if (PyUnicode_READY(substring) == -1) |
| 10170 | return NULL; |
| 10171 | |
| 10172 | kind1 = PyUnicode_KIND(self); |
| 10173 | kind2 = PyUnicode_KIND(substring); |
| 10174 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10175 | buf1 = PyUnicode_DATA(self); |
| 10176 | buf2 = PyUnicode_DATA(substring); |
| 10177 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10178 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | if (!buf1) |
| 10180 | return NULL; |
| 10181 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10182 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10183 | if (!buf2) { |
| 10184 | if (kind1 != kind) PyMem_Free(buf1); |
| 10185 | return NULL; |
| 10186 | } |
| 10187 | len1 = PyUnicode_GET_LENGTH(self); |
| 10188 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10189 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10190 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10192 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 10193 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10194 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10195 | else |
| 10196 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10197 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | break; |
| 10199 | case PyUnicode_2BYTE_KIND: |
| 10200 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10201 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10202 | break; |
| 10203 | case PyUnicode_4BYTE_KIND: |
| 10204 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10205 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10206 | break; |
| 10207 | default: |
| 10208 | out = NULL; |
| 10209 | } |
| 10210 | if (kind1 != kind) |
| 10211 | PyMem_Free(buf1); |
| 10212 | if (kind2 != kind) |
| 10213 | PyMem_Free(buf2); |
| 10214 | return out; |
| 10215 | } |
| 10216 | |
| 10217 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10218 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 10219 | 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] | 10220 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10221 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10222 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10223 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 10224 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 10225 | else |
| 10226 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10227 | case PyUnicode_2BYTE_KIND: |
| 10228 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 10229 | case PyUnicode_4BYTE_KIND: |
| 10230 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 10231 | } |
| 10232 | assert(0); |
| 10233 | return -1; |
| 10234 | } |
| 10235 | |
| 10236 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10237 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 10238 | 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] | 10239 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 10240 | switch (kind) { |
| 10241 | case PyUnicode_1BYTE_KIND: |
| 10242 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 10243 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 10244 | else |
| 10245 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10246 | case PyUnicode_2BYTE_KIND: |
| 10247 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10248 | case PyUnicode_4BYTE_KIND: |
| 10249 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 10250 | } |
| 10251 | assert(0); |
| 10252 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 10253 | } |
| 10254 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10255 | static void |
| 10256 | replace_1char_inplace(PyObject *u, Py_ssize_t pos, |
| 10257 | Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount) |
| 10258 | { |
| 10259 | int kind = PyUnicode_KIND(u); |
| 10260 | void *data = PyUnicode_DATA(u); |
| 10261 | Py_ssize_t len = PyUnicode_GET_LENGTH(u); |
| 10262 | if (kind == PyUnicode_1BYTE_KIND) { |
| 10263 | ucs1lib_replace_1char_inplace((Py_UCS1 *)data + pos, |
| 10264 | (Py_UCS1 *)data + len, |
| 10265 | u1, u2, maxcount); |
| 10266 | } |
| 10267 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 10268 | ucs2lib_replace_1char_inplace((Py_UCS2 *)data + pos, |
| 10269 | (Py_UCS2 *)data + len, |
| 10270 | u1, u2, maxcount); |
| 10271 | } |
| 10272 | else { |
| 10273 | assert(kind == PyUnicode_4BYTE_KIND); |
| 10274 | ucs4lib_replace_1char_inplace((Py_UCS4 *)data + pos, |
| 10275 | (Py_UCS4 *)data + len, |
| 10276 | u1, u2, maxcount); |
| 10277 | } |
| 10278 | } |
| 10279 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10280 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10281 | replace(PyObject *self, PyObject *str1, |
| 10282 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10283 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10284 | PyObject *u; |
| 10285 | char *sbuf = PyUnicode_DATA(self); |
| 10286 | char *buf1 = PyUnicode_DATA(str1); |
| 10287 | char *buf2 = PyUnicode_DATA(str2); |
| 10288 | int srelease = 0, release1 = 0, release2 = 0; |
| 10289 | int skind = PyUnicode_KIND(self); |
| 10290 | int kind1 = PyUnicode_KIND(str1); |
| 10291 | int kind2 = PyUnicode_KIND(str2); |
| 10292 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 10293 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 10294 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10295 | int mayshrink; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10296 | Py_UCS4 maxchar, maxchar_str1, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10297 | |
| 10298 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10299 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10300 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10301 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10302 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 10303 | if (str1 == str2) |
| 10304 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10306 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10307 | maxchar_str1 = PyUnicode_MAX_CHAR_VALUE(str1); |
| 10308 | if (maxchar < maxchar_str1) |
| 10309 | /* substring too wide to be present */ |
| 10310 | goto nothing; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10311 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 10312 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 10313 | result string. */ |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10314 | mayshrink = (maxchar_str2 < maxchar_str1) && (maxchar == maxchar_str1); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 10315 | maxchar = Py_MAX(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10316 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10317 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10318 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10319 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10320 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10321 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10322 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10323 | Py_UCS4 u1, u2; |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10324 | Py_ssize_t pos; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10325 | |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10326 | u1 = PyUnicode_READ(kind1, buf1, 0); |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10327 | pos = findchar(sbuf, skind, slen, u1, 1); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10328 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10329 | goto nothing; |
Victor Stinner | 69ed0f4 | 2013-04-09 21:48:24 +0200 | [diff] [blame] | 10330 | u2 = PyUnicode_READ(kind2, buf2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10331 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10332 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10333 | goto error; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10334 | |
Serhiy Storchaka | e2cef88 | 2013-04-13 22:45:04 +0300 | [diff] [blame] | 10335 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
| 10336 | replace_1char_inplace(u, pos, u1, u2, maxcount); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10337 | } |
| 10338 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | int rkind = skind; |
| 10340 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 10341 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10342 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10343 | if (kind1 < rkind) { |
| 10344 | /* widen substring */ |
| 10345 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10346 | if (!buf1) goto error; |
| 10347 | release1 = 1; |
| 10348 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10349 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10350 | if (i < 0) |
| 10351 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10352 | if (rkind > kind2) { |
| 10353 | /* widen replacement */ |
| 10354 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10355 | if (!buf2) goto error; |
| 10356 | release2 = 1; |
| 10357 | } |
| 10358 | else if (rkind < kind2) { |
| 10359 | /* widen self and buf1 */ |
| 10360 | rkind = kind2; |
| 10361 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10362 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10363 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10364 | if (!sbuf) goto error; |
| 10365 | srelease = 1; |
| 10366 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10367 | if (!buf1) goto error; |
| 10368 | release1 = 1; |
| 10369 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10370 | u = PyUnicode_New(slen, maxchar); |
| 10371 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10372 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10373 | assert(PyUnicode_KIND(u) == rkind); |
| 10374 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10375 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10376 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10377 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10378 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10379 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10380 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10381 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10382 | |
| 10383 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10384 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10385 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10386 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10387 | if (i == -1) |
| 10388 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10389 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10390 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10391 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10392 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10393 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10394 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10395 | } |
| 10396 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10397 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10398 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10399 | int rkind = skind; |
| 10400 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10402 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10403 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10404 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10405 | if (!buf1) goto error; |
| 10406 | release1 = 1; |
| 10407 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10408 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10409 | if (n == 0) |
| 10410 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10411 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10412 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10413 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10414 | if (!buf2) goto error; |
| 10415 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10416 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10417 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10418 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10419 | rkind = kind2; |
| 10420 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10421 | if (!sbuf) goto error; |
| 10422 | srelease = 1; |
| 10423 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10424 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10425 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10426 | if (!buf1) goto error; |
| 10427 | release1 = 1; |
| 10428 | } |
| 10429 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10430 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10431 | if (len2 > len1 && len2 - len1 > (PY_SSIZE_T_MAX - slen) / n) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10432 | PyErr_SetString(PyExc_OverflowError, |
| 10433 | "replace string is too long"); |
| 10434 | goto error; |
| 10435 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10436 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10437 | if (new_size == 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 10438 | _Py_INCREF_UNICODE_EMPTY(); |
| 10439 | if (!unicode_empty) |
| 10440 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10441 | u = unicode_empty; |
| 10442 | goto done; |
| 10443 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10444 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | PyErr_SetString(PyExc_OverflowError, |
| 10446 | "replace string is too long"); |
| 10447 | goto error; |
| 10448 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10449 | u = PyUnicode_New(new_size, maxchar); |
| 10450 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10451 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10452 | assert(PyUnicode_KIND(u) == rkind); |
| 10453 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10454 | ires = i = 0; |
| 10455 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10456 | while (n-- > 0) { |
| 10457 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10458 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10459 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10460 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10461 | if (j == -1) |
| 10462 | break; |
| 10463 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10464 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10465 | memcpy(res + rkind * ires, |
| 10466 | sbuf + rkind * i, |
| 10467 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10468 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10469 | } |
| 10470 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10472 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10473 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10474 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10475 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10477 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10478 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10479 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10480 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10481 | memcpy(res + rkind * ires, |
| 10482 | sbuf + rkind * i, |
| 10483 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10484 | } |
| 10485 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10486 | /* interleave */ |
| 10487 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10488 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10489 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10490 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10492 | if (--n <= 0) |
| 10493 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10494 | memcpy(res + rkind * ires, |
| 10495 | sbuf + rkind * i, |
| 10496 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10497 | ires++; |
| 10498 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10499 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10500 | memcpy(res + rkind * ires, |
| 10501 | sbuf + rkind * i, |
| 10502 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10503 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10504 | } |
| 10505 | |
| 10506 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10507 | unicode_adjust_maxchar(&u); |
| 10508 | if (u == NULL) |
| 10509 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10510 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10511 | |
| 10512 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10513 | if (srelease) |
| 10514 | PyMem_FREE(sbuf); |
| 10515 | if (release1) |
| 10516 | PyMem_FREE(buf1); |
| 10517 | if (release2) |
| 10518 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10519 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10520 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10521 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10522 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10523 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10524 | if (srelease) |
| 10525 | PyMem_FREE(sbuf); |
| 10526 | if (release1) |
| 10527 | PyMem_FREE(buf1); |
| 10528 | if (release2) |
| 10529 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10530 | return unicode_result_unchanged(self); |
| 10531 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10532 | error: |
| 10533 | if (srelease && sbuf) |
| 10534 | PyMem_FREE(sbuf); |
| 10535 | if (release1 && buf1) |
| 10536 | PyMem_FREE(buf1); |
| 10537 | if (release2 && buf2) |
| 10538 | PyMem_FREE(buf2); |
| 10539 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10540 | } |
| 10541 | |
| 10542 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10543 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10544 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10545 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10546 | \n\ |
| 10547 | 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] | 10548 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10549 | |
| 10550 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10551 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10552 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10553 | if (PyUnicode_READY(self) == -1) |
| 10554 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10555 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10556 | } |
| 10557 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10558 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10559 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10560 | \n\ |
| 10561 | 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] | 10562 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10563 | |
| 10564 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10565 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10566 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10567 | if (PyUnicode_READY(self) == -1) |
| 10568 | return NULL; |
| 10569 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10570 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10571 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10572 | } |
| 10573 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10574 | PyDoc_STRVAR(casefold__doc__, |
| 10575 | "S.casefold() -> str\n\ |
| 10576 | \n\ |
| 10577 | Return a version of S suitable for caseless comparisons."); |
| 10578 | |
| 10579 | static PyObject * |
| 10580 | unicode_casefold(PyObject *self) |
| 10581 | { |
| 10582 | if (PyUnicode_READY(self) == -1) |
| 10583 | return NULL; |
| 10584 | if (PyUnicode_IS_ASCII(self)) |
| 10585 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10586 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10587 | } |
| 10588 | |
| 10589 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10590 | /* Argument converter. Coerces to a single unicode character */ |
| 10591 | |
| 10592 | static int |
| 10593 | convert_uc(PyObject *obj, void *addr) |
| 10594 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10595 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10596 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10597 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10598 | uniobj = PyUnicode_FromObject(obj); |
| 10599 | if (uniobj == NULL) { |
| 10600 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10601 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10602 | return 0; |
| 10603 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10604 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10605 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10606 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10607 | Py_DECREF(uniobj); |
| 10608 | return 0; |
| 10609 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10610 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10611 | Py_DECREF(uniobj); |
| 10612 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10613 | } |
| 10614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10615 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10616 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10617 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10618 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10619 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10620 | |
| 10621 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10622 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10623 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10624 | Py_ssize_t marg, left; |
| 10625 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10626 | Py_UCS4 fillchar = ' '; |
| 10627 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10628 | 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] | 10629 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10630 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10631 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10632 | return NULL; |
| 10633 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10634 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10635 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10636 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10637 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10638 | left = marg / 2 + (marg & width & 1); |
| 10639 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10640 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10641 | } |
| 10642 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10643 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10644 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10645 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10646 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10647 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10648 | #define COMPARE(TYPE1, TYPE2) \ |
| 10649 | do { \ |
| 10650 | TYPE1* p1 = (TYPE1 *)data1; \ |
| 10651 | TYPE2* p2 = (TYPE2 *)data2; \ |
| 10652 | TYPE1* end = p1 + len; \ |
| 10653 | Py_UCS4 c1, c2; \ |
| 10654 | for (; p1 != end; p1++, p2++) { \ |
| 10655 | c1 = *p1; \ |
| 10656 | c2 = *p2; \ |
| 10657 | if (c1 != c2) \ |
| 10658 | return (c1 < c2) ? -1 : 1; \ |
| 10659 | } \ |
| 10660 | } \ |
| 10661 | while (0) |
| 10662 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10663 | int kind1, kind2; |
| 10664 | void *data1, *data2; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10665 | Py_ssize_t len1, len2, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10666 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10667 | kind1 = PyUnicode_KIND(str1); |
| 10668 | kind2 = PyUnicode_KIND(str2); |
| 10669 | data1 = PyUnicode_DATA(str1); |
| 10670 | data2 = PyUnicode_DATA(str2); |
| 10671 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10672 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10673 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10674 | |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10675 | switch(kind1) { |
| 10676 | case PyUnicode_1BYTE_KIND: |
| 10677 | { |
| 10678 | switch(kind2) { |
| 10679 | case PyUnicode_1BYTE_KIND: |
| 10680 | { |
| 10681 | int cmp = memcmp(data1, data2, len); |
| 10682 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10683 | if (cmp < 0) |
| 10684 | return -1; |
| 10685 | if (cmp > 0) |
| 10686 | return 1; |
| 10687 | break; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10688 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10689 | case PyUnicode_2BYTE_KIND: |
| 10690 | COMPARE(Py_UCS1, Py_UCS2); |
| 10691 | break; |
| 10692 | case PyUnicode_4BYTE_KIND: |
| 10693 | COMPARE(Py_UCS1, Py_UCS4); |
| 10694 | break; |
| 10695 | default: |
| 10696 | assert(0); |
| 10697 | } |
| 10698 | break; |
| 10699 | } |
| 10700 | case PyUnicode_2BYTE_KIND: |
| 10701 | { |
| 10702 | switch(kind2) { |
| 10703 | case PyUnicode_1BYTE_KIND: |
| 10704 | COMPARE(Py_UCS2, Py_UCS1); |
| 10705 | break; |
| 10706 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10707 | { |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10708 | COMPARE(Py_UCS2, Py_UCS2); |
| 10709 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10710 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10711 | case PyUnicode_4BYTE_KIND: |
| 10712 | COMPARE(Py_UCS2, Py_UCS4); |
| 10713 | break; |
| 10714 | default: |
| 10715 | assert(0); |
| 10716 | } |
| 10717 | break; |
| 10718 | } |
| 10719 | case PyUnicode_4BYTE_KIND: |
| 10720 | { |
| 10721 | switch(kind2) { |
| 10722 | case PyUnicode_1BYTE_KIND: |
| 10723 | COMPARE(Py_UCS4, Py_UCS1); |
| 10724 | break; |
| 10725 | case PyUnicode_2BYTE_KIND: |
| 10726 | COMPARE(Py_UCS4, Py_UCS2); |
| 10727 | break; |
| 10728 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10729 | { |
| 10730 | #if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4 |
| 10731 | int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len); |
| 10732 | /* normalize result of wmemcmp() into the range [-1; 1] */ |
| 10733 | if (cmp < 0) |
| 10734 | return -1; |
| 10735 | if (cmp > 0) |
| 10736 | return 1; |
| 10737 | #else |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10738 | COMPARE(Py_UCS4, Py_UCS4); |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10739 | #endif |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10740 | break; |
Victor Stinner | cd777ea | 2013-04-08 22:43:44 +0200 | [diff] [blame] | 10741 | } |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10742 | default: |
| 10743 | assert(0); |
| 10744 | } |
| 10745 | break; |
| 10746 | } |
| 10747 | default: |
| 10748 | assert(0); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10749 | } |
| 10750 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10751 | if (len1 == len2) |
| 10752 | return 0; |
| 10753 | if (len1 < len2) |
| 10754 | return -1; |
| 10755 | else |
| 10756 | return 1; |
Victor Stinner | c1302bb | 2013-04-08 21:50:54 +0200 | [diff] [blame] | 10757 | |
| 10758 | #undef COMPARE |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10759 | } |
| 10760 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10761 | Py_LOCAL(int) |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10762 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10763 | { |
| 10764 | int kind; |
| 10765 | void *data1, *data2; |
| 10766 | Py_ssize_t len; |
| 10767 | int cmp; |
| 10768 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10769 | len = PyUnicode_GET_LENGTH(str1); |
| 10770 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10771 | return 0; |
| 10772 | kind = PyUnicode_KIND(str1); |
| 10773 | if (PyUnicode_KIND(str2) != kind) |
| 10774 | return 0; |
| 10775 | data1 = PyUnicode_DATA(str1); |
| 10776 | data2 = PyUnicode_DATA(str2); |
| 10777 | |
| 10778 | cmp = memcmp(data1, data2, len * kind); |
| 10779 | return (cmp == 0); |
| 10780 | } |
| 10781 | |
| 10782 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10783 | int |
| 10784 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10785 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10786 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10787 | if (PyUnicode_READY(left) == -1 || |
| 10788 | PyUnicode_READY(right) == -1) |
| 10789 | return -1; |
Victor Stinner | f0c7b2a | 2013-11-04 11:27:14 +0100 | [diff] [blame] | 10790 | |
| 10791 | /* a string is equal to itself */ |
| 10792 | if (left == right) |
| 10793 | return 0; |
| 10794 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10795 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10796 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10797 | PyErr_Format(PyExc_TypeError, |
| 10798 | "Can't compare %.100s and %.100s", |
| 10799 | left->ob_type->tp_name, |
| 10800 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10801 | return -1; |
| 10802 | } |
| 10803 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10804 | int |
Victor Stinner | ad14ccd | 2013-11-07 00:46:04 +0100 | [diff] [blame] | 10805 | _PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) |
| 10806 | { |
| 10807 | PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ |
| 10808 | if (right_str == NULL) |
| 10809 | return -1; |
| 10810 | return PyUnicode_Compare(left, right_str); |
| 10811 | } |
| 10812 | |
| 10813 | int |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10814 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10815 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10816 | Py_ssize_t i; |
| 10817 | int kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10818 | Py_UCS4 chr; |
| 10819 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10820 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10821 | if (PyUnicode_READY(uni) == -1) |
| 10822 | return -1; |
| 10823 | kind = PyUnicode_KIND(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10824 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | a6b9b07 | 2013-10-30 18:27:13 +0100 | [diff] [blame] | 10825 | const void *data = PyUnicode_1BYTE_DATA(uni); |
Victor Stinner | e1b1592 | 2013-11-03 13:53:12 +0100 | [diff] [blame] | 10826 | size_t len1 = (size_t)PyUnicode_GET_LENGTH(uni); |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10827 | size_t len, len2 = strlen(str); |
| 10828 | int cmp; |
| 10829 | |
| 10830 | len = Py_MIN(len1, len2); |
| 10831 | cmp = memcmp(data, str, len); |
Victor Stinner | 21ea21e | 2013-11-04 11:28:26 +0100 | [diff] [blame] | 10832 | if (cmp != 0) { |
| 10833 | if (cmp < 0) |
| 10834 | return -1; |
| 10835 | else |
| 10836 | return 1; |
| 10837 | } |
Victor Stinner | 602f7cf | 2013-10-29 23:31:50 +0100 | [diff] [blame] | 10838 | if (len1 > len2) |
| 10839 | return 1; /* uni is longer */ |
| 10840 | if (len2 > len1) |
| 10841 | return -1; /* str is longer */ |
| 10842 | return 0; |
| 10843 | } |
| 10844 | else { |
| 10845 | void *data = PyUnicode_DATA(uni); |
| 10846 | /* Compare Unicode string and source character set string */ |
| 10847 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10848 | if (chr != str[i]) |
| 10849 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
| 10850 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10851 | to C strings identical up to that point. */ |
| 10852 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
| 10853 | return 1; /* uni is longer */ |
| 10854 | if (str[i]) |
| 10855 | return -1; /* str is longer */ |
| 10856 | return 0; |
| 10857 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10858 | } |
| 10859 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10860 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10861 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10862 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10863 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10864 | PyObject * |
| 10865 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10866 | { |
| 10867 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10868 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10869 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10870 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10871 | Py_RETURN_NOTIMPLEMENTED; |
| 10872 | |
| 10873 | if (PyUnicode_READY(left) == -1 || |
| 10874 | PyUnicode_READY(right) == -1) |
| 10875 | return NULL; |
| 10876 | |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 10877 | if (left == right) { |
| 10878 | switch (op) { |
| 10879 | case Py_EQ: |
| 10880 | case Py_LE: |
| 10881 | case Py_GE: |
| 10882 | /* a string is equal to itself */ |
| 10883 | v = Py_True; |
| 10884 | break; |
| 10885 | case Py_NE: |
| 10886 | case Py_LT: |
| 10887 | case Py_GT: |
| 10888 | v = Py_False; |
| 10889 | break; |
| 10890 | default: |
| 10891 | PyErr_BadArgument(); |
| 10892 | return NULL; |
| 10893 | } |
| 10894 | } |
| 10895 | else if (op == Py_EQ || op == Py_NE) { |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10896 | result = unicode_compare_eq(left, right); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 10897 | result ^= (op == Py_NE); |
| 10898 | v = TEST_COND(result); |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10899 | } |
| 10900 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10901 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10902 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10903 | /* Convert the return value to a Boolean */ |
| 10904 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10905 | case Py_LE: |
| 10906 | v = TEST_COND(result <= 0); |
| 10907 | break; |
| 10908 | case Py_GE: |
| 10909 | v = TEST_COND(result >= 0); |
| 10910 | break; |
| 10911 | case Py_LT: |
| 10912 | v = TEST_COND(result == -1); |
| 10913 | break; |
| 10914 | case Py_GT: |
| 10915 | v = TEST_COND(result == 1); |
| 10916 | break; |
| 10917 | default: |
| 10918 | PyErr_BadArgument(); |
| 10919 | return NULL; |
| 10920 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10921 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10922 | Py_INCREF(v); |
| 10923 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10924 | } |
| 10925 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10926 | int |
| 10927 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10928 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10929 | PyObject *str, *sub; |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10930 | int kind1, kind2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10931 | void *buf1, *buf2; |
| 10932 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10933 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10934 | |
| 10935 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10936 | sub = PyUnicode_FromObject(element); |
| 10937 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10938 | PyErr_Format(PyExc_TypeError, |
| 10939 | "'in <string>' requires string as left operand, not %s", |
| 10940 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10941 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10942 | } |
| 10943 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10944 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10945 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10946 | Py_DECREF(sub); |
| 10947 | return -1; |
| 10948 | } |
| 10949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10950 | kind1 = PyUnicode_KIND(str); |
| 10951 | kind2 = PyUnicode_KIND(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10952 | buf1 = PyUnicode_DATA(str); |
| 10953 | buf2 = PyUnicode_DATA(sub); |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10954 | if (kind2 != kind1) { |
| 10955 | if (kind2 > kind1) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10956 | Py_DECREF(sub); |
| 10957 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10958 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10959 | } |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10960 | buf2 = _PyUnicode_AsKind(sub, kind1); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10961 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10962 | if (!buf2) { |
| 10963 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10964 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10965 | return -1; |
| 10966 | } |
| 10967 | len1 = PyUnicode_GET_LENGTH(str); |
| 10968 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10969 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10970 | switch (kind1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10971 | case PyUnicode_1BYTE_KIND: |
| 10972 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10973 | break; |
| 10974 | case PyUnicode_2BYTE_KIND: |
| 10975 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10976 | break; |
| 10977 | case PyUnicode_4BYTE_KIND: |
| 10978 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10979 | break; |
| 10980 | default: |
| 10981 | result = -1; |
| 10982 | assert(0); |
| 10983 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10984 | |
| 10985 | Py_DECREF(str); |
| 10986 | Py_DECREF(sub); |
| 10987 | |
Victor Stinner | 77282cb | 2013-04-14 19:22:47 +0200 | [diff] [blame] | 10988 | if (kind2 != kind1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10989 | PyMem_Free(buf2); |
| 10990 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10991 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10992 | } |
| 10993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10994 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10995 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10996 | PyObject * |
| 10997 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10999 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11000 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11001 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | |
| 11003 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11004 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11005 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11006 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11007 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11009 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11010 | |
| 11011 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11012 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11013 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11014 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11015 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11016 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11017 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11018 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11019 | } |
| 11020 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11021 | u_len = PyUnicode_GET_LENGTH(u); |
| 11022 | v_len = PyUnicode_GET_LENGTH(v); |
| 11023 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 11024 | PyErr_SetString(PyExc_OverflowError, |
| 11025 | "strings are too large to concat"); |
| 11026 | goto onError; |
| 11027 | } |
| 11028 | new_len = u_len + v_len; |
| 11029 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11030 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 11031 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11032 | maxchar = Py_MAX(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11033 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11034 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11035 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11036 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11037 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11038 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 11039 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11040 | Py_DECREF(u); |
| 11041 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11042 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11043 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11044 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11045 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11046 | Py_XDECREF(u); |
| 11047 | Py_XDECREF(v); |
| 11048 | return NULL; |
| 11049 | } |
| 11050 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11051 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11052 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11053 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11054 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11055 | Py_UCS4 maxchar, maxchar2; |
| 11056 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11057 | |
| 11058 | if (p_left == NULL) { |
| 11059 | if (!PyErr_Occurred()) |
| 11060 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11061 | return; |
| 11062 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11063 | left = *p_left; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11064 | if (right == NULL || left == NULL |
| 11065 | || !PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11066 | if (!PyErr_Occurred()) |
| 11067 | PyErr_BadInternalCall(); |
| 11068 | goto error; |
| 11069 | } |
| 11070 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11071 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11072 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11073 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 11074 | goto error; |
| 11075 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11076 | /* Shortcuts */ |
| 11077 | if (left == unicode_empty) { |
| 11078 | Py_DECREF(left); |
| 11079 | Py_INCREF(right); |
| 11080 | *p_left = right; |
| 11081 | return; |
| 11082 | } |
| 11083 | if (right == unicode_empty) |
| 11084 | return; |
| 11085 | |
| 11086 | left_len = PyUnicode_GET_LENGTH(left); |
| 11087 | right_len = PyUnicode_GET_LENGTH(right); |
| 11088 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 11089 | PyErr_SetString(PyExc_OverflowError, |
| 11090 | "strings are too large to concat"); |
| 11091 | goto error; |
| 11092 | } |
| 11093 | new_len = left_len + right_len; |
| 11094 | |
| 11095 | if (unicode_modifiable(left) |
| 11096 | && PyUnicode_CheckExact(right) |
| 11097 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11098 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 11099 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 11100 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 11101 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11102 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 11103 | { |
| 11104 | /* append inplace */ |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11105 | if (unicode_resize(p_left, new_len) != 0) |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11106 | goto error; |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 11107 | |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11108 | /* copy 'right' into the newly allocated area of 'left' */ |
| 11109 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11110 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11111 | else { |
| 11112 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 11113 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Benjamin Peterson | 7e30373 | 2013-06-10 09:19:46 -0700 | [diff] [blame] | 11114 | maxchar = Py_MAX(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11115 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11116 | /* Concat the two Unicode strings */ |
| 11117 | res = PyUnicode_New(new_len, maxchar); |
| 11118 | if (res == NULL) |
| 11119 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11120 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 11121 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11122 | Py_DECREF(left); |
Victor Stinner | bb4503f | 2013-04-18 09:41:34 +0200 | [diff] [blame] | 11123 | *p_left = res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11124 | } |
| 11125 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 11126 | return; |
| 11127 | |
| 11128 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 11129 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11130 | } |
| 11131 | |
| 11132 | void |
| 11133 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 11134 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11135 | PyUnicode_Append(pleft, right); |
| 11136 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 11137 | } |
| 11138 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11139 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11140 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11141 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11142 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11143 | 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] | 11144 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11145 | |
| 11146 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11147 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11148 | { |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 11149 | PyObject *substring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11150 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11151 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11152 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11153 | int kind1, kind2, kind; |
| 11154 | void *buf1, *buf2; |
| 11155 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11156 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11157 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 11158 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11159 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11160 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11161 | kind1 = PyUnicode_KIND(self); |
| 11162 | kind2 = PyUnicode_KIND(substring); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11163 | if (kind2 > kind1) { |
| 11164 | Py_DECREF(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11165 | return PyLong_FromLong(0); |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11166 | } |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 11167 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11168 | buf1 = PyUnicode_DATA(self); |
| 11169 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11170 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11171 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11172 | if (!buf2) { |
| 11173 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11174 | return NULL; |
| 11175 | } |
| 11176 | len1 = PyUnicode_GET_LENGTH(self); |
| 11177 | len2 = PyUnicode_GET_LENGTH(substring); |
| 11178 | |
| 11179 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 11180 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11181 | case PyUnicode_1BYTE_KIND: |
| 11182 | iresult = ucs1lib_count( |
| 11183 | ((Py_UCS1*)buf1) + start, end - start, |
| 11184 | buf2, len2, PY_SSIZE_T_MAX |
| 11185 | ); |
| 11186 | break; |
| 11187 | case PyUnicode_2BYTE_KIND: |
| 11188 | iresult = ucs2lib_count( |
| 11189 | ((Py_UCS2*)buf1) + start, end - start, |
| 11190 | buf2, len2, PY_SSIZE_T_MAX |
| 11191 | ); |
| 11192 | break; |
| 11193 | case PyUnicode_4BYTE_KIND: |
| 11194 | iresult = ucs4lib_count( |
| 11195 | ((Py_UCS4*)buf1) + start, end - start, |
| 11196 | buf2, len2, PY_SSIZE_T_MAX |
| 11197 | ); |
| 11198 | break; |
| 11199 | default: |
| 11200 | assert(0); iresult = 0; |
| 11201 | } |
| 11202 | |
| 11203 | result = PyLong_FromSsize_t(iresult); |
| 11204 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11205 | if (kind2 != kind) |
| 11206 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11207 | |
| 11208 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11209 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | return result; |
| 11211 | } |
| 11212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11213 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 11214 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11215 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 11216 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 11217 | 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] | 11218 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 11219 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 11220 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 11221 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11222 | |
| 11223 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11224 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11225 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11226 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11227 | char *encoding = NULL; |
| 11228 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 11229 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 11230 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 11231 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11232 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11233 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 11234 | } |
| 11235 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11236 | PyDoc_STRVAR(expandtabs__doc__, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11237 | "S.expandtabs(tabsize=8) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11238 | \n\ |
| 11239 | 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] | 11240 | 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] | 11241 | |
| 11242 | static PyObject* |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11243 | unicode_expandtabs(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11244 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11245 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 11246 | Py_UCS4 ch; |
| 11247 | PyObject *u; |
| 11248 | void *src_data, *dest_data; |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11249 | static char *kwlist[] = {"tabsize", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11250 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11251 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11252 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11253 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 11254 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:expandtabs", |
| 11255 | kwlist, &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11256 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11257 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 11258 | if (PyUnicode_READY(self) == -1) |
| 11259 | return NULL; |
| 11260 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11261 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11262 | src_len = PyUnicode_GET_LENGTH(self); |
| 11263 | i = j = line_pos = 0; |
| 11264 | kind = PyUnicode_KIND(self); |
| 11265 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11266 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11267 | for (; i < src_len; i++) { |
| 11268 | ch = PyUnicode_READ(kind, src_data, i); |
| 11269 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 11270 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11271 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11272 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11273 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11274 | goto overflow; |
| 11275 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11276 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11277 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11278 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11279 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11280 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11281 | goto overflow; |
| 11282 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11283 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11284 | if (ch == '\n' || ch == '\r') |
| 11285 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11286 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11287 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11288 | if (!found) |
| 11289 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11291 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11292 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11293 | if (!u) |
| 11294 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11295 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11296 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11297 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11299 | for (; i < src_len; i++) { |
| 11300 | ch = PyUnicode_READ(kind, src_data, i); |
| 11301 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11302 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11303 | incr = tabsize - (line_pos % tabsize); |
| 11304 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 11305 | FILL(kind, dest_data, ' ', j, incr); |
| 11306 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11307 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11308 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11309 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11310 | line_pos++; |
| 11311 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11312 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11313 | if (ch == '\n' || ch == '\r') |
| 11314 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11315 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11316 | } |
| 11317 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 11318 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11319 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 11320 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11321 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 11322 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11323 | } |
| 11324 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11325 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11326 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11327 | \n\ |
| 11328 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11329 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11330 | arguments start and end are interpreted as in slice notation.\n\ |
| 11331 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11332 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11333 | |
| 11334 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11335 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11336 | { |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 11337 | PyObject *substring = NULL; |
| 11338 | Py_ssize_t start = 0; |
| 11339 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11340 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11341 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11342 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 11343 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11344 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11345 | |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11346 | if (PyUnicode_READY(self) == -1) { |
| 11347 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11348 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11349 | } |
| 11350 | if (PyUnicode_READY(substring) == -1) { |
| 11351 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11352 | return NULL; |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 11353 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11354 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11355 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11356 | |
| 11357 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11358 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11359 | if (result == -2) |
| 11360 | return NULL; |
| 11361 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11362 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11363 | } |
| 11364 | |
| 11365 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 11366 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11367 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11368 | void *data; |
| 11369 | enum PyUnicode_Kind kind; |
| 11370 | Py_UCS4 ch; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11371 | |
| 11372 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 11373 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 11375 | } |
| 11376 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 11377 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11378 | return NULL; |
| 11379 | } |
| 11380 | kind = PyUnicode_KIND(self); |
| 11381 | data = PyUnicode_DATA(self); |
| 11382 | ch = PyUnicode_READ(kind, data, index); |
Victor Stinner | 985a82a | 2014-01-03 12:53:47 +0100 | [diff] [blame] | 11383 | return unicode_char(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11384 | } |
| 11385 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11386 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 11387 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 11388 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11389 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11390 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11391 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 11392 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11393 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11394 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11395 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 11396 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11397 | if (_PyUnicode_HASH(self) != -1) |
| 11398 | return _PyUnicode_HASH(self); |
| 11399 | if (PyUnicode_READY(self) == -1) |
| 11400 | return -1; |
| 11401 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 11402 | /* |
| 11403 | We make the hash of the empty string be 0, rather than using |
| 11404 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 11405 | */ |
| 11406 | if (len == 0) { |
| 11407 | _PyUnicode_HASH(self) = 0; |
| 11408 | return 0; |
| 11409 | } |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 11410 | x = _Py_HashBytes(PyUnicode_DATA(self), |
| 11411 | PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11412 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 11413 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11414 | } |
| 11415 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11416 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11417 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11419 | 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] | 11420 | |
| 11421 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11422 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11424 | Py_ssize_t result; |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 11425 | PyObject *substring = NULL; |
| 11426 | Py_ssize_t start = 0; |
| 11427 | Py_ssize_t end = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11428 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11429 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 11430 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11432 | |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11433 | if (PyUnicode_READY(self) == -1) { |
| 11434 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11435 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11436 | } |
| 11437 | if (PyUnicode_READY(substring) == -1) { |
| 11438 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11439 | return NULL; |
Christian Heimes | d47a045 | 2013-06-29 21:21:37 +0200 | [diff] [blame] | 11440 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11441 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11442 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | |
| 11444 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11445 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11446 | if (result == -2) |
| 11447 | return NULL; |
| 11448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | if (result < 0) { |
| 11450 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11451 | return NULL; |
| 11452 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11453 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11454 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11455 | } |
| 11456 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11457 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11458 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11460 | 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] | 11461 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | |
| 11463 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11464 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11465 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11466 | Py_ssize_t i, length; |
| 11467 | int kind; |
| 11468 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11469 | int cased; |
| 11470 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11471 | if (PyUnicode_READY(self) == -1) |
| 11472 | return NULL; |
| 11473 | length = PyUnicode_GET_LENGTH(self); |
| 11474 | kind = PyUnicode_KIND(self); |
| 11475 | data = PyUnicode_DATA(self); |
| 11476 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11477 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11478 | if (length == 1) |
| 11479 | return PyBool_FromLong( |
| 11480 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11482 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11483 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11484 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11486 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11487 | for (i = 0; i < length; i++) { |
| 11488 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11489 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11490 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11491 | return PyBool_FromLong(0); |
| 11492 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11493 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11494 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11495 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11496 | } |
| 11497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11498 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11499 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11500 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11501 | 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] | 11502 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11503 | |
| 11504 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11505 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11506 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11507 | Py_ssize_t i, length; |
| 11508 | int kind; |
| 11509 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11510 | int cased; |
| 11511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11512 | if (PyUnicode_READY(self) == -1) |
| 11513 | return NULL; |
| 11514 | length = PyUnicode_GET_LENGTH(self); |
| 11515 | kind = PyUnicode_KIND(self); |
| 11516 | data = PyUnicode_DATA(self); |
| 11517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11518 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11519 | if (length == 1) |
| 11520 | return PyBool_FromLong( |
| 11521 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11522 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11523 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11524 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11525 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11527 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11528 | for (i = 0; i < length; i++) { |
| 11529 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11530 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11531 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11532 | return PyBool_FromLong(0); |
| 11533 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11534 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11535 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11536 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11537 | } |
| 11538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11539 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11540 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11541 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11542 | Return True if S is a titlecased string and there is at least one\n\ |
| 11543 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11544 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11545 | Return 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_istitle(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, previous_is_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 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11564 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11565 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11566 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11567 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11568 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11569 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11570 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11572 | cased = 0; |
| 11573 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11574 | for (i = 0; i < length; i++) { |
| 11575 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11576 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11577 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11578 | if (previous_is_cased) |
| 11579 | return PyBool_FromLong(0); |
| 11580 | previous_is_cased = 1; |
| 11581 | cased = 1; |
| 11582 | } |
| 11583 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11584 | if (!previous_is_cased) |
| 11585 | return PyBool_FromLong(0); |
| 11586 | previous_is_cased = 1; |
| 11587 | cased = 1; |
| 11588 | } |
| 11589 | else |
| 11590 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11591 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11592 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11593 | } |
| 11594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11595 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11596 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11597 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11598 | Return True if all characters in S are whitespace\n\ |
| 11599 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11600 | |
| 11601 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11602 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11603 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11604 | Py_ssize_t i, length; |
| 11605 | int kind; |
| 11606 | void *data; |
| 11607 | |
| 11608 | if (PyUnicode_READY(self) == -1) |
| 11609 | return NULL; |
| 11610 | length = PyUnicode_GET_LENGTH(self); |
| 11611 | kind = PyUnicode_KIND(self); |
| 11612 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11613 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11614 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11615 | if (length == 1) |
| 11616 | return PyBool_FromLong( |
| 11617 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11618 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11619 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11620 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11621 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11623 | for (i = 0; i < length; i++) { |
| 11624 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11625 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11626 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11627 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11628 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11629 | } |
| 11630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11631 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11632 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11633 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11634 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11635 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11636 | |
| 11637 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11638 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11639 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11640 | Py_ssize_t i, length; |
| 11641 | int kind; |
| 11642 | void *data; |
| 11643 | |
| 11644 | if (PyUnicode_READY(self) == -1) |
| 11645 | return NULL; |
| 11646 | length = PyUnicode_GET_LENGTH(self); |
| 11647 | kind = PyUnicode_KIND(self); |
| 11648 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11649 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11650 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11651 | if (length == 1) |
| 11652 | return PyBool_FromLong( |
| 11653 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11654 | |
| 11655 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11656 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11657 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11658 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11659 | for (i = 0; i < length; i++) { |
| 11660 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11661 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11662 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11663 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11664 | } |
| 11665 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11666 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11667 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11668 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11669 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11670 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11671 | |
| 11672 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11673 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11674 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11675 | int kind; |
| 11676 | void *data; |
| 11677 | Py_ssize_t len, i; |
| 11678 | |
| 11679 | if (PyUnicode_READY(self) == -1) |
| 11680 | return NULL; |
| 11681 | |
| 11682 | kind = PyUnicode_KIND(self); |
| 11683 | data = PyUnicode_DATA(self); |
| 11684 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11685 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11686 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11687 | if (len == 1) { |
| 11688 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11689 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11690 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11691 | |
| 11692 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11693 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11694 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11695 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11696 | for (i = 0; i < len; i++) { |
| 11697 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11698 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11699 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11700 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11701 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11702 | } |
| 11703 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11704 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11705 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11706 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11707 | 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] | 11708 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11709 | |
| 11710 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11711 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11712 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11713 | Py_ssize_t i, length; |
| 11714 | int kind; |
| 11715 | void *data; |
| 11716 | |
| 11717 | if (PyUnicode_READY(self) == -1) |
| 11718 | return NULL; |
| 11719 | length = PyUnicode_GET_LENGTH(self); |
| 11720 | kind = PyUnicode_KIND(self); |
| 11721 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11723 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11724 | if (length == 1) |
| 11725 | return PyBool_FromLong( |
| 11726 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11727 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11728 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11729 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11730 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11732 | for (i = 0; i < length; i++) { |
| 11733 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11734 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11735 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11736 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11737 | } |
| 11738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11739 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11740 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11741 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11742 | Return True if all characters in S are digits\n\ |
| 11743 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11744 | |
| 11745 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11746 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11748 | Py_ssize_t i, length; |
| 11749 | int kind; |
| 11750 | void *data; |
| 11751 | |
| 11752 | if (PyUnicode_READY(self) == -1) |
| 11753 | return NULL; |
| 11754 | length = PyUnicode_GET_LENGTH(self); |
| 11755 | kind = PyUnicode_KIND(self); |
| 11756 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11757 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11758 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11759 | if (length == 1) { |
| 11760 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11761 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11762 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11763 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11764 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11765 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11766 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11768 | for (i = 0; i < length; i++) { |
| 11769 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11770 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11771 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11772 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11773 | } |
| 11774 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11775 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11776 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11777 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11778 | 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] | 11779 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11780 | |
| 11781 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11782 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11783 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11784 | Py_ssize_t i, length; |
| 11785 | int kind; |
| 11786 | void *data; |
| 11787 | |
| 11788 | if (PyUnicode_READY(self) == -1) |
| 11789 | return NULL; |
| 11790 | length = PyUnicode_GET_LENGTH(self); |
| 11791 | kind = PyUnicode_KIND(self); |
| 11792 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11794 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11795 | if (length == 1) |
| 11796 | return PyBool_FromLong( |
| 11797 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11798 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11799 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11800 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11801 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11802 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11803 | for (i = 0; i < length; i++) { |
| 11804 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11805 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11806 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11807 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11808 | } |
| 11809 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11810 | int |
| 11811 | PyUnicode_IsIdentifier(PyObject *self) |
| 11812 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11813 | int kind; |
| 11814 | void *data; |
| 11815 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11816 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11818 | if (PyUnicode_READY(self) == -1) { |
| 11819 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11820 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11821 | } |
| 11822 | |
| 11823 | /* Special case for empty strings */ |
| 11824 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11825 | return 0; |
| 11826 | kind = PyUnicode_KIND(self); |
| 11827 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11828 | |
| 11829 | /* PEP 3131 says that the first character must be in |
| 11830 | XID_Start and subsequent characters in XID_Continue, |
| 11831 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11832 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11833 | letters, digits, underscore). However, given the current |
| 11834 | definition of XID_Start and XID_Continue, it is sufficient |
| 11835 | to check just for these, except that _ must be allowed |
| 11836 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11837 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11838 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11839 | return 0; |
| 11840 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11841 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11842 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11843 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11844 | return 1; |
| 11845 | } |
| 11846 | |
| 11847 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11848 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11849 | \n\ |
| 11850 | Return True if S is a valid identifier according\n\ |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 11851 | to the language definition.\n\ |
| 11852 | \n\ |
| 11853 | Use keyword.iskeyword() to test for reserved identifiers\n\ |
| 11854 | such as \"def\" and \"class\".\n"); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11855 | |
| 11856 | static PyObject* |
| 11857 | unicode_isidentifier(PyObject *self) |
| 11858 | { |
| 11859 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11860 | } |
| 11861 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11862 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11863 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11864 | \n\ |
| 11865 | Return True if all characters in S are considered\n\ |
| 11866 | printable in repr() or S is empty, False otherwise."); |
| 11867 | |
| 11868 | static PyObject* |
| 11869 | unicode_isprintable(PyObject *self) |
| 11870 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11871 | Py_ssize_t i, length; |
| 11872 | int kind; |
| 11873 | void *data; |
| 11874 | |
| 11875 | if (PyUnicode_READY(self) == -1) |
| 11876 | return NULL; |
| 11877 | length = PyUnicode_GET_LENGTH(self); |
| 11878 | kind = PyUnicode_KIND(self); |
| 11879 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11880 | |
| 11881 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11882 | if (length == 1) |
| 11883 | return PyBool_FromLong( |
| 11884 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11885 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11886 | for (i = 0; i < length; i++) { |
| 11887 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11888 | Py_RETURN_FALSE; |
| 11889 | } |
| 11890 | } |
| 11891 | Py_RETURN_TRUE; |
| 11892 | } |
| 11893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11894 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11895 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11896 | \n\ |
| 11897 | 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] | 11898 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11899 | |
| 11900 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11901 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11902 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11903 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | } |
| 11905 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11906 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11907 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11908 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11909 | if (PyUnicode_READY(self) == -1) |
| 11910 | return -1; |
| 11911 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11912 | } |
| 11913 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11914 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11915 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11916 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11917 | 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] | 11918 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11919 | |
| 11920 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11921 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11922 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11923 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11924 | Py_UCS4 fillchar = ' '; |
| 11925 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11926 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11927 | return NULL; |
| 11928 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11929 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11930 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11931 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11932 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11933 | return unicode_result_unchanged(self); |
| 11934 | |
| 11935 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11936 | } |
| 11937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11938 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11939 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11940 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11941 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11942 | |
| 11943 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11944 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11945 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11946 | if (PyUnicode_READY(self) == -1) |
| 11947 | return NULL; |
| 11948 | if (PyUnicode_IS_ASCII(self)) |
| 11949 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11950 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | } |
| 11952 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11953 | #define LEFTSTRIP 0 |
| 11954 | #define RIGHTSTRIP 1 |
| 11955 | #define BOTHSTRIP 2 |
| 11956 | |
| 11957 | /* Arrays indexed by above */ |
| 11958 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11959 | |
| 11960 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11961 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11962 | /* externally visible for str.strip(unicode) */ |
| 11963 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11964 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11965 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11966 | void *data; |
| 11967 | int kind; |
| 11968 | Py_ssize_t i, j, len; |
| 11969 | BLOOM_MASK sepmask; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11970 | Py_ssize_t seplen; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11971 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11972 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11973 | return NULL; |
| 11974 | |
| 11975 | kind = PyUnicode_KIND(self); |
| 11976 | data = PyUnicode_DATA(self); |
| 11977 | len = PyUnicode_GET_LENGTH(self); |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11978 | seplen = PyUnicode_GET_LENGTH(sepobj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11979 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11980 | PyUnicode_DATA(sepobj), |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11981 | seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11982 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11983 | i = 0; |
| 11984 | if (striptype != RIGHTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11985 | while (i < len) { |
| 11986 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 11987 | if (!BLOOM(sepmask, ch)) |
| 11988 | break; |
| 11989 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 11990 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11991 | i++; |
| 11992 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11993 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11994 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11995 | j = len; |
| 11996 | if (striptype != LEFTSTRIP) { |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 11997 | j--; |
| 11998 | while (j >= i) { |
| 11999 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12000 | if (!BLOOM(sepmask, ch)) |
| 12001 | break; |
| 12002 | if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0) |
| 12003 | break; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12004 | j--; |
Victor Stinner | b3a6014 | 2013-04-09 22:19:21 +0200 | [diff] [blame] | 12005 | } |
| 12006 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12007 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12008 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12009 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12010 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12011 | } |
| 12012 | |
| 12013 | PyObject* |
| 12014 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 12015 | { |
| 12016 | unsigned char *data; |
| 12017 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12018 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12019 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12020 | if (PyUnicode_READY(self) == -1) |
| 12021 | return NULL; |
| 12022 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12023 | length = PyUnicode_GET_LENGTH(self); |
| 12024 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12025 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12026 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12027 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12028 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 12029 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12030 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 12031 | return NULL; |
| 12032 | } |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 12033 | if (start >= length || end < start) |
| 12034 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12035 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 12036 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12037 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12038 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12039 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12040 | } |
| 12041 | else { |
| 12042 | kind = PyUnicode_KIND(self); |
| 12043 | data = PyUnicode_1BYTE_DATA(self); |
| 12044 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12045 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 12046 | length); |
| 12047 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12048 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12049 | |
| 12050 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12051 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12052 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12053 | Py_ssize_t len, i, j; |
| 12054 | |
| 12055 | if (PyUnicode_READY(self) == -1) |
| 12056 | return NULL; |
| 12057 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12058 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12059 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12060 | if (PyUnicode_IS_ASCII(self)) { |
| 12061 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(self); |
| 12062 | |
| 12063 | i = 0; |
| 12064 | if (striptype != RIGHTSTRIP) { |
| 12065 | while (i < len) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12066 | Py_UCS1 ch = data[i]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12067 | if (!_Py_ascii_whitespace[ch]) |
| 12068 | break; |
| 12069 | i++; |
| 12070 | } |
| 12071 | } |
| 12072 | |
| 12073 | j = len; |
| 12074 | if (striptype != LEFTSTRIP) { |
| 12075 | j--; |
| 12076 | while (j >= i) { |
Victor Stinner | d92e078 | 2013-04-14 19:17:42 +0200 | [diff] [blame] | 12077 | Py_UCS1 ch = data[j]; |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12078 | if (!_Py_ascii_whitespace[ch]) |
| 12079 | break; |
| 12080 | j--; |
| 12081 | } |
| 12082 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12083 | } |
| 12084 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12085 | else { |
| 12086 | int kind = PyUnicode_KIND(self); |
| 12087 | void *data = PyUnicode_DATA(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12088 | |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12089 | i = 0; |
| 12090 | if (striptype != RIGHTSTRIP) { |
| 12091 | while (i < len) { |
| 12092 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 12093 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12094 | break; |
| 12095 | i++; |
| 12096 | } |
Victor Stinner | 9c79e41 | 2013-04-09 22:21:08 +0200 | [diff] [blame] | 12097 | } |
Victor Stinner | cc7af72 | 2013-04-09 22:39:24 +0200 | [diff] [blame] | 12098 | |
| 12099 | j = len; |
| 12100 | if (striptype != LEFTSTRIP) { |
| 12101 | j--; |
| 12102 | while (j >= i) { |
| 12103 | Py_UCS4 ch = PyUnicode_READ(kind, data, j); |
| 12104 | if (!Py_UNICODE_ISSPACE(ch)) |
| 12105 | break; |
| 12106 | j--; |
| 12107 | } |
| 12108 | j++; |
| 12109 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12110 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12111 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12112 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12113 | } |
| 12114 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12115 | |
| 12116 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12117 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12118 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12119 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12120 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 12121 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12122 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12123 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12124 | if (sep != NULL && sep != Py_None) { |
| 12125 | if (PyUnicode_Check(sep)) |
| 12126 | return _PyUnicode_XStrip(self, striptype, sep); |
| 12127 | else { |
| 12128 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12129 | "%s arg must be None or str", |
| 12130 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12131 | return NULL; |
| 12132 | } |
| 12133 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12134 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12135 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12136 | } |
| 12137 | |
| 12138 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12139 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12140 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12141 | \n\ |
| 12142 | Return a copy of the string S with leading and trailing\n\ |
| 12143 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12144 | 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] | 12145 | |
| 12146 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12147 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12148 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12149 | if (PyTuple_GET_SIZE(args) == 0) |
| 12150 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 12151 | else |
| 12152 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12153 | } |
| 12154 | |
| 12155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12156 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12157 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12158 | \n\ |
| 12159 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12160 | 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] | 12161 | |
| 12162 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12163 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12164 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12165 | if (PyTuple_GET_SIZE(args) == 0) |
| 12166 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 12167 | else |
| 12168 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12169 | } |
| 12170 | |
| 12171 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12172 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12173 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12174 | \n\ |
| 12175 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12176 | 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] | 12177 | |
| 12178 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12179 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12180 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12181 | if (PyTuple_GET_SIZE(args) == 0) |
| 12182 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 12183 | else |
| 12184 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12185 | } |
| 12186 | |
| 12187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12188 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12189 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12190 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12191 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12192 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12193 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 12194 | if (len < 1) |
| 12195 | _Py_RETURN_UNICODE_EMPTY(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12196 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12197 | /* no repeat, return original string */ |
| 12198 | if (len == 1) |
| 12199 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 12200 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12201 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12202 | return NULL; |
| 12203 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 12204 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12205 | PyErr_SetString(PyExc_OverflowError, |
| 12206 | "repeated string is too long"); |
| 12207 | return NULL; |
| 12208 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12209 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12210 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12211 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | if (!u) |
| 12213 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12214 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12215 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12216 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 12217 | const int kind = PyUnicode_KIND(str); |
| 12218 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12219 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12220 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12221 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12222 | } |
| 12223 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12224 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12225 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 12226 | ucs2[n] = fill_char; |
| 12227 | } else { |
| 12228 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 12229 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12230 | for (n = 0; n < len; ++n) |
| 12231 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 12232 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12233 | } |
| 12234 | else { |
| 12235 | /* number of characters copied this far */ |
| 12236 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12237 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12238 | char *to = (char *) PyUnicode_DATA(u); |
| 12239 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 12240 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12241 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12242 | n = (done <= nchars-done) ? done : nchars-done; |
| 12243 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12244 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12245 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12246 | } |
| 12247 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12248 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12249 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12250 | } |
| 12251 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12252 | PyObject * |
| 12253 | PyUnicode_Replace(PyObject *obj, |
| 12254 | PyObject *subobj, |
| 12255 | PyObject *replobj, |
| 12256 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12257 | { |
| 12258 | PyObject *self; |
| 12259 | PyObject *str1; |
| 12260 | PyObject *str2; |
| 12261 | PyObject *result; |
| 12262 | |
| 12263 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12264 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12265 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12266 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12267 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12268 | Py_DECREF(self); |
| 12269 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12270 | } |
| 12271 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12272 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12273 | Py_DECREF(self); |
| 12274 | Py_DECREF(str1); |
| 12275 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12276 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12277 | if (PyUnicode_READY(self) == -1 || |
| 12278 | PyUnicode_READY(str1) == -1 || |
| 12279 | PyUnicode_READY(str2) == -1) |
| 12280 | result = NULL; |
| 12281 | else |
| 12282 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | Py_DECREF(self); |
| 12284 | Py_DECREF(str1); |
| 12285 | Py_DECREF(str2); |
| 12286 | return result; |
| 12287 | } |
| 12288 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12289 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 12290 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12291 | \n\ |
| 12292 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 12293 | old replaced by new. If the optional argument count is\n\ |
| 12294 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12295 | |
| 12296 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12297 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12298 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12299 | PyObject *str1; |
| 12300 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12301 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12302 | PyObject *result; |
| 12303 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12304 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12305 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12306 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12307 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12308 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12309 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12310 | return NULL; |
| 12311 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12312 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12313 | Py_DECREF(str1); |
| 12314 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 12315 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12316 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 12317 | result = NULL; |
| 12318 | else |
| 12319 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12320 | |
| 12321 | Py_DECREF(str1); |
| 12322 | Py_DECREF(str2); |
| 12323 | return result; |
| 12324 | } |
| 12325 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12326 | static PyObject * |
| 12327 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12328 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12329 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12330 | Py_ssize_t isize; |
| 12331 | Py_ssize_t osize, squote, dquote, i, o; |
| 12332 | Py_UCS4 max, quote; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12333 | int ikind, okind, unchanged; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12336 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12337 | return NULL; |
| 12338 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12339 | isize = PyUnicode_GET_LENGTH(unicode); |
| 12340 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12341 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12342 | /* Compute length of output, quote characters, and |
| 12343 | maximum character */ |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12344 | osize = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12345 | max = 127; |
| 12346 | squote = dquote = 0; |
| 12347 | ikind = PyUnicode_KIND(unicode); |
| 12348 | for (i = 0; i < isize; i++) { |
| 12349 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12350 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12351 | switch (ch) { |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12352 | case '\'': squote++; break; |
| 12353 | case '"': dquote++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12354 | case '\\': case '\t': case '\r': case '\n': |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12355 | incr = 2; |
| 12356 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12357 | default: |
| 12358 | /* Fast-path ASCII */ |
| 12359 | if (ch < ' ' || ch == 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12360 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12361 | else if (ch < 0x7f) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12362 | ; |
| 12363 | else if (Py_UNICODE_ISPRINTABLE(ch)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12364 | max = ch > max ? ch : max; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12365 | else if (ch < 0x100) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12366 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12367 | else if (ch < 0x10000) |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12368 | incr = 6; /* \uHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12369 | else |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12370 | incr = 10; /* \uHHHHHHHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12371 | } |
Benjamin Peterson | 736b801 | 2014-09-29 23:02:15 -0400 | [diff] [blame] | 12372 | if (osize > PY_SSIZE_T_MAX - incr) { |
| 12373 | PyErr_SetString(PyExc_OverflowError, |
| 12374 | "string is too long to generate repr"); |
| 12375 | return NULL; |
| 12376 | } |
| 12377 | osize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12378 | } |
| 12379 | |
| 12380 | quote = '\''; |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12381 | unchanged = (osize == isize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12382 | if (squote) { |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12383 | unchanged = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12384 | if (dquote) |
| 12385 | /* Both squote and dquote present. Use squote, |
| 12386 | and escape them */ |
| 12387 | osize += squote; |
| 12388 | else |
| 12389 | quote = '"'; |
| 12390 | } |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12391 | osize += 2; /* quotes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12392 | |
| 12393 | repr = PyUnicode_New(osize, max); |
| 12394 | if (repr == NULL) |
| 12395 | return NULL; |
| 12396 | okind = PyUnicode_KIND(repr); |
| 12397 | odata = PyUnicode_DATA(repr); |
| 12398 | |
| 12399 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 12400 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12401 | if (unchanged) { |
| 12402 | _PyUnicode_FastCopyCharacters(repr, 1, |
| 12403 | unicode, 0, |
| 12404 | isize); |
| 12405 | } |
| 12406 | else { |
| 12407 | for (i = 0, o = 1; i < isize; i++) { |
| 12408 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12409 | |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12410 | /* Escape quotes and backslashes */ |
| 12411 | if ((ch == quote) || (ch == '\\')) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 12412 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12413 | PyUnicode_WRITE(okind, odata, o++, ch); |
Victor Stinner | 55c0878 | 2013-04-14 18:45:39 +0200 | [diff] [blame] | 12414 | continue; |
| 12415 | } |
| 12416 | |
| 12417 | /* Map special whitespace to '\t', \n', '\r' */ |
| 12418 | if (ch == '\t') { |
| 12419 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12420 | PyUnicode_WRITE(okind, odata, o++, 't'); |
| 12421 | } |
| 12422 | else if (ch == '\n') { |
| 12423 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12424 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
| 12425 | } |
| 12426 | else if (ch == '\r') { |
| 12427 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12428 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
| 12429 | } |
| 12430 | |
| 12431 | /* Map non-printable US ASCII to '\xhh' */ |
| 12432 | else if (ch < ' ' || ch == 0x7F) { |
| 12433 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12434 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12435 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12436 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12437 | } |
| 12438 | |
| 12439 | /* Copy ASCII characters as-is */ |
| 12440 | else if (ch < 0x7F) { |
| 12441 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12442 | } |
| 12443 | |
| 12444 | /* Non-ASCII characters */ |
| 12445 | else { |
| 12446 | /* Map Unicode whitespace and control characters |
| 12447 | (categories Z* and C* except ASCII space) |
| 12448 | */ |
| 12449 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
| 12450 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 12451 | /* Map 8-bit characters to '\xhh' */ |
| 12452 | if (ch <= 0xff) { |
| 12453 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 12454 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 12455 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
| 12456 | } |
| 12457 | /* Map 16-bit characters to '\uxxxx' */ |
| 12458 | else if (ch <= 0xffff) { |
| 12459 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 12460 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12461 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12462 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12463 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12464 | } |
| 12465 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 12466 | else { |
| 12467 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 12468 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 12469 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 12470 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 12471 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
| 12472 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 12473 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 12474 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 12475 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
| 12476 | } |
| 12477 | } |
| 12478 | /* Copy characters as-is */ |
| 12479 | else { |
| 12480 | PyUnicode_WRITE(okind, odata, o++, ch); |
| 12481 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12482 | } |
| 12483 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12484 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12485 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 12486 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 12487 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12488 | } |
| 12489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12490 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12491 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12492 | \n\ |
| 12493 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 12494 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12495 | arguments start and end are interpreted as in slice notation.\n\ |
| 12496 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12497 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12498 | |
| 12499 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12500 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12501 | { |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 12502 | PyObject *substring = NULL; |
| 12503 | Py_ssize_t start = 0; |
| 12504 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12505 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12506 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12507 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 12508 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12509 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12510 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12511 | if (PyUnicode_READY(self) == -1) { |
| 12512 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12513 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12514 | } |
| 12515 | if (PyUnicode_READY(substring) == -1) { |
| 12516 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12517 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12518 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12519 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12520 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12521 | |
| 12522 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12523 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12524 | if (result == -2) |
| 12525 | return NULL; |
| 12526 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12527 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12528 | } |
| 12529 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12530 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12531 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12532 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12533 | 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] | 12534 | |
| 12535 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12536 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12537 | { |
Antoine Pitrou | f6d1f1f | 2015-05-19 21:04:33 +0200 | [diff] [blame] | 12538 | PyObject *substring = NULL; |
| 12539 | Py_ssize_t start = 0; |
| 12540 | Py_ssize_t end = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12541 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12542 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12543 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12544 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12545 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12546 | |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12547 | if (PyUnicode_READY(self) == -1) { |
| 12548 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12549 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12550 | } |
| 12551 | if (PyUnicode_READY(substring) == -1) { |
| 12552 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12553 | return NULL; |
Christian Heimes | ea71a52 | 2013-06-29 21:17:34 +0200 | [diff] [blame] | 12554 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12555 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12556 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12557 | |
| 12558 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12559 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12560 | if (result == -2) |
| 12561 | return NULL; |
| 12562 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12563 | if (result < 0) { |
| 12564 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12565 | return NULL; |
| 12566 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12567 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12568 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12569 | } |
| 12570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12571 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12572 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12574 | Return S right-justified in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12575 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12576 | |
| 12577 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12578 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12579 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12580 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12581 | Py_UCS4 fillchar = ' '; |
| 12582 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12583 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12584 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12585 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12586 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12587 | return NULL; |
| 12588 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12589 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12590 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12591 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12592 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12593 | } |
| 12594 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12595 | PyObject * |
| 12596 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12597 | { |
| 12598 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12600 | s = PyUnicode_FromObject(s); |
| 12601 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12602 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12603 | if (sep != NULL) { |
| 12604 | sep = PyUnicode_FromObject(sep); |
| 12605 | if (sep == NULL) { |
| 12606 | Py_DECREF(s); |
| 12607 | return NULL; |
| 12608 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12609 | } |
| 12610 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12611 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12612 | |
| 12613 | Py_DECREF(s); |
| 12614 | Py_XDECREF(sep); |
| 12615 | return result; |
| 12616 | } |
| 12617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12618 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12619 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12620 | \n\ |
| 12621 | Return a list of the words in S, using sep as the\n\ |
| 12622 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12623 | splits are done. If sep is not specified or is None, any\n\ |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 12624 | whitespace string is a separator and empty strings are\n\ |
| 12625 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12626 | |
| 12627 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12628 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12629 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12630 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12631 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12632 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12633 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12634 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12635 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12636 | return NULL; |
| 12637 | |
| 12638 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12639 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12640 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12641 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12642 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12643 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12644 | } |
| 12645 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12646 | PyObject * |
| 12647 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12648 | { |
| 12649 | PyObject* str_obj; |
| 12650 | PyObject* sep_obj; |
| 12651 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12652 | int kind1, kind2, kind; |
| 12653 | void *buf1 = NULL, *buf2 = NULL; |
| 12654 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12655 | |
| 12656 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12657 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12658 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12659 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12660 | if (!sep_obj) { |
| 12661 | Py_DECREF(str_obj); |
| 12662 | return NULL; |
| 12663 | } |
| 12664 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12665 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12666 | Py_DECREF(str_obj); |
| 12667 | return NULL; |
| 12668 | } |
| 12669 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12670 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12671 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12672 | kind = Py_MAX(kind1, kind2); |
| 12673 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12674 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12675 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12676 | if (!buf1) |
| 12677 | goto onError; |
| 12678 | buf2 = PyUnicode_DATA(sep_obj); |
| 12679 | if (kind2 != kind) |
| 12680 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12681 | if (!buf2) |
| 12682 | goto onError; |
| 12683 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12684 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12685 | |
Serhiy Storchaka | 48070c1 | 2015-03-29 19:21:02 +0300 | [diff] [blame] | 12686 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12687 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12688 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12689 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12690 | else |
| 12691 | 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] | 12692 | break; |
| 12693 | case PyUnicode_2BYTE_KIND: |
| 12694 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12695 | break; |
| 12696 | case PyUnicode_4BYTE_KIND: |
| 12697 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12698 | break; |
| 12699 | default: |
| 12700 | assert(0); |
| 12701 | out = 0; |
| 12702 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12703 | |
| 12704 | Py_DECREF(sep_obj); |
| 12705 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12706 | if (kind1 != kind) |
| 12707 | PyMem_Free(buf1); |
| 12708 | if (kind2 != kind) |
| 12709 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12710 | |
| 12711 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12712 | onError: |
| 12713 | Py_DECREF(sep_obj); |
| 12714 | Py_DECREF(str_obj); |
| 12715 | if (kind1 != kind && buf1) |
| 12716 | PyMem_Free(buf1); |
| 12717 | if (kind2 != kind && buf2) |
| 12718 | PyMem_Free(buf2); |
| 12719 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12720 | } |
| 12721 | |
| 12722 | |
| 12723 | PyObject * |
| 12724 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12725 | { |
| 12726 | PyObject* str_obj; |
| 12727 | PyObject* sep_obj; |
| 12728 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12729 | int kind1, kind2, kind; |
| 12730 | void *buf1 = NULL, *buf2 = NULL; |
| 12731 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12732 | |
| 12733 | str_obj = PyUnicode_FromObject(str_in); |
| 12734 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12735 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12736 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12737 | if (!sep_obj) { |
| 12738 | Py_DECREF(str_obj); |
| 12739 | return NULL; |
| 12740 | } |
| 12741 | |
Serhiy Storchaka | 48070c1 | 2015-03-29 19:21:02 +0300 | [diff] [blame] | 12742 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12743 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12744 | kind = Py_MAX(kind1, kind2); |
Serhiy Storchaka | 48070c1 | 2015-03-29 19:21:02 +0300 | [diff] [blame] | 12745 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12746 | if (kind1 != kind) |
Serhiy Storchaka | 48070c1 | 2015-03-29 19:21:02 +0300 | [diff] [blame] | 12747 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12748 | if (!buf1) |
| 12749 | goto onError; |
| 12750 | buf2 = PyUnicode_DATA(sep_obj); |
| 12751 | if (kind2 != kind) |
| 12752 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12753 | if (!buf2) |
| 12754 | goto onError; |
| 12755 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12756 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12757 | |
Serhiy Storchaka | 48070c1 | 2015-03-29 19:21:02 +0300 | [diff] [blame] | 12758 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12759 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12760 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12761 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12762 | else |
| 12763 | 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] | 12764 | break; |
| 12765 | case PyUnicode_2BYTE_KIND: |
| 12766 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12767 | break; |
| 12768 | case PyUnicode_4BYTE_KIND: |
| 12769 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12770 | break; |
| 12771 | default: |
| 12772 | assert(0); |
| 12773 | out = 0; |
| 12774 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12775 | |
| 12776 | Py_DECREF(sep_obj); |
| 12777 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12778 | if (kind1 != kind) |
| 12779 | PyMem_Free(buf1); |
| 12780 | if (kind2 != kind) |
| 12781 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12782 | |
| 12783 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12784 | onError: |
| 12785 | Py_DECREF(sep_obj); |
| 12786 | Py_DECREF(str_obj); |
| 12787 | if (kind1 != kind && buf1) |
| 12788 | PyMem_Free(buf1); |
| 12789 | if (kind2 != kind && buf2) |
| 12790 | PyMem_Free(buf2); |
| 12791 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12792 | } |
| 12793 | |
| 12794 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12795 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12796 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12797 | 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] | 12798 | 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] | 12799 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12800 | |
| 12801 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12802 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12803 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12804 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12805 | } |
| 12806 | |
| 12807 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12808 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12809 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12810 | 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] | 12811 | 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] | 12812 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12813 | |
| 12814 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12815 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12816 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12817 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12818 | } |
| 12819 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12820 | PyObject * |
| 12821 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12822 | { |
| 12823 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12824 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12825 | s = PyUnicode_FromObject(s); |
| 12826 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12827 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12828 | if (sep != NULL) { |
| 12829 | sep = PyUnicode_FromObject(sep); |
| 12830 | if (sep == NULL) { |
| 12831 | Py_DECREF(s); |
| 12832 | return NULL; |
| 12833 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12834 | } |
| 12835 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12836 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12837 | |
| 12838 | Py_DECREF(s); |
| 12839 | Py_XDECREF(sep); |
| 12840 | return result; |
| 12841 | } |
| 12842 | |
| 12843 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12844 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12845 | \n\ |
| 12846 | Return a list of the words in S, using sep as the\n\ |
| 12847 | delimiter string, starting at the end of the string and\n\ |
| 12848 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12849 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12850 | is a separator."); |
| 12851 | |
| 12852 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12853 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12854 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12855 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12856 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12857 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12858 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12859 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12860 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12861 | return NULL; |
| 12862 | |
| 12863 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12864 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12865 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12866 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12867 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12868 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12869 | } |
| 12870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12871 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12872 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12873 | \n\ |
| 12874 | 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] | 12875 | 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] | 12876 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12877 | |
| 12878 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12879 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12880 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12881 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12882 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12883 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12884 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12885 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12886 | return NULL; |
| 12887 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12888 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12889 | } |
| 12890 | |
| 12891 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12892 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12893 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12894 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12895 | } |
| 12896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12897 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12898 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12899 | \n\ |
| 12900 | 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] | 12901 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12902 | |
| 12903 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12904 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12905 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12906 | if (PyUnicode_READY(self) == -1) |
| 12907 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12908 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12909 | } |
| 12910 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 12911 | /*[clinic input] |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12912 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12913 | @staticmethod |
| 12914 | str.maketrans as unicode_maketrans |
| 12915 | |
| 12916 | x: object |
| 12917 | |
| 12918 | y: unicode=NULL |
| 12919 | |
| 12920 | z: unicode=NULL |
| 12921 | |
| 12922 | / |
| 12923 | |
| 12924 | Return a translation table usable for str.translate(). |
| 12925 | |
| 12926 | If there is only one argument, it must be a dictionary mapping Unicode |
| 12927 | ordinals (integers) or characters to Unicode ordinals, strings or None. |
| 12928 | Character keys will be then converted to ordinals. |
| 12929 | If there are two arguments, they must be strings of equal length, and |
| 12930 | in the resulting dictionary, each character in x will be mapped to the |
| 12931 | character at the same position in y. If there is a third argument, it |
| 12932 | 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] | 12933 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12934 | |
| 12935 | PyDoc_STRVAR(unicode_maketrans__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 12936 | "maketrans(x, y=None, z=None, /)\n" |
| 12937 | "--\n" |
| 12938 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12939 | "Return a translation table usable for str.translate().\n" |
| 12940 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12941 | "If there is only one argument, it must be a dictionary mapping Unicode\n" |
| 12942 | "ordinals (integers) or characters to Unicode ordinals, strings or None.\n" |
| 12943 | "Character keys will be then converted to ordinals.\n" |
| 12944 | "If there are two arguments, they must be strings of equal length, and\n" |
| 12945 | "in the resulting dictionary, each character in x will be mapped to the\n" |
| 12946 | "character at the same position in y. If there is a third argument, it\n" |
| 12947 | "must be a string, whose characters will be mapped to None in the result."); |
| 12948 | |
| 12949 | #define UNICODE_MAKETRANS_METHODDEF \ |
| 12950 | {"maketrans", (PyCFunction)unicode_maketrans, METH_VARARGS|METH_STATIC, unicode_maketrans__doc__}, |
| 12951 | |
| 12952 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12953 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12954 | |
| 12955 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 12956 | unicode_maketrans(void *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12957 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12958 | PyObject *return_value = NULL; |
| 12959 | PyObject *x; |
| 12960 | PyObject *y = NULL; |
| 12961 | PyObject *z = NULL; |
| 12962 | |
| 12963 | if (!PyArg_ParseTuple(args, |
| 12964 | "O|UU:maketrans", |
| 12965 | &x, &y, &z)) |
| 12966 | goto exit; |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12967 | return_value = unicode_maketrans_impl(x, y, z); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12968 | |
| 12969 | exit: |
| 12970 | return return_value; |
| 12971 | } |
| 12972 | |
| 12973 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 12974 | unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 12975 | /*[clinic end generated code: output=566edf630f77436a input=7bfbf529a293c6c5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12976 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12977 | PyObject *new = NULL, *key, *value; |
| 12978 | Py_ssize_t i = 0; |
| 12979 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12980 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12981 | new = PyDict_New(); |
| 12982 | if (!new) |
| 12983 | return NULL; |
| 12984 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12985 | int x_kind, y_kind, z_kind; |
| 12986 | void *x_data, *y_data, *z_data; |
| 12987 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12988 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12989 | if (!PyUnicode_Check(x)) { |
| 12990 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12991 | "be a string if there is a second argument"); |
| 12992 | goto err; |
| 12993 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12994 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12995 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12996 | "arguments must have equal length"); |
| 12997 | goto err; |
| 12998 | } |
| 12999 | /* 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] | 13000 | x_kind = PyUnicode_KIND(x); |
| 13001 | y_kind = PyUnicode_KIND(y); |
| 13002 | x_data = PyUnicode_DATA(x); |
| 13003 | y_data = PyUnicode_DATA(y); |
| 13004 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 13005 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 13006 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13007 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 13008 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 13009 | if (!value) { |
| 13010 | Py_DECREF(key); |
| 13011 | goto err; |
| 13012 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13013 | res = PyDict_SetItem(new, key, value); |
| 13014 | Py_DECREF(key); |
| 13015 | Py_DECREF(value); |
| 13016 | if (res < 0) |
| 13017 | goto err; |
| 13018 | } |
| 13019 | /* create entries for deleting chars in z */ |
| 13020 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13021 | z_kind = PyUnicode_KIND(z); |
| 13022 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13023 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13024 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13025 | if (!key) |
| 13026 | goto err; |
| 13027 | res = PyDict_SetItem(new, key, Py_None); |
| 13028 | Py_DECREF(key); |
| 13029 | if (res < 0) |
| 13030 | goto err; |
| 13031 | } |
| 13032 | } |
| 13033 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13034 | int kind; |
| 13035 | void *data; |
| 13036 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13037 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 13038 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13039 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 13040 | "to maketrans it must be a dict"); |
| 13041 | goto err; |
| 13042 | } |
| 13043 | /* copy entries into the new dict, converting string keys to int keys */ |
| 13044 | while (PyDict_Next(x, &i, &key, &value)) { |
| 13045 | if (PyUnicode_Check(key)) { |
| 13046 | /* convert string keys to integer keys */ |
| 13047 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 13048 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13049 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 13050 | "table must be of length 1"); |
| 13051 | goto err; |
| 13052 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13053 | kind = PyUnicode_KIND(key); |
| 13054 | data = PyUnicode_DATA(key); |
| 13055 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13056 | if (!newkey) |
| 13057 | goto err; |
| 13058 | res = PyDict_SetItem(new, newkey, value); |
| 13059 | Py_DECREF(newkey); |
| 13060 | if (res < 0) |
| 13061 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13062 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 13063 | /* just keep integer keys */ |
| 13064 | if (PyDict_SetItem(new, key, value) < 0) |
| 13065 | goto err; |
| 13066 | } else { |
| 13067 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 13068 | "be strings or integers"); |
| 13069 | goto err; |
| 13070 | } |
| 13071 | } |
| 13072 | } |
| 13073 | return new; |
| 13074 | err: |
| 13075 | Py_DECREF(new); |
| 13076 | return NULL; |
| 13077 | } |
| 13078 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13079 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13080 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13081 | \n\ |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 13082 | Return a copy of the string S in which each character has been mapped\n\ |
| 13083 | through the given translation table. The table must implement\n\ |
| 13084 | lookup/indexing via __getitem__, for instance a dictionary or list,\n\ |
| 13085 | mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n\ |
| 13086 | this operation raises LookupError, the character is left untouched.\n\ |
| 13087 | Characters mapped to None are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13088 | |
| 13089 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13090 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13091 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13092 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13093 | } |
| 13094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13095 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13096 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13097 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13098 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13099 | |
| 13100 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13101 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13102 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 13103 | if (PyUnicode_READY(self) == -1) |
| 13104 | return NULL; |
| 13105 | if (PyUnicode_IS_ASCII(self)) |
| 13106 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 13107 | return case_operation(self, do_upper); |
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(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13111 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13112 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 13113 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 13114 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13115 | |
| 13116 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13117 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13118 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13119 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 13120 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13121 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13122 | int kind; |
| 13123 | void *data; |
| 13124 | Py_UCS4 chr; |
| 13125 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13126 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13127 | return NULL; |
| 13128 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 13129 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13130 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13131 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13132 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 13133 | return unicode_result_unchanged(self); |
| 13134 | |
| 13135 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13136 | |
| 13137 | u = pad(self, fill, 0, '0'); |
| 13138 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13139 | if (u == NULL) |
| 13140 | return NULL; |
| 13141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13142 | kind = PyUnicode_KIND(u); |
| 13143 | data = PyUnicode_DATA(u); |
| 13144 | chr = PyUnicode_READ(kind, data, fill); |
| 13145 | |
| 13146 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13147 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13148 | PyUnicode_WRITE(kind, data, 0, chr); |
| 13149 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13150 | } |
| 13151 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13152 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 13153 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13154 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13155 | |
| 13156 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13157 | static PyObject * |
| 13158 | unicode__decimal2ascii(PyObject *self) |
| 13159 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13160 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13161 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13162 | #endif |
| 13163 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13164 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13165 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13166 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13167 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 13168 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13169 | With optional end, stop comparing S at that position.\n\ |
| 13170 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13171 | |
| 13172 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13173 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13174 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13175 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13176 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13177 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13178 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13179 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13180 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13181 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13182 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13183 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13184 | if (PyTuple_Check(subobj)) { |
| 13185 | Py_ssize_t i; |
| 13186 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13187 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13188 | if (substring == NULL) |
| 13189 | return NULL; |
| 13190 | result = tailmatch(self, substring, start, end, -1); |
| 13191 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13192 | if (result == -1) |
| 13193 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13194 | if (result) { |
| 13195 | Py_RETURN_TRUE; |
| 13196 | } |
| 13197 | } |
| 13198 | /* nothing matched */ |
| 13199 | Py_RETURN_FALSE; |
| 13200 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13201 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13202 | if (substring == NULL) { |
| 13203 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13204 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 13205 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13206 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13207 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13208 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13209 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13210 | if (result == -1) |
| 13211 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13212 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13213 | } |
| 13214 | |
| 13215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 13216 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13217 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13218 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 13219 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 13220 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13221 | With optional end, stop comparing S at that position.\n\ |
| 13222 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13223 | |
| 13224 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13225 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13226 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13227 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13228 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13229 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13230 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13231 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13232 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13233 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 13234 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13235 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13236 | if (PyTuple_Check(subobj)) { |
| 13237 | Py_ssize_t i; |
| 13238 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13239 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13240 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13241 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13242 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13243 | result = tailmatch(self, substring, start, end, +1); |
| 13244 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13245 | if (result == -1) |
| 13246 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13247 | if (result) { |
| 13248 | Py_RETURN_TRUE; |
| 13249 | } |
| 13250 | } |
| 13251 | Py_RETURN_FALSE; |
| 13252 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13253 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13254 | if (substring == NULL) { |
| 13255 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13256 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 13257 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13258 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 13259 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13260 | result = tailmatch(self, substring, start, end, +1); |
Christian Heimes | 305e49e | 2013-06-29 20:41:06 +0200 | [diff] [blame] | 13261 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 13262 | if (result == -1) |
| 13263 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13264 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13265 | } |
| 13266 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13267 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13268 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13269 | { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13270 | if (!writer->readonly) |
| 13271 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
| 13272 | else { |
| 13273 | /* Copy-on-write mode: set buffer size to 0 so |
| 13274 | * _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on |
| 13275 | * next write. */ |
| 13276 | writer->size = 0; |
| 13277 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13278 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 13279 | writer->data = PyUnicode_DATA(writer->buffer); |
| 13280 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 13281 | } |
| 13282 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13283 | void |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13284 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13285 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13286 | memset(writer, 0, sizeof(*writer)); |
| 13287 | #ifdef Py_DEBUG |
| 13288 | writer->kind = 5; /* invalid kind */ |
| 13289 | #endif |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13290 | writer->min_char = 127; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13291 | } |
| 13292 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13293 | int |
| 13294 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 13295 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13296 | { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13297 | #ifdef MS_WINDOWS |
| 13298 | /* On Windows, overallocate by 50% is the best factor */ |
| 13299 | # define OVERALLOCATE_FACTOR 2 |
| 13300 | #else |
| 13301 | /* On Linux, overallocate by 25% is the best factor */ |
| 13302 | # define OVERALLOCATE_FACTOR 4 |
| 13303 | #endif |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13304 | Py_ssize_t newlen; |
| 13305 | PyObject *newbuffer; |
| 13306 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13307 | assert(length > 0); |
| 13308 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13309 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 13310 | PyErr_NoMemory(); |
| 13311 | return -1; |
| 13312 | } |
| 13313 | newlen = writer->pos + length; |
| 13314 | |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 13315 | maxchar = Py_MAX(maxchar, writer->min_char); |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13316 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13317 | if (writer->buffer == NULL) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13318 | assert(!writer->readonly); |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13319 | if (writer->overallocate |
| 13320 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13321 | /* overallocate to limit the number of realloc() */ |
| 13322 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13323 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13324 | if (newlen < writer->min_length) |
| 13325 | newlen = writer->min_length; |
| 13326 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13327 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 13328 | if (writer->buffer == NULL) |
| 13329 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13330 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13331 | else if (newlen > writer->size) { |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13332 | if (writer->overallocate |
| 13333 | && newlen <= (PY_SSIZE_T_MAX - newlen / OVERALLOCATE_FACTOR)) { |
| 13334 | /* overallocate to limit the number of realloc() */ |
| 13335 | newlen += newlen / OVERALLOCATE_FACTOR; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13336 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13337 | if (newlen < writer->min_length) |
| 13338 | newlen = writer->min_length; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13339 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13340 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13341 | /* resize + widen */ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 13342 | maxchar = Py_MAX(maxchar, writer->maxchar); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13343 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 13344 | if (newbuffer == NULL) |
| 13345 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13346 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13347 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13348 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13349 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13350 | } |
| 13351 | else { |
| 13352 | newbuffer = resize_compact(writer->buffer, newlen); |
| 13353 | if (newbuffer == NULL) |
| 13354 | return -1; |
| 13355 | } |
| 13356 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13357 | } |
| 13358 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13359 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13360 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 13361 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13362 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13363 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 13364 | writer->buffer, 0, writer->pos); |
| 13365 | Py_DECREF(writer->buffer); |
| 13366 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13367 | } |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13368 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13369 | return 0; |
Victor Stinner | 6989ba0 | 2013-11-18 21:08:39 +0100 | [diff] [blame] | 13370 | |
| 13371 | #undef OVERALLOCATE_FACTOR |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13372 | } |
| 13373 | |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13374 | Py_LOCAL_INLINE(int) |
| 13375 | _PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 13376 | { |
| 13377 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0) |
| 13378 | return -1; |
| 13379 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13380 | writer->pos++; |
| 13381 | return 0; |
| 13382 | } |
| 13383 | |
| 13384 | int |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 13385 | _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch) |
| 13386 | { |
| 13387 | return _PyUnicodeWriter_WriteCharInline(writer, ch); |
| 13388 | } |
| 13389 | |
| 13390 | int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13391 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 13392 | { |
| 13393 | Py_UCS4 maxchar; |
| 13394 | Py_ssize_t len; |
| 13395 | |
| 13396 | if (PyUnicode_READY(str) == -1) |
| 13397 | return -1; |
| 13398 | len = PyUnicode_GET_LENGTH(str); |
| 13399 | if (len == 0) |
| 13400 | return 0; |
| 13401 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 13402 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13403 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13404 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13405 | Py_INCREF(str); |
| 13406 | writer->buffer = str; |
| 13407 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13408 | writer->pos += len; |
| 13409 | return 0; |
| 13410 | } |
| 13411 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 13412 | return -1; |
| 13413 | } |
| 13414 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13415 | str, 0, len); |
| 13416 | writer->pos += len; |
| 13417 | return 0; |
| 13418 | } |
| 13419 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13420 | int |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 13421 | _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer, PyObject *str, |
| 13422 | Py_ssize_t start, Py_ssize_t end) |
| 13423 | { |
| 13424 | Py_UCS4 maxchar; |
| 13425 | Py_ssize_t len; |
| 13426 | |
| 13427 | if (PyUnicode_READY(str) == -1) |
| 13428 | return -1; |
| 13429 | |
| 13430 | assert(0 <= start); |
| 13431 | assert(end <= PyUnicode_GET_LENGTH(str)); |
| 13432 | assert(start <= end); |
| 13433 | |
| 13434 | if (end == 0) |
| 13435 | return 0; |
| 13436 | |
| 13437 | if (start == 0 && end == PyUnicode_GET_LENGTH(str)) |
| 13438 | return _PyUnicodeWriter_WriteStr(writer, str); |
| 13439 | |
| 13440 | if (PyUnicode_MAX_CHAR_VALUE(str) > writer->maxchar) |
| 13441 | maxchar = _PyUnicode_FindMaxChar(str, start, end); |
| 13442 | else |
| 13443 | maxchar = writer->maxchar; |
| 13444 | len = end - start; |
| 13445 | |
| 13446 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) < 0) |
| 13447 | return -1; |
| 13448 | |
| 13449 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13450 | str, start, len); |
| 13451 | writer->pos += len; |
| 13452 | return 0; |
| 13453 | } |
| 13454 | |
| 13455 | int |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13456 | _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, |
| 13457 | const char *ascii, Py_ssize_t len) |
| 13458 | { |
| 13459 | if (len == -1) |
| 13460 | len = strlen(ascii); |
| 13461 | |
| 13462 | assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128); |
| 13463 | |
| 13464 | if (writer->buffer == NULL && !writer->overallocate) { |
| 13465 | PyObject *str; |
| 13466 | |
| 13467 | str = _PyUnicode_FromASCII(ascii, len); |
| 13468 | if (str == NULL) |
| 13469 | return -1; |
| 13470 | |
| 13471 | writer->readonly = 1; |
| 13472 | writer->buffer = str; |
| 13473 | _PyUnicodeWriter_Update(writer); |
| 13474 | writer->pos += len; |
| 13475 | return 0; |
| 13476 | } |
| 13477 | |
| 13478 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) |
| 13479 | return -1; |
| 13480 | |
| 13481 | switch (writer->kind) |
| 13482 | { |
| 13483 | case PyUnicode_1BYTE_KIND: |
| 13484 | { |
| 13485 | const Py_UCS1 *str = (const Py_UCS1 *)ascii; |
| 13486 | Py_UCS1 *data = writer->data; |
| 13487 | |
| 13488 | Py_MEMCPY(data + writer->pos, str, len); |
| 13489 | break; |
| 13490 | } |
| 13491 | case PyUnicode_2BYTE_KIND: |
| 13492 | { |
| 13493 | _PyUnicode_CONVERT_BYTES( |
| 13494 | Py_UCS1, Py_UCS2, |
| 13495 | ascii, ascii + len, |
| 13496 | (Py_UCS2 *)writer->data + writer->pos); |
| 13497 | break; |
| 13498 | } |
| 13499 | case PyUnicode_4BYTE_KIND: |
| 13500 | { |
| 13501 | _PyUnicode_CONVERT_BYTES( |
| 13502 | Py_UCS1, Py_UCS4, |
| 13503 | ascii, ascii + len, |
| 13504 | (Py_UCS4 *)writer->data + writer->pos); |
| 13505 | break; |
| 13506 | } |
| 13507 | default: |
| 13508 | assert(0); |
| 13509 | } |
| 13510 | |
| 13511 | writer->pos += len; |
| 13512 | return 0; |
| 13513 | } |
| 13514 | |
| 13515 | int |
| 13516 | _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer, |
| 13517 | const char *str, Py_ssize_t len) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 13518 | { |
| 13519 | Py_UCS4 maxchar; |
| 13520 | |
| 13521 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 13522 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 13523 | return -1; |
| 13524 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 13525 | writer->pos += len; |
| 13526 | return 0; |
| 13527 | } |
| 13528 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13529 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13530 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13531 | { |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13532 | PyObject *str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13533 | if (writer->pos == 0) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13534 | Py_CLEAR(writer->buffer); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13535 | _Py_RETURN_UNICODE_EMPTY(); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13536 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 13537 | if (writer->readonly) { |
Victor Stinner | 9e6b4d7 | 2013-07-09 00:37:24 +0200 | [diff] [blame] | 13538 | str = writer->buffer; |
| 13539 | writer->buffer = NULL; |
| 13540 | assert(PyUnicode_GET_LENGTH(str) == writer->pos); |
| 13541 | return str; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13542 | } |
| 13543 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 13544 | PyObject *newbuffer; |
| 13545 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 13546 | if (newbuffer == NULL) { |
Serhiy Storchaka | dfe98a1 | 2014-02-09 13:46:20 +0200 | [diff] [blame] | 13547 | Py_CLEAR(writer->buffer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13548 | return NULL; |
| 13549 | } |
| 13550 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13551 | } |
Victor Stinner | 15a0bd3 | 2013-07-08 22:29:55 +0200 | [diff] [blame] | 13552 | str = writer->buffer; |
| 13553 | writer->buffer = NULL; |
| 13554 | assert(_PyUnicode_CheckConsistency(str, 1)); |
| 13555 | return unicode_result_ready(str); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13556 | } |
| 13557 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13558 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 13559 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 13560 | { |
| 13561 | Py_CLEAR(writer->buffer); |
| 13562 | } |
| 13563 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13564 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13565 | |
| 13566 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13567 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13568 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13569 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 13570 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13571 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13572 | PyDoc_STRVAR(format_map__doc__, |
| 13573 | "S.format_map(mapping) -> str\n\ |
| 13574 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13575 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 13576 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13577 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13578 | static PyObject * |
| 13579 | unicode__format__(PyObject* self, PyObject* args) |
| 13580 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13581 | PyObject *format_spec; |
| 13582 | _PyUnicodeWriter writer; |
| 13583 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13584 | |
| 13585 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 13586 | return NULL; |
| 13587 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13588 | if (PyUnicode_READY(self) == -1) |
| 13589 | return NULL; |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 13590 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13591 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 13592 | self, format_spec, 0, |
| 13593 | PyUnicode_GET_LENGTH(format_spec)); |
| 13594 | if (ret == -1) { |
| 13595 | _PyUnicodeWriter_Dealloc(&writer); |
| 13596 | return NULL; |
| 13597 | } |
| 13598 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13599 | } |
| 13600 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13601 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13602 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13603 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 13604 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 13605 | |
| 13606 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13607 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13609 | Py_ssize_t size; |
| 13610 | |
| 13611 | /* If it's a compact object, account for base structure + |
| 13612 | character data. */ |
| 13613 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 13614 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 13615 | else if (PyUnicode_IS_COMPACT(v)) |
| 13616 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13617 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13618 | else { |
| 13619 | /* If it is a two-block object, account for base object, and |
| 13620 | for character block if present. */ |
| 13621 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 13622 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13623 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 13624 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13625 | } |
| 13626 | /* 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] | 13627 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 13628 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13629 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 13630 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 13631 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13632 | |
| 13633 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13634 | } |
| 13635 | |
| 13636 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13637 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13638 | |
| 13639 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 13640 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13641 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 13642 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13643 | if (!copy) |
| 13644 | return NULL; |
| 13645 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 13646 | } |
| 13647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13648 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 13649 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13650 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 13651 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 13652 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13653 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 13654 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 13655 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13656 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 13657 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 13658 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13659 | {"expandtabs", (PyCFunction) unicode_expandtabs, |
| 13660 | METH_VARARGS | METH_KEYWORDS, expandtabs__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13661 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13662 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13663 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 13664 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 13665 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13666 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13667 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 13668 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 13669 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13670 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13671 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 13672 | {"splitlines", (PyCFunction) unicode_splitlines, |
| 13673 | METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 13674 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13675 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 13676 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 13677 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 13678 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 13679 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 13680 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 13681 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 13682 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 13683 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 13684 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 13685 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 13686 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 13687 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 13688 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 13689 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 13690 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 13691 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 13692 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 13693 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 13694 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13695 | UNICODE_MAKETRANS_METHODDEF |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 13696 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 13697 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13698 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 13699 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13700 | #endif |
| 13701 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13702 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13703 | {NULL, NULL} |
| 13704 | }; |
| 13705 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13706 | static PyObject * |
| 13707 | unicode_mod(PyObject *v, PyObject *w) |
| 13708 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13709 | if (!PyUnicode_Check(v)) |
| 13710 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13711 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13712 | } |
| 13713 | |
| 13714 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13715 | 0, /*nb_add*/ |
| 13716 | 0, /*nb_subtract*/ |
| 13717 | 0, /*nb_multiply*/ |
| 13718 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13719 | }; |
| 13720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13721 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13722 | (lenfunc) unicode_length, /* sq_length */ |
| 13723 | PyUnicode_Concat, /* sq_concat */ |
| 13724 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13725 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13726 | 0, /* sq_slice */ |
| 13727 | 0, /* sq_ass_item */ |
| 13728 | 0, /* sq_ass_slice */ |
| 13729 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13730 | }; |
| 13731 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13732 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13733 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13734 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13735 | if (PyUnicode_READY(self) == -1) |
| 13736 | return NULL; |
| 13737 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13738 | if (PyIndex_Check(item)) { |
| 13739 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13740 | if (i == -1 && PyErr_Occurred()) |
| 13741 | return NULL; |
| 13742 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13743 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13744 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13745 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13746 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13747 | PyObject *result; |
| 13748 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13749 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13750 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13751 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13752 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13753 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13754 | return NULL; |
| 13755 | } |
| 13756 | |
| 13757 | if (slicelength <= 0) { |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 13758 | _Py_RETURN_UNICODE_EMPTY(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13759 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13760 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13761 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13762 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13763 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13764 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13765 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13766 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13767 | src_kind = PyUnicode_KIND(self); |
| 13768 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13769 | if (!PyUnicode_IS_ASCII(self)) { |
| 13770 | kind_limit = kind_maxchar_limit(src_kind); |
| 13771 | max_char = 0; |
| 13772 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13773 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13774 | if (ch > max_char) { |
| 13775 | max_char = ch; |
| 13776 | if (max_char >= kind_limit) |
| 13777 | break; |
| 13778 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13779 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13780 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13781 | else |
| 13782 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13783 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13784 | if (result == NULL) |
| 13785 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13786 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13787 | dest_data = PyUnicode_DATA(result); |
| 13788 | |
| 13789 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13790 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13791 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13792 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13793 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13794 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13795 | } else { |
| 13796 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13797 | return NULL; |
| 13798 | } |
| 13799 | } |
| 13800 | |
| 13801 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13802 | (lenfunc)unicode_length, /* mp_length */ |
| 13803 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13804 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13805 | }; |
| 13806 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13807 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13808 | /* Helpers for PyUnicode_Format() */ |
| 13809 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13810 | struct unicode_formatter_t { |
| 13811 | PyObject *args; |
| 13812 | int args_owned; |
| 13813 | Py_ssize_t arglen, argidx; |
| 13814 | PyObject *dict; |
| 13815 | |
| 13816 | enum PyUnicode_Kind fmtkind; |
| 13817 | Py_ssize_t fmtcnt, fmtpos; |
| 13818 | void *fmtdata; |
| 13819 | PyObject *fmtstr; |
| 13820 | |
| 13821 | _PyUnicodeWriter writer; |
| 13822 | }; |
| 13823 | |
| 13824 | struct unicode_format_arg_t { |
| 13825 | Py_UCS4 ch; |
| 13826 | int flags; |
| 13827 | Py_ssize_t width; |
| 13828 | int prec; |
| 13829 | int sign; |
| 13830 | }; |
| 13831 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13832 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13833 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13834 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13835 | Py_ssize_t argidx = ctx->argidx; |
| 13836 | |
| 13837 | if (argidx < ctx->arglen) { |
| 13838 | ctx->argidx++; |
| 13839 | if (ctx->arglen < 0) |
| 13840 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13841 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13842 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13843 | } |
| 13844 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13845 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13846 | return NULL; |
| 13847 | } |
| 13848 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13849 | /* 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] | 13850 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13851 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13852 | otherwise. |
| 13853 | |
| 13854 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13855 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13856 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13857 | PyObject **p_output, |
| 13858 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13859 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13860 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13861 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13862 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13863 | int prec; |
| 13864 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13865 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13866 | x = PyFloat_AsDouble(v); |
| 13867 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13868 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13869 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13870 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13871 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13872 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13873 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13874 | if (arg->flags & F_ALT) |
| 13875 | dtoa_flags = Py_DTSF_ALT; |
| 13876 | else |
| 13877 | dtoa_flags = 0; |
| 13878 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13879 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13880 | return -1; |
| 13881 | len = strlen(p); |
| 13882 | if (writer) { |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 13883 | if (_PyUnicodeWriter_WriteASCIIString(writer, p, len) < 0) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13884 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13885 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13886 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13887 | } |
| 13888 | else |
| 13889 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13890 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13891 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13892 | } |
| 13893 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13894 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13895 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13896 | * Python's regular ints. |
| 13897 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13898 | * The output string is of the form |
| 13899 | * "-"? ("0x" | "0X")? digit+ |
| 13900 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13901 | * set in flags. The case of hex digits will be correct, |
| 13902 | * There will be at least prec digits, zero-filled on the left if |
| 13903 | * necessary to get that many. |
| 13904 | * val object to be converted |
| 13905 | * flags bitmask of format flags; only F_ALT is looked at |
| 13906 | * prec minimum number of digits; 0-fill on left if needed |
| 13907 | * type a character in [duoxX]; u acts the same as d |
| 13908 | * |
| 13909 | * CAUTION: o, x and X conversions on regular ints can never |
| 13910 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13911 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13912 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13913 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13914 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13915 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13916 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13917 | Py_ssize_t i; |
| 13918 | int sign; /* 1 if '-', else 0 */ |
| 13919 | int len; /* number of characters */ |
| 13920 | Py_ssize_t llen; |
| 13921 | int numdigits; /* len == numnondigits + numdigits */ |
| 13922 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13923 | int prec = arg->prec; |
| 13924 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13925 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13926 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13927 | if (prec > INT_MAX-3) { |
| 13928 | PyErr_SetString(PyExc_OverflowError, |
| 13929 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13930 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13931 | } |
| 13932 | |
| 13933 | assert(PyLong_Check(val)); |
| 13934 | |
| 13935 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13936 | default: |
| 13937 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13938 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13939 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13940 | case 'u': |
Ethan Furman | fb13721 | 2013-08-31 10:18:55 -0700 | [diff] [blame] | 13941 | /* int and int subclasses should print numerically when a numeric */ |
| 13942 | /* format code is used (see issue18780) */ |
| 13943 | result = PyNumber_ToBase(val, 10); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13944 | break; |
| 13945 | case 'o': |
| 13946 | numnondigits = 2; |
| 13947 | result = PyNumber_ToBase(val, 8); |
| 13948 | break; |
| 13949 | case 'x': |
| 13950 | case 'X': |
| 13951 | numnondigits = 2; |
| 13952 | result = PyNumber_ToBase(val, 16); |
| 13953 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13954 | } |
| 13955 | if (!result) |
| 13956 | return NULL; |
| 13957 | |
| 13958 | assert(unicode_modifiable(result)); |
| 13959 | assert(PyUnicode_IS_READY(result)); |
| 13960 | assert(PyUnicode_IS_ASCII(result)); |
| 13961 | |
| 13962 | /* To modify the string in-place, there can only be one reference. */ |
| 13963 | if (Py_REFCNT(result) != 1) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13964 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13965 | PyErr_BadInternalCall(); |
| 13966 | return NULL; |
| 13967 | } |
| 13968 | buf = PyUnicode_DATA(result); |
| 13969 | llen = PyUnicode_GET_LENGTH(result); |
| 13970 | if (llen > INT_MAX) { |
Christian Heimes | d47802e | 2013-06-29 21:33:36 +0200 | [diff] [blame] | 13971 | Py_DECREF(result); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13972 | PyErr_SetString(PyExc_ValueError, |
| 13973 | "string too large in _PyBytes_FormatLong"); |
| 13974 | return NULL; |
| 13975 | } |
| 13976 | len = (int)llen; |
| 13977 | sign = buf[0] == '-'; |
| 13978 | numnondigits += sign; |
| 13979 | numdigits = len - numnondigits; |
| 13980 | assert(numdigits > 0); |
| 13981 | |
| 13982 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13983 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13984 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13985 | assert(buf[sign] == '0'); |
| 13986 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13987 | buf[sign+1] == 'o'); |
| 13988 | numnondigits -= 2; |
| 13989 | buf += 2; |
| 13990 | len -= 2; |
| 13991 | if (sign) |
| 13992 | buf[0] = '-'; |
| 13993 | assert(len == numnondigits + numdigits); |
| 13994 | assert(numdigits > 0); |
| 13995 | } |
| 13996 | |
| 13997 | /* Fill with leading zeroes to meet minimum width. */ |
| 13998 | if (prec > numdigits) { |
| 13999 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 14000 | numnondigits + prec); |
| 14001 | char *b1; |
| 14002 | if (!r1) { |
| 14003 | Py_DECREF(result); |
| 14004 | return NULL; |
| 14005 | } |
| 14006 | b1 = PyBytes_AS_STRING(r1); |
| 14007 | for (i = 0; i < numnondigits; ++i) |
| 14008 | *b1++ = *buf++; |
| 14009 | for (i = 0; i < prec - numdigits; i++) |
| 14010 | *b1++ = '0'; |
| 14011 | for (i = 0; i < numdigits; i++) |
| 14012 | *b1++ = *buf++; |
| 14013 | *b1 = '\0'; |
| 14014 | Py_DECREF(result); |
| 14015 | result = r1; |
| 14016 | buf = PyBytes_AS_STRING(result); |
| 14017 | len = numnondigits + prec; |
| 14018 | } |
| 14019 | |
| 14020 | /* Fix up case for hex conversions. */ |
| 14021 | if (type == 'X') { |
| 14022 | /* Need to convert all lower case letters to upper case. |
| 14023 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 14024 | for (i = 0; i < len; i++) |
| 14025 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 14026 | buf[i] -= 'a'-'A'; |
| 14027 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14028 | if (!PyUnicode_Check(result) |
| 14029 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14030 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 14031 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 14032 | Py_DECREF(result); |
| 14033 | result = unicode; |
| 14034 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14035 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 14036 | if (PyUnicode_Resize(&result, len) < 0) |
| 14037 | Py_CLEAR(result); |
| 14038 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14039 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 14040 | } |
| 14041 | |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14042 | /* Format an integer or a float as an integer. |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14043 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14044 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14045 | * -1 and raise an exception on error */ |
| 14046 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14047 | mainformatlong(PyObject *v, |
| 14048 | struct unicode_format_arg_t *arg, |
| 14049 | PyObject **p_output, |
| 14050 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14051 | { |
| 14052 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14053 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14054 | |
| 14055 | if (!PyNumber_Check(v)) |
| 14056 | goto wrongtype; |
| 14057 | |
Ethan Furman | f9bba9c | 2014-01-11 23:20:58 -0800 | [diff] [blame] | 14058 | /* make sure number is a type of integer */ |
Ethan Furman | a70805e | 2014-01-12 08:42:35 -0800 | [diff] [blame] | 14059 | /* if not, issue deprecation warning for now */ |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14060 | if (!PyLong_Check(v)) { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14061 | if (type == 'o' || type == 'x' || type == 'X') { |
| 14062 | iobj = PyNumber_Index(v); |
| 14063 | if (iobj == NULL) { |
Ethan Furman | f9bba9c | 2014-01-11 23:20:58 -0800 | [diff] [blame] | 14064 | PyErr_Clear(); |
| 14065 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 14066 | "automatic int conversions have been deprecated", |
| 14067 | 1)) { |
| 14068 | return -1; |
| 14069 | } |
| 14070 | iobj = PyNumber_Long(v); |
| 14071 | if (iobj == NULL ) { |
| 14072 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14073 | goto wrongtype; |
| 14074 | return -1; |
| 14075 | } |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14076 | } |
| 14077 | } |
| 14078 | else { |
| 14079 | iobj = PyNumber_Long(v); |
| 14080 | if (iobj == NULL ) { |
| 14081 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14082 | goto wrongtype; |
| 14083 | return -1; |
| 14084 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14085 | } |
| 14086 | assert(PyLong_Check(iobj)); |
| 14087 | } |
| 14088 | else { |
| 14089 | iobj = v; |
| 14090 | Py_INCREF(iobj); |
| 14091 | } |
| 14092 | |
| 14093 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14094 | && arg->width == -1 && arg->prec == -1 |
| 14095 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 14096 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14097 | { |
| 14098 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14099 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14100 | int base; |
| 14101 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14102 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14103 | { |
| 14104 | default: |
| 14105 | assert(0 && "'type' not in [diuoxX]"); |
| 14106 | case 'd': |
| 14107 | case 'i': |
| 14108 | case 'u': |
| 14109 | base = 10; |
| 14110 | break; |
| 14111 | case 'o': |
| 14112 | base = 8; |
| 14113 | break; |
| 14114 | case 'x': |
| 14115 | case 'X': |
| 14116 | base = 16; |
| 14117 | break; |
| 14118 | } |
| 14119 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14120 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 14121 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14122 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 14123 | } |
| 14124 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14125 | return 1; |
| 14126 | } |
| 14127 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14128 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14129 | Py_DECREF(iobj); |
| 14130 | if (res == NULL) |
| 14131 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14132 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14133 | return 0; |
| 14134 | |
| 14135 | wrongtype: |
| 14136 | PyErr_Format(PyExc_TypeError, |
| 14137 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14138 | "not %.200s", |
| 14139 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 14140 | return -1; |
| 14141 | } |
| 14142 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14143 | static Py_UCS4 |
| 14144 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14145 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14146 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14147 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14148 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14149 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14150 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14151 | goto onError; |
| 14152 | } |
| 14153 | else { |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14154 | PyObject *iobj; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14155 | long x; |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14156 | /* make sure number is a type of integer */ |
Ethan Furman | a70805e | 2014-01-12 08:42:35 -0800 | [diff] [blame] | 14157 | /* if not, issue deprecation warning for now */ |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14158 | if (!PyLong_Check(v)) { |
| 14159 | iobj = PyNumber_Index(v); |
| 14160 | if (iobj == NULL) { |
Ethan Furman | f9bba9c | 2014-01-11 23:20:58 -0800 | [diff] [blame] | 14161 | PyErr_Clear(); |
| 14162 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 14163 | "automatic int conversions have been deprecated", |
| 14164 | 1)) { |
| 14165 | return -1; |
| 14166 | } |
| 14167 | iobj = PyNumber_Long(v); |
| 14168 | if (iobj == NULL ) { |
| 14169 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 14170 | goto onError; |
| 14171 | return -1; |
| 14172 | } |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14173 | } |
| 14174 | v = iobj; |
| 14175 | Py_DECREF(iobj); |
| 14176 | } |
| 14177 | /* Integer input truncated to a character */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14178 | x = PyLong_AsLong(v); |
| 14179 | if (x == -1 && PyErr_Occurred()) |
| 14180 | goto onError; |
| 14181 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 14182 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14183 | PyErr_SetString(PyExc_OverflowError, |
| 14184 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14185 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14186 | } |
| 14187 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14188 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14189 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 14190 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14191 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 14192 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14193 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14194 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14195 | } |
| 14196 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14197 | /* Parse options of an argument: flags, width, precision. |
| 14198 | Handle also "%(name)" syntax. |
| 14199 | |
| 14200 | Return 0 if the argument has been formatted into arg->str. |
| 14201 | Return 1 if the argument has been written into ctx->writer, |
| 14202 | Raise an exception and return -1 on error. */ |
| 14203 | static int |
| 14204 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 14205 | struct unicode_format_arg_t *arg) |
| 14206 | { |
| 14207 | #define FORMAT_READ(ctx) \ |
| 14208 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 14209 | |
| 14210 | PyObject *v; |
| 14211 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14212 | if (arg->ch == '(') { |
| 14213 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 14214 | Py_ssize_t keystart; |
| 14215 | Py_ssize_t keylen; |
| 14216 | PyObject *key; |
| 14217 | int pcount = 1; |
| 14218 | |
| 14219 | if (ctx->dict == NULL) { |
| 14220 | PyErr_SetString(PyExc_TypeError, |
| 14221 | "format requires a mapping"); |
| 14222 | return -1; |
| 14223 | } |
| 14224 | ++ctx->fmtpos; |
| 14225 | --ctx->fmtcnt; |
| 14226 | keystart = ctx->fmtpos; |
| 14227 | /* Skip over balanced parentheses */ |
| 14228 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 14229 | arg->ch = FORMAT_READ(ctx); |
| 14230 | if (arg->ch == ')') |
| 14231 | --pcount; |
| 14232 | else if (arg->ch == '(') |
| 14233 | ++pcount; |
| 14234 | ctx->fmtpos++; |
| 14235 | } |
| 14236 | keylen = ctx->fmtpos - keystart - 1; |
| 14237 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 14238 | PyErr_SetString(PyExc_ValueError, |
| 14239 | "incomplete format key"); |
| 14240 | return -1; |
| 14241 | } |
| 14242 | key = PyUnicode_Substring(ctx->fmtstr, |
| 14243 | keystart, keystart + keylen); |
| 14244 | if (key == NULL) |
| 14245 | return -1; |
| 14246 | if (ctx->args_owned) { |
| 14247 | Py_DECREF(ctx->args); |
| 14248 | ctx->args_owned = 0; |
| 14249 | } |
| 14250 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 14251 | Py_DECREF(key); |
| 14252 | if (ctx->args == NULL) |
| 14253 | return -1; |
| 14254 | ctx->args_owned = 1; |
| 14255 | ctx->arglen = -1; |
| 14256 | ctx->argidx = -2; |
| 14257 | } |
| 14258 | |
| 14259 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14260 | while (--ctx->fmtcnt >= 0) { |
| 14261 | arg->ch = FORMAT_READ(ctx); |
| 14262 | ctx->fmtpos++; |
| 14263 | switch (arg->ch) { |
| 14264 | case '-': arg->flags |= F_LJUST; continue; |
| 14265 | case '+': arg->flags |= F_SIGN; continue; |
| 14266 | case ' ': arg->flags |= F_BLANK; continue; |
| 14267 | case '#': arg->flags |= F_ALT; continue; |
| 14268 | case '0': arg->flags |= F_ZERO; continue; |
| 14269 | } |
| 14270 | break; |
| 14271 | } |
| 14272 | |
| 14273 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14274 | if (arg->ch == '*') { |
| 14275 | v = unicode_format_getnextarg(ctx); |
| 14276 | if (v == NULL) |
| 14277 | return -1; |
| 14278 | if (!PyLong_Check(v)) { |
| 14279 | PyErr_SetString(PyExc_TypeError, |
| 14280 | "* wants int"); |
| 14281 | return -1; |
| 14282 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14283 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14284 | if (arg->width == -1 && PyErr_Occurred()) |
| 14285 | return -1; |
| 14286 | if (arg->width < 0) { |
| 14287 | arg->flags |= F_LJUST; |
| 14288 | arg->width = -arg->width; |
| 14289 | } |
| 14290 | if (--ctx->fmtcnt >= 0) { |
| 14291 | arg->ch = FORMAT_READ(ctx); |
| 14292 | ctx->fmtpos++; |
| 14293 | } |
| 14294 | } |
| 14295 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14296 | arg->width = arg->ch - '0'; |
| 14297 | while (--ctx->fmtcnt >= 0) { |
| 14298 | arg->ch = FORMAT_READ(ctx); |
| 14299 | ctx->fmtpos++; |
| 14300 | if (arg->ch < '0' || arg->ch > '9') |
| 14301 | break; |
| 14302 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 14303 | mixing signed and unsigned comparison. Since arg->ch is between |
| 14304 | '0' and '9', casting to int is safe. */ |
| 14305 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 14306 | PyErr_SetString(PyExc_ValueError, |
| 14307 | "width too big"); |
| 14308 | return -1; |
| 14309 | } |
| 14310 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 14311 | } |
| 14312 | } |
| 14313 | |
| 14314 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14315 | if (arg->ch == '.') { |
| 14316 | arg->prec = 0; |
| 14317 | if (--ctx->fmtcnt >= 0) { |
| 14318 | arg->ch = FORMAT_READ(ctx); |
| 14319 | ctx->fmtpos++; |
| 14320 | } |
| 14321 | if (arg->ch == '*') { |
| 14322 | v = unicode_format_getnextarg(ctx); |
| 14323 | if (v == NULL) |
| 14324 | return -1; |
| 14325 | if (!PyLong_Check(v)) { |
| 14326 | PyErr_SetString(PyExc_TypeError, |
| 14327 | "* wants int"); |
| 14328 | return -1; |
| 14329 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 14330 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14331 | if (arg->prec == -1 && PyErr_Occurred()) |
| 14332 | return -1; |
| 14333 | if (arg->prec < 0) |
| 14334 | arg->prec = 0; |
| 14335 | if (--ctx->fmtcnt >= 0) { |
| 14336 | arg->ch = FORMAT_READ(ctx); |
| 14337 | ctx->fmtpos++; |
| 14338 | } |
| 14339 | } |
| 14340 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 14341 | arg->prec = arg->ch - '0'; |
| 14342 | while (--ctx->fmtcnt >= 0) { |
| 14343 | arg->ch = FORMAT_READ(ctx); |
| 14344 | ctx->fmtpos++; |
| 14345 | if (arg->ch < '0' || arg->ch > '9') |
| 14346 | break; |
| 14347 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 14348 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 14349 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14350 | return -1; |
| 14351 | } |
| 14352 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 14353 | } |
| 14354 | } |
| 14355 | } |
| 14356 | |
| 14357 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 14358 | if (ctx->fmtcnt >= 0) { |
| 14359 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 14360 | if (--ctx->fmtcnt >= 0) { |
| 14361 | arg->ch = FORMAT_READ(ctx); |
| 14362 | ctx->fmtpos++; |
| 14363 | } |
| 14364 | } |
| 14365 | } |
| 14366 | if (ctx->fmtcnt < 0) { |
| 14367 | PyErr_SetString(PyExc_ValueError, |
| 14368 | "incomplete format"); |
| 14369 | return -1; |
| 14370 | } |
| 14371 | return 0; |
| 14372 | |
| 14373 | #undef FORMAT_READ |
| 14374 | } |
| 14375 | |
| 14376 | /* Format one argument. Supported conversion specifiers: |
| 14377 | |
| 14378 | - "s", "r", "a": any type |
Ethan Furman | df3ed24 | 2014-01-05 06:50:30 -0800 | [diff] [blame] | 14379 | - "i", "d", "u": int or float |
| 14380 | - "o", "x", "X": int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14381 | - "e", "E", "f", "F", "g", "G": float |
| 14382 | - "c": int or str (1 character) |
| 14383 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14384 | When possible, the output is written directly into the Unicode writer |
| 14385 | (ctx->writer). A string is created when padding is required. |
| 14386 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14387 | Return 0 if the argument has been formatted into *p_str, |
| 14388 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14389 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14390 | static int |
| 14391 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 14392 | struct unicode_format_arg_t *arg, |
| 14393 | PyObject **p_str) |
| 14394 | { |
| 14395 | PyObject *v; |
| 14396 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14397 | |
| 14398 | if (ctx->fmtcnt == 0) |
| 14399 | ctx->writer.overallocate = 0; |
| 14400 | |
| 14401 | if (arg->ch == '%') { |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14402 | if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14403 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14404 | return 1; |
| 14405 | } |
| 14406 | |
| 14407 | v = unicode_format_getnextarg(ctx); |
| 14408 | if (v == NULL) |
| 14409 | return -1; |
| 14410 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14411 | |
| 14412 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14413 | case 's': |
| 14414 | case 'r': |
| 14415 | case 'a': |
| 14416 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 14417 | /* Fast path */ |
| 14418 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 14419 | return -1; |
| 14420 | return 1; |
| 14421 | } |
| 14422 | |
| 14423 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 14424 | *p_str = v; |
| 14425 | Py_INCREF(*p_str); |
| 14426 | } |
| 14427 | else { |
| 14428 | if (arg->ch == 's') |
| 14429 | *p_str = PyObject_Str(v); |
| 14430 | else if (arg->ch == 'r') |
| 14431 | *p_str = PyObject_Repr(v); |
| 14432 | else |
| 14433 | *p_str = PyObject_ASCII(v); |
| 14434 | } |
| 14435 | break; |
| 14436 | |
| 14437 | case 'i': |
| 14438 | case 'd': |
| 14439 | case 'u': |
| 14440 | case 'o': |
| 14441 | case 'x': |
| 14442 | case 'X': |
| 14443 | { |
| 14444 | int ret = mainformatlong(v, arg, p_str, writer); |
| 14445 | if (ret != 0) |
| 14446 | return ret; |
| 14447 | arg->sign = 1; |
| 14448 | break; |
| 14449 | } |
| 14450 | |
| 14451 | case 'e': |
| 14452 | case 'E': |
| 14453 | case 'f': |
| 14454 | case 'F': |
| 14455 | case 'g': |
| 14456 | case 'G': |
| 14457 | if (arg->width == -1 && arg->prec == -1 |
| 14458 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14459 | { |
| 14460 | /* Fast path */ |
| 14461 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 14462 | return -1; |
| 14463 | return 1; |
| 14464 | } |
| 14465 | |
| 14466 | arg->sign = 1; |
| 14467 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 14468 | return -1; |
| 14469 | break; |
| 14470 | |
| 14471 | case 'c': |
| 14472 | { |
| 14473 | Py_UCS4 ch = formatchar(v); |
| 14474 | if (ch == (Py_UCS4) -1) |
| 14475 | return -1; |
| 14476 | if (arg->width == -1 && arg->prec == -1) { |
| 14477 | /* Fast path */ |
Victor Stinner | 8a1a6cf | 2013-04-14 02:35:33 +0200 | [diff] [blame] | 14478 | if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14479 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14480 | return 1; |
| 14481 | } |
| 14482 | *p_str = PyUnicode_FromOrdinal(ch); |
| 14483 | break; |
| 14484 | } |
| 14485 | |
| 14486 | default: |
| 14487 | PyErr_Format(PyExc_ValueError, |
| 14488 | "unsupported format character '%c' (0x%x) " |
Victor Stinner | a33bce0 | 2014-07-04 22:47:46 +0200 | [diff] [blame] | 14489 | "at index %zd", |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14490 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 14491 | (int)arg->ch, |
| 14492 | ctx->fmtpos - 1); |
| 14493 | return -1; |
| 14494 | } |
| 14495 | if (*p_str == NULL) |
| 14496 | return -1; |
| 14497 | assert (PyUnicode_Check(*p_str)); |
| 14498 | return 0; |
| 14499 | } |
| 14500 | |
| 14501 | static int |
| 14502 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 14503 | struct unicode_format_arg_t *arg, |
| 14504 | PyObject *str) |
| 14505 | { |
| 14506 | Py_ssize_t len; |
| 14507 | enum PyUnicode_Kind kind; |
| 14508 | void *pbuf; |
| 14509 | Py_ssize_t pindex; |
| 14510 | Py_UCS4 signchar; |
| 14511 | Py_ssize_t buflen; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14512 | Py_UCS4 maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14513 | Py_ssize_t sublen; |
| 14514 | _PyUnicodeWriter *writer = &ctx->writer; |
| 14515 | Py_UCS4 fill; |
| 14516 | |
| 14517 | fill = ' '; |
| 14518 | if (arg->sign && arg->flags & F_ZERO) |
| 14519 | fill = '0'; |
| 14520 | |
| 14521 | if (PyUnicode_READY(str) == -1) |
| 14522 | return -1; |
| 14523 | |
| 14524 | len = PyUnicode_GET_LENGTH(str); |
| 14525 | if ((arg->width == -1 || arg->width <= len) |
| 14526 | && (arg->prec == -1 || arg->prec >= len) |
| 14527 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 14528 | { |
| 14529 | /* Fast path */ |
| 14530 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 14531 | return -1; |
| 14532 | return 0; |
| 14533 | } |
| 14534 | |
| 14535 | /* Truncate the string for "s", "r" and "a" formats |
| 14536 | if the precision is set */ |
| 14537 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 14538 | if (arg->prec >= 0 && len > arg->prec) |
| 14539 | len = arg->prec; |
| 14540 | } |
| 14541 | |
| 14542 | /* Adjust sign and width */ |
| 14543 | kind = PyUnicode_KIND(str); |
| 14544 | pbuf = PyUnicode_DATA(str); |
| 14545 | pindex = 0; |
| 14546 | signchar = '\0'; |
| 14547 | if (arg->sign) { |
| 14548 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 14549 | if (ch == '-' || ch == '+') { |
| 14550 | signchar = ch; |
| 14551 | len--; |
| 14552 | pindex++; |
| 14553 | } |
| 14554 | else if (arg->flags & F_SIGN) |
| 14555 | signchar = '+'; |
| 14556 | else if (arg->flags & F_BLANK) |
| 14557 | signchar = ' '; |
| 14558 | else |
| 14559 | arg->sign = 0; |
| 14560 | } |
| 14561 | if (arg->width < len) |
| 14562 | arg->width = len; |
| 14563 | |
| 14564 | /* Prepare the writer */ |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14565 | maxchar = writer->maxchar; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14566 | if (!(arg->flags & F_LJUST)) { |
| 14567 | if (arg->sign) { |
| 14568 | if ((arg->width-1) > len) |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14569 | maxchar = Py_MAX(maxchar, fill); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14570 | } |
| 14571 | else { |
| 14572 | if (arg->width > 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 | } |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14576 | if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { |
| 14577 | Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
Benjamin Peterson | 3164f5d | 2013-06-10 09:24:01 -0700 | [diff] [blame] | 14578 | maxchar = Py_MAX(maxchar, strmaxchar); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14579 | } |
| 14580 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14581 | buflen = arg->width; |
| 14582 | if (arg->sign && len == arg->width) |
| 14583 | buflen++; |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 14584 | if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14585 | return -1; |
| 14586 | |
| 14587 | /* Write the sign if needed */ |
| 14588 | if (arg->sign) { |
| 14589 | if (fill != ' ') { |
| 14590 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14591 | writer->pos += 1; |
| 14592 | } |
| 14593 | if (arg->width > len) |
| 14594 | arg->width--; |
| 14595 | } |
| 14596 | |
| 14597 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 14598 | if the alternate form is used. |
| 14599 | For example, write "0x" for the "%#x" format. */ |
| 14600 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14601 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14602 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 14603 | if (fill != ' ') { |
| 14604 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14605 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14606 | writer->pos += 2; |
| 14607 | pindex += 2; |
| 14608 | } |
| 14609 | arg->width -= 2; |
| 14610 | if (arg->width < 0) |
| 14611 | arg->width = 0; |
| 14612 | len -= 2; |
| 14613 | } |
| 14614 | |
| 14615 | /* Pad left with the fill character if needed */ |
| 14616 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 14617 | sublen = arg->width - len; |
| 14618 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 14619 | writer->pos += sublen; |
| 14620 | arg->width = len; |
| 14621 | } |
| 14622 | |
| 14623 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 14624 | the alternate form is used */ |
| 14625 | if (fill == ' ') { |
| 14626 | if (arg->sign) { |
| 14627 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 14628 | writer->pos += 1; |
| 14629 | } |
| 14630 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 14631 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 14632 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 14633 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 14634 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 14635 | writer->pos += 2; |
| 14636 | pindex += 2; |
| 14637 | } |
| 14638 | } |
| 14639 | |
| 14640 | /* Write characters */ |
| 14641 | if (len) { |
| 14642 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 14643 | str, pindex, len); |
| 14644 | writer->pos += len; |
| 14645 | } |
| 14646 | |
| 14647 | /* Pad right with the fill character if needed */ |
| 14648 | if (arg->width > len) { |
| 14649 | sublen = arg->width - len; |
| 14650 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 14651 | writer->pos += sublen; |
| 14652 | } |
| 14653 | return 0; |
| 14654 | } |
| 14655 | |
| 14656 | /* Helper of PyUnicode_Format(): format one arg. |
| 14657 | Return 0 on success, raise an exception and return -1 on error. */ |
| 14658 | static int |
| 14659 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 14660 | { |
| 14661 | struct unicode_format_arg_t arg; |
| 14662 | PyObject *str; |
| 14663 | int ret; |
| 14664 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 14665 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 14666 | arg.flags = 0; |
| 14667 | arg.width = -1; |
| 14668 | arg.prec = -1; |
| 14669 | arg.sign = 0; |
| 14670 | str = NULL; |
| 14671 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14672 | ret = unicode_format_arg_parse(ctx, &arg); |
| 14673 | if (ret == -1) |
| 14674 | return -1; |
| 14675 | |
| 14676 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 14677 | if (ret == -1) |
| 14678 | return -1; |
| 14679 | |
| 14680 | if (ret != 1) { |
| 14681 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 14682 | Py_DECREF(str); |
| 14683 | if (ret == -1) |
| 14684 | return -1; |
| 14685 | } |
| 14686 | |
| 14687 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 14688 | PyErr_SetString(PyExc_TypeError, |
| 14689 | "not all arguments converted during string formatting"); |
| 14690 | return -1; |
| 14691 | } |
| 14692 | return 0; |
| 14693 | } |
| 14694 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14695 | PyObject * |
| 14696 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14697 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14698 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 14699 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14700 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14701 | PyErr_BadInternalCall(); |
| 14702 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14703 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14704 | |
| 14705 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 14706 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14707 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14708 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 14709 | Py_DECREF(ctx.fmtstr); |
| 14710 | return NULL; |
| 14711 | } |
| 14712 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 14713 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 14714 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 14715 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14716 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 14717 | _PyUnicodeWriter_Init(&ctx.writer); |
| 14718 | ctx.writer.min_length = ctx.fmtcnt + 100; |
| 14719 | ctx.writer.overallocate = 1; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 14720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14721 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14722 | ctx.arglen = PyTuple_Size(args); |
| 14723 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14724 | } |
| 14725 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14726 | ctx.arglen = -1; |
| 14727 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14728 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14729 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 14730 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14731 | ctx.dict = args; |
| 14732 | else |
| 14733 | ctx.dict = NULL; |
| 14734 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14735 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14736 | while (--ctx.fmtcnt >= 0) { |
| 14737 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14738 | Py_ssize_t nonfmtpos; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14739 | |
| 14740 | nonfmtpos = ctx.fmtpos++; |
| 14741 | while (ctx.fmtcnt >= 0 && |
| 14742 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 14743 | ctx.fmtpos++; |
| 14744 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14745 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14746 | if (ctx.fmtcnt < 0) { |
| 14747 | ctx.fmtpos--; |
| 14748 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14749 | } |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14750 | |
Victor Stinner | cfc4c13 | 2013-04-03 01:48:39 +0200 | [diff] [blame] | 14751 | if (_PyUnicodeWriter_WriteSubstring(&ctx.writer, ctx.fmtstr, |
| 14752 | nonfmtpos, ctx.fmtpos) < 0) |
| 14753 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14754 | } |
| 14755 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14756 | ctx.fmtpos++; |
| 14757 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14758 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14759 | } |
| 14760 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14761 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14762 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14763 | PyErr_SetString(PyExc_TypeError, |
| 14764 | "not all arguments converted during string formatting"); |
| 14765 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14766 | } |
| 14767 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14768 | if (ctx.args_owned) { |
| 14769 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14770 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14771 | Py_DECREF(ctx.fmtstr); |
| 14772 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14773 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14774 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14775 | Py_DECREF(ctx.fmtstr); |
| 14776 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14777 | if (ctx.args_owned) { |
| 14778 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14779 | } |
| 14780 | return NULL; |
| 14781 | } |
| 14782 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14783 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14784 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14785 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14786 | static PyObject * |
| 14787 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14788 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14789 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14790 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14791 | char *encoding = NULL; |
| 14792 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14793 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14794 | if (type != &PyUnicode_Type) |
| 14795 | return unicode_subtype_new(type, args, kwds); |
| 14796 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14797 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14798 | return NULL; |
| 14799 | if (x == NULL) |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14800 | _Py_RETURN_UNICODE_EMPTY(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14801 | if (encoding == NULL && errors == NULL) |
| 14802 | return PyObject_Str(x); |
| 14803 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14804 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14805 | } |
| 14806 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14807 | static PyObject * |
| 14808 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14809 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14810 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14811 | Py_ssize_t length, char_size; |
| 14812 | int share_wstr, share_utf8; |
| 14813 | unsigned int kind; |
| 14814 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14815 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14816 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14817 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14818 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14819 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14820 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14821 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14822 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14823 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14824 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14825 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14826 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14827 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14828 | if (self == NULL) { |
| 14829 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14830 | return NULL; |
| 14831 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14832 | kind = PyUnicode_KIND(unicode); |
| 14833 | length = PyUnicode_GET_LENGTH(unicode); |
| 14834 | |
| 14835 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14836 | #ifdef Py_DEBUG |
| 14837 | _PyUnicode_HASH(self) = -1; |
| 14838 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14839 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14840 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14841 | _PyUnicode_STATE(self).interned = 0; |
| 14842 | _PyUnicode_STATE(self).kind = kind; |
| 14843 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14844 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14845 | _PyUnicode_STATE(self).ready = 1; |
| 14846 | _PyUnicode_WSTR(self) = NULL; |
| 14847 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14848 | _PyUnicode_UTF8(self) = NULL; |
| 14849 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14850 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14851 | |
| 14852 | share_utf8 = 0; |
| 14853 | share_wstr = 0; |
| 14854 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14855 | char_size = 1; |
| 14856 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14857 | share_utf8 = 1; |
| 14858 | } |
| 14859 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14860 | char_size = 2; |
| 14861 | if (sizeof(wchar_t) == 2) |
| 14862 | share_wstr = 1; |
| 14863 | } |
| 14864 | else { |
| 14865 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14866 | char_size = 4; |
| 14867 | if (sizeof(wchar_t) == 4) |
| 14868 | share_wstr = 1; |
| 14869 | } |
| 14870 | |
| 14871 | /* Ensure we won't overflow the length. */ |
| 14872 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14873 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14874 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14875 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14876 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14877 | if (data == NULL) { |
| 14878 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14879 | goto onError; |
| 14880 | } |
| 14881 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14882 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14883 | if (share_utf8) { |
| 14884 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14885 | _PyUnicode_UTF8(self) = data; |
| 14886 | } |
| 14887 | if (share_wstr) { |
| 14888 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14889 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14890 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14891 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14892 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14893 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14894 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14895 | #ifdef Py_DEBUG |
| 14896 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14897 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14898 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14899 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14900 | |
| 14901 | onError: |
| 14902 | Py_DECREF(unicode); |
| 14903 | Py_DECREF(self); |
| 14904 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14905 | } |
| 14906 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14907 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14908 | "str(object='') -> str\n\ |
| 14909 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14910 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14911 | Create a new string object from the given object. If encoding or\n\ |
| 14912 | errors is specified, then the object must expose a data buffer\n\ |
| 14913 | that will be decoded using the given encoding and error handler.\n\ |
| 14914 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14915 | or repr(object).\n\ |
| 14916 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14917 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14918 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14919 | static PyObject *unicode_iter(PyObject *seq); |
| 14920 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14921 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14922 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14923 | "str", /* tp_name */ |
| 14924 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14925 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14926 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14927 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14928 | 0, /* tp_print */ |
| 14929 | 0, /* tp_getattr */ |
| 14930 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14931 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14932 | unicode_repr, /* tp_repr */ |
| 14933 | &unicode_as_number, /* tp_as_number */ |
| 14934 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14935 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14936 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14937 | 0, /* tp_call*/ |
| 14938 | (reprfunc) unicode_str, /* tp_str */ |
| 14939 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14940 | 0, /* tp_setattro */ |
| 14941 | 0, /* tp_as_buffer */ |
| 14942 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14943 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14944 | unicode_doc, /* tp_doc */ |
| 14945 | 0, /* tp_traverse */ |
| 14946 | 0, /* tp_clear */ |
| 14947 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14948 | 0, /* tp_weaklistoffset */ |
| 14949 | unicode_iter, /* tp_iter */ |
| 14950 | 0, /* tp_iternext */ |
| 14951 | unicode_methods, /* tp_methods */ |
| 14952 | 0, /* tp_members */ |
| 14953 | 0, /* tp_getset */ |
| 14954 | &PyBaseObject_Type, /* tp_base */ |
| 14955 | 0, /* tp_dict */ |
| 14956 | 0, /* tp_descr_get */ |
| 14957 | 0, /* tp_descr_set */ |
| 14958 | 0, /* tp_dictoffset */ |
| 14959 | 0, /* tp_init */ |
| 14960 | 0, /* tp_alloc */ |
| 14961 | unicode_new, /* tp_new */ |
| 14962 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14963 | }; |
| 14964 | |
| 14965 | /* Initialize the Unicode implementation */ |
| 14966 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14967 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14968 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14969 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14970 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14971 | 0x000A, /* LINE FEED */ |
| 14972 | 0x000D, /* CARRIAGE RETURN */ |
| 14973 | 0x001C, /* FILE SEPARATOR */ |
| 14974 | 0x001D, /* GROUP SEPARATOR */ |
| 14975 | 0x001E, /* RECORD SEPARATOR */ |
| 14976 | 0x0085, /* NEXT LINE */ |
| 14977 | 0x2028, /* LINE SEPARATOR */ |
| 14978 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14979 | }; |
| 14980 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14981 | /* Init the implementation */ |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14982 | _Py_INCREF_UNICODE_EMPTY(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14983 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14984 | Py_FatalError("Can't create empty string"); |
Serhiy Storchaka | 678db84 | 2013-01-26 12:16:36 +0200 | [diff] [blame] | 14985 | Py_DECREF(unicode_empty); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14986 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14987 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14988 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14989 | |
| 14990 | /* initialize the linebreak bloom filter */ |
| 14991 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14992 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14993 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14994 | |
Christian Heimes | 26532f7 | 2013-07-20 14:57:16 +0200 | [diff] [blame] | 14995 | if (PyType_Ready(&EncodingMapType) < 0) |
| 14996 | Py_FatalError("Can't initialize encoding map type"); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14997 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14998 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14999 | Py_FatalError("Can't initialize field name iterator type"); |
| 15000 | |
| 15001 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 15002 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 15003 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 15004 | #ifdef HAVE_MBCS |
| 15005 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 15006 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 15007 | PyErr_SetFromWindowsErr(0); |
| 15008 | return -1; |
| 15009 | } |
| 15010 | #endif |
| 15011 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15012 | } |
| 15013 | |
| 15014 | /* Finalize the Unicode implementation */ |
| 15015 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15016 | int |
| 15017 | PyUnicode_ClearFreeList(void) |
| 15018 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15019 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15020 | } |
| 15021 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15022 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 15023 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15024 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 15025 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15026 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15027 | Py_CLEAR(unicode_empty); |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 15028 | |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15029 | for (i = 0; i < 256; i++) |
| 15030 | Py_CLEAR(unicode_latin1[i]); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 15031 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 15032 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 15033 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 15034 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15035 | void |
| 15036 | PyUnicode_InternInPlace(PyObject **p) |
| 15037 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15038 | PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15039 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15040 | #ifdef Py_DEBUG |
| 15041 | assert(s != NULL); |
| 15042 | assert(_PyUnicode_CHECK(s)); |
| 15043 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15044 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 15045 | return; |
| 15046 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15047 | /* If it's a subclass, we don't really know what putting |
| 15048 | it in the interned dict might do. */ |
| 15049 | if (!PyUnicode_CheckExact(s)) |
| 15050 | return; |
| 15051 | if (PyUnicode_CHECK_INTERNED(s)) |
| 15052 | return; |
| 15053 | if (interned == NULL) { |
| 15054 | interned = PyDict_New(); |
| 15055 | if (interned == NULL) { |
| 15056 | PyErr_Clear(); /* Don't leave an exception */ |
| 15057 | return; |
| 15058 | } |
| 15059 | } |
| 15060 | /* It might be that the GetItem call fails even |
| 15061 | though the key is present in the dictionary, |
| 15062 | namely when this happens during a stack overflow. */ |
| 15063 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15064 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15065 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15066 | |
Victor Stinner | f033510 | 2013-04-14 19:13:03 +0200 | [diff] [blame] | 15067 | if (t) { |
| 15068 | Py_INCREF(t); |
| 15069 | Py_DECREF(*p); |
| 15070 | *p = t; |
| 15071 | return; |
| 15072 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15073 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15074 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 15075 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15076 | PyErr_Clear(); |
| 15077 | PyThreadState_GET()->recursion_critical = 0; |
| 15078 | return; |
| 15079 | } |
| 15080 | PyThreadState_GET()->recursion_critical = 0; |
| 15081 | /* The two references in interned are not counted by refcnt. |
| 15082 | The deallocator will take care of this */ |
| 15083 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15084 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15085 | } |
| 15086 | |
| 15087 | void |
| 15088 | PyUnicode_InternImmortal(PyObject **p) |
| 15089 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15090 | PyUnicode_InternInPlace(p); |
| 15091 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 15092 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15093 | Py_INCREF(*p); |
| 15094 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15095 | } |
| 15096 | |
| 15097 | PyObject * |
| 15098 | PyUnicode_InternFromString(const char *cp) |
| 15099 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15100 | PyObject *s = PyUnicode_FromString(cp); |
| 15101 | if (s == NULL) |
| 15102 | return NULL; |
| 15103 | PyUnicode_InternInPlace(&s); |
| 15104 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15105 | } |
| 15106 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 15107 | void |
| 15108 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15109 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15110 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15111 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15112 | Py_ssize_t i, n; |
| 15113 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15114 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15115 | if (interned == NULL || !PyDict_Check(interned)) |
| 15116 | return; |
| 15117 | keys = PyDict_Keys(interned); |
| 15118 | if (keys == NULL || !PyList_Check(keys)) { |
| 15119 | PyErr_Clear(); |
| 15120 | return; |
| 15121 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15122 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15123 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 15124 | detector, interned unicode strings are not forcibly deallocated; |
| 15125 | rather, we give them their stolen references back, and then clear |
| 15126 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15127 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15128 | n = PyList_GET_SIZE(keys); |
| 15129 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15130 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15131 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15132 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15133 | if (PyUnicode_READY(s) == -1) { |
| 15134 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15135 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 15136 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15137 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15138 | case SSTATE_NOT_INTERNED: |
| 15139 | /* XXX Shouldn't happen */ |
| 15140 | break; |
| 15141 | case SSTATE_INTERNED_IMMORTAL: |
| 15142 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15143 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15144 | break; |
| 15145 | case SSTATE_INTERNED_MORTAL: |
| 15146 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15147 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15148 | break; |
| 15149 | default: |
| 15150 | Py_FatalError("Inconsistent interned string state."); |
| 15151 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15152 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15153 | } |
| 15154 | fprintf(stderr, "total size of all interned strings: " |
| 15155 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 15156 | "mortal/immortal\n", mortal_size, immortal_size); |
| 15157 | Py_DECREF(keys); |
| 15158 | PyDict_Clear(interned); |
Serhiy Storchaka | 0599725 | 2013-01-26 12:14:02 +0200 | [diff] [blame] | 15159 | Py_CLEAR(interned); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 15160 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15161 | |
| 15162 | |
| 15163 | /********************* Unicode Iterator **************************/ |
| 15164 | |
| 15165 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15166 | PyObject_HEAD |
| 15167 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15168 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15169 | } unicodeiterobject; |
| 15170 | |
| 15171 | static void |
| 15172 | unicodeiter_dealloc(unicodeiterobject *it) |
| 15173 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15174 | _PyObject_GC_UNTRACK(it); |
| 15175 | Py_XDECREF(it->it_seq); |
| 15176 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15177 | } |
| 15178 | |
| 15179 | static int |
| 15180 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 15181 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15182 | Py_VISIT(it->it_seq); |
| 15183 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15184 | } |
| 15185 | |
| 15186 | static PyObject * |
| 15187 | unicodeiter_next(unicodeiterobject *it) |
| 15188 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15189 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15190 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15191 | assert(it != NULL); |
| 15192 | seq = it->it_seq; |
| 15193 | if (seq == NULL) |
| 15194 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 15195 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15196 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15197 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 15198 | int kind = PyUnicode_KIND(seq); |
| 15199 | void *data = PyUnicode_DATA(seq); |
| 15200 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 15201 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15202 | if (item != NULL) |
| 15203 | ++it->it_index; |
| 15204 | return item; |
| 15205 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15206 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15207 | Py_DECREF(seq); |
| 15208 | it->it_seq = NULL; |
| 15209 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15210 | } |
| 15211 | |
| 15212 | static PyObject * |
| 15213 | unicodeiter_len(unicodeiterobject *it) |
| 15214 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15215 | Py_ssize_t len = 0; |
| 15216 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 15217 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15218 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15219 | } |
| 15220 | |
| 15221 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 15222 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15223 | static PyObject * |
| 15224 | unicodeiter_reduce(unicodeiterobject *it) |
| 15225 | { |
| 15226 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15227 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15228 | it->it_seq, it->it_index); |
| 15229 | } else { |
| 15230 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 15231 | if (u == NULL) |
| 15232 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 15233 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15234 | } |
| 15235 | } |
| 15236 | |
| 15237 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 15238 | |
| 15239 | static PyObject * |
| 15240 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 15241 | { |
| 15242 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 15243 | if (index == -1 && PyErr_Occurred()) |
| 15244 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 15245 | if (it->it_seq != NULL) { |
| 15246 | if (index < 0) |
| 15247 | index = 0; |
| 15248 | else if (index > PyUnicode_GET_LENGTH(it->it_seq)) |
| 15249 | index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */ |
| 15250 | it->it_index = index; |
| 15251 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15252 | Py_RETURN_NONE; |
| 15253 | } |
| 15254 | |
| 15255 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 15256 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15257 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15258 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 15259 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 15260 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 15261 | reduce_doc}, |
| 15262 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 15263 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15264 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15265 | }; |
| 15266 | |
| 15267 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15268 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15269 | "str_iterator", /* tp_name */ |
| 15270 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 15271 | 0, /* tp_itemsize */ |
| 15272 | /* methods */ |
| 15273 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 15274 | 0, /* tp_print */ |
| 15275 | 0, /* tp_getattr */ |
| 15276 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 15277 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15278 | 0, /* tp_repr */ |
| 15279 | 0, /* tp_as_number */ |
| 15280 | 0, /* tp_as_sequence */ |
| 15281 | 0, /* tp_as_mapping */ |
| 15282 | 0, /* tp_hash */ |
| 15283 | 0, /* tp_call */ |
| 15284 | 0, /* tp_str */ |
| 15285 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 15286 | 0, /* tp_setattro */ |
| 15287 | 0, /* tp_as_buffer */ |
| 15288 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 15289 | 0, /* tp_doc */ |
| 15290 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 15291 | 0, /* tp_clear */ |
| 15292 | 0, /* tp_richcompare */ |
| 15293 | 0, /* tp_weaklistoffset */ |
| 15294 | PyObject_SelfIter, /* tp_iter */ |
| 15295 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 15296 | unicodeiter_methods, /* tp_methods */ |
| 15297 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15298 | }; |
| 15299 | |
| 15300 | static PyObject * |
| 15301 | unicode_iter(PyObject *seq) |
| 15302 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15303 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15304 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15305 | if (!PyUnicode_Check(seq)) { |
| 15306 | PyErr_BadInternalCall(); |
| 15307 | return NULL; |
| 15308 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15309 | if (PyUnicode_READY(seq) == -1) |
| 15310 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15311 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 15312 | if (it == NULL) |
| 15313 | return NULL; |
| 15314 | it->it_index = 0; |
| 15315 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15316 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 15317 | _PyObject_GC_TRACK(it); |
| 15318 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 15319 | } |
| 15320 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15321 | |
| 15322 | size_t |
| 15323 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 15324 | { |
| 15325 | int res = 0; |
| 15326 | while(*u++) |
| 15327 | res++; |
| 15328 | return res; |
| 15329 | } |
| 15330 | |
| 15331 | Py_UNICODE* |
| 15332 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15333 | { |
| 15334 | Py_UNICODE *u = s1; |
| 15335 | while ((*u++ = *s2++)); |
| 15336 | return s1; |
| 15337 | } |
| 15338 | |
| 15339 | Py_UNICODE* |
| 15340 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15341 | { |
| 15342 | Py_UNICODE *u = s1; |
| 15343 | while ((*u++ = *s2++)) |
| 15344 | if (n-- == 0) |
| 15345 | break; |
| 15346 | return s1; |
| 15347 | } |
| 15348 | |
| 15349 | Py_UNICODE* |
| 15350 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15351 | { |
| 15352 | Py_UNICODE *u1 = s1; |
| 15353 | u1 += Py_UNICODE_strlen(u1); |
| 15354 | Py_UNICODE_strcpy(u1, s2); |
| 15355 | return s1; |
| 15356 | } |
| 15357 | |
| 15358 | int |
| 15359 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 15360 | { |
| 15361 | while (*s1 && *s2 && *s1 == *s2) |
| 15362 | s1++, s2++; |
| 15363 | if (*s1 && *s2) |
| 15364 | return (*s1 < *s2) ? -1 : +1; |
| 15365 | if (*s1) |
| 15366 | return 1; |
| 15367 | if (*s2) |
| 15368 | return -1; |
| 15369 | return 0; |
| 15370 | } |
| 15371 | |
| 15372 | int |
| 15373 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 15374 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 15375 | Py_UNICODE u1, u2; |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 15376 | for (; n != 0; n--) { |
| 15377 | u1 = *s1; |
| 15378 | u2 = *s2; |
| 15379 | if (u1 != u2) |
| 15380 | return (u1 < u2) ? -1 : +1; |
| 15381 | if (u1 == '\0') |
| 15382 | return 0; |
| 15383 | s1++; |
| 15384 | s2++; |
| 15385 | } |
| 15386 | return 0; |
| 15387 | } |
| 15388 | |
| 15389 | Py_UNICODE* |
| 15390 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15391 | { |
| 15392 | const Py_UNICODE *p; |
| 15393 | for (p = s; *p; p++) |
| 15394 | if (*p == c) |
| 15395 | return (Py_UNICODE*)p; |
| 15396 | return NULL; |
| 15397 | } |
| 15398 | |
| 15399 | Py_UNICODE* |
| 15400 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 15401 | { |
| 15402 | const Py_UNICODE *p; |
| 15403 | p = s + Py_UNICODE_strlen(s); |
| 15404 | while (p != s) { |
| 15405 | p--; |
| 15406 | if (*p == c) |
| 15407 | return (Py_UNICODE*)p; |
| 15408 | } |
| 15409 | return NULL; |
| 15410 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 15411 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15412 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 15413 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15414 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15415 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15416 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15417 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 15418 | if (!PyUnicode_Check(unicode)) { |
| 15419 | PyErr_BadArgument(); |
| 15420 | return NULL; |
| 15421 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15422 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15423 | if (u == NULL) |
| 15424 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15425 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15426 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15427 | PyErr_NoMemory(); |
| 15428 | return NULL; |
| 15429 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 15430 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15431 | size *= sizeof(Py_UNICODE); |
| 15432 | copy = PyMem_Malloc(size); |
| 15433 | if (copy == NULL) { |
| 15434 | PyErr_NoMemory(); |
| 15435 | return NULL; |
| 15436 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 15437 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 15438 | return copy; |
| 15439 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 15440 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 15441 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 15442 | to the string.Formatter class implemented in Python. */ |
| 15443 | |
| 15444 | static PyMethodDef _string_methods[] = { |
| 15445 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 15446 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 15447 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 15448 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 15449 | {NULL, NULL} |
| 15450 | }; |
| 15451 | |
| 15452 | static struct PyModuleDef _string_module = { |
| 15453 | PyModuleDef_HEAD_INIT, |
| 15454 | "_string", |
| 15455 | PyDoc_STR("string helper module"), |
| 15456 | 0, |
| 15457 | _string_methods, |
| 15458 | NULL, |
| 15459 | NULL, |
| 15460 | NULL, |
| 15461 | NULL |
| 15462 | }; |
| 15463 | |
| 15464 | PyMODINIT_FUNC |
| 15465 | PyInit__string(void) |
| 15466 | { |
| 15467 | return PyModule_Create(&_string_module); |
| 15468 | } |
| 15469 | |
| 15470 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15471 | #ifdef __cplusplus |
| 15472 | } |
| 15473 | #endif |